-
Notifications
You must be signed in to change notification settings - Fork 2
/
model_test.go
154 lines (149 loc) · 2.9 KB
/
model_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
package myjson
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestQuery(t *testing.T) {
t.Run("validate empty query", func(t *testing.T) {
q := Query{
Select: nil,
Where: nil,
}
assert.NotNil(t, q.Validate(context.Background()))
})
t.Run("validate no select", func(t *testing.T) {
a := Query{}
assert.NotNil(t, a.Validate(context.Background()))
})
t.Run("validate bad group by", func(t *testing.T) {
a := Query{
GroupBy: []string{"account_id"},
Select: []Select{
{
Field: "age",
Aggregate: AggregateFunctionSum,
As: "age_sum",
},
},
OrderBy: []OrderBy{
{
Field: "account_id",
Direction: OrderByDirectionAsc,
},
},
}
assert.NotNil(t, a.Validate(context.Background()))
})
t.Run("validate bad order by", func(t *testing.T) {
a := Query{
Select: []Select{
{
Field: "*",
},
},
OrderBy: []OrderBy{
{
Field: "account_id",
Direction: "dsc",
},
},
}
assert.NotNil(t, a.Validate(context.Background()))
})
t.Run("validate bad where op", func(t *testing.T) {
a := Query{
Select: []Select{
{
Field: "*",
},
},
Where: []Where{
{
Field: "account_id",
Op: "==",
Value: 9,
},
},
}
assert.NotNil(t, a.Validate(context.Background()))
})
t.Run("validate bad where field", func(t *testing.T) {
a := Query{
Select: []Select{
{
Field: "*",
},
},
Where: []Where{
{
Field: "",
Op: WhereOpEq,
Value: 9,
},
},
}
assert.NotNil(t, a.Validate(context.Background()))
})
t.Run("validate bad where value", func(t *testing.T) {
a := Query{
Select: []Select{
{
Field: "*",
},
},
Where: []Where{
{
Field: "name",
Op: WhereOpEq,
},
},
}
assert.NotNil(t, a.Validate(context.Background()))
})
t.Run("validate bad limit", func(t *testing.T) {
a := Query{
Select: []Select{
{
Field: "*",
},
},
Limit: -1,
}
assert.NotNil(t, a.Validate(context.Background()))
})
t.Run("validate bad page", func(t *testing.T) {
a := Query{
Select: []Select{
{
Field: "*",
},
},
Page: -1,
}
assert.NotNil(t, a.Validate(context.Background()))
})
t.Run("validate good query", func(t *testing.T) {
a := Query{
Select: []Select{
{
Field: "test",
Aggregate: AggregateFunctionMax,
As: "max_test",
},
},
}
assert.Nil(t, a.Validate(context.Background()))
})
}
func TestMetadataContext(t *testing.T) {
ctx := context.Background()
assert.Equal(t, "default", GetMetadataValue(ctx, MetadataKeyNamespace))
c := ExtractMetadata(ctx)
assert.NotNil(t, c)
assert.Equal(t, "default", c.GetString(MetadataKeyNamespace))
c.Set("testing", true)
assert.NotNil(t, c)
c.Set(MetadataKeyNamespace, "acme.com")
assert.Equal(t, "acme.com", c.GetString(MetadataKeyNamespace))
}