diff options
author | Michael Muré <batolettre@gmail.com> | 2022-07-25 13:16:16 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2022-07-25 13:27:17 +0200 |
commit | 3d454d9dc8ba2409046c0938618a70864e6eb8ef (patch) | |
tree | 8745f656cc8218654632ce003f997a39988d3043 /entity/dag/op_set_metadata_test.go | |
parent | 2ade8fb1d570ddcb4aedc9386af46d208b129daa (diff) | |
download | git-bug-3d454d9dc8ba2409046c0938618a70864e6eb8ef.tar.gz |
entity/dag: proper base operation for simplified implementation
- reduce boilerplace necessary to implement an operation
- consolidate what an operation is in the core, which in turn pave the way for a generic cache layer mechanism
- avoid the previously complex unmarshalling process
- support operation metadata from the core
- simplified testing
Diffstat (limited to 'entity/dag/op_set_metadata_test.go')
-rw-r--r-- | entity/dag/op_set_metadata_test.go | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/entity/dag/op_set_metadata_test.go b/entity/dag/op_set_metadata_test.go new file mode 100644 index 00000000..4dab8a96 --- /dev/null +++ b/entity/dag/op_set_metadata_test.go @@ -0,0 +1,106 @@ +package dag + +import ( + "testing" + "time" + + "github.com/MichaelMure/git-bug/identity" + "github.com/MichaelMure/git-bug/repository" + + "github.com/stretchr/testify/require" +) + +type snapshotMock struct { + ops []Operation +} + +func (s *snapshotMock) AllOperations() []Operation { + return s.ops +} + +func TestSetMetadata(t *testing.T) { + snap := &snapshotMock{} + + repo := repository.NewMockRepo() + + rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr") + require.NoError(t, err) + + unix := time.Now().Unix() + + target1 := NewNoOpOp[*snapshotMock](1, rene, unix) + target1.SetMetadata("key", "value") + snap.ops = append(snap.ops, target1) + + target2 := NewNoOpOp[*snapshotMock](1, rene, unix) + target2.SetMetadata("key2", "value2") + snap.ops = append(snap.ops, target2) + + op1 := NewSetMetadataOp[*snapshotMock](2, rene, unix, target1.Id(), map[string]string{ + "key": "override", + "key2": "value", + }) + + op1.Apply(snap) + snap.ops = append(snap.ops, op1) + + target1Metadata := snap.AllOperations()[0].AllMetadata() + require.Len(t, target1Metadata, 2) + // original key is not overrided + require.Equal(t, target1Metadata["key"], "value") + // new key is set + require.Equal(t, target1Metadata["key2"], "value") + + target2Metadata := snap.AllOperations()[1].AllMetadata() + require.Len(t, target2Metadata, 1) + require.Equal(t, target2Metadata["key2"], "value2") + + op2 := NewSetMetadataOp[*snapshotMock](2, rene, unix, target2.Id(), map[string]string{ + "key2": "value", + "key3": "value3", + }) + + op2.Apply(snap) + snap.ops = append(snap.ops, op2) + + target1Metadata = snap.AllOperations()[0].AllMetadata() + require.Len(t, target1Metadata, 2) + require.Equal(t, target1Metadata["key"], "value") + require.Equal(t, target1Metadata["key2"], "value") + + target2Metadata = snap.AllOperations()[1].AllMetadata() + require.Len(t, target2Metadata, 2) + // original key is not overrided + require.Equal(t, target2Metadata["key2"], "value2") + // new key is set + require.Equal(t, target2Metadata["key3"], "value3") + + op3 := NewSetMetadataOp[*snapshotMock](2, rene, unix, target1.Id(), map[string]string{ + "key": "override", + "key2": "override", + }) + + op3.Apply(snap) + snap.ops = append(snap.ops, op3) + + target1Metadata = snap.AllOperations()[0].AllMetadata() + require.Len(t, target1Metadata, 2) + // original key is not overrided + require.Equal(t, target1Metadata["key"], "value") + // previously set key is not overrided + require.Equal(t, target1Metadata["key2"], "value") + + target2Metadata = snap.AllOperations()[1].AllMetadata() + require.Len(t, target2Metadata, 2) + require.Equal(t, target2Metadata["key2"], "value2") + require.Equal(t, target2Metadata["key3"], "value3") +} + +func TestSetMetadataSerialize(t *testing.T) { + SerializeRoundTripTest(t, func(author identity.Interface, unixTime int64) *SetMetadataOperation[*snapshotMock] { + return NewSetMetadataOp[*snapshotMock](1, author, unixTime, "message", map[string]string{ + "key1": "value1", + "key2": "value2", + }) + }) +} |