-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataservices_test.go
213 lines (190 loc) · 7.19 KB
/
dataservices_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
package dataservicex_test
import (
"context"
"fmt"
"net"
"testing"
"time"
sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/server"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/information_schema"
"github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/mysql"
"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/vnteamopen/dataservicex"
)
type Person struct {
ID int64 `db:"person_id" goqu:"skipupdate"`
Name string `db:"name"`
Age int64 `db:"age"`
Height float64 `db:"height"`
CreatedAt mysql.NullTime `db:"created_at"`
}
func (Person) TableName() string {
return "person"
}
func (Person) IDColumnName() string {
return "person_id"
}
func TestCRUD(t *testing.T) {
address, port, dbName := fakeDB()
db := setup(address, port, dbName)
defer db.Close()
dialect := goqu.Dialect("mysql")
dataService := dataservicex.NewDataServices(db, dataservicex.WithDialect[Person](dialect))
// CREATE DB
createdAt := time.Now()
createdPerson, err := dataService.Create(context.Background(), Person{
Name: "Thuc Le",
Age: 25,
Height: 165.5,
CreatedAt: mysql.NullTime{Valid: true, Time: createdAt},
})
assert.NoError(t, err, "Create person")
assert.NotEqual(t, 0, createdPerson.ID, "created person ID shouldn't be 0")
assert.Equal(t, "Thuc Le", createdPerson.Name, "created person Name")
assert.Equal(t, int64(25), createdPerson.Age, "created person Age")
assert.Equal(t, 165.5, createdPerson.Height, "created person Height")
assert.Equal(t, createdAt.UTC().Format(time.RFC3339), createdPerson.CreatedAt.Time.UTC().Format(time.RFC3339), "created person CreatedAt")
// UPDATE
updatingPerson := createdPerson
updatingPerson.Name = "ledongthuc"
updatingPerson.Height = 170.1
updatedPerson, err := dataService.Update(context.Background(), updatingPerson.ID, updatingPerson)
assert.NoError(t, err)
assert.Equal(t, updatingPerson.ID, updatedPerson.ID, "updated person ID")
assert.Equal(t, "ledongthuc", updatedPerson.Name, "updated person Name")
assert.Equal(t, int64(25), createdPerson.Age, "updated person Age")
assert.Equal(t, 170.1, updatedPerson.Height, "updated person Height")
assert.Equal(t, createdAt.UTC().Format(time.RFC3339), createdPerson.CreatedAt.Time.UTC().Format(time.RFC3339), "created person CreatedAt")
// UPDATE columns
updatingPerson = updatedPerson
updatingPerson.Name = "Thuc Le"
updatingPerson.Height = 16
err = dataService.UpdateColumns(context.Background(), updatingPerson.ID, goqu.Record{
"name": "Thuc Le",
"height": 165.5,
})
assert.NoError(t, err)
// GET after update
updatedPerson, err = dataService.GetByID(context.Background(), updatingPerson.ID)
assert.NoError(t, err)
assert.Equal(t, updatingPerson.ID, updatedPerson.ID, "updated columns person ID")
assert.Equal(t, "Thuc Le", updatedPerson.Name, "updated columns person Name")
assert.Equal(t, int64(25), createdPerson.Age, "updated columns person Age")
assert.Equal(t, 165.5, updatedPerson.Height, "updated columns person Height")
assert.Equal(t, createdAt.UTC().Format(time.RFC3339), createdPerson.CreatedAt.Time.UTC().Format(time.RFC3339), "created person CreatedAt")
// CREATE 2nd records
createdPerson2, err := dataService.Create(context.Background(), Person{
Name: "Thuc Le (2)",
Age: 252,
Height: 165.52,
CreatedAt: mysql.NullTime{Valid: true, Time: createdAt},
})
assert.NoError(t, err, "Create person (2)")
assert.NotEqual(t, 0, createdPerson2.ID, "created person (2) ID shouldn't be 0")
assert.Equal(t, "Thuc Le (2)", createdPerson2.Name, "created person (2) Name")
assert.Equal(t, int64(252), createdPerson2.Age, "created person (2) Age")
assert.Equal(t, 165.52, createdPerson2.Height, "created person (2) Height")
assert.Equal(t, createdAt.UTC().Format(time.RFC3339), createdPerson2.CreatedAt.Time.UTC().Format(time.RFC3339), "created person (2) CreatedAt")
// GET LIST
personList, err := dataService.GetList(context.Background())
assert.NoError(t, err)
assert.Equal(t, 2, len(personList))
assert.NotEqual(t, personList[0].ID, personList[1].ID)
for index, person := range personList {
switch person.ID {
case updatedPerson.ID:
assert.Equal(t, "Thuc Le", person.Name, "updated columns person Name")
assert.Equal(t, int64(25), person.Age, "updated columns person Age")
assert.Equal(t, 165.5, person.Height, "updated columns person Height")
assert.Equal(t, createdAt.UTC().Format(time.RFC3339), person.CreatedAt.Time.UTC().Format(time.RFC3339), "created person CreatedAt")
case createdPerson2.ID:
assert.Equal(t, "Thuc Le (2)", person.Name, "created person (2) Name")
assert.Equal(t, int64(252), person.Age, "created person (2) Age")
assert.Equal(t, 165.52, person.Height, "created person (2) Height")
assert.Equal(t, createdAt.UTC().Format(time.RFC3339), person.CreatedAt.Time.UTC().Format(time.RFC3339), "created person (2) CreatedAt")
default:
t.Errorf("Unexpected person[%d] ID: %v. Expected in: %v, %v", index, person.ID, updatedPerson.ID, createdPerson2.ID)
}
}
// DELETE
assert.NoError(t, dataService.Delete(context.Background(), personList[0].ID))
assert.NoError(t, dataService.Delete(context.Background(), personList[1].ID))
personList, err = dataService.GetList(context.Background())
assert.NoError(t, err)
assert.Equal(t, 0, len(personList))
}
func fakeDB() (address string, port int, dbName string) {
dbName = "testDB"
engine := sqle.NewDefault(
sql.NewDatabaseProvider(
func(_ *sql.Context) *memory.Database {
db := memory.NewDatabase(dbName)
return db
}(sql.NewEmptyContext()),
information_schema.NewInformationSchemaDatabase(),
))
listener, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
port = listener.Addr().(*net.TCPAddr).Port
if err != nil {
panic(err)
}
listener.Close()
config := server.Config{
Protocol: "tcp",
Address: fmt.Sprintf("%s:%d", address, port),
}
s, err := server.NewDefaultServer(config, engine)
if err != nil {
panic(err)
}
go func() {
if err = s.Start(); err != nil {
panic(err)
}
}()
return "localhost", port, dbName
}
func setup(address string, port int, dbName string) *sqlx.DB {
db, err := sqlx.Connect("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName))
if err != nil {
panic(err)
}
var schema = `
CREATE TABLE person (
person_id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
name TEXT,
age INT,
height REAL,
created_at TIMESTAMP
);`
db.MustExec(schema)
if err = db.Ping(); err != nil {
panic(err)
}
return db
}
func TestGetDialect(t *testing.T) {
address, port, dbName := fakeDB()
db := setup(address, port, dbName)
defer db.Close()
dialect := goqu.Dialect("mysql")
dataService := dataservicex.NewDataServices(db, dataservicex.WithDialect[Person](dialect))
assert.Equal(t, dialect, dataService.GetDialect())
}
func TestGetDBx(t *testing.T) {
address, port, dbName := fakeDB()
db := setup(address, port, dbName)
defer db.Close()
dialect := goqu.Dialect("mysql")
dataService := dataservicex.NewDataServices(db, dataservicex.WithDialect[Person](dialect))
assert.Equal(t, db, dataService.GetDBx())
}