-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_test.go
97 lines (78 loc) · 1.5 KB
/
create_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
package gatsby
import "testing"
func BenchmarkCreateAndDelete(b *testing.B) {
var db = openDB()
staff := Staff{}
staff.Name = "John"
b.ResetTimer()
for i := 0; i < b.N; i++ {
Create(db, &staff, DriverPg)
Delete(db, &staff)
}
}
func TestCreate(t *testing.T) {
var db = openDB()
staff := Staff{}
staff.Name = "John"
Create(db, &staff, DriverPg)
Delete(db, &staff)
}
func TestCreateWithTransactionAndCommit(t *testing.T) {
var db = openDB()
SetupConnection(db, DriverPg)
db.Query("delete from staffs;")
staff := Staff{}
staff.Init()
staff.Name = "Txn Test"
staff.Gender = "f"
tx, err := staff.Begin()
if err != nil {
t.Fatal(err)
}
if tx == nil {
t.Fatal("transaction is nil")
}
// create with transaction
res := staff.Create()
if res.Error != nil {
t.Fatal(res.Error)
}
err = staff.Commit()
if err != nil {
t.Fatal(err)
}
CloseConnection()
}
func TestCreateWithTransactionAndRollback(t *testing.T) {
var db = openDB()
SetupConnection(db, DriverPg)
staff := Staff{}
staff.Init()
staff.Name = "Txn Test With Rollback"
tx, err := staff.Begin()
if err != nil {
t.Fatal(err)
}
if tx == nil {
t.Fatal("transaction is nil")
}
// create with transaction
res := staff.Create()
if res.Error != nil {
t.Fatal(res.Error)
}
t.Log(res)
t.Log(staff)
err = staff.Rollback()
if err != nil {
t.Fatal(err)
}
pId := staff.Id
staff2 := Staff{}
staff2.Init()
res = staff2.Load(pId)
if !res.IsEmpty {
t.Fatal("Still found the record.")
}
CloseConnection()
}