-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
132 lines (114 loc) · 2.71 KB
/
store_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package store
import (
"errors"
"reflect"
"testing"
)
func TestNew(t *testing.T) {
New()
}
func TestNewMapStore(t *testing.T) {
var i interface{} = NewMapStore()
if _, ok := i.(Store); !ok {
t.Error("NewMapStore() does not return a valid Store")
}
}
func TestMapStore_Add(t *testing.T) {
ms := NewMapStore()
wantValue := "Some value"
wantId := ms.Add(wantValue)
for gotId, gotValue := range ms.ledger {
if gotId != wantId {
t.Errorf("MapStore.Add() = %v, want %v", gotId, wantId)
}
if gotValue != wantValue {
t.Errorf("MapStore.Add() = %v, want %v", gotValue, wantValue)
}
}
}
func TestMapStore_Get(t *testing.T) {
type args struct {
id string
}
tests := []struct {
name string
args args
wantValue interface{}
wantOk bool
}{
{"Bool", args{"bool"}, true, true},
{"Int", args{"int"}, 42, true},
{"Error", args{"error"}, errUpdateId, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ms := NewMapStore()
ms.ledger[tt.args.id] = tt.wantValue
gotValue, gotOk := ms.Get(tt.args.id)
if !reflect.DeepEqual(gotValue, tt.wantValue) {
t.Errorf("MapStore.Get() gotValue = %v, want %v", gotValue, tt.wantValue)
}
if gotOk != tt.wantOk {
t.Errorf("MapStore.Get() gotOk = %v, want %v", gotOk, tt.wantOk)
}
})
}
t.Run("Missing", func(t *testing.T) {
ms := NewMapStore()
_, gotOk := ms.Get("DOES NOT EXIST")
if gotOk {
t.Errorf("MapStore.Get() gotOk = %v, want %v", gotOk, false)
}
})
}
func TestMapStore_Update(t *testing.T) {
ms := NewMapStore()
id := "TESTID"
wrongId := "WRONGID"
ms.ledger[id] = 15
wantValue := 42
err := ms.Update(wrongId, wantValue)
if err == nil {
t.Errorf("MapStore.Update() err = %v, want %v", err, errors.New("Update failed no such id"))
}
err = ms.Update(id, wantValue)
if err != nil {
t.Errorf("MapStore.Update() err = %v, want %v", err, nil)
}
if gotValue := ms.ledger[id]; wantValue != gotValue {
t.Errorf("MapStore.Update() gotValue = %v, want %v", gotValue, wantValue)
}
}
func TestMapStore_Delete(t *testing.T) {
id := "TESTID"
ms := NewMapStore()
ms.ledger[id] = true
if _, ok := ms.ledger[id]; !ok {
t.Errorf("MapStore.Delete() setup failed")
}
ms.Delete(id)
if _, ok := ms.ledger[id]; ok {
t.Errorf("MapStore.Delete() failed: %v still exists", id)
}
}
type ClashDetector map[string]bool
func (cd ClashDetector) Clash(id string) bool {
if cd[id] {
return true
}
cd[id] = true
return false
}
func TestNewId(t *testing.T) {
cd := ClashDetector(make(map[string]bool))
cc := 0
for i := 0; i < 20000; i++ {
if id := NewId(); cd.Clash(id) {
t.Log("Clash detected: ", id)
cc++
}
}
if cc > 0 {
t.Errorf("Repeated calls to NewId() resulted in %d clashes", cc)
}
}