-
Notifications
You must be signed in to change notification settings - Fork 7
/
table_test.go
122 lines (102 loc) · 3.6 KB
/
table_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
package yaorm_test
import (
"context"
"testing"
"github.com/geoffreybauduin/yaorm"
"github.com/geoffreybauduin/yaorm/testdata"
"github.com/juju/errors"
"github.com/stretchr/testify/assert"
)
func TestNewTable(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{})
assert.NotNil(t, table)
}
func TestTable_Fields(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{})
assert.NotNil(t, table)
assert.Len(t, table.Fields(), 4)
assert.Equal(t, table.Fields()[0], "id")
assert.Equal(t, table.Fields()[1], "name")
assert.Equal(t, table.Fields()[2], "created_at")
assert.Equal(t, table.Fields()[3], "updated_at")
}
func TestTable_Name(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{})
assert.NotNil(t, table)
assert.Equal(t, "category", table.Name())
}
func TestTable_NewModel(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{})
assert.NotNil(t, table)
m, err := table.NewModel()
assert.Nil(t, err)
assert.IsType(t, &testdata.Category{}, m)
}
func TestTable_NewFilter(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{}).WithFilter(testdata.NewCategoryFilter())
assert.NotNil(t, table)
f, err := table.NewFilter()
assert.Nil(t, err)
assert.IsType(t, &testdata.CategoryFilter{}, f)
}
func TestTable_NewSlice(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{})
assert.NotNil(t, table)
m, err := table.NewSlice()
assert.Nil(t, err)
assert.IsType(t, []*testdata.Category{}, m)
}
func TestTable_FieldIndex(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{})
assert.NotNil(t, table)
assert.Equal(t, 1, table.FieldIndex("id"))
assert.Equal(t, -1, table.FieldIndex("unknown"))
}
func TestTable_FilterFieldIndex(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{}).WithFilter(testdata.NewCategoryFilter())
assert.NotNil(t, table)
assert.Equal(t, 1, table.FilterFieldIndex("id"))
assert.Equal(t, -1, table.FilterFieldIndex("unknown"))
}
func TestGetTable(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{})
assert.NotNil(t, table)
retrievedTable, err := yaorm.GetTable("test", "category")
assert.NotNil(t, retrievedTable)
assert.Nil(t, err)
assert.Equal(t, table, retrievedTable)
notFoundTable, err := yaorm.GetTable("test", "category2")
assert.Nil(t, notFoundTable)
assert.NotNil(t, err)
assert.True(t, errors.IsNotFound(err))
notFoundDb, err := yaorm.GetTable("test2", "category2")
assert.Nil(t, notFoundDb)
assert.NotNil(t, err)
assert.True(t, errors.IsNotFound(err))
}
func TestGetTableByFilter(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{}).WithFilter(&testdata.CategoryFilter{})
assert.NotNil(t, table)
foundTable, err := yaorm.GetTableByFilter(&testdata.CategoryFilter{})
assert.Nil(t, err)
assert.NotNil(t, foundTable)
assert.Equal(t, table, foundTable)
}
func TestGetTableByModel(t *testing.T) {
table := yaorm.NewTable("test", "category", &testdata.Category{}).WithFilter(&testdata.CategoryFilter{})
assert.NotNil(t, table)
foundTable, err := yaorm.GetTableByModel(&testdata.Category{})
assert.Nil(t, err)
assert.NotNil(t, foundTable)
assert.Equal(t, table, foundTable)
}
func TestTable_NameForQuery(t *testing.T) {
killDB, err := testdata.SetupTestDatabase("test")
defer killDB()
assert.Nil(t, err)
dbp, err := yaorm.NewDBProvider(context.TODO(), "test")
assert.Nil(t, err)
table, err := yaorm.GetTable("test", "category")
assert.Nil(t, err)
assert.Contains(t, table.NameForQuery(dbp), "category")
}