-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_get_or_create_test.go
44 lines (36 loc) · 1.09 KB
/
table_get_or_create_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
package yti_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type Lookup struct {
mock.Mock
}
func (l *Lookup) itemFound(item TestStruct) bool {
args := l.Called(item)
return args.Bool(0)
}
func TestGetOrCreate(t *testing.T) {
t.Run("get or create - item not present", func(t *testing.T) {
t.Parallel()
lookup := &Lookup{}
expectedStruct := TestStruct{Id: 1, Name: "test"}
table := InMemoryTable(t, nil)
item, err := table.GetOrCreate(lookup.itemFound, expectedStruct)
assert.NoError(t, err)
assert.Equal(t, expectedStruct, item)
lookup.AssertNotCalled(t, "itemFound", expectedStruct)
})
t.Run("get or create - item not present", func(t *testing.T) {
t.Parallel()
lookup := &Lookup{}
expectedStruct := TestStruct{Id: 1, Name: "test"}
table := InMemoryPopulatedTable(t, []TestStruct{expectedStruct}, nil)
lookup.On("itemFound", expectedStruct).Return(true)
item, err := table.GetOrCreate(lookup.itemFound, expectedStruct)
assert.NoError(t, err)
assert.Equal(t, expectedStruct, item)
lookup.AssertExpectations(t)
})
}