-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
131 lines (100 loc) · 1.99 KB
/
storage_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
package debugserver
import (
"reflect"
"testing"
)
func contains(s string, l *list) bool {
item := l.First
for {
if item == nil {
return false
}
if item.Value == s {
return true
}
item = item.Next
}
}
func TestStoreSingleRequest(t *testing.T) {
storage := NewStorage()
r := Request{Body: "test"}
key := "yo"
storage.Add(key, r)
records := storage.Get(key)
if len(records) != 1 {
t.Fatalf("Got %d records, expected: 1", len(records))
}
if records[0].Body != r.Body {
t.Error("Added item is not equal to retrieved")
}
}
func TestAddSingleRecord(t *testing.T) {
l := &list{}
expected := "yo"
l.add(expected)
actual := l.First
if actual.Value != expected {
t.Fatalf("Got: %s, expected: %s", actual.Value, expected)
}
if l.First != l.Last {
t.Error("Last item not set")
}
}
func TestDeleteTheOnlyRecord(t *testing.T) {
l := &list{}
value := "test"
l.add(value)
l.del(value)
if l.First != nil {
t.Error("Reference to first item was not updated")
}
if l.Last != nil {
t.Error("Reference to last item was not updated")
}
}
func TestDeleteMiddleRecord(t *testing.T) {
var (
l = &list{}
first = "test1"
target = "test2"
last = "test3"
)
l.add(first)
l.add(target)
l.add(last)
l.del(target)
if l.First.Value != first {
t.Error("Unexpected first item")
}
if l.Last.Value != last {
t.Error("Unexpected last item")
}
if contains(target, l) {
t.Error("Item was not deleted")
}
}
func TestAddMultipleRecords(t *testing.T) {
l := &list{}
expected := []string{"1", "2", "3", "4", "5"}
for _, s := range expected {
l.add(s)
}
var actual []string
item := l.First
for {
if item == nil {
break
}
actual = append(actual, item.Value)
item = item.Next
}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Got: %s, expected: %s", actual, expected)
}
if l.First.Value != expected[0] {
t.Error("Unexpected first item")
}
if l.Last.Value != expected[len(expected)-1] {
t.Error("Unexpected last item")
}
}