Exemplo n.º 1
0
func TestDocApply(t *testing.T) {
	cases := []struct {
		doc  Doc
		chgs []Change
		exp  ot.Blob
	}{
		{
			doc: Doc{
				Blob: ot.Blob("Lem sum lor"),
			},
			chgs: []Change{
				{
					Root: 0,
					Ops:  ot.Ops{ot.RetainOp(1), ot.InsertOp("or"), ot.RetainOp(10)},
				},
				{
					Root: 0,
					Ops:  ot.Ops{ot.RetainOp(8), ot.InsertOp("do"), ot.RetainOp(3)},
				},
				{
					Root: 1,
					Ops:  ot.Ops{ot.RetainOp(6), ot.InsertOp("ip"), ot.RetainOp(7)},
				},
			},
			exp: ot.Blob("Lorem ipsum dolor"),
		},
	}
	for i, c := range cases {
		for _, ch := range c.chgs {
			_, err := c.doc.Apply(ch)
			if err != nil {
				t.Fatalf("%d: expected err == nil, got %#v", i, err)
			}
		}
		if !bytes.Equal(c.doc.Blob, c.exp) {
			t.Fatalf("%d: expected blob == %q, got %q", i, c.exp, c.doc.Blob)
		}
	}
}
Exemplo n.º 2
0
func (r *Repo) Get(id string) (*hub.Doc, error) {
	if !bson.IsObjectIdHex(id) {
		return nil, ErrBadID
	}
	doc, err := data.GetDocument(bson.ObjectIdHex(id))
	if err != nil {
		return nil, err
	}
	if doc == nil {
		return nil, nil
	}
	return &hub.Doc{
		ID:   doc.ID.Hex(),
		Blob: ot.Blob(doc.Content),
	}, nil
}