forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinders_test.go
199 lines (155 loc) · 3.93 KB
/
finders_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package pop_test
import (
"testing"
"github.com/markbates/pop"
"github.com/markbates/pop/nulls"
"github.com/stretchr/testify/require"
)
func Test_Find(t *testing.T) {
transaction(func(tx *pop.Connection) {
a := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
a.NoError(err)
u := User{}
err = tx.Find(&u, user.ID)
a.NoError(err)
a.NotEqual(u.ID, 0)
a.Equal(u.Name.String, "Mark")
})
}
func Test_First(t *testing.T) {
transaction(func(tx *pop.Connection) {
a := require.New(t)
first := User{Name: nulls.NewString("Mark")}
err := tx.Create(&first)
a.NoError(err)
last := User{Name: nulls.NewString("Mark")}
err = tx.Create(&last)
a.NoError(err)
u := User{}
err = tx.Where("name = 'Mark'").First(&u)
a.NoError(err)
a.Equal(first.ID, u.ID)
})
}
func Test_Last(t *testing.T) {
transaction(func(tx *pop.Connection) {
a := require.New(t)
first := User{Name: nulls.NewString("Mark")}
err := tx.Create(&first)
a.NoError(err)
last := User{Name: nulls.NewString("Mark")}
err = tx.Create(&last)
a.NoError(err)
u := User{}
err = tx.Where("name = 'Mark'").Last(&u)
a.NoError(err)
a.Equal(last.ID, u.ID)
})
}
func Test_All(t *testing.T) {
transaction(func(tx *pop.Connection) {
a := require.New(t)
for _, name := range []string{"Mark", "Joe", "Jane"} {
user := User{Name: nulls.NewString(name)}
err := tx.Create(&user)
a.NoError(err)
}
u := Users{}
err := tx.All(&u)
a.NoError(err)
a.Equal(len(u), 3)
u = Users{}
err = tx.Where("name = 'Mark'").All(&u)
a.NoError(err)
a.Equal(len(u), 1)
})
}
func Test_Count(t *testing.T) {
transaction(func(tx *pop.Connection) {
a := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
a.NoError(err)
c, err := tx.Count(&user)
a.NoError(err)
a.Equal(c, 1)
c, err = tx.Where("1=1").CountByField(&user, "distinct id")
a.NoError(err)
a.Equal(c, 1)
// should ignore order in count
c, err = tx.Order("id desc").Count(&user)
a.NoError(err)
a.Equal(c, 1)
})
}
func Test_Count_Disregards_Pagination(t *testing.T) {
transaction(func(tx *pop.Connection) {
a := require.New(t)
names := []string{
"Jack",
"Hurley",
"Charlie",
"Desmond",
"Juliet",
"Locke",
"Sawyer",
"Kate",
"Benjamin Linus",
}
for _, name := range names {
user := User{Name: nulls.NewString(name)}
err := tx.Create(&user)
a.NoError(err)
}
first_users := Users{}
second_users := Users{}
q := tx.Paginate(1, 3)
q.All(&first_users)
a.Equal(3, len(first_users))
totalFirstPage := q.Paginator.TotalPages
q = tx.Paginate(2, 3)
q.All(&second_users)
a.Equal(3, len(second_users))
totalSecondPage := q.Paginator.TotalPages
a.NotEqual(0, totalFirstPage)
a.NotEqual(0, totalSecondPage)
a.Equal(totalFirstPage, totalSecondPage)
})
}
func Test_Count_RawQuery(t *testing.T) {
transaction(func(tx *pop.Connection) {
a := require.New(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
a.NoError(err)
c, err := tx.RawQuery("select * from users as users").Count(nil)
a.NoError(err)
a.Equal(c, 1)
c, err = tx.RawQuery("select * from users as users where id = -1").Count(nil)
a.NoError(err)
a.Equal(c, 0)
c, err = tx.RawQuery("select name, max(created_at) from users as users group by name").Count(nil)
a.NoError(err)
a.Equal(c, 1)
c, err = tx.RawQuery("select name from users order by name asc limit 5").Count(nil)
a.NoError(err)
a.Equal(c, 1)
c, err = tx.RawQuery("select name from users order by name asc limit 5 offset 0").Count(nil)
a.NoError(err)
a.Equal(c, 1)
})
}
func Test_Exists(t *testing.T) {
transaction(func(tx *pop.Connection) {
a := require.New(t)
t, _ := tx.Where("id = ?", 0).Exists("users")
a.False(t)
user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
a.NoError(err)
t, _ = tx.Where("id = ?", user.ID).Exists("users")
a.True(t)
})
}