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
|
|
|
|
2022-08-19 00:34:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/identity"
|
2021-01-04 01:59:25 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains an example dummy entity to be used in the tests
|
|
|
|
|
|
|
|
/*
|
|
|
|
Operations
|
|
|
|
*/
|
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
const (
|
|
|
|
_ OperationType = iota
|
|
|
|
Op1
|
|
|
|
Op2
|
|
|
|
)
|
2021-01-04 01:59:25 +03:00
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
type op1 struct {
|
|
|
|
OpBase
|
|
|
|
Field1 string `json:"field_1"`
|
|
|
|
Files []repository.Hash `json:"files"`
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
2021-02-20 17:48:44 +03:00
|
|
|
func newOp1(author identity.Interface, field1 string, files ...repository.Hash) *op1 {
|
2022-07-25 14:16:16 +03:00
|
|
|
return &op1{OpBase: NewOpBase(Op1, author, 0), Field1: field1, Files: files}
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
func (op *op1) Id() entity.Id {
|
|
|
|
return IdOperation(op, &op.OpBase)
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
func (op *op1) Validate() error { return nil }
|
2021-02-20 17:48:44 +03:00
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
func (op *op1) GetFiles() []repository.Hash {
|
|
|
|
return op.Files
|
2021-02-20 17:48:44 +03:00
|
|
|
}
|
2021-01-04 01:59:25 +03:00
|
|
|
|
|
|
|
type op2 struct {
|
2022-07-25 14:16:16 +03:00
|
|
|
OpBase
|
|
|
|
Field2 string `json:"field_2"`
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newOp2(author identity.Interface, field2 string) *op2 {
|
2022-07-25 14:16:16 +03:00
|
|
|
return &op2{OpBase: NewOpBase(Op2, author, 0), Field2: field2}
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
func (op *op2) Id() entity.Id {
|
|
|
|
return IdOperation(op, &op.OpBase)
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
func (op *op2) Validate() error { return nil }
|
2021-02-20 17:48:44 +03:00
|
|
|
|
2022-08-13 13:08:48 +03:00
|
|
|
func unmarshaler(raw json.RawMessage, resolvers entity.Resolvers) (Operation, error) {
|
2021-01-04 01:59:25 +03:00
|
|
|
var t struct {
|
2022-07-25 14:16:16 +03:00
|
|
|
OperationType OperationType `json:"type"`
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(raw, &t); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
var op Operation
|
|
|
|
|
2021-01-04 01:59:25 +03:00
|
|
|
switch t.OperationType {
|
2022-07-25 14:16:16 +03:00
|
|
|
case Op1:
|
|
|
|
op = &op1{}
|
|
|
|
case Op2:
|
|
|
|
op = &op2{}
|
2021-01-04 01:59:25 +03:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown operation type %v", t.OperationType)
|
|
|
|
}
|
2022-07-25 14:16:16 +03:00
|
|
|
|
|
|
|
err := json.Unmarshal(raw, &op)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return op, nil
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Identities + repo + definition
|
|
|
|
*/
|
|
|
|
|
2022-08-13 13:08:48 +03:00
|
|
|
func makeTestContext() (repository.ClockedRepo, identity.Interface, identity.Interface, entity.Resolvers, Definition) {
|
2021-01-04 01:59:25 +03:00
|
|
|
repo := repository.NewMockRepo()
|
2022-08-13 13:08:48 +03:00
|
|
|
id1, id2, resolvers, def := makeTestContextInternal(repo)
|
|
|
|
return repo, id1, id2, resolvers, def
|
2021-01-25 14:39:34 +03:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:08:48 +03:00
|
|
|
func makeTestContextRemote(t *testing.T) (repository.ClockedRepo, repository.ClockedRepo, repository.ClockedRepo, identity.Interface, identity.Interface, entity.Resolvers, Definition) {
|
2022-06-16 16:02:52 +03:00
|
|
|
repoA := repository.CreateGoGitTestRepo(t, false)
|
|
|
|
repoB := repository.CreateGoGitTestRepo(t, false)
|
|
|
|
remote := repository.CreateGoGitTestRepo(t, true)
|
2021-01-25 14:39:34 +03:00
|
|
|
|
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
|
|
|
|
2021-02-14 12:02:01 +03:00
|
|
|
id1, id2, resolver, def := makeTestContextInternal(repoA)
|
2021-01-25 14:39:34 +03:00
|
|
|
|
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-02-14 12:02:01 +03:00
|
|
|
return repoA, repoB, remote, id1, id2, resolver, def
|
2021-01-25 14:39:34 +03:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:08:48 +03:00
|
|
|
func makeTestContextInternal(repo repository.ClockedRepo) (identity.Interface, identity.Interface, entity.Resolvers, 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)
|
|
|
|
}
|
|
|
|
|
2022-08-13 13:08:48 +03:00
|
|
|
resolvers := entity.Resolvers{
|
|
|
|
&identity.Identity{}: entity.ResolverFunc(func(id entity.Id) (entity.Interface, error) {
|
|
|
|
switch id {
|
|
|
|
case id1.Id():
|
|
|
|
return id1, nil
|
|
|
|
case id2.Id():
|
|
|
|
return id2, nil
|
|
|
|
default:
|
|
|
|
return nil, identity.ErrIdentityNotExist
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2021-01-04 01:59:25 +03:00
|
|
|
|
|
|
|
def := Definition{
|
2021-02-14 12:06:16 +03:00
|
|
|
Typename: "foo",
|
|
|
|
Namespace: "foos",
|
|
|
|
OperationUnmarshaler: unmarshaler,
|
|
|
|
FormatVersion: 1,
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:08:48 +03:00
|
|
|
return id1, id2, resolvers, def
|
2021-01-04 01:59:25 +03:00
|
|
|
}
|