-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdatabase_test.go
More file actions
152 lines (135 loc) · 3.56 KB
/
database_test.go
File metadata and controls
152 lines (135 loc) · 3.56 KB
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 limen
import (
"testing"
"time"
)
func TestWhereConditions(t *testing.T) {
tests := []struct {
name string
condition Where
expected Where
}{
{
name: "Eq condition",
condition: Eq("email", "test@example.com"),
expected: Where{
Column: "email",
Operator: OpEq,
Value: "test@example.com",
Connector: ConnectorAnd,
},
},
{
name: "Ne condition",
condition: Ne("status", "inactive"),
expected: Where{
Column: "status",
Operator: OpNe,
Value: "inactive",
Connector: ConnectorAnd,
},
},
{
name: "Lt condition",
condition: Lt("age", 18),
expected: Where{
Column: "age",
Operator: OpLt,
Value: 18,
Connector: ConnectorAnd,
},
},
{
name: "Contains condition",
condition: Contains("name", "john"),
expected: Where{
Column: "name",
Operator: OpContains,
Value: "john",
Connector: ConnectorAnd,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.condition.Column != tt.expected.Column {
t.Errorf("Expected field %s, got %s", tt.expected.Column, tt.condition.Column)
}
if tt.condition.Operator != tt.expected.Operator {
t.Errorf("Expected operator %s, got %s", tt.expected.Operator, tt.condition.Operator)
}
if tt.condition.Value != tt.expected.Value {
t.Errorf("Expected value %v, got %v", tt.expected.Value, tt.condition.Value)
}
if tt.condition.Connector != tt.expected.Connector {
t.Errorf("Expected connector %s, got %s", tt.expected.Connector, tt.condition.Connector)
}
})
}
}
func TestOrModifier(t *testing.T) {
condition := Eq("email", "test@example.com").Or()
if condition.Connector != ConnectorOr {
t.Errorf("Expected connector to be OR, got %s", condition.Connector)
}
if condition.Column != "email" {
t.Errorf("Expected field to remain 'email', got %s", condition.Column)
}
if condition.Operator != OpEq {
t.Errorf("Expected operator to remain 'eq', got %s", condition.Operator)
}
}
func TestQueryOptions(t *testing.T) {
options := &QueryOptions{
Limit: 10,
Offset: 20,
OrderBy: []OrderBy{
{
Column: "created_at",
Direction: OrderByDesc,
},
{
Column: "name",
Direction: OrderByAsc,
},
},
}
if options.Limit != 10 {
t.Errorf("Expected limit to be 10, got %d", options.Limit)
}
if options.Offset != 20 {
t.Errorf("Expected offset to be 20, got %d", options.Offset)
}
if len(options.OrderBy) != 2 {
t.Errorf("Expected 2 order by clauses, got %d", len(options.OrderBy))
}
if options.OrderBy[0].Column != "created_at" || options.OrderBy[0].Direction != OrderByDesc {
t.Errorf("Expected first order by to be 'created_at DESC', got %s", options.OrderBy[0])
}
}
func TestComplexConditions(t *testing.T) {
now := time.Now()
conditions := []Where{
Eq("status", "active"),
Gt("created_at", now.AddDate(0, -1, 0)),
Contains("email", "gmail").Or(),
In("role", []any{"admin", "user"}),
}
if len(conditions) != 4 {
t.Errorf("Expected 4 conditions, got %d", len(conditions))
}
// Check that OR condition is properly set
orCondition := conditions[2]
if orCondition.Connector != ConnectorOr {
t.Errorf("Expected third condition to have OR connector, got %s", orCondition.Connector)
}
// Check that other conditions have AND connector
for i, condition := range conditions {
if i == 2 {
continue // Skip the OR condition
}
if condition.Connector != ConnectorAnd {
t.Errorf("Expected condition %d to have AND connector, got %s", i, condition.Connector)
}
}
}