@@ -25,11 +25,15 @@ func TestTodoRepo_Create(t *testing.T) {
25
25
}
26
26
defer db .Close ()
27
27
28
+ mock .ExpectBegin ()
29
+
28
30
query := "INSERT INTO todos"
29
31
mock .ExpectExec (regexp .QuoteMeta (query )).
30
32
WithArgs (todo .Name , todo .CreatedAt , todo .UpdatedAt ).
31
33
WillReturnResult (sqlmock .NewResult (1 , 1 ))
32
34
35
+ mock .ExpectCommit ()
36
+
33
37
todoRepo := pgsql .NewPgsqlTodoRepository (db )
34
38
err = todoRepo .Create (context .TODO (), todo )
35
39
assert .NoError (t , err )
@@ -52,11 +56,15 @@ func TestTodoRepo_GetByID(t *testing.T) {
52
56
rows := sqlmock .NewRows ([]string {"id" , "name" , "created_at" , "updated_at" }).
53
57
AddRow (todoMock .ID , todoMock .Name , todoMock .CreatedAt , todoMock .UpdatedAt )
54
58
59
+ mock .ExpectBegin ()
60
+
55
61
query := "SELECT id, name, created_at, updated_at FROM todos WHERE id = $1"
56
62
mock .ExpectQuery (regexp .QuoteMeta (query )).
57
63
WithArgs (1 ).
58
64
WillReturnRows (rows )
59
65
66
+ mock .ExpectCommit ()
67
+
60
68
todoRepo := pgsql .NewPgsqlTodoRepository (db )
61
69
todo , err := todoRepo .GetByID (context .TODO (), 1 )
62
70
assert .NoError (t , err )
@@ -80,9 +88,13 @@ func TestTodoRepo_Fetch(t *testing.T) {
80
88
AddRow (mockTodos [0 ].ID , mockTodos [0 ].Name , mockTodos [0 ].CreatedAt , mockTodos [0 ].UpdatedAt ).
81
89
AddRow (mockTodos [1 ].ID , mockTodos [1 ].Name , mockTodos [1 ].CreatedAt , mockTodos [1 ].UpdatedAt )
82
90
91
+ mock .ExpectBegin ()
92
+
83
93
query := "SELECT id, name, created_at, updated_at FROM todos"
84
94
mock .ExpectQuery (query ).WillReturnRows (rows )
85
95
96
+ mock .ExpectCommit ()
97
+
86
98
todoRepo := pgsql .NewPgsqlTodoRepository (db )
87
99
todos , err := todoRepo .Fetch (context .TODO ())
88
100
assert .NoError (t , err )
@@ -103,11 +115,15 @@ func TestTodoRepo_Update(t *testing.T) {
103
115
UpdatedAt : time .Now (),
104
116
}
105
117
118
+ mock .ExpectBegin ()
119
+
106
120
query := "UPDATE todos SET name = $1, updated_at = $2 WHERE id = $3"
107
121
mock .ExpectExec (regexp .QuoteMeta (query )).
108
122
WithArgs (todo .Name , todo .UpdatedAt , todo .ID ).
109
123
WillReturnResult (sqlmock .NewResult (1 , 1 ))
110
124
125
+ mock .ExpectCommit ()
126
+
111
127
todoRepo := pgsql .NewPgsqlTodoRepository (db )
112
128
err = todoRepo .Update (context .TODO (), todo )
113
129
assert .NoError (t , err )
@@ -120,11 +136,15 @@ func TestTodoRepo_Delete(t *testing.T) {
120
136
}
121
137
defer db .Close ()
122
138
139
+ mock .ExpectBegin ()
140
+
123
141
query := "DELETE FROM todos WHERE id = $1"
124
142
mock .ExpectExec (regexp .QuoteMeta (query )).
125
143
WithArgs (1 ).
126
144
WillReturnResult (sqlmock .NewResult (1 , 1 ))
127
145
146
+ mock .ExpectCommit ()
147
+
128
148
todoRepo := pgsql .NewPgsqlTodoRepository (db )
129
149
err = todoRepo .Delete (context .TODO (), 1 )
130
150
assert .NoError (t , err )
0 commit comments