-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser_test.go
148 lines (108 loc) · 3.46 KB
/
parser_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
package mgs
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var booleanTests = []struct {
Input string
Caster *CastType
Expected bool
}{
{"true", nil, true},
{"True", nil, true},
{"TRUE", nil, true},
{"TrUe", nil, true},
{"false", nil, false},
{"False", nil, false},
{"FALSE", nil, false},
{"FaLsE", nil, false},
}
func TestShouldReturnCriteriaForParseWithQuery(t *testing.T) {
opts := &FindOptions{}
var value CastType
criteria := Parse("name=John", opts.Caster)
expected := []SearchCriteria{
{
Prefix: false,
Key: "name",
Operation: EQUAL,
Value: "John",
Caster: &value,
},
}
if len(criteria) == 0 {
t.Errorf("Error creating criteria array")
}
assert.Equal(t, expected, criteria)
}
func TestShouldBoolTypeForParseValue(t *testing.T) {
for _, test := range booleanTests {
assert.Equal(t, test.Expected, ParseValue(test.Input, queryHandler, nil))
}
}
func TestShouldIntegerTypeForParseValue(t *testing.T) {
assert.Equal(t, int64(10), ParseValue("10", queryHandler, nil))
}
func TestShouldFloatTypeForParseValue(t *testing.T) {
assert.Equal(t, float64(3.1415), ParseValue("3.1415", queryHandler, nil))
}
func TestShouldDateTimeTypeForParseValue(t *testing.T) {
expected := time.Date(2023, 10, 9, 20, 00, 00, 999000000, time.UTC)
assert.Equal(t, expected, ParseValue("2023-10-09T20:00:00.999Z", queryHandler, nil))
}
func TestShouldStringListTypeForParseValue(t *testing.T) {
expected := []interface{}{"apple", "banana", "grape"}
assert.Equal(t, expected, ParseValue("apple,banana,grape", queryHandler, nil))
}
func TestShouldIntegerListTypeForParseValue(t *testing.T) {
expected := []interface{}{int64(1), int64(2), int64(3)}
assert.Equal(t, expected, ParseValue("1,2,3", queryHandler, nil))
}
func TestShouldRegexTypeForParseValue(t *testing.T) {
assert.Equal(t, "John", ParseValue("John", queryHandler, nil))
}
func TestShouldStringTypeForParseValue(t *testing.T) {
expected := Regex{
Pattern: "@gmail\\.com$",
}
assert.Equal(t, expected, ParseValue("/@gmail\\.com$/", queryHandler, nil))
}
func TestShouldReturnUnsignedIntForParseIntValueToUnsignedIntWhenIsValidNumberString(t *testing.T) {
result, err := parseIntValueToInt("1")
if err != nil {
t.Errorf("Error format string to uint")
}
assert.Equal(t, int64(1), result)
}
func TestShouldReturnErrorForParseIntValueToUnsignedIntWhenIsInvalidNumberString(t *testing.T) {
result, err := parseIntValueToInt("a")
if err == nil {
t.Errorf("Error format string to uint")
}
assert.Equal(t, int64(0), result)
}
func TestShouldIntegerTypeForParseValueWithCastBoolean(t *testing.T) {
cast := BOOLEAN
assert.Equal(t, true, ParseValue("true", queryHandler, &cast))
}
func TestShouldIntegerTypeForParseValueWithCastDate(t *testing.T) {
cast := DATE
expected := time.Date(2023, 10, 9, 20, 00, 00, 999000000, time.UTC)
assert.Equal(t, expected, ParseValue("2023-10-09T20:00:00.999Z", queryHandler, &cast))
}
func TestShouldIntegerTypeForParseValueWithIntCastNumber(t *testing.T) {
cast := NUMBER
assert.Equal(t, int64(10), ParseValue("10", queryHandler, &cast))
}
func TestShouldIntegerTypeForParseValueWithFloatCastNumber(t *testing.T) {
cast := NUMBER
assert.Equal(t, float64(10.5), ParseValue("10.5", queryHandler, &cast))
}
func TestShouldIntegerTypeForParseValueWithCastRegex(t *testing.T) {
cast := PATTERN
expected := Regex{
Pattern: "@gmail\\.com$",
}
assert.Equal(t, expected, ParseValue("/@gmail\\.com$/", queryHandler, &cast))
}