summaryrefslogtreecommitdiff
path: root/internal/types
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-06-04 23:32:38 +0300
committerPaul Buetow <paul@buetow.org>2024-06-04 23:32:38 +0300
commitdad73343d8d0d2fe0f69d3749ba2ac853e6e464c (patch)
tree242d089b827e9773c2351dd1a77258f7e1251d6f /internal/types
parenta2001d4db7f19d0023d7c3a76da314571a1a4877 (diff)
dealing with two TODO items
Diffstat (limited to 'internal/types')
-rw-r--r--internal/types/entry.go4
-rw-r--r--internal/types/entry_test.go67
2 files changed, 39 insertions, 32 deletions
diff --git a/internal/types/entry.go b/internal/types/entry.go
index 6211d7b..ca2a742 100644
--- a/internal/types/entry.go
+++ b/internal/types/entry.go
@@ -40,6 +40,9 @@ type Entry struct {
checksum string
checksumDirty bool
mu *sync.Mutex
+
+ // To identify whether this entry was changed.
+ Changed bool `json:"-"`
}
func NewEntry(bytes []byte) (Entry, error) {
@@ -102,6 +105,7 @@ func (e Entry) Update(other Entry) (Entry, error) {
return e, fmt.Errorf("can update entry only with other entry with same ID: %s %s", e, other)
}
e.checksumDirty = true
+ e.Changed = true
if e.Body != other.Body {
e.Body = other.Body
diff --git a/internal/types/entry_test.go b/internal/types/entry_test.go
index 3a1deb7..90e7972 100644
--- a/internal/types/entry_test.go
+++ b/internal/types/entry_test.go
@@ -21,35 +21,6 @@ func TestEntryChecksum(t *testing.T) {
t.Log(entry.Checksum())
}
-func twoDifferentEntries() (entry1, entry2 Entry, err error) {
- entry1Str := `
- {
- "Body": "Body text here",
- "Shared": [
- { "Name": "Foo", "Is": true },
- { "Name": "Bar", "Is": false }
- ]
- }
- `
- entry1, err = NewEntry([]byte(entry1Str))
- if err != nil {
- return
- }
-
- entry2Str := `
- {
- "Body": "Body text here",
- "Shared": [
- { "Name": "Foo", "Is": true },
- { "Name": "Bar", "Is": true },
- { "Name": "Baz", "Is": false }
- ]
- }
- `
- entry2, err = NewEntry([]byte(entry2Str))
- return
-}
-
func TestEquals(t *testing.T) {
t.Parallel()
@@ -72,13 +43,19 @@ func TestUpdate(t *testing.T) {
entry1, entry2, err := twoDifferentEntries()
if err != nil {
t.Error(err)
- return
+ }
+
+ if entry1.Changed {
+ t.Error("didn't expect the entry to be changed before the update", entry1)
}
entry1, _ = entry1.Update(entry2)
if len(entry1.Shared) != 3 {
t.Error("expected 3 entries after update", entry1)
- return
+ }
+
+ if !entry1.Changed {
+ t.Error("expected the entry to be changed after update")
}
var isShared int
@@ -90,8 +67,34 @@ func TestUpdate(t *testing.T) {
if isShared != 2 {
t.Error("expected 2 shared entries after update but got", isShared, entry1)
+ }
+}
+
+func twoDifferentEntries() (entry1, entry2 Entry, err error) {
+ entry1Str := `
+ {
+ "Body": "Body text here",
+ "Shared": [
+ { "Name": "Foo", "Is": true },
+ { "Name": "Bar", "Is": false }
+ ]
+ }
+ `
+ entry1, err = NewEntry([]byte(entry1Str))
+ if err != nil {
return
}
- t.Log("entry as expected after update", entry1)
+ entry2Str := `
+ {
+ "Body": "Body text here",
+ "Shared": [
+ { "Name": "Foo", "Is": true },
+ { "Name": "Bar", "Is": true },
+ { "Name": "Baz", "Is": false }
+ ]
+ }
+ `
+ entry2, err = NewEntry([]byte(entry2Str))
+ return
}