-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_get_test.go
78 lines (68 loc) · 1.7 KB
/
table_get_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
package yti_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/toudi/yti"
)
func TestTableGet(t *testing.T) {
t.Run("get on empty table", func(t *testing.T) {
t.Parallel()
table := InMemoryTable(t, nil)
_, err := table.Get(func(item TestStruct) bool { return false })
assert.Equal(
t,
yti.ErrItemDoesNotExist,
err,
)
})
t.Run("get - non-unique items", func(t *testing.T) {
t.Parallel()
table := InMemoryPopulatedTable(
t,
[]TestStruct{{Id: 1, Name: "test"}, {Id: 2, Name: "test"}},
nil,
)
_, err := table.Get(func(item TestStruct) bool { return item.Name == "test" })
assert.Equal(
t,
yti.ErrRowsNotUnique,
err,
)
})
t.Run("get - happy path", func(t *testing.T) {
t.Parallel()
var expected = TestStruct{Id: 2, Name: "test"}
table := InMemoryPopulatedTable(
t,
[]TestStruct{{Id: 1, Name: "test"}, expected},
nil,
)
item, err := table.Get(func(item TestStruct) bool { return item.Id == expected.Id })
assert.NoError(t, err)
assert.Equal(
t,
expected,
item,
)
})
}
func TestTableFirst(t *testing.T) {
t.Run("first - empty table", func(t *testing.T) {
t.Parallel()
table := InMemoryTable(t, nil)
_, err := table.First(func(item TestStruct) bool { return true })
assert.Equal(t, yti.ErrItemDoesNotExist, err)
})
t.Run("first - happy path", func(t *testing.T) {
t.Parallel()
var expected = TestStruct{Id: 2, Name: "lookup"}
table := InMemoryPopulatedTable(
t,
[]TestStruct{{Id: 1, Name: "test"}, expected, {Id: 3, Name: "lookup"}},
nil,
)
item, err := table.First(func(item TestStruct) bool { return item.Name == expected.Name })
assert.Equal(t, expected, item)
assert.NoError(t, err)
})
}