generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_test.go
178 lines (149 loc) · 4.8 KB
/
client_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
package datastore
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testClient will generate a test client
func testClient(ctx context.Context, t *testing.T, opts ...ClientOps) (ClientInterface, func()) {
client, err := NewClient(ctx, opts...)
require.NoError(t, err)
require.NotNil(t, client)
return client, func() {
_ = client.Close(ctx)
}
}
// TestClient_IsDebug will test the method IsDebug()
func TestClient_IsDebug(t *testing.T) {
t.Run("toggle debug", func(t *testing.T) {
c, err := NewClient(context.Background(), WithDebugging())
require.NotNil(t, c)
require.NoError(t, err)
assert.True(t, c.IsDebug())
c.Debug(false)
assert.False(t, c.IsDebug())
})
// Attempt to remove a file created during the test
t.Cleanup(func() {
_ = os.Remove("datastore.db")
})
}
// TestClient_Debug will test the method Debug()
func TestClient_Debug(t *testing.T) {
t.Run("turn debug on", func(t *testing.T) {
c, err := NewClient(context.Background())
require.NotNil(t, c)
require.NoError(t, err)
assert.False(t, c.IsDebug())
c.Debug(true)
assert.True(t, c.IsDebug())
})
// Attempt to remove a file created during the test
t.Cleanup(func() {
_ = os.Remove("datastore.db")
})
}
// TestClient_DebugLog will test the method DebugLog()
func TestClient_DebugLog(t *testing.T) {
t.Run("write debug log", func(t *testing.T) {
c, err := NewClient(context.Background(), WithDebugging())
require.NotNil(t, c)
require.NoError(t, err)
c.DebugLog(context.Background(), "test message")
})
// Attempt to remove a file created during the test
t.Cleanup(func() {
_ = os.Remove("datastore.db")
})
}
// TestClient_Engine will test the method Engine()
func TestClient_Engine(t *testing.T) {
t.Run("[sqlite] - get engine", func(t *testing.T) {
c, err := NewClient(context.Background(), WithSQLite(&SQLiteConfig{
DatabasePath: "",
Shared: true,
}))
assert.NotNil(t, c)
require.NoError(t, err)
assert.Equal(t, SQLite, c.Engine())
})
t.Run("[mongo] - failed to load", func(t *testing.T) {
c, err := NewClient(context.Background(), WithMongo(&MongoDBConfig{
DatabaseName: "test",
Transactions: false,
URI: "",
}))
assert.Nil(t, c)
require.Error(t, err)
})
// todo: add MySQL, Postgresql and MongoDB
}
// TestClient_GetTableName will test the method GetTableName()
func TestClient_GetTableName(t *testing.T) {
t.Run("table prefix", func(t *testing.T) {
c, err := NewClient(context.Background(), WithDebugging(), WithSQLite(&SQLiteConfig{
CommonConfig: CommonConfig{
TablePrefix: testTablePrefix,
},
DatabasePath: "",
Shared: true,
}))
require.NotNil(t, c)
require.NoError(t, err)
tableName := c.GetTableName(testModelName)
assert.Equal(t, testTablePrefix+"_"+testModelName, tableName)
})
t.Run("no table prefix", func(t *testing.T) {
c, err := NewClient(context.Background(), WithDebugging(), WithSQLite(&SQLiteConfig{
CommonConfig: CommonConfig{
TablePrefix: "",
},
DatabasePath: "",
Shared: true,
}))
require.NotNil(t, c)
require.NoError(t, err)
tableName := c.GetTableName(testModelName)
assert.Equal(t, testModelName, tableName)
})
// Attempt to remove a file created during the test
t.Cleanup(func() {
_ = os.Remove("datastore.db")
})
}
// TestClient_GetDatabaseName will test the method GetDatabaseName()
func TestClient_GetDatabaseName(t *testing.T) {
t.Skip("these do not fully work since they try to connect to the database")
t.Run("MySQL database name", func(t *testing.T) {
c, err := NewClient(context.Background(), WithSQL(MySQL, []*SQLConfig{{Name: "test_db"}}))
require.Error(t, err)
require.Nil(t, c)
assert.Equal(t, "test_db", c.GetDatabaseName())
})
t.Run("MongoDB database name", func(t *testing.T) {
c, err := NewClient(context.Background(), WithMongo(&MongoDBConfig{DatabaseName: "test_db", URI: "mongodb://localhost:27017"}))
require.Error(t, err)
require.Nil(t, c)
assert.Equal(t, "test_db", c.GetDatabaseName())
})
}
// TestClient_GetArrayFields will test the method GetArrayFields()
func TestClient_GetArrayFields(t *testing.T) {
t.Run("array fields", func(t *testing.T) {
c, err := NewClient(context.Background(), WithCustomFields([]string{"field1", "field2"}, nil))
require.NoError(t, err)
assert.Equal(t, []string{"field1", "field2"}, c.GetArrayFields())
})
}
// TestClient_GetObjectFields will test the method GetObjectFields()
func TestClient_GetObjectFields(t *testing.T) {
t.Run("object fields", func(t *testing.T) {
c, err := NewClient(context.Background(), WithCustomFields(nil, []string{"field1", "field2"}))
require.NoError(t, err)
assert.Contains(t, c.GetObjectFields(), "metadata")
assert.Contains(t, c.GetObjectFields(), "field1")
assert.Contains(t, c.GetObjectFields(), "field2")
})
}