@@ -2301,4 +2301,88 @@ def test_post_partial_success(self):
23012301
23022302 # Both should succeed since they're valid
23032303 self .assertEqual (len (result ["created_questions" ]), 2 )
2304- self .assertEqual (len (result ["errors" ]), 0 )
2304+ self .assertEqual (len (result ["errors" ]), 0 )
2305+
2306+ def test_post_update_state_from_closed_to_open (self ):
2307+ """Test POST updates homework state from closed to open"""
2308+ self .assertEqual (self .homework .state , "CL" )
2309+
2310+ data = {
2311+ "questions" : [
2312+ {"text" : "New question" , "question_type" : "FF" }
2313+ ],
2314+ "state" : "OP"
2315+ }
2316+
2317+ response = self .client .post (
2318+ self .url , json .dumps (data ), content_type = "application/json"
2319+ )
2320+
2321+ self .assertEqual (response .status_code , 200 )
2322+ result = response .json ()
2323+
2324+ self .assertIn ("homework_state" , result )
2325+ self .assertEqual (result ["homework_state" ]["old" ], "CL" )
2326+ self .assertEqual (result ["homework_state" ]["new" ], "OP" )
2327+
2328+ # Verify state was updated in DB
2329+ self .homework .refresh_from_db ()
2330+ self .assertEqual (self .homework .state , "OP" )
2331+
2332+ def test_post_update_state_only (self ):
2333+ """Test POST can update state without adding questions"""
2334+ self .assertEqual (self .homework .state , "CL" )
2335+
2336+ data = {"state" : "OP" }
2337+
2338+ response = self .client .post (
2339+ self .url , json .dumps (data ), content_type = "application/json"
2340+ )
2341+
2342+ self .assertEqual (response .status_code , 200 )
2343+ result = response .json ()
2344+
2345+ self .assertEqual (result ["homework_state" ]["old" ], "CL" )
2346+ self .assertEqual (result ["homework_state" ]["new" ], "OP" )
2347+ self .assertEqual (len (result ["created_questions" ]), 0 )
2348+
2349+ self .homework .refresh_from_db ()
2350+ self .assertEqual (self .homework .state , "OP" )
2351+
2352+ def test_post_update_state_invalid (self ):
2353+ """Test POST with invalid state returns error"""
2354+ data = {
2355+ "questions" : [{"text" : "Question" , "question_type" : "FF" }],
2356+ "state" : "INVALID"
2357+ }
2358+
2359+ response = self .client .post (
2360+ self .url , json .dumps (data ), content_type = "application/json"
2361+ )
2362+
2363+ self .assertEqual (response .status_code , 400 )
2364+ result = response .json ()
2365+ self .assertIn ("Invalid state" , result ["error" ])
2366+
2367+ def test_post_update_all_states (self ):
2368+ """Test POST can update to all valid states"""
2369+ # CL -> OP
2370+ data = {"state" : "OP" }
2371+ response = self .client .post (self .url , json .dumps (data ), content_type = "application/json" )
2372+ self .assertEqual (response .status_code , 200 )
2373+ self .homework .refresh_from_db ()
2374+ self .assertEqual (self .homework .state , "OP" )
2375+
2376+ # OP -> SC
2377+ data = {"state" : "SC" }
2378+ response = self .client .post (self .url , json .dumps (data ), content_type = "application/json" )
2379+ self .assertEqual (response .status_code , 200 )
2380+ self .homework .refresh_from_db ()
2381+ self .assertEqual (self .homework .state , "SC" )
2382+
2383+ # SC -> CL
2384+ data = {"state" : "CL" }
2385+ response = self .client .post (self .url , json .dumps (data ), content_type = "application/json" )
2386+ self .assertEqual (response .status_code , 200 )
2387+ self .homework .refresh_from_db ()
2388+ self .assertEqual (self .homework .state , "CL" )
0 commit comments