-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort_test.go
152 lines (122 loc) · 3.55 KB
/
sort_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
package es_test
import (
"reflect"
"testing"
Mode "github.com/Trendyol/es-query-builder/es/enums/sort/mode"
Order "github.com/Trendyol/es-query-builder/es/enums/sort/order"
"github.com/Trendyol/es-query-builder/es"
"github.com/Trendyol/es-query-builder/test/assert"
)
func Test_Sort_should_exist_on_es_package(t *testing.T) {
t.Parallel()
// Given When Then
assert.NotNil(t, es.Sort)
}
func Test_Sort_method_should_create_sortType(t *testing.T) {
t.Parallel()
// Given
sort := es.Sort("date")
// Then
assert.NotNil(t, sort)
assert.IsTypeString(t, "es.sortType", sort)
}
func Test_Sort_should_have_Mode_method(t *testing.T) {
t.Parallel()
// Given
sort := es.Sort("date")
// When Then
assert.NotNil(t, sort.Mode)
}
func Test_Mode_should_add_mode_field_into_Sort(t *testing.T) {
t.Parallel()
// Given
sort := es.Sort("date").Mode(Mode.Median)
// When Then
assert.NotNil(t, sort)
bodyJSON := assert.MarshalWithoutError(t, sort)
assert.Equal(t, "{\"date\":{\"mode\":\"median\"}}", bodyJSON)
}
func Test_Sort_should_have_Order_method(t *testing.T) {
t.Parallel()
// Given
sort := es.Sort("date")
// When Then
assert.NotNil(t, sort.Order)
}
func Test_Order_should_add_order_field_into_Sort(t *testing.T) {
t.Parallel()
// Given
sort := es.Sort("date").Order(Order.Desc)
// When Then
assert.NotNil(t, sort)
bodyJSON := assert.MarshalWithoutError(t, sort)
assert.Equal(t, "{\"date\":{\"order\":\"desc\"}}", bodyJSON)
}
func Test_Sort_should_have_Nested_method(t *testing.T) {
t.Parallel()
// Given
sort := es.Sort("date")
// When Then
assert.NotNil(t, sort.Nested)
}
func Test_Nested_should_add_nested_field_into_Sort(t *testing.T) {
t.Parallel()
// Given
sort := es.Sort("date").Nested(es.NestedSort("timestamp"))
// When Then
assert.NotNil(t, sort)
bodyJSON := assert.MarshalWithoutError(t, sort)
assert.Equal(t, "{\"date\":{\"nested\":{\"path\":\"timestamp\"}}}", bodyJSON)
}
func Test_Sort_should_return_sortType_with_order(t *testing.T) {
t.Parallel()
// Given
sort := es.Sort("name").Order(Order.Asc)
// When
bodyType := reflect.TypeOf(sort).String()
// Then
assert.NotNil(t, sort)
assert.Equal(t, "es.sortType", bodyType)
bodyJSON := assert.MarshalWithoutError(t, sort)
assert.Equal(t, "{\"name\":{\"order\":\"asc\"}}", bodyJSON)
}
func Test_Sort_should_return_sortType_with_mode(t *testing.T) {
t.Parallel()
// Given
sort := es.Sort("age").Mode(Mode.Median)
// When
bodyType := reflect.TypeOf(sort).String()
// Then
assert.NotNil(t, sort)
assert.Equal(t, "es.sortType", bodyType)
bodyJSON := assert.MarshalWithoutError(t, sort)
assert.Equal(t, "{\"age\":{\"mode\":\"median\"}}", bodyJSON)
}
func Test_Sort_should_return_sortType_with_order_and_mode(t *testing.T) {
t.Parallel()
// Given
sort := es.Sort("salary").Order(Order.Desc).Mode(Mode.Sum)
// When
bodyType := reflect.TypeOf(sort).String()
// Then
assert.NotNil(t, sort)
assert.Equal(t, "es.sortType", bodyType)
bodyJSON := assert.MarshalWithoutError(t, sort)
assert.Equal(t, "{\"salary\":{\"mode\":\"sum\",\"order\":\"desc\"}}", bodyJSON)
}
func Test_Sort_should_add_sort_field_into_Object(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
_, beforeExists := query["sort"]
query.Sort(es.Sort("name").Order(Order.Desc))
sort, afterExists := query["sort"]
// Then
assert.NotNil(t, sort)
assert.False(t, beforeExists)
assert.True(t, afterExists)
assert.IsTypeString(t, "[]es.sortType", sort)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"query\":{},\"sort\":[{\"name\":{\"order\":\"desc\"}}]}", bodyJSON)
}