This repository was archived by the owner on Mar 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_test.go
More file actions
134 lines (121 loc) · 3.4 KB
/
exec_test.go
File metadata and controls
134 lines (121 loc) · 3.4 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
package torm
import (
"context"
"regexp"
"testing"
"time"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
"github.com/pinnacles/torm/internal/test"
)
func init() {
Register(test.TestSchema{})
}
func TestInsert(t *testing.T) {
if err := test.WithSqlxMock(func(ctx context.Context, db *sqlx.DB, mock sqlmock.Sqlmock) {
tm := time.Now()
mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `test` (`foo`,`created_at`,`updated_at`) VALUES")).
WithArgs(1, tm, tm).
WillReturnResult(sqlmock.NewResult(1, 1))
builder := NewBuilder(db)
builder.SetTime(&tm)
ts := test.TestSchema{
Foo: 1,
Bar: 2,
}
if _, err := builder.Insert().Exec(ctx, &ts); err != nil {
t.Fatal(err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}); err != nil {
t.Fatal(err)
}
}
func TestInsertColumn(t *testing.T) {
if err := test.WithSqlxMock(func(ctx context.Context, db *sqlx.DB, mock sqlmock.Sqlmock) {
tm := time.Now()
mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `test` (`bar`,`created_at`,`updated_at`) VALUES")).
WithArgs(2, tm, tm).
WillReturnResult(sqlmock.NewResult(1, 1))
builder := NewBuilder(db)
builder.SetTime(&tm)
ts := test.TestSchema{
Foo: 1,
Bar: 2,
}
if _, err := builder.Insert("bar").Exec(ctx, &ts); err != nil {
t.Fatal(err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}); err != nil {
t.Fatal(err)
}
}
func TestUpdate(t *testing.T) {
if err := test.WithSqlxMock(func(ctx context.Context, db *sqlx.DB, mock sqlmock.Sqlmock) {
tm := time.Now()
mock.ExpectExec(regexp.QuoteMeta("UPDATE `test` SET `foo`=?,`updated_at`=? WHERE `foo` = ?")).
WithArgs(1, tm, 1).
WillReturnResult(sqlmock.NewResult(1, 1))
builder := NewBuilder(db)
builder.SetTime(&tm)
ts := test.TestSchema{
Foo: 1,
Bar: 2,
}
if _, err := builder.Update().Where("`foo` = :foo").Exec(ctx, &ts); err != nil {
t.Fatal(err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}); err != nil {
t.Fatal(err)
}
}
func TestUpdateColumn(t *testing.T) {
if err := test.WithSqlxMock(func(ctx context.Context, db *sqlx.DB, mock sqlmock.Sqlmock) {
tm := time.Now()
mock.ExpectExec(regexp.QuoteMeta("UPDATE `test` SET `bar`=?,`updated_at`=? WHERE foo = ?")).
WithArgs(2, tm, 1).
WillReturnResult(sqlmock.NewResult(1, 1))
builder := NewBuilder(db)
builder.SetTime(&tm)
ts := test.TestSchema{
Foo: 1,
Bar: 2,
}
if _, err := builder.Update("bar").Where("foo = :foo").Exec(ctx, &ts); err != nil {
t.Fatal(err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}); err != nil {
t.Fatal(err)
}
}
func TestDelete(t *testing.T) {
if err := test.WithSqlxMock(func(ctx context.Context, db *sqlx.DB, mock sqlmock.Sqlmock) {
mock.ExpectExec(regexp.QuoteMeta("DELETE FROM `test` WHERE foo = ?")).
WithArgs(1).
WillReturnResult(sqlmock.NewResult(1, 1))
builder := NewBuilder(db)
ts := test.TestSchema{
Foo: 1,
Bar: 2,
}
if _, err := builder.Delete().Where("foo = :foo").Exec(ctx, &ts); err != nil {
t.Fatal(err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}); err != nil {
t.Fatal(err)
}
}