|
1
|
package main |
|
2
|
|
|
3
|
import ( |
|
4
|
"errors" |
|
5
|
"testing" |
|
6
|
|
|
7
|
"olexsmir.xyz/x/is" |
|
8
|
) |
|
9
|
|
|
10
|
func testCache(t *testing.T) *Cache { |
|
11
|
t.Helper() |
|
12
|
t.Setenv("HOME", t.TempDir()) |
|
13
|
c, err := NewCache() |
|
14
|
is.Err(t, err, nil) |
|
15
|
return c |
|
16
|
} |
|
17
|
|
|
18
|
func testEntry() *Entry { |
|
19
|
return &Entry{ |
|
20
|
Word: "test", |
|
21
|
POSBlocks: []POSBlock{ |
|
22
|
{POS: "noun", IPA: "/test/", Senses: []Sense{{Definition: "a test"}}}, |
|
23
|
}, |
|
24
|
} |
|
25
|
} |
|
26
|
|
|
27
|
func TestCache_WriteRead(t *testing.T) { |
|
28
|
c := testCache(t) |
|
29
|
entry := testEntry() |
|
30
|
is.Err(t, c.Write("test", entry, true), nil) |
|
31
|
|
|
32
|
got, err := c.Read("test") |
|
33
|
is.Err(t, err, nil) |
|
34
|
is.Equal(t, entry.Word, got.Word) |
|
35
|
is.Equal(t, len(entry.POSBlocks), len(got.POSBlocks)) |
|
36
|
is.Equal(t, entry.POSBlocks[0].POS, got.POSBlocks[0].POS) |
|
37
|
is.Equal(t, entry.POSBlocks[0].IPA, got.POSBlocks[0].IPA) |
|
38
|
is.Equal(t, entry.POSBlocks[0].Senses[0].Definition, got.POSBlocks[0].Senses[0].Definition) |
|
39
|
} |
|
40
|
|
|
41
|
func TestCache_Read_miss(t *testing.T) { |
|
42
|
c := testCache(t) |
|
43
|
_, err := c.Read("nonexistent") |
|
44
|
is.Err(t, err, "no such file or directory") |
|
45
|
} |
|
46
|
|
|
47
|
func TestCache_Clear(t *testing.T) { |
|
48
|
c := testCache(t) |
|
49
|
is.Err(t, c.Write("test", testEntry(), true), nil) |
|
50
|
is.Err(t, c.Clear("test"), nil) |
|
51
|
_, err := c.Read("test") |
|
52
|
is.Err(t, err, "no such file or directory") |
|
53
|
} |
|
54
|
|
|
55
|
func TestCache_Clear_notFound(t *testing.T) { |
|
56
|
c := testCache(t) |
|
57
|
is.Err(t, c.Write("test", nil, false), nil) |
|
58
|
is.Err(t, c.Clear("test"), "no such file or directory") |
|
59
|
_, err := c.Read("test") |
|
60
|
is.Err(t, err, "no such file or directory") |
|
61
|
} |
|
62
|
|
|
63
|
func TestCache_Clear_missing(t *testing.T) { |
|
64
|
c := testCache(t) |
|
65
|
is.Err(t, c.Clear("nonexistent"), "no such file or directory") |
|
66
|
} |
|
67
|
|
|
68
|
func TestCache_Read_isolationBetweenWords(t *testing.T) { |
|
69
|
c := testCache(t) |
|
70
|
is.Err(t, c.Write("foo", testEntry(), true), nil) |
|
71
|
_, err := c.Read("bar") |
|
72
|
is.Err(t, err, "no such file or directory") |
|
73
|
} |
|
74
|
|
|
75
|
func TestCache_Write_notFound(t *testing.T) { |
|
76
|
c := testCache(t) |
|
77
|
is.Err(t, c.Write("xyz", nil, false), nil) |
|
78
|
_, err := c.Read("xyz") |
|
79
|
is.Equal(t, true, errors.Is(err, ErrNotFound)) |
|
80
|
} |
|
81
|
|
|
82
|
func TestCache_Write_notFound_overridesEntry(t *testing.T) { |
|
83
|
c := testCache(t) |
|
84
|
is.Err(t, c.Write("test", testEntry(), true), nil) |
|
85
|
is.Err(t, c.Write("test", nil, false), nil) |
|
86
|
_, err := c.Read("test") |
|
87
|
is.Equal(t, true, errors.Is(err, ErrNotFound)) |
|
88
|
} |
|
89
|
|
|
90
|
func TestCache_Write_notFound_isolation(t *testing.T) { |
|
91
|
c := testCache(t) |
|
92
|
is.Err(t, c.Write("foo", nil, false), nil) |
|
93
|
is.Err(t, c.Write("bar", testEntry(), true), nil) |
|
94
|
|
|
95
|
_, err := c.Read("foo") |
|
96
|
is.Equal(t, true, errors.Is(err, ErrNotFound)) |
|
97
|
|
|
98
|
got, err := c.Read("bar") |
|
99
|
is.Err(t, err, nil) |
|
100
|
is.Equal(t, "test", got.Word) |
|
101
|
} |