git-bug/entity/dag/common_test.go

170 lines
3.8 KiB
Go
Raw Normal View History

2021-01-04 01:59:25 +03:00
package dag
import (
"encoding/json"
"fmt"
2021-02-05 13:18:38 +03:00
"testing"
"github.com/stretchr/testify/require"
2021-01-04 01:59:25 +03:00
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/repository"
)
// This file contains an example dummy entity to be used in the tests
/*
Operations
*/
type op1 struct {
author identity.Interface
OperationType int `json:"type"`
Field1 string `json:"field_1"`
}
func newOp1(author identity.Interface, field1 string) *op1 {
return &op1{author: author, OperationType: 1, Field1: field1}
}
2021-01-25 14:39:34 +03:00
func (o *op1) Id() entity.Id {
2021-01-04 01:59:25 +03:00
data, _ := json.Marshal(o)
return entity.DeriveId(data)
}
2021-01-25 14:39:34 +03:00
func (o *op1) Author() identity.Interface {
2021-01-04 01:59:25 +03:00
return o.author
}
2021-01-25 14:39:34 +03:00
func (o *op1) Validate() error { return nil }
2021-01-04 01:59:25 +03:00
type op2 struct {
author identity.Interface
OperationType int `json:"type"`
Field2 string `json:"field_2"`
}
func newOp2(author identity.Interface, field2 string) *op2 {
return &op2{author: author, OperationType: 2, Field2: field2}
}
2021-01-25 14:39:34 +03:00
func (o *op2) Id() entity.Id {
2021-01-04 01:59:25 +03:00
data, _ := json.Marshal(o)
return entity.DeriveId(data)
}
2021-01-25 14:39:34 +03:00
func (o *op2) Author() identity.Interface {
2021-01-04 01:59:25 +03:00
return o.author
}
2021-01-25 14:39:34 +03:00
func (o *op2) Validate() error { return nil }
2021-01-04 01:59:25 +03:00
func unmarshaler(author identity.Interface, raw json.RawMessage) (Operation, error) {
var t struct {
OperationType int `json:"type"`
}
if err := json.Unmarshal(raw, &t); err != nil {
return nil, err
}
switch t.OperationType {
case 1:
op := &op1{}
err := json.Unmarshal(raw, &op)
op.author = author
return op, err
case 2:
op := &op2{}
err := json.Unmarshal(raw, &op)
op.author = author
return op, err
default:
return nil, fmt.Errorf("unknown operation type %v", t.OperationType)
}
}
/*
Identities + repo + definition
*/
func makeTestContext() (repository.ClockedRepo, identity.Interface, identity.Interface, Definition) {
repo := repository.NewMockRepo()
2021-01-25 14:39:34 +03:00
id1, id2, def := makeTestContextInternal(repo)
return repo, id1, id2, def
}
2021-02-05 13:18:38 +03:00
func makeTestContextRemote(t *testing.T) (repository.ClockedRepo, repository.ClockedRepo, repository.ClockedRepo, identity.Interface, identity.Interface, Definition) {
2021-01-25 14:39:34 +03:00
repoA := repository.CreateGoGitTestRepo(false)
repoB := repository.CreateGoGitTestRepo(false)
remote := repository.CreateGoGitTestRepo(true)
2021-02-05 13:18:38 +03:00
err := repoA.AddRemote("remote", remote.GetLocalRemote())
require.NoError(t, err)
err = repoA.AddRemote("repoB", repoB.GetLocalRemote())
require.NoError(t, err)
err = repoB.AddRemote("remote", remote.GetLocalRemote())
require.NoError(t, err)
err = repoB.AddRemote("repoA", repoA.GetLocalRemote())
require.NoError(t, err)
2021-01-25 14:39:34 +03:00
id1, id2, def := makeTestContextInternal(repoA)
2021-02-05 13:18:38 +03:00
// distribute the identities
_, err = identity.Push(repoA, "remote")
require.NoError(t, err)
err = identity.Pull(repoB, "remote")
require.NoError(t, err)
2021-01-25 14:39:34 +03:00
return repoA, repoB, remote, id1, id2, def
}
func makeTestContextInternal(repo repository.ClockedRepo) (identity.Interface, identity.Interface, Definition) {
2021-01-04 01:59:25 +03:00
id1, err := identity.NewIdentity(repo, "name1", "email1")
if err != nil {
panic(err)
}
err = id1.Commit(repo)
if err != nil {
panic(err)
}
id2, err := identity.NewIdentity(repo, "name2", "email2")
if err != nil {
panic(err)
}
err = id2.Commit(repo)
if err != nil {
panic(err)
}
resolver := identityResolverFunc(func(id entity.Id) (identity.Interface, error) {
switch id {
case id1.Id():
return id1, nil
case id2.Id():
return id2, nil
default:
return nil, identity.ErrIdentityNotExist
}
})
def := Definition{
typename: "foo",
namespace: "foos",
operationUnmarshaler: unmarshaler,
identityResolver: resolver,
formatVersion: 1,
}
2021-01-25 14:39:34 +03:00
return id1, id2, def
2021-01-04 01:59:25 +03:00
}
type identityResolverFunc func(id entity.Id) (identity.Interface, error)
func (fn identityResolverFunc) ResolveIdentity(id entity.Id) (identity.Interface, error) {
return fn(id)
}