-
Notifications
You must be signed in to change notification settings - Fork 115
/
sorting_test.go
61 lines (55 loc) · 2.01 KB
/
sorting_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
package query
import (
"testing"
)
func TestSortCriteria(t *testing.T) {
c := SortCriteria{Tag: "name", Order: SortCriteria_ASC}
if !c.IsAsc() {
t.Errorf("invalid sort order: IsAsc = %v - expected: %v", c.IsAsc(), true)
}
if c.GoString() != "name ASC" {
t.Errorf("invalid string representation: %v - expected: %s", c, "name ASC")
}
c = SortCriteria{Tag: "age", Order: SortCriteria_DESC}
if !c.IsDesc() {
t.Errorf("invalid sort order: IsDesc = %v - expected: %v", c.IsDesc(), true)
}
if c.GoString() != "age DESC" {
t.Errorf("invalid string representation: %v - expected: %s", c, "age DESC")
}
}
func TestParseSorting(t *testing.T) {
s, err := ParseSorting("name")
if err != nil {
t.Fatalf("failed to parse sort parameters: %s", err)
}
if len(s.GetCriterias()) != 1 {
t.Fatalf("invalid number of sort criterias: %d - expected: %d", len(s.GetCriterias()), 1)
}
if c := s.GetCriterias()[0]; !c.IsAsc() || c.Tag != "name" {
t.Errorf("invalid sort criteria: %v - expected: %v", c, SortCriteria{Tag: "name", Order: SortCriteria_ASC})
}
s, err = ParseSorting("name desc, age")
if err != nil {
t.Fatalf("failed to parse sort parameters: %s", err)
}
if len(s.GetCriterias()) != 2 {
t.Fatalf("invalid number of sort criterias: %d - expected: %d", len(s.GetCriterias()), 2)
}
if c := s.GetCriterias()[0]; !c.IsDesc() || c.Tag != "name" {
t.Errorf("invalid sort criteria: %v - expected: %v", c, SortCriteria{Tag: "name", Order: SortCriteria_DESC})
}
if c := s.GetCriterias()[1]; !c.IsAsc() || c.Tag != "age" {
t.Errorf("invalid sort criteria: %v - expected: %v", c, SortCriteria{Tag: "age", Order: SortCriteria_ASC})
}
if s.GoString() != "name DESC, age ASC" {
t.Errorf("invalid sorting: %v - expected: %s", s, "name DESC, age ASC")
}
_, err = ParseSorting("name dask")
if err == nil {
t.Fatal("expected error - got nil")
}
if err.Error() != "invalid sort order - \"dask\" in \"name dask\"" {
t.Errorf("invalid error message: %s - expected: %s", err, "invalid sort order - \"dask\" in \"name dask\"")
}
}