aboutsummaryrefslogtreecommitdiffstats
path: root/entity/dag/operation_testing.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2022-08-01 17:16:45 +0200
committerGitHub <noreply@github.com>2022-08-01 17:16:45 +0200
commitcd52872475f1b39f3fb6546606c1e78afb6c08e3 (patch)
tree9f04b1e1ee362e210e74aaa324d950058ed14bf2 /entity/dag/operation_testing.go
parent2ade8fb1d570ddcb4aedc9386af46d208b129daa (diff)
parent56966fec5562c3a0e23340d0fbe754626c3beb64 (diff)
downloadgit-bug-cd52872475f1b39f3fb6546606c1e78afb6c08e3.tar.gz
Merge pull request #835 from MichaelMure/op-base
entity/dag: proper base operation for simplified implementation
Diffstat (limited to 'entity/dag/operation_testing.go')
-rw-r--r--entity/dag/operation_testing.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/entity/dag/operation_testing.go b/entity/dag/operation_testing.go
new file mode 100644
index 00000000..29923eec
--- /dev/null
+++ b/entity/dag/operation_testing.go
@@ -0,0 +1,57 @@
+package dag
+
+import (
+ "encoding/json"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/MichaelMure/git-bug/entity"
+ "github.com/MichaelMure/git-bug/identity"
+ "github.com/MichaelMure/git-bug/repository"
+)
+
+// SerializeRoundTripTest realize a marshall/unmarshall round-trip in the same condition as with OperationPack,
+// and check if the recovered operation is identical.
+func SerializeRoundTripTest[OpT Operation](t *testing.T, maker func(author identity.Interface, unixTime int64) OpT) {
+ repo := repository.NewMockRepo()
+
+ rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
+ require.NoError(t, err)
+
+ op := maker(rene, time.Now().Unix())
+ // enforce having an id
+ op.Id()
+
+ rdt := &roundTripper[OpT]{Before: op, author: rene}
+
+ data, err := json.Marshal(rdt)
+ require.NoError(t, err)
+
+ err = json.Unmarshal(data, &rdt)
+ require.NoError(t, err)
+
+ require.Equal(t, op, rdt.after)
+}
+
+type roundTripper[OpT Operation] struct {
+ Before OpT
+ author identity.Interface
+ after OpT
+}
+
+func (r *roundTripper[OpT]) MarshalJSON() ([]byte, error) {
+ return json.Marshal(r.Before)
+}
+
+func (r *roundTripper[OpT]) UnmarshalJSON(data []byte) error {
+ if err := json.Unmarshal(data, &r.after); err != nil {
+ return err
+ }
+ // Set the id from the serialized data
+ r.after.setId(entity.DeriveId(data))
+ // Set the author, as OperationPack would do
+ r.after.setAuthor(r.author)
+ return nil
+}