...
1
16
17 package cache
18
19 import (
20 "reflect"
21 "testing"
22 )
23
24
25
26
27 type testUndeltaObject struct {
28 name string
29 val interface{}
30 }
31
32 func testUndeltaKeyFunc(obj interface{}) (string, error) {
33 return obj.(testUndeltaObject).name, nil
34 }
35
36
43
44 func TestUpdateCallsPush(t *testing.T) {
45 mkObj := func(name string, val interface{}) testUndeltaObject {
46 return testUndeltaObject{name: name, val: val}
47 }
48
49 var got []interface{}
50 var callcount = 0
51 push := func(m []interface{}) {
52 callcount++
53 got = m
54 }
55
56 u := NewUndeltaStore(push, testUndeltaKeyFunc)
57
58 u.Add(mkObj("a", 2))
59 u.Update(mkObj("a", 1))
60 if callcount != 2 {
61 t.Errorf("Expected 2 calls, got %d", callcount)
62 }
63
64 l := []interface{}{mkObj("a", 1)}
65 if !reflect.DeepEqual(l, got) {
66 t.Errorf("Expected %#v, Got %#v", l, got)
67 }
68 }
69
70 func TestDeleteCallsPush(t *testing.T) {
71 mkObj := func(name string, val interface{}) testUndeltaObject {
72 return testUndeltaObject{name: name, val: val}
73 }
74
75 var got []interface{}
76 var callcount = 0
77 push := func(m []interface{}) {
78 callcount++
79 got = m
80 }
81
82 u := NewUndeltaStore(push, testUndeltaKeyFunc)
83
84 u.Add(mkObj("a", 2))
85 u.Delete(mkObj("a", ""))
86 if callcount != 2 {
87 t.Errorf("Expected 2 calls, got %d", callcount)
88 }
89 expected := []interface{}{}
90 if !reflect.DeepEqual(expected, got) {
91 t.Errorf("Expected %#v, Got %#v", expected, got)
92 }
93 }
94
95 func TestReadsDoNotCallPush(t *testing.T) {
96 push := func(m []interface{}) {
97 t.Errorf("Unexpected call to push!")
98 }
99
100 u := NewUndeltaStore(push, testUndeltaKeyFunc)
101
102
103 _ = u.List()
104 _, _, _ = u.Get(testUndeltaObject{"a", ""})
105 }
106
107 func TestReplaceCallsPush(t *testing.T) {
108 mkObj := func(name string, val interface{}) testUndeltaObject {
109 return testUndeltaObject{name: name, val: val}
110 }
111
112 var got []interface{}
113 var callcount = 0
114 push := func(m []interface{}) {
115 callcount++
116 got = m
117 }
118
119 u := NewUndeltaStore(push, testUndeltaKeyFunc)
120
121 m := []interface{}{mkObj("a", 1)}
122
123 u.Replace(m, "0")
124 if callcount != 1 {
125 t.Errorf("Expected 1 calls, got %d", callcount)
126 }
127 expected := []interface{}{mkObj("a", 1)}
128 if !reflect.DeepEqual(expected, got) {
129 t.Errorf("Expected %#v, Got %#v", expected, got)
130 }
131 }
132
View as plain text