22package db
33
44import (
5+ "context"
56 "errors"
67 "testing"
78
@@ -14,8 +15,7 @@ import (
1415)
1516
1617func TestPostgresThumbRepository_Create (t * testing.T ) {
17- mockDB := new (adaptermocks.IDB )
18- repo := NewPostgresThumbRepository (mockDB )
18+ mockDB , repo , _ , ctx := setupTest ()
1919
2020 t .Run ("successful creation" , func (t * testing.T ) {
2121 process := & entity.ThumbProcess {
@@ -31,7 +31,7 @@ func TestPostgresThumbRepository_Create(t *testing.T) {
3131
3232 mockDB .On ("Create" , mock .AnythingOfType ("*db.ThumbPostgres" )).Return (& gorm.DB {}).Once ()
3333
34- err := repo .Create (process )
34+ err := repo .Create (ctx , process )
3535
3636 assert .NoError (t , err )
3737 })
@@ -51,26 +51,44 @@ func TestPostgresThumbRepository_Create(t *testing.T) {
5151 expectedError := errors .New ("database error" )
5252 mockDB .On ("Create" , mock .AnythingOfType ("*db.ThumbPostgres" )).Return (& gorm.DB {Error : expectedError }).Once ()
5353
54- err := repo .Create (process )
54+ err := repo .Create (ctx , process )
5555
5656 assert .Error (t , err )
5757 assert .Equal (t , expectedError , err )
5858 })
59+
60+ t .Run ("create with missing user ID" , func (t * testing.T ) {
61+ process := & entity.ThumbProcess {
62+ Video : entity.ThumbProcessVideo {
63+ Path : "test-video.mp4" ,
64+ },
65+ Thumbnail : entity.ThumbProcessThumb {
66+ Path : "test-thumb.jpg" ,
67+ },
68+ Status : "pending" ,
69+ Error : "" ,
70+ }
71+
72+ err := repo .Create (context .Background (), process )
73+
74+ assert .Error (t , err )
75+ assert .Equal (t , RequiredUserIDError , err )
76+ })
5977}
6078
6179func TestPostgresThumbRepository_List (t * testing.T ) {
62- mockDB := new (adaptermocks.IDB )
63- repo := NewPostgresThumbRepository (mockDB )
80+ mockDB , repo , mockedUserID , ctx := setupTest ()
6481
6582 t .Run ("successful list retrieval" , func (t * testing.T ) {
66- mockDB .On ("Find" , mock .AnythingOfType ("*[]db.ThumbPostgres" ), mock . Anything ).
83+ mockDB .On ("Find" , mock .AnythingOfType ("*[]db.ThumbPostgres" ), "user_id = ?" , mockedUserID ).
6784 Run (func (args mock.Arguments ) {
6885 arg := args .Get (0 ).(* []ThumbPostgres )
6986 * arg = []ThumbPostgres {
7087 {
7188 BaseModel : BaseModel {
7289 ID : uuid .New (),
7390 },
91+ UserID : 1 ,
7492 VideoPath : "video1.mp4" ,
7593 ThumbnailPath : "thumb1.jpg" ,
7694 Status : "completed" ,
@@ -80,6 +98,7 @@ func TestPostgresThumbRepository_List(t *testing.T) {
8098 BaseModel : BaseModel {
8199 ID : uuid .New (),
82100 },
101+ UserID : 1 ,
83102 VideoPath : "video2.mp4" ,
84103 ThumbnailPath : "thumb2.jpg" ,
85104 Status : "pending" ,
@@ -88,7 +107,7 @@ func TestPostgresThumbRepository_List(t *testing.T) {
88107 }
89108 }).Return (& gorm.DB {}).Once ()
90109
91- processes := * repo .List ()
110+ processes := * repo .List (ctx )
92111
93112 assert .Len (t , processes , 2 )
94113 assert .Equal (t , "video1.mp4" , processes [0 ].Video .Path )
@@ -100,21 +119,25 @@ func TestPostgresThumbRepository_List(t *testing.T) {
100119 })
101120
102121 t .Run ("empty list" , func (t * testing.T ) {
103- mockDB .On ("Find" , mock .AnythingOfType ("*[]db.ThumbPostgres" ), mock . Anything ).
122+ mockDB .On ("Find" , mock .AnythingOfType ("*[]db.ThumbPostgres" ), "user_id = ?" , mockedUserID ).
104123 Run (func (args mock.Arguments ) {
105124 arg := args .Get (0 ).(* []ThumbPostgres )
106125 * arg = []ThumbPostgres {}
107126 }).Return (& gorm.DB {}).Once ()
108127
109- processes := repo .List ()
128+ processes := repo .List (ctx )
129+
130+ assert .Empty (t , processes )
131+ })
110132
133+ t .Run ("list with missing user ID" , func (t * testing.T ) {
134+ processes := repo .List (context .Background ())
111135 assert .Empty (t , processes )
112136 })
113137}
114138
115139func TestPostgresThumbRepository_Update (t * testing.T ) {
116- mockDB := new (adaptermocks.IDB )
117- repo := NewPostgresThumbRepository (mockDB )
140+ mockDB , repo , _ , ctx := setupTest ()
118141
119142 t .Run ("successful update" , func (t * testing.T ) {
120143 processID := uuid .New ()
@@ -130,9 +153,9 @@ func TestPostgresThumbRepository_Update(t *testing.T) {
130153 Error : "" ,
131154 }
132155
133- mockDB .On ("Save " , mock .AnythingOfType ("*db.ThumbPostgres" )).Return (& gorm.DB {}).Once ()
156+ mockDB .On ("Updates " , mock .AnythingOfType ("*db.ThumbPostgres" )).Return (& gorm.DB {}).Once ()
134157
135- updated , err := repo .Update (process )
158+ updated , err := repo .Update (ctx , process )
136159
137160 assert .NoError (t , err )
138161 assert .NotNil (t , updated )
@@ -150,12 +173,11 @@ func TestPostgresThumbRepository_Update(t *testing.T) {
150173 Error : "" ,
151174 }
152175
153- updated , err := repo .Update (process )
176+ updated , err := repo .Update (ctx , process )
154177
155178 assert .Nil (t , updated )
156179 assert .Error (t , err )
157180 assert .Equal (t , "process id is required" , err .Error ())
158- mockDB .AssertNotCalled (t , "Save" )
159181 })
160182
161183 t .Run ("database error during update" , func (t * testing.T ) {
@@ -173,9 +195,9 @@ func TestPostgresThumbRepository_Update(t *testing.T) {
173195 }
174196
175197 expectedError := errors .New ("database error" )
176- mockDB .On ("Save " , mock .AnythingOfType ("*db.ThumbPostgres" )).Return (& gorm.DB {Error : expectedError }).Once ()
198+ mockDB .On ("Updates " , mock .AnythingOfType ("*db.ThumbPostgres" )).Return (& gorm.DB {Error : expectedError }).Once ()
177199
178- updated , err := repo .Update (process )
200+ updated , err := repo .Update (ctx , process )
179201
180202 assert .Nil (t , updated )
181203 assert .Error (t , err )
@@ -195,3 +217,13 @@ func TestThumbPostgres_TableName(t *testing.T) {
195217 thumb := ThumbPostgres {}
196218 assert .Equal (t , "thumb" , thumb .TableName ())
197219}
220+
221+ func setupTest () (* adaptermocks.IDB , * PostgresThumbRepository , int , context.Context ) {
222+ mockedUserID := 1
223+ mockDB := new (adaptermocks.IDB )
224+
225+ return mockDB ,
226+ NewPostgresThumbRepository (mockDB ),
227+ mockedUserID ,
228+ context .WithValue (context .Background (), "logged_user_id" , mockedUserID )
229+ }
0 commit comments