-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatastore_test.go
236 lines (204 loc) · 5.18 KB
/
datastore_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package frame_test
import (
"github.com/pitabwire/frame"
"gorm.io/datatypes"
"os"
"reflect"
"strconv"
"testing"
)
func TestService_Datastore(t *testing.T) {
testDBURL := frame.GetEnv("TEST_DATABASE_URL", "postgres://frame:secret@localhost:5431/framedatabase?sslmode=disable")
ctx, srv := frame.NewService("Test Srv", frame.NoopDriver())
mainDB := frame.DatastoreConnection(ctx, testDBURL, false)
srv.Init(mainDB)
if srv.Name() != "Test Srv" {
t.Errorf("s")
}
w := srv.DB(ctx, false)
if w == nil {
t.Errorf("No default service could be instantiated")
return
}
r := srv.DB(ctx, true)
if r == nil {
t.Errorf("Could not get read db instantiated")
return
}
rd, _ := r.DB()
if wd, _ := w.DB(); wd != rd {
t.Errorf("Read and write db services should not be different ")
}
srv.Stop(ctx)
}
func TestService_DatastoreSet(t *testing.T) {
os.Setenv("DATABASE_URL", "postgres://frame:secret@localhost:5431/framedatabase?sslmode=disable")
var defConf frame.ConfigurationDefault
err := frame.ConfigProcess("", &defConf)
if err != nil {
t.Errorf("Could not processFunc test configurations %v", err)
return
}
ctx, srv := frame.NewService("Test Srv", frame.Config(&defConf))
srv.Init(frame.Datastore(ctx))
w := srv.DB(ctx, false)
r := srv.DB(ctx, true)
if w == nil || r == nil {
t.Errorf("Read and write services setup but one couldn't be found")
return
}
}
func TestService_DatastoreRead(t *testing.T) {
testDBURL := frame.GetEnv("TEST_DATABASE_URL", "postgres://frame:secret@localhost:5431/framedatabase?sslmode=disable")
ctx, srv := frame.NewService("Test Srv")
mainDB := frame.DatastoreConnection(ctx, testDBURL, false)
readDB := frame.DatastoreConnection(ctx, testDBURL, true)
srv.Init(mainDB, readDB)
w := srv.DB(ctx, false)
r := srv.DB(ctx, true)
if w == nil || r == nil {
t.Errorf("Read and write services setup but one couldn't be found")
return
}
rd, _ := r.DB()
wd, _ := w.DB()
if wd == rd {
t.Errorf("Read and write db services are same but we set different")
}
}
func TestService_DatastoreNotSet(t *testing.T) {
ctx, srv := frame.NewService("Test Srv")
if w := srv.DB(ctx, false); w != nil {
t.Errorf("When no connection is set no db is expected")
}
}
func TestDBPropertiesFromMap(t *testing.T) {
tests := []struct {
name string
propsMap map[string]string
want datatypes.JSONMap
}{
{
name: "happy case",
propsMap: map[string]string{
"a": "a",
"b": "751",
"c": "23.5",
"d": "true",
"e": "[23, 35, 37, 55]",
"f": "{\"x\": \"t\", \"y\": \"g\" }",
},
want: datatypes.JSONMap{
"a": "a",
"b": "751",
"c": "23.5",
"d": "true",
"e": []any{23, 35, 37, 55},
"f": map[string]any{"x": "t", "y": "g"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := frame.DBPropertiesFromMap(tt.propsMap); !compareMapsByValue(got, tt.want) {
t.Errorf("DBPropertiesFromMap() = %v, want %v", got, tt.want)
}
})
}
}
// compareMapsByValue compares two maps only by their values, with special handling for numeric values.
func compareMapsByValue(map1, map2 map[string]interface{}) bool {
if len(map1) != len(map2) {
return false
}
for key, val1 := range map1 {
val2, exists := map2[key]
if !exists {
return false
}
if !compareValues(val1, val2) {
return false
}
}
return true
}
// compareValues compares two interface{} values, handling basic types, slices, nested maps, and numeric comparisons.
func compareValues(val1, val2 interface{}) bool {
// Handle other types including slices and nested maps
switch v1 := val1.(type) {
case string:
return val1 == val2
case []interface{}:
v2, ok := val2.([]interface{})
if !ok || len(v1) != len(v2) {
return false
}
for i := range v1 {
if !compareValues(v1[i], v2[i]) {
return false
}
}
return true
case map[string]interface{}:
v2, ok := val2.(map[string]interface{})
if !ok {
return false
}
return compareMapsByValue(v1, v2)
default:
f1, ok1 := toFloat64(val1)
f2, ok2 := toFloat64(val2)
if ok1 && ok2 {
return f1 == f2
}
return reflect.DeepEqual(val1, val2)
}
}
// toFloat64 attempts to convert an interface{} to float64, returning the value and whether it was successful.
func toFloat64(val interface{}) (float64, bool) {
switch v := val.(type) {
case float64:
return v, true
case int:
return float64(v), true
case string:
if f, err := strconv.ParseFloat(v, 64); err == nil {
return f, true
}
}
return 0, false
}
func TestDBPropertiesToMap(t *testing.T) {
tests := []struct {
name string
dbProps datatypes.JSONMap
want map[string]string
}{
{
name: "happy case",
want: map[string]string{
"a": "a",
"b": "751",
"c": "23.5",
"d": "true",
"e": "[23,35,37,55]",
"f": "{\"x\":\"t\",\"y\":\"g\"}",
},
dbProps: datatypes.JSONMap{
"a": "a",
"b": "751",
"c": "23.5",
"d": "true",
"e": []any{23, 35, 37, 55},
"f": map[string]any{"x": "t", "y": "g"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := frame.DBPropertiesToMap(tt.dbProps); !reflect.DeepEqual(got, tt.want) {
t.Errorf("DBPropertiesToMap() = %v, want %v", got, tt.want)
}
})
}
}