@@ -83,25 +83,29 @@ def test_create_collection_with_prompt(
8383 with patch (
8484 "src.flashcards.services.generate_ai_collection" , new_callable = AsyncMock
8585 ) as mock_ai_generate :
86- mock_ai_generate .return_value = mock_collection
87-
88- rsp = client .post (
89- f"{ settings .API_V1_STR } /collections/" ,
90- json = collection_data .model_dump (),
91- headers = normal_user_token_headers ,
92- )
93-
94- assert rsp .status_code == 200
95- content = rsp .json ()
96- assert content ["name" ] == collection_data .name
97- assert "id" in content
98- assert isinstance (content ["id" ], str )
99- assert len (content ["cards" ]) == len (mock_collection .cards )
100- for i , card in enumerate (mock_collection .cards ):
101- assert content ["cards" ][i ]["front" ] == card .front
102- assert content ["cards" ][i ]["back" ] == card .back
103-
104- mock_ai_generate .assert_called_once ()
86+ with patch (
87+ "src.flashcards.api.check_and_increment_ai_usage_quota"
88+ ) as mock_quota_check :
89+ mock_ai_generate .return_value = mock_collection
90+ mock_quota_check .return_value = True
91+
92+ rsp = client .post (
93+ f"{ settings .API_V1_STR } /collections/" ,
94+ json = collection_data .model_dump (),
95+ headers = normal_user_token_headers ,
96+ )
97+
98+ assert rsp .status_code == 200
99+ content = rsp .json ()
100+ assert content ["name" ] == collection_data .name
101+ assert "id" in content
102+ assert isinstance (content ["id" ], str )
103+ assert len (content ["cards" ]) == len (mock_collection .cards )
104+ for i , card in enumerate (mock_collection .cards ):
105+ assert content ["cards" ][i ]["front" ] == card .front
106+ assert content ["cards" ][i ]["back" ] == card .back
107+
108+ mock_ai_generate .assert_called_once ()
105109
106110
107111def test_create_collection_with_ai_generation_error (
@@ -114,19 +118,23 @@ def test_create_collection_with_ai_generation_error(
114118 with patch (
115119 "src.flashcards.services.generate_ai_collection" , new_callable = AsyncMock
116120 ) as mock_ai_generate :
117- err_msg = "AI service is unavailable"
118- mock_ai_generate .side_effect = AIGenerationError (err_msg )
119-
120- rsp = client .post (
121- f"{ settings .API_V1_STR } /collections/" ,
122- json = collection_data .model_dump (),
123- headers = normal_user_token_headers ,
124- )
125-
126- assert rsp .status_code == 500
127- content = rsp .json ()
128- assert "detail" in content
129- assert err_msg in content ["detail" ]
121+ with patch (
122+ "src.flashcards.api.check_and_increment_ai_usage_quota"
123+ ) as mock_quota_check :
124+ err_msg = "AI service is unavailable"
125+ mock_ai_generate .side_effect = AIGenerationError (err_msg )
126+ mock_quota_check .return_value = True
127+
128+ rsp = client .post (
129+ f"{ settings .API_V1_STR } /collections/" ,
130+ json = collection_data .model_dump (),
131+ headers = normal_user_token_headers ,
132+ )
133+
134+ assert rsp .status_code == 500
135+ content = rsp .json ()
136+ assert "detail" in content
137+ assert err_msg in content ["detail" ]
130138
131139
132140def test_read_collection (
0 commit comments