forked from mrz1836/go-datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
139 lines (115 loc) · 3.31 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
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")
})
}