-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_system_store_test.go
147 lines (120 loc) · 3.37 KB
/
file_system_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package poker_test
import (
"io/ioutil"
"log"
"os"
"reflect"
"testing"
poker "github.com/charlie-chiu/go_with_test"
)
func TestFileSystemStore(t *testing.T) {
t.Run("/league from a reader", func(t *testing.T) {
database, cleanDatabase := createTempFile(t, `[
{"Name": "Cleo", "Wins": 10},
{"Name": "Charlie", "Wins": 999}]`)
defer cleanDatabase()
store, err := poker.NewFileSystemPlayerStore(database)
if err != nil {
log.Fatalf("problem creating file system player store, %v", err)
}
got := store.GetLeague()
want := poker.League{
{"Charlie", 999},
{"Cleo", 10},
}
assertLeague(t, got, want)
//read again
got = store.GetLeague()
assertLeague(t, got, want)
})
t.Run("get player score", func(t *testing.T) {
database, cleanDatabase := createTempFile(t, `[
{"Name": "Cleo", "Wins": 10},
{"Name": "Charlie", "Wins": 999}]`)
defer cleanDatabase()
store, err := poker.NewFileSystemPlayerStore(database)
if err != nil {
log.Fatalf("problem creating File system player store, %v", err)
}
got := store.GetPlayerScore("Charlie")
want := 999
AssertScoreEquals(t, got, want)
})
t.Run("store wins for existing players", func(t *testing.T) {
database, cleanDatabase := createTempFile(t, `[
{"Name": "Cleo", "Wins": 10},
{"Name": "Charlie", "Wins": 999}]`)
defer cleanDatabase()
store, err := poker.NewFileSystemPlayerStore(database)
if err != nil {
log.Fatalf("problem creating File system player store, %v", err)
}
store.RecordWin("Cleo")
got := store.GetPlayerScore("Cleo")
want := 11
AssertScoreEquals(t, got, want)
})
t.Run("store wins for new players", func(t *testing.T) {
database, cleanDatabase := createTempFile(t, `[
{"Name": "Cleo", "Wins": 10},
{"Name": "Charlie", "Wins": 999}]`)
defer cleanDatabase()
store, err := poker.NewFileSystemPlayerStore(database)
if err != nil {
log.Fatalf("problem creating file system player store, %v", err)
}
store.RecordWin("Frog")
got := store.GetPlayerScore("Frog")
want := 1
AssertScoreEquals(t, got, want)
})
t.Run("works with an empty file", func(t *testing.T) {
database, cleanDatabase := createTempFile(t, "")
defer cleanDatabase()
_, err := poker.NewFileSystemPlayerStore(database)
AssertNoError(t, err)
})
t.Run("league sorted from highest to lowest", func(t *testing.T) {
file, cleanDatabase := createTempFile(t, `[
{"Name": "Cleo", "Wins": 10},
{"Name": "Charlie", "Wins": 999}]`)
defer cleanDatabase()
store, _ := poker.NewFileSystemPlayerStore(file)
got := store.GetLeague()
want := poker.League{
{"Charlie", 999},
{"Cleo", 10},
}
assertLeague(t, got, want)
})
}
func createTempFile(t *testing.T, initialData string) (*os.File, func()) {
t.Helper()
tempFile, err := ioutil.TempFile("", "db")
if err != nil {
t.Fatalf("could not create temp file, %v", err)
}
tempFile.Write([]byte(initialData))
removeFile := func() {
tempFile.Close()
os.Remove(tempFile.Name())
}
return tempFile, removeFile
}
func assertLeague(t *testing.T, got, want poker.League) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}
func AssertScoreEquals(t *testing.T, got, want int) {
t.Helper()
if got != want {
t.Errorf("Got %d want %d", got, want)
}
}
func AssertNoError(t *testing.T, err error) {
if err != nil {
t.Errorf("didn't expect an error but got one, %v", err)
}
}