This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor_test.go
138 lines (114 loc) · 3.24 KB
/
executor_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
package layer_test
import (
"context"
"testing"
_ "github.com/lib/pq"
"github.com/partyzanex/layer"
"github.com/partyzanex/testutils"
"github.com/volatiletech/sqlboiler/v4/boil"
)
func TestGetExecutor(t *testing.T) {
t.Parallel()
ex := testutils.NewSqlDB(t, "postgres", "PG_TEST")
ctx, e := layer.GetExecutor(nil, nil)
if e != nil {
t.Errorf("getting executor failed: expected nil, got %v", e)
}
if ctx == context.Background() {
t.Errorf("getting context failed: expected %v, got %v", context.Background(), ctx)
}
ctx = context.WithValue(ctx, layer.ExecutorKey, ex)
_, e = layer.GetExecutor(ctx, ex)
if _, ok := e.(layer.BoilExecutor); !ok && e != ex {
t.Errorf("wrong executor: expected %v, got %v", ex, e)
}
}
func TestGetTransactor(t *testing.T) {
t.Parallel()
ex := testutils.NewSqlDB(t, "postgres", "PG_TEST")
var err error
ctx, tr := layer.GetTransactor(nil)
if tr != nil {
t.Errorf("getting transactor failed: expected nil, got %v", tr)
}
if ctx != context.TODO() {
t.Errorf("getting context failed: expected %v, got %v", context.Background(), ctx)
}
tr, err = ex.BeginTx(ctx, nil)
if err != nil {
t.Fatalf("creating transaction failed: %s", err)
}
if _, ok := tr.(boil.ContextTransactor); !ok {
t.Fatalf("transaction is not implements boil.ContextTransactor %v", tr)
}
if _, ok := tr.(boil.ContextExecutor); !ok {
t.Fatalf("transaction is not implements boil.ContextExecutor %v", tr)
}
defer tr.Rollback()
ctx = context.WithValue(ctx, layer.TransactorKey, tr)
_, tx := layer.GetTransactor(ctx)
if tx == nil {
t.Errorf("transactor is nil")
}
if _, ok := tx.(boil.ContextExecutor); !ok {
t.Errorf("transaction is not implements boil.ContextExecutor %v", tx)
}
if _, ok := tx.(boil.ContextTransactor); !ok {
t.Errorf("transaction is not implements boil.ContextExecutor %v", tr)
}
if tx != tr {
t.Errorf("getting transactor failed: expected %v, got %v", tr, tx)
}
}
func TestExecuteTransaction(t *testing.T) {
t.Parallel()
var err error
ex := testutils.NewSqlDB(t, "postgres", "PG_TEST")
ctx := context.TODO()
tr, err := ex.BeginTx(ctx, nil)
if err != nil {
t.Fatalf("creating transaction failed")
}
result, err := tr.Exec("select now() as dt")
if err != nil {
t.Errorf("executing query failed: %s", err)
}
n, err := result.RowsAffected()
if err != nil {
t.Errorf("getting affected rows failed: %s", err)
}
if n != 1 {
t.Errorf("wrong count of affected rows: expected 1, got %d", n)
}
layer.ExecuteTransaction(tr, &err)
if err != nil {
t.Errorf("executing transaction failed: %s", err)
}
}
func TestCreateTransaction(t *testing.T) {
t.Parallel()
var err error
ex := testutils.NewSqlDB(t, "postgres", "PG_TEST")
ctx := context.Background()
ctx, tr, err := layer.CreateTransaction(ctx, ex)
if err != nil {
t.Fatalf("creating transaction failed: %s", err)
}
defer func() {
layer.ExecuteTransaction(tr, &err)
if err != nil {
t.Errorf("executing transaction failed: %s", err)
}
}()
result, err := tr.Exec("select now() as dt")
if err != nil {
t.Errorf("executing query failed: %s", err)
}
n, err := result.RowsAffected()
if err != nil {
t.Errorf("getting affected rows failed: %s", err)
}
if n != 1 {
t.Errorf("wrong count of affected rows: expected 1, got %d", n)
}
}