@@ -25,11 +25,15 @@ func TestTodoRepo_Create(t *testing.T) {
2525 }
2626 defer db .Close ()
2727
28+ mock .ExpectBegin ()
29+
2830 query := "INSERT INTO todos"
2931 mock .ExpectExec (regexp .QuoteMeta (query )).
3032 WithArgs (todo .Name , todo .CreatedAt , todo .UpdatedAt ).
3133 WillReturnResult (sqlmock .NewResult (1 , 1 ))
3234
35+ mock .ExpectCommit ()
36+
3337 todoRepo := pgsql .NewPgsqlTodoRepository (db )
3438 err = todoRepo .Create (context .TODO (), todo )
3539 assert .NoError (t , err )
@@ -52,11 +56,15 @@ func TestTodoRepo_GetByID(t *testing.T) {
5256 rows := sqlmock .NewRows ([]string {"id" , "name" , "created_at" , "updated_at" }).
5357 AddRow (todoMock .ID , todoMock .Name , todoMock .CreatedAt , todoMock .UpdatedAt )
5458
59+ mock .ExpectBegin ()
60+
5561 query := "SELECT id, name, created_at, updated_at FROM todos WHERE id = $1"
5662 mock .ExpectQuery (regexp .QuoteMeta (query )).
5763 WithArgs (1 ).
5864 WillReturnRows (rows )
5965
66+ mock .ExpectCommit ()
67+
6068 todoRepo := pgsql .NewPgsqlTodoRepository (db )
6169 todo , err := todoRepo .GetByID (context .TODO (), 1 )
6270 assert .NoError (t , err )
@@ -80,9 +88,13 @@ func TestTodoRepo_Fetch(t *testing.T) {
8088 AddRow (mockTodos [0 ].ID , mockTodos [0 ].Name , mockTodos [0 ].CreatedAt , mockTodos [0 ].UpdatedAt ).
8189 AddRow (mockTodos [1 ].ID , mockTodos [1 ].Name , mockTodos [1 ].CreatedAt , mockTodos [1 ].UpdatedAt )
8290
91+ mock .ExpectBegin ()
92+
8393 query := "SELECT id, name, created_at, updated_at FROM todos"
8494 mock .ExpectQuery (query ).WillReturnRows (rows )
8595
96+ mock .ExpectCommit ()
97+
8698 todoRepo := pgsql .NewPgsqlTodoRepository (db )
8799 todos , err := todoRepo .Fetch (context .TODO ())
88100 assert .NoError (t , err )
@@ -103,11 +115,15 @@ func TestTodoRepo_Update(t *testing.T) {
103115 UpdatedAt : time .Now (),
104116 }
105117
118+ mock .ExpectBegin ()
119+
106120 query := "UPDATE todos SET name = $1, updated_at = $2 WHERE id = $3"
107121 mock .ExpectExec (regexp .QuoteMeta (query )).
108122 WithArgs (todo .Name , todo .UpdatedAt , todo .ID ).
109123 WillReturnResult (sqlmock .NewResult (1 , 1 ))
110124
125+ mock .ExpectCommit ()
126+
111127 todoRepo := pgsql .NewPgsqlTodoRepository (db )
112128 err = todoRepo .Update (context .TODO (), todo )
113129 assert .NoError (t , err )
@@ -120,11 +136,15 @@ func TestTodoRepo_Delete(t *testing.T) {
120136 }
121137 defer db .Close ()
122138
139+ mock .ExpectBegin ()
140+
123141 query := "DELETE FROM todos WHERE id = $1"
124142 mock .ExpectExec (regexp .QuoteMeta (query )).
125143 WithArgs (1 ).
126144 WillReturnResult (sqlmock .NewResult (1 , 1 ))
127145
146+ mock .ExpectCommit ()
147+
128148 todoRepo := pgsql .NewPgsqlTodoRepository (db )
129149 err = todoRepo .Delete (context .TODO (), 1 )
130150 assert .NoError (t , err )
0 commit comments