-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcsvdb_test.go
More file actions
129 lines (110 loc) · 2.74 KB
/
Copy pathcsvdb_test.go
File metadata and controls
129 lines (110 loc) · 2.74 KB
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
package pennybase
import (
"crypto/rand"
"path/filepath"
"slices"
"strconv"
"sync"
"testing"
)
var _ DB = (*csvDB)(nil)
func TestDBBasicOperations(t *testing.T) {
db := must(NewCSVDB(filepath.Join(t.TempDir(), "test.csv"))).T(t)
defer db.Close()
id := rand.Text()
if rec, err := db.Get(id); err == nil {
t.Fatalf("want not record, got %v", rec)
}
initialRec := Record{id, "1", "foo"}
must0(t, db.Create(initialRec))
if rec := must(db.Get(id)).T(t); !slices.Equal(rec, initialRec) {
t.Fatalf("get after create got %v, want %v", rec, initialRec)
}
updatedRec := Record{id, "2", "bar"}
must0(t, db.Update(updatedRec))
if rec := must(db.Get(id)).T(t); !slices.Equal(rec, updatedRec) {
t.Fatalf("get after update got %v, want %v", rec, updatedRec)
}
if err := db.Update(updatedRec); err == nil {
t.Fatal("want error on same value update")
}
must0(t, db.Delete(id))
if rec, err := db.Get(id); err == nil {
t.Fatalf("got unexpected record after delete: %v", rec)
}
if err := db.Update(Record{id, "3", "qux"}); err == nil {
t.Fatal("want error on update after delete")
}
}
func TestEmptyIterator(t *testing.T) {
db, _ := NewCSVDB(filepath.Join(t.TempDir(), "test.csv"))
defer db.Close()
count := 0
for range db.Iter() {
count++
}
if count != 0 {
t.Fatalf("Expected 0 records, got %d", count)
}
}
func TestIteratorWithDeletes(t *testing.T) {
db := must(NewCSVDB(filepath.Join(t.TempDir(), "test.csv"))).T(t)
defer db.Close()
for i := range 10 {
must0(t, db.Create(Record{strconv.Itoa(i), "1", "data"}))
must0(t, db.Delete(strconv.Itoa(i)))
}
must0(t, db.Create(Record{"active", "1", "data"}))
count := 0
for r := range db.Iter() {
if r[0] == "active" {
count++
} else if r[1] != "0" {
t.Errorf("Unexpected record: %v", r)
}
}
if count != 1 {
t.Errorf("Expected 1 active record, got %d", count)
}
}
func TestConcurrent(t *testing.T) {
db := must(NewCSVDB(filepath.Join(t.TempDir(), "test.csv"))).T(t)
defer db.Close()
var wg sync.WaitGroup
for i := range 1000 {
wg.Add(1)
go func() {
defer wg.Done()
id := strconv.Itoa(i)
must0(t, db.Create(Record{id, "1", "data"}))
must(db.Get(id)).T(t)
}()
}
wg.Wait()
for i := range 1000 {
id := strconv.Itoa(i)
must(db.Get(id)).T(t)
}
}
func BenchmarkWrite(b *testing.B) {
db, _ := NewCSVDB(filepath.Join(b.TempDir(), "test.csv"))
defer db.Close()
b.ResetTimer()
for i := range b.N {
id := strconv.Itoa(i)
_ = db.Create(Record{id, "1", "data"})
}
}
func BenchmarkRead(b *testing.B) {
db, _ := NewCSVDB(filepath.Join(b.TempDir(), "test.csv"))
defer db.Close()
for i := range 1000 {
id := strconv.Itoa(i)
_ = db.Create(Record{id, "1", "data"})
}
b.ResetTimer()
for i := range b.N {
id := strconv.Itoa(i % 1000)
_, _ = db.Get(id)
}
}