-
Notifications
You must be signed in to change notification settings - Fork 5
/
query_orderby_test.go
159 lines (120 loc) · 5.97 KB
/
query_orderby_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
package tormenta_test
import (
"fmt"
"testing"
"github.com/jpincas/tormenta"
"github.com/jpincas/tormenta/testtypes"
)
// Test range queries across different types
func Test_OrderBy(t *testing.T) {
var fullStructs []tormenta.Record
for i := 0; i < 10; i++ {
// Notice that the intField and StringField increment in oposite ways,
// such that sorting by either field will produce inverse results.
// Also - we only go up to 9 so as to avoid alphabetical sorting
// issues with numbers prefixed by 0
fullStructs = append(fullStructs, &testtypes.FullStruct{
IntField: 10 - i,
StringField: fmt.Sprintf("int-%v", i),
})
}
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
db.Save(fullStructs...)
// INTFIELD
intFieldResults := []testtypes.FullStruct{}
n, err := db.Find(&intFieldResults).OrderBy("IntField").Run()
if err != nil {
t.Errorf("Testing ORDER BY intfield, got error %s", err)
}
if n != len(fullStructs) {
t.Fatalf("Testing ORDER BY intfield, n (%v) does not equal actual number of records saved (%v)", n, len(fullStructs))
}
if n != len(intFieldResults) {
t.Errorf("Testing ORDER BY intfield, n (%v) does not equal actual number of results (%v)", n, len(intFieldResults))
}
if intFieldResults[0].IntField != 1 {
t.Errorf("Testing ORDER BY intfield, first member should be 1 but is %v", intFieldResults[0].IntField)
}
if intFieldResults[len(intFieldResults)-1].IntField != 10 {
t.Errorf("Testing ORDER BY intfield, last member should be 10 but is %v", intFieldResults[len(intFieldResults)-1].IntField)
}
// STRING FIELD
stringFieldResults := []testtypes.FullStruct{}
n, err = db.Find(&stringFieldResults).OrderBy("StringField").Run()
if err != nil {
t.Errorf("Testing ORDER BY stringfield, got error %s", err)
}
if n != len(fullStructs) {
t.Errorf("Testing ORDER BY stringfield, n (%v) does not equal actual number of records saved (%v)", n, len(fullStructs))
}
if n != len(stringFieldResults) {
t.Errorf("Testing ORDER BY stringfield, n (%v) does not equal actual number of results (%v)", n, len(stringFieldResults))
}
if stringFieldResults[0].StringField != "int-0" {
t.Errorf("Testing ORDER BY stringfield, first member should be int-0 but is %s", stringFieldResults[0].StringField)
}
if stringFieldResults[len(stringFieldResults)-1].StringField != "int-9" {
t.Errorf("Testing ORDER BY stringfield, last member should be int-9 but is %s", stringFieldResults[len(intFieldResults)-1].StringField)
}
// Now compare first members and make sure they are different
if intFieldResults[0].ID == stringFieldResults[0].ID {
t.Errorf("Testing ORDER BY. ID's of first member of both results arrays are the same")
}
// Now compare last members and make sure they are different
if intFieldResults[len(intFieldResults)-1].ID == stringFieldResults[len(stringFieldResults)-1].ID {
t.Errorf("Testing ORDER BY. ID's of first member of both results arrays are the same")
}
//Now compare first and last members and make sure they are the same
if intFieldResults[0].ID != stringFieldResults[len(stringFieldResults)-1].ID {
t.Errorf("Testing ORDER BY. First member of array A should be the same as last member of Array B but got %v vs %v", intFieldResults[0].IntField, stringFieldResults[len(stringFieldResults)-1].IntField)
}
// INTFIELD REVERSE
intFieldResults = []testtypes.FullStruct{}
n, err = db.Find(&intFieldResults).OrderBy("IntField").Reverse().Run()
if err != nil {
t.Errorf("Testing ORDER BY, REVERSE intfield, got error %s", err)
}
if n != len(fullStructs) {
t.Fatalf("Testing ORDER BY, REVERSE intfield, n (%v) does not equal actual number of records saved (%v)", n, len(fullStructs))
}
if n != len(intFieldResults) {
t.Errorf("Testing ORDER BY, REVERSE intfield, n (%v) does not equal actual number of results (%v)", n, len(intFieldResults))
}
if intFieldResults[0].IntField != 10 {
t.Errorf("Testing ORDER BY, REVERSE intfield, first member should be 10 but is %v", intFieldResults[0].IntField)
}
if intFieldResults[len(intFieldResults)-1].IntField != 1 {
t.Errorf("Testing ORDER BY, REVERSE intfield, last member should be 1 but is %v", intFieldResults[len(intFieldResults)-1].IntField)
}
// STRING FIELD REVERSE
stringFieldResults = []testtypes.FullStruct{}
n, err = db.Find(&stringFieldResults).OrderBy("StringField").Reverse().Run()
if err != nil {
t.Errorf("Testing ORDER BY, REVERSE stringfield, got error %s", err)
}
if n != len(fullStructs) {
t.Errorf("Testing ORDER BY, REVERSE stringfield, n (%v) does not equal actual number of records saved (%v)", n, len(fullStructs))
}
if n != len(stringFieldResults) {
t.Errorf("Testing ORDER BY, REVERSE stringfield, n (%v) does not equal actual number of results (%v)", n, len(stringFieldResults))
}
if stringFieldResults[0].StringField != "int-9" {
t.Errorf("Testing ORDER BY, REVERSE stringfield, first member should be int-9 but is %s", stringFieldResults[0].StringField)
}
if stringFieldResults[len(stringFieldResults)-1].StringField != "int-0" {
t.Errorf("Testing ORDER BY, REVERSE stringfield, last member should be int-0 but is %s", stringFieldResults[len(intFieldResults)-1].StringField)
}
// Now compare first members and make sure they are different
if intFieldResults[0].ID == stringFieldResults[0].ID {
t.Errorf("Testing ORDER BY, REVERSE. ID's of first member of both results arrays are the same")
}
// Now compare last members and make sure they are different
if intFieldResults[len(intFieldResults)-1].ID == stringFieldResults[len(stringFieldResults)-1].ID {
t.Errorf("Testing ORDER BY, REVERSE. ID's of first member of both results arrays are the same")
}
//Now compare first and last members and make sure they are the same
if intFieldResults[0].ID != stringFieldResults[len(stringFieldResults)-1].ID {
t.Errorf("Testing ORDER BY, REVERSE. First member of array A should be the same as last member of Array B but got %v vs %v", intFieldResults[0].IntField, stringFieldResults[len(stringFieldResults)-1].IntField)
}
}