@@ -102,7 +102,6 @@ func TestCategoryHandler_GetAllCategories(t *testing.T) {
102102}
103103
104104func  TestCategoryHandler_GetCategory (t  * testing.T ) {
105- 	mockRepo , _ , _ , _ , router  :=  setupCategoryTest (t )
106105	testID  :=  uuid .New ()
107106	foundCategory  :=  models.Category {ID : testID , Name : "Specific Category" , CreatedAt : time .Now (), UpdatedAt : time .Now ()}
108107
@@ -150,10 +149,12 @@ func TestCategoryHandler_GetCategory(t *testing.T) {
150149
151150	for  _ , tc  :=  range  tests  {
152151		t .Run (tc .name , func (t  * testing.T ) {
152+ 			// Moved setup inside t.Run for isolation 
153+ 			mockRepo , _ , _ , _ , router  :=  setupCategoryTest (t )
154+ 
153155			// Setup mock expectation only if the UUID is valid 
154156			if  tc .categoryID  !=  "not-a-uuid"  {
155157				parsedID , _  :=  uuid .Parse (tc .categoryID )
156- 				// Use Once() unless mock isn't expected to be called 
157158				mockRepo .On ("FindByID" , mock .Anything , parsedID ).Return (tc .mockReturn , tc .mockError ).Once ()
158159			}
159160
@@ -172,7 +173,6 @@ func TestCategoryHandler_GetCategory(t *testing.T) {
172173// --- Tests for Protected Routes (Require Authentication) --- 
173174
174175func  TestCategoryHandler_CreateCategory (t  * testing.T ) {
175- 	mockCategoryRepo , mockUserRepo , _ , _ , router  :=  setupCategoryTest (t )
176176	testUserID  :=  uuid .New ()
177177	testJwtSecret  :=  "test-secret-for-jwt-please-change" 
178178	testToken  :=  generateTestToken (testUserID , testJwtSecret )
@@ -253,25 +253,26 @@ func TestCategoryHandler_CreateCategory(t *testing.T) {
253253
254254	for  _ , tc  :=  range  tests  {
255255		t .Run (tc .name , func (t  * testing.T ) {
256+ 			// Moved setup inside t.Run for isolation 
257+ 			mockCategoryRepo , mockUserRepo , _ , _ , router  :=  setupCategoryTest (t )
258+ 
256259			// Mock middleware user check (only if token is expected) 
257260			if  tc .expectedStatus  !=  http .StatusUnauthorized  ||  tc .expectedBody  ==  `{"error":"user associated with token not found"}`  {
258- 				// Use Once() for middleware mock 
259261				mockUserRepo .On ("FindByID" , mock .Anything , testUserID ).Return (tc .mockUserReturn , tc .mockUserErr ).Once ()
260262			}
261263
262264			// Mock category repo create (only if middleware check passes and validation passes) 
263265			if  tc .mockUserErr  ==  nil  &&  tc .expectedStatus  !=  http .StatusBadRequest  &&  tc .expectedStatus  !=  http .StatusUnauthorized  {
264266				mockCategoryRepo .On ("Create" , mock .Anything , mock .AnythingOfType ("*models.Category" )).
265267					Return (func (ctx  context.Context , c  * models.Category ) * models.Category  {
266- 						if  tc .mockCreateErr  !=  nil  {  // <<< CORRECTION: Return nil if error 
268+ 						if  tc .mockCreateErr  !=  nil  {
267269							return  nil 
268270						}
269- 						// Simulate DB assigning ID and timestamps 
270271						c .ID  =  uuid .New ()
271272						c .CreatedAt  =  time .Now ()
272273						c .UpdatedAt  =  c .CreatedAt 
273274						return  c 
274- 					}, tc .mockCreateErr ).Once ()  // <<< CORRECTION: Use Once() 
275+ 					}, tc .mockCreateErr ).Once ()
275276			}
276277
277278			req  :=  httptest .NewRequest (http .MethodPost , "/api/categories" , strings .NewReader (tc .body ))
@@ -294,7 +295,6 @@ func TestCategoryHandler_CreateCategory(t *testing.T) {
294295}
295296
296297func  TestCategoryHandler_UpdateCategory (t  * testing.T ) {
297- 	mockCategoryRepo , mockUserRepo , _ , _ , router  :=  setupCategoryTest (t )
298298	testUserID  :=  uuid .New ()
299299	categoryToUpdateID  :=  uuid .New ()
300300	testJwtSecret  :=  "test-secret-for-jwt-please-change" 
@@ -404,24 +404,26 @@ func TestCategoryHandler_UpdateCategory(t *testing.T) {
404404
405405	for  _ , tc  :=  range  tests  {
406406		t .Run (tc .name , func (t  * testing.T ) {
407+ 			// Moved setup inside t.Run for isolation 
408+ 			mockCategoryRepo , mockUserRepo , _ , _ , router  :=  setupCategoryTest (t )
409+ 
407410			// Mock middleware user check 
408411			if  tc .expectedStatus  !=  http .StatusUnauthorized  ||  tc .expectedBody  ==  `{"error":"user associated with token not found"}`  {
409- 				mockUserRepo .On ("FindByID" , mock .Anything , testUserID ).Return (tc .mockUserReturn , tc .mockUserErr ).Once ()  // <<< CORRECTION: Use Once() 
412+ 				mockUserRepo .On ("FindByID" , mock .Anything , testUserID ).Return (tc .mockUserReturn , tc .mockUserErr ).Once ()
410413			}
411414
412415			// Mock category repo update (only if middleware/validation/parsing passes) 
413416			if  tc .categoryID  !=  "not-a-uuid"  &&  tc .mockUserErr  ==  nil  &&  tc .expectedStatus  !=  http .StatusBadRequest  &&  tc .expectedStatus  !=  http .StatusUnauthorized  {
414417				parsedID , _  :=  uuid .Parse (tc .categoryID )
415418				mockCategoryRepo .On ("Update" , mock .Anything , parsedID , mock .AnythingOfType ("*models.Category" )).
416419					Return (func (ctx  context.Context , id  uuid.UUID , c  * models.Category ) * models.Category  {
417- 						if  tc .mockUpdateErr  !=  nil  {  // <<< CORRECTION: Return nil if error 
420+ 						if  tc .mockUpdateErr  !=  nil  {
418421							return  nil 
419422						}
420- 						// Simulate DB updating timestamps 
421423						c .ID  =  id 
422- 						c .UpdatedAt  =  time .Now ()  // Actual repo only returns UpdatedAt, but mock can return full 
424+ 						c .UpdatedAt  =  time .Now ()
423425						return  c 
424- 					}, tc .mockUpdateErr ).Once ()  // <<< CORRECTION: Use Once() 
426+ 					}, tc .mockUpdateErr ).Once ()
425427			}
426428
427429			req  :=  httptest .NewRequest (http .MethodPut , "/api/categories/" + tc .categoryID , strings .NewReader (tc .body ))
@@ -442,7 +444,6 @@ func TestCategoryHandler_UpdateCategory(t *testing.T) {
442444}
443445
444446func  TestCategoryHandler_DeleteCategory (t  * testing.T ) {
445- 	mockCategoryRepo , mockUserRepo , _ , _ , router  :=  setupCategoryTest (t )
446447	testUserID  :=  uuid .New ()
447448	categoryToDeleteID  :=  uuid .New ()
448449	testJwtSecret  :=  "test-secret-for-jwt-please-change" 
@@ -515,15 +516,18 @@ func TestCategoryHandler_DeleteCategory(t *testing.T) {
515516
516517	for  _ , tc  :=  range  tests  {
517518		t .Run (tc .name , func (t  * testing.T ) {
519+ 			// Moved setup inside t.Run for isolation 
520+ 			mockCategoryRepo , mockUserRepo , _ , _ , router  :=  setupCategoryTest (t )
521+ 
518522			// Mock middleware user check 
519523			if  tc .expectedStatus  !=  http .StatusUnauthorized  ||  tc .expectedBody  ==  `{"error":"user associated with token not found"}`  {
520- 				mockUserRepo .On ("FindByID" , mock .Anything , testUserID ).Return (tc .mockUserReturn , tc .mockUserErr ).Once ()  // <<< CORRECTION: Use Once() 
524+ 				mockUserRepo .On ("FindByID" , mock .Anything , testUserID ).Return (tc .mockUserReturn , tc .mockUserErr ).Once ()
521525			}
522526
523527			// Mock category repo delete (only if middleware/parsing passes) 
524528			if  tc .categoryID  !=  "not-a-uuid"  &&  tc .mockUserErr  ==  nil  &&  tc .expectedStatus  !=  http .StatusBadRequest  &&  tc .expectedStatus  !=  http .StatusUnauthorized  {
525529				parsedID , _  :=  uuid .Parse (tc .categoryID )
526- 				mockCategoryRepo .On ("Delete" , mock .Anything , parsedID ).Return (tc .mockDeleteErr ).Once ()  // <<< CORRECTION: Use Once() 
530+ 				mockCategoryRepo .On ("Delete" , mock .Anything , parsedID ).Return (tc .mockDeleteErr ).Once ()
527531			}
528532
529533			req  :=  httptest .NewRequest (http .MethodDelete , "/api/categories/" + tc .categoryID , nil )
0 commit comments