common_test.go

  1package dag
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6
  7	"github.com/MichaelMure/git-bug/entity"
  8	"github.com/MichaelMure/git-bug/identity"
  9	"github.com/MichaelMure/git-bug/repository"
 10)
 11
 12// This file contains an example dummy entity to be used in the tests
 13
 14/*
 15 Operations
 16*/
 17
 18type op1 struct {
 19	author identity.Interface
 20
 21	OperationType int    `json:"type"`
 22	Field1        string `json:"field_1"`
 23}
 24
 25func newOp1(author identity.Interface, field1 string) *op1 {
 26	return &op1{author: author, OperationType: 1, Field1: field1}
 27}
 28
 29func (o *op1) Id() entity.Id {
 30	data, _ := json.Marshal(o)
 31	return entity.DeriveId(data)
 32}
 33
 34func (o *op1) Author() identity.Interface {
 35	return o.author
 36}
 37
 38func (o *op1) Validate() error { return nil }
 39
 40type op2 struct {
 41	author identity.Interface
 42
 43	OperationType int    `json:"type"`
 44	Field2        string `json:"field_2"`
 45}
 46
 47func newOp2(author identity.Interface, field2 string) *op2 {
 48	return &op2{author: author, OperationType: 2, Field2: field2}
 49}
 50
 51func (o *op2) Id() entity.Id {
 52	data, _ := json.Marshal(o)
 53	return entity.DeriveId(data)
 54}
 55
 56func (o *op2) Author() identity.Interface {
 57	return o.author
 58}
 59
 60func (o *op2) Validate() error { return nil }
 61
 62func unmarshaler(author identity.Interface, raw json.RawMessage) (Operation, error) {
 63	var t struct {
 64		OperationType int `json:"type"`
 65	}
 66
 67	if err := json.Unmarshal(raw, &t); err != nil {
 68		return nil, err
 69	}
 70
 71	switch t.OperationType {
 72	case 1:
 73		op := &op1{}
 74		err := json.Unmarshal(raw, &op)
 75		op.author = author
 76		return op, err
 77	case 2:
 78		op := &op2{}
 79		err := json.Unmarshal(raw, &op)
 80		op.author = author
 81		return op, err
 82	default:
 83		return nil, fmt.Errorf("unknown operation type %v", t.OperationType)
 84	}
 85}
 86
 87/*
 88  Identities + repo + definition
 89*/
 90
 91func makeTestContext() (repository.ClockedRepo, identity.Interface, identity.Interface, Definition) {
 92	repo := repository.NewMockRepo()
 93	id1, id2, def := makeTestContextInternal(repo)
 94	return repo, id1, id2, def
 95}
 96
 97func makeTestContextRemote() (repository.ClockedRepo, repository.ClockedRepo, repository.ClockedRepo, identity.Interface, identity.Interface, Definition) {
 98	repoA := repository.CreateGoGitTestRepo(false)
 99	repoB := repository.CreateGoGitTestRepo(false)
100	remote := repository.CreateGoGitTestRepo(true)
101
102	err := repoA.AddRemote("origin", remote.GetLocalRemote())
103	if err != nil {
104		panic(err)
105	}
106
107	err = repoB.AddRemote("origin", remote.GetLocalRemote())
108	if err != nil {
109		panic(err)
110	}
111
112	id1, id2, def := makeTestContextInternal(repoA)
113
114	return repoA, repoB, remote, id1, id2, def
115}
116
117func makeTestContextInternal(repo repository.ClockedRepo) (identity.Interface, identity.Interface, Definition) {
118	id1, err := identity.NewIdentity(repo, "name1", "email1")
119	if err != nil {
120		panic(err)
121	}
122	err = id1.Commit(repo)
123	if err != nil {
124		panic(err)
125	}
126	id2, err := identity.NewIdentity(repo, "name2", "email2")
127	if err != nil {
128		panic(err)
129	}
130	err = id2.Commit(repo)
131	if err != nil {
132		panic(err)
133	}
134
135	resolver := identityResolverFunc(func(id entity.Id) (identity.Interface, error) {
136		switch id {
137		case id1.Id():
138			return id1, nil
139		case id2.Id():
140			return id2, nil
141		default:
142			return nil, identity.ErrIdentityNotExist
143		}
144	})
145
146	def := Definition{
147		typename:             "foo",
148		namespace:            "foos",
149		operationUnmarshaler: unmarshaler,
150		identityResolver:     resolver,
151		formatVersion:        1,
152	}
153
154	return id1, id2, def
155}
156
157type identityResolverFunc func(id entity.Id) (identity.Interface, error)
158
159func (fn identityResolverFunc) ResolveIdentity(id entity.Id) (identity.Interface, error) {
160	return fn(id)
161}