From 8d63c983c982f93cc48d3996d6bd097ddeeb327f Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 3 Jan 2021 23:59:25 +0100 Subject: WIP --- entity/dag/common_test.go | 137 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 entity/dag/common_test.go (limited to 'entity/dag/common_test.go') diff --git a/entity/dag/common_test.go b/entity/dag/common_test.go new file mode 100644 index 00000000..29f1279e --- /dev/null +++ b/entity/dag/common_test.go @@ -0,0 +1,137 @@ +package dag + +import ( + "encoding/json" + "fmt" + + "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} +} + +func (o op1) Id() entity.Id { + data, _ := json.Marshal(o) + return entity.DeriveId(data) +} + +func (o op1) Author() identity.Interface { + return o.author +} + +func (o op1) Validate() error { return nil } + +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} +} + +func (o op2) Id() entity.Id { + data, _ := json.Marshal(o) + return entity.DeriveId(data) +} + +func (o op2) Author() identity.Interface { + return o.author +} + +func (o op2) Validate() error { return nil } + +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() + + 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, + } + + return repo, id1, id2, def +} + +type identityResolverFunc func(id entity.Id) (identity.Interface, error) + +func (fn identityResolverFunc) ResolveIdentity(id entity.Id) (identity.Interface, error) { + return fn(id) +} -- cgit From fe4237df3c62bd6dfd1f385893295f93072d0e51 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Mon, 25 Jan 2021 12:39:34 +0100 Subject: entity: readAll and more testing --- entity/dag/common_test.go | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'entity/dag/common_test.go') diff --git a/entity/dag/common_test.go b/entity/dag/common_test.go index 29f1279e..b822fc79 100644 --- a/entity/dag/common_test.go +++ b/entity/dag/common_test.go @@ -26,16 +26,16 @@ func newOp1(author identity.Interface, field1 string) *op1 { return &op1{author: author, OperationType: 1, Field1: field1} } -func (o op1) Id() entity.Id { +func (o *op1) Id() entity.Id { data, _ := json.Marshal(o) return entity.DeriveId(data) } -func (o op1) Author() identity.Interface { +func (o *op1) Author() identity.Interface { return o.author } -func (o op1) Validate() error { return nil } +func (o *op1) Validate() error { return nil } type op2 struct { author identity.Interface @@ -48,16 +48,16 @@ func newOp2(author identity.Interface, field2 string) *op2 { return &op2{author: author, OperationType: 2, Field2: field2} } -func (o op2) Id() entity.Id { +func (o *op2) Id() entity.Id { data, _ := json.Marshal(o) return entity.DeriveId(data) } -func (o op2) Author() identity.Interface { +func (o *op2) Author() identity.Interface { return o.author } -func (o op2) Validate() error { return nil } +func (o *op2) Validate() error { return nil } func unmarshaler(author identity.Interface, raw json.RawMessage) (Operation, error) { var t struct { @@ -90,7 +90,31 @@ func unmarshaler(author identity.Interface, raw json.RawMessage) (Operation, err func makeTestContext() (repository.ClockedRepo, identity.Interface, identity.Interface, Definition) { repo := repository.NewMockRepo() + id1, id2, def := makeTestContextInternal(repo) + return repo, id1, id2, def +} + +func makeTestContextRemote() (repository.ClockedRepo, repository.ClockedRepo, repository.ClockedRepo, identity.Interface, identity.Interface, Definition) { + repoA := repository.CreateGoGitTestRepo(false) + repoB := repository.CreateGoGitTestRepo(false) + remote := repository.CreateGoGitTestRepo(true) + + err := repoA.AddRemote("origin", remote.GetLocalRemote()) + if err != nil { + panic(err) + } + err = repoB.AddRemote("origin", remote.GetLocalRemote()) + if err != nil { + panic(err) + } + + id1, id2, def := makeTestContextInternal(repoA) + + return repoA, repoB, remote, id1, id2, def +} + +func makeTestContextInternal(repo repository.ClockedRepo) (identity.Interface, identity.Interface, Definition) { id1, err := identity.NewIdentity(repo, "name1", "email1") if err != nil { panic(err) @@ -127,7 +151,7 @@ func makeTestContext() (repository.ClockedRepo, identity.Interface, identity.Int formatVersion: 1, } - return repo, id1, id2, def + return id1, id2, def } type identityResolverFunc func(id entity.Id) (identity.Interface, error) -- cgit From e35c7c4d170d1b682992c95f1c14772158501015 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Fri, 5 Feb 2021 11:18:38 +0100 Subject: entity: more testing and bug fixing --- entity/dag/common_test.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'entity/dag/common_test.go') diff --git a/entity/dag/common_test.go b/entity/dag/common_test.go index b822fc79..05d85898 100644 --- a/entity/dag/common_test.go +++ b/entity/dag/common_test.go @@ -3,6 +3,9 @@ package dag import ( "encoding/json" "fmt" + "testing" + + "github.com/stretchr/testify/require" "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/identity" @@ -94,23 +97,28 @@ func makeTestContext() (repository.ClockedRepo, identity.Interface, identity.Int return repo, id1, id2, def } -func makeTestContextRemote() (repository.ClockedRepo, repository.ClockedRepo, repository.ClockedRepo, identity.Interface, identity.Interface, Definition) { +func makeTestContextRemote(t *testing.T) (repository.ClockedRepo, repository.ClockedRepo, repository.ClockedRepo, identity.Interface, identity.Interface, Definition) { repoA := repository.CreateGoGitTestRepo(false) repoB := repository.CreateGoGitTestRepo(false) remote := repository.CreateGoGitTestRepo(true) - err := repoA.AddRemote("origin", remote.GetLocalRemote()) - if err != nil { - panic(err) - } - - err = repoB.AddRemote("origin", remote.GetLocalRemote()) - if err != nil { - panic(err) - } + 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) id1, id2, def := makeTestContextInternal(repoA) + // distribute the identities + _, err = identity.Push(repoA, "remote") + require.NoError(t, err) + err = identity.Pull(repoB, "remote") + require.NoError(t, err) + return repoA, repoB, remote, id1, id2, def } -- cgit From 94f06cd54defa73f5e8b79345597279e454c78e6 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 14 Feb 2021 10:02:01 +0100 Subject: entity: pass the identity resolver instead of defining it once Having the resolver in Definition doesn't actually work well as the resolver is very situational. --- entity/dag/common_test.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'entity/dag/common_test.go') diff --git a/entity/dag/common_test.go b/entity/dag/common_test.go index 05d85898..0ddbca47 100644 --- a/entity/dag/common_test.go +++ b/entity/dag/common_test.go @@ -91,13 +91,13 @@ func unmarshaler(author identity.Interface, raw json.RawMessage) (Operation, err Identities + repo + definition */ -func makeTestContext() (repository.ClockedRepo, identity.Interface, identity.Interface, Definition) { +func makeTestContext() (repository.ClockedRepo, identity.Interface, identity.Interface, identity.Resolver, Definition) { repo := repository.NewMockRepo() - id1, id2, def := makeTestContextInternal(repo) - return repo, id1, id2, def + id1, id2, resolver, def := makeTestContextInternal(repo) + return repo, id1, id2, resolver, def } -func makeTestContextRemote(t *testing.T) (repository.ClockedRepo, repository.ClockedRepo, repository.ClockedRepo, identity.Interface, identity.Interface, Definition) { +func makeTestContextRemote(t *testing.T) (repository.ClockedRepo, repository.ClockedRepo, repository.ClockedRepo, identity.Interface, identity.Interface, identity.Resolver, Definition) { repoA := repository.CreateGoGitTestRepo(false) repoB := repository.CreateGoGitTestRepo(false) remote := repository.CreateGoGitTestRepo(true) @@ -111,7 +111,7 @@ func makeTestContextRemote(t *testing.T) (repository.ClockedRepo, repository.Clo err = repoB.AddRemote("repoA", repoA.GetLocalRemote()) require.NoError(t, err) - id1, id2, def := makeTestContextInternal(repoA) + id1, id2, resolver, def := makeTestContextInternal(repoA) // distribute the identities _, err = identity.Push(repoA, "remote") @@ -119,10 +119,10 @@ func makeTestContextRemote(t *testing.T) (repository.ClockedRepo, repository.Clo err = identity.Pull(repoB, "remote") require.NoError(t, err) - return repoA, repoB, remote, id1, id2, def + return repoA, repoB, remote, id1, id2, resolver, def } -func makeTestContextInternal(repo repository.ClockedRepo) (identity.Interface, identity.Interface, Definition) { +func makeTestContextInternal(repo repository.ClockedRepo) (identity.Interface, identity.Interface, identity.Resolver, Definition) { id1, err := identity.NewIdentity(repo, "name1", "email1") if err != nil { panic(err) @@ -155,11 +155,10 @@ func makeTestContextInternal(repo repository.ClockedRepo) (identity.Interface, i typename: "foo", namespace: "foos", operationUnmarshaler: unmarshaler, - identityResolver: resolver, formatVersion: 1, } - return id1, id2, def + return id1, id2, resolver, def } type identityResolverFunc func(id entity.Id) (identity.Interface, error) -- cgit From 99b9dd84cb4b0cfd3eb1fd50b07c8b826eb52d19 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 14 Feb 2021 10:06:16 +0100 Subject: entity: support different author in staging operations --- entity/dag/common_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'entity/dag/common_test.go') diff --git a/entity/dag/common_test.go b/entity/dag/common_test.go index 0ddbca47..fa15cd1f 100644 --- a/entity/dag/common_test.go +++ b/entity/dag/common_test.go @@ -152,10 +152,10 @@ func makeTestContextInternal(repo repository.ClockedRepo) (identity.Interface, i }) def := Definition{ - typename: "foo", - namespace: "foos", - operationUnmarshaler: unmarshaler, - formatVersion: 1, + Typename: "foo", + Namespace: "foos", + OperationUnmarshaler: unmarshaler, + FormatVersion: 1, } return id1, id2, resolver, def -- cgit From 214abe4dea1984086e45d1399538fb12aa010642 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 20 Feb 2021 15:48:44 +0100 Subject: WIP operation with files --- entity/dag/common_test.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'entity/dag/common_test.go') diff --git a/entity/dag/common_test.go b/entity/dag/common_test.go index fa15cd1f..1898451d 100644 --- a/entity/dag/common_test.go +++ b/entity/dag/common_test.go @@ -23,10 +23,11 @@ type op1 struct { OperationType int `json:"type"` Field1 string `json:"field_1"` + Files []repository.Hash } -func newOp1(author identity.Interface, field1 string) *op1 { - return &op1{author: author, OperationType: 1, Field1: field1} +func newOp1(author identity.Interface, field1 string, files ...repository.Hash) *op1 { + return &op1{author: author, OperationType: 1, Field1: field1, Files: files} } func (o *op1) Id() entity.Id { @@ -34,11 +35,15 @@ func (o *op1) Id() entity.Id { return entity.DeriveId(data) } +func (o *op1) Validate() error { return nil } + func (o *op1) Author() identity.Interface { return o.author } -func (o *op1) Validate() error { return nil } +func (o *op1) GetFiles() []repository.Hash { + return o.Files +} type op2 struct { author identity.Interface @@ -56,12 +61,12 @@ func (o *op2) Id() entity.Id { return entity.DeriveId(data) } +func (o *op2) Validate() error { return nil } + func (o *op2) Author() identity.Interface { return o.author } -func (o *op2) Validate() error { return nil } - func unmarshaler(author identity.Interface, raw json.RawMessage) (Operation, error) { var t struct { OperationType int `json:"type"` -- cgit From 5215634d0dca37c545904fbc8a12ddd9b8eb72df Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 21 Mar 2021 18:22:04 +0100 Subject: entity: add support for storing files --- entity/dag/common_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'entity/dag/common_test.go') diff --git a/entity/dag/common_test.go b/entity/dag/common_test.go index 1898451d..25289b76 100644 --- a/entity/dag/common_test.go +++ b/entity/dag/common_test.go @@ -21,9 +21,9 @@ import ( type op1 struct { author identity.Interface - OperationType int `json:"type"` - Field1 string `json:"field_1"` - Files []repository.Hash + OperationType int `json:"type"` + Field1 string `json:"field_1"` + Files []repository.Hash `json:"files"` } func newOp1(author identity.Interface, field1 string, files ...repository.Hash) *op1 { -- cgit