-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_test.go
109 lines (92 loc) · 2.16 KB
/
data_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
package main
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"go.etcd.io/bbolt"
)
func Test_saveLoad(t *testing.T) {
tempFile, err := os.CreateTemp("", "test.db")
if err != nil {
panic(err)
}
tempFile.Close()
os.Remove(tempFile.Name())
db, err := bbolt.Open(tempFile.Name(), 0600, nil)
if err != nil {
panic(err)
}
defer os.Remove(tempFile.Name())
a := &appData{db: db}
defer a.Close()
sl, err := a.newShoppingList("test")
assert.Nil(t, err)
assert.NotNil(t, sl)
assert.Equal(t, "test", sl.Name)
sl.Items = []item{
{
What: "unchecked",
Checked: false,
},
{
What: "checked",
Checked: true,
},
}
err = a.saveShoppingList(sl)
assert.Nil(t, err)
slEmpty, err := a.newShoppingList("test empty")
assert.Nil(t, err)
assert.NotNil(t, slEmpty)
assert.Equal(t, "test empty", slEmpty.Name)
a.shoppingLists = []*shoppingList{}
err = a.loadShoppingListsFromDB()
assert.Nil(t, err)
assert.Equal(t, 2, len(a.shoppingLists))
assert.Equal(t, "test", a.shoppingLists[0].Name)
assert.Equal(t, "test empty", a.shoppingLists[1].Name)
assert.Equal(t, 2, len(a.shoppingLists[0].Items))
assert.Equal(t, 0, len(a.shoppingLists[1].Items))
assert.Equal(t, "unchecked", a.shoppingLists[0].Items[0].What)
assert.Equal(t, false, a.shoppingLists[0].Items[0].Checked)
assert.Equal(t, "checked", a.shoppingLists[0].Items[1].What)
assert.Equal(t, true, a.shoppingLists[0].Items[1].Checked)
}
func Test_importExportYaml(t *testing.T) {
sl := &shoppingList{
Name: "test",
Items: []item{
{
What: "unchecked",
Checked: false,
},
{
What: "checked",
Checked: true,
},
},
}
yaml := `name: test
items:
- what: unchecked
checked: false
- what: checked
checked: true
`
tmp, err := os.CreateTemp("", "test.yaml")
assert.Nil(t, err)
defer os.Remove(tmp.Name())
err = sl.exportYaml(tmp)
assert.Nil(t, err)
b, err := os.ReadFile(tmp.Name())
assert.Nil(t, err)
assert.Equal(t, yaml, string(b))
reader, err := os.Open(tmp.Name())
assert.Nil(t, err)
assert.NotNil(t, reader)
sl2 := &shoppingList{}
assert.NotEqual(t, sl, sl2)
err = sl2.importYaml(reader)
assert.Nil(t, err)
assert.Equal(t, sl, sl2)
}