1111import shutil
1212import unittest
1313
14- from util import TestFixture
14+ from util import TestFixture , modify_config
1515
1616from pisek .user_errors import UserError
1717from pisek .task_jobs .data .testcase_info import TestcaseGenerationMode
@@ -24,6 +24,20 @@ class TestFixtureOpendata(TestFixture):
2424 def expecting_success (self ) -> bool :
2525 return True
2626
27+ def modify_task (self ) -> None :
28+ pass
29+
30+ @property
31+ def _testcase_files (self ) -> list [str ]:
32+ files = []
33+ for file in [self .input_path , self .output_path , self .contestant_path ]:
34+ if file is not None :
35+ files .append (file )
36+ return files
37+
38+ def created_files (self ):
39+ return [os .path .basename (file ) for file in self ._testcase_files ]
40+
2741 def runTest (self ) -> None :
2842 if not self .fixture_path :
2943 return
@@ -33,19 +47,28 @@ def runTest(self) -> None:
3347
3448 self .built_task : BuiltTask
3549
50+ self .input_path : str | None = None
51+ self .output_path : str | None = None
52+ self .contestant_path : str | None = None
53+
3654 @unittest .mock .patch ("sys.stdout" , new_callable = io .StringIO )
3755 @unittest .mock .patch ("sys.stderr" , new_callable = io .StringIO )
3856 def run (* args ) -> bool :
3957 try :
58+ self .modify_task ()
4059 self ._build_task_dir = tempfile .mkdtemp (prefix = "pisek-test_" )
4160 self .built_task = task .build (self ._build_task_dir )
4261 self .run_opendata_test ()
4362 return True
44- except UserError :
63+ except UserError as e :
64+ print (e )
4565 return False
4666
4767 self .assertEqual (run (), self .expecting_success ())
4868
69+ for file in self ._testcase_files :
70+ self .assertTrue (os .path .isfile (file ))
71+
4972 self .check_end_state ()
5073 self .check_files ()
5174
@@ -72,6 +95,23 @@ class TestSumKasiopeaOpendataBuild(TestFixtureOpendata):
7295 def fixture_path (self ) -> str :
7396 return "../fixtures/sum_kasiopea/"
7497
98+ def init_testcase (self , name : str ) -> None :
99+ self .input_path = os .path .join (self .task_dir , f"{ name } .opendata.in" )
100+ self .output_path = os .path .join (self .task_dir , f"{ name } .opendata.out" )
101+ self .testcase = self .built_task .get_testcase (
102+ name , int (name ), int ("deadbeef" , 16 ), self .input_path , self .output_path
103+ )
104+
105+ def create_contestant_file (self , content : str | bytes ) -> None :
106+ self .contestant_path = os .path .join (self .task_dir , "02.out" )
107+
108+ if isinstance (content , bytes ):
109+ with open (self .contestant_path , "xb" ) as f :
110+ f .write (content )
111+ else :
112+ with open (self .contestant_path , "x" ) as f :
113+ f .write (content )
114+
75115
76116class TestSumKasiopeaOpendataListInputs (TestSumKasiopeaOpendataBuild ):
77117 def run_opendata_test (self ):
@@ -98,71 +138,70 @@ def run_opendata_test(self):
98138
99139
100140class TestSumKasiopeaOpendataSequential (TestSumKasiopeaOpendataBuild ):
101- def created_files (self ):
102- return ["01.in" , "01.out" ]
103-
104- def run_opendata_test (self ):
105- input_path = os .path .join (self .task_dir , "01.in" )
106- output_path = os .path .join (self .task_dir , "01.out" )
107-
108- testcase = self .built_task .get_testcase (
109- "01" , 1 , int ("deadbeef" , 16 ), input_path , output_path
110- )
111- testcase .gen_input ()
112- self .assertTrue (os .path .exists (input_path ))
113- testcase .gen_output ()
114- self .assertTrue (os .path .exists (output_path ))
141+ def run_opendata_test (self ) -> None :
142+ self .init_testcase ("01" )
143+ assert self .input_path is not None
144+ assert self .output_path is not None
145+
146+ self .testcase .gen_input ()
147+ self .assertTrue (os .path .exists (self .input_path ))
148+ self .testcase .gen_output ()
149+ self .assertTrue (os .path .exists (self .output_path ))
115150 self .assertEqual (
116- testcase .judge (output_path ),
151+ self . testcase .judge (self . output_path ),
117152 OpendataVerdict (Verdict .ok , None , Decimal (4 ), None , None ),
118153 )
119154
120155
121- class TestSumKasiopeaOpendataJudgeRightaway (TestSumKasiopeaOpendataBuild ):
122- def created_files (self ):
123- return ["02.in" , "02.out" ]
156+ class TestSumKasiopeaOpendataCheckRightaway (TestSumKasiopeaOpendataBuild ):
157+ def run_opendata_test (self ) -> None :
158+ self .init_testcase ("02" )
159+ self .assertEqual (
160+ self .testcase .judge (os .path .join (self .task_dir , "sample.out" )),
161+ OpendataVerdict (Verdict .wrong_answer , None , Decimal (0 ), None , None ),
162+ )
163+
124164
165+ class TestSumKasiopeaOpendataCheckBinary (TestSumKasiopeaOpendataBuild ):
125166 def run_opendata_test (self ):
126- input_path = os . path . join ( self .task_dir , "02.in " )
127- output_path = os . path . join ( self .task_dir , "02.out " )
167+ self .init_testcase ( "02" )
168+ self .create_contestant_file ( b" \x07 \n " )
128169
129- testcase = self .built_task .get_testcase (
130- "02" , 2 , int ("deadbeef" , 16 ), input_path , output_path
131- )
132170 self .assertEqual (
133- testcase .judge (os .path .join (self .task_dir , "sample.out" )),
134- OpendataVerdict (Verdict .wrong_answer , None , Decimal (0 ), None , None ),
171+ self .testcase .judge (os .path .join (self .task_dir , self .contestant_path )),
172+ OpendataVerdict (
173+ Verdict .normalization_fail ,
174+ "File contains non-printable character (code 7 at position 0)" ,
175+ Decimal (0 ),
176+ None ,
177+ None ,
178+ ),
135179 )
136- self .assertTrue (os .path .exists (input_path ))
137- self .assertTrue (os .path .exists (output_path ))
138180
139181
140- class TestSumKasiopeaOpendataJudgeBinary (TestSumKasiopeaOpendataBuild ):
141- def created_files (self ):
142- return ["02.in" , "02.out" , "02.ok" ]
182+ class TestSumKasiopeaOpendataJudge (TestSumKasiopeaOpendataBuild ):
183+ def modify_task (self ) -> None :
184+ def modification_fn (raw_config ):
185+ raw_config ["tests" ]["out_check" ] = "judge"
186+ raw_config ["tests" ]["out_judge" ] = "judge"
187+ raw_config ["tests" ]["judge_type" ] = "opendata-v2"
188+
189+ modify_config (self .task_dir , modification_fn )
143190
144191 def run_opendata_test (self ):
145- input_path = os .path .join (self .task_dir , "02.in" )
146- output_path = os .path .join (self .task_dir , "02.ok" )
147- contestant_path = os .path .join (self .task_dir , "02.out" )
148- with open (contestant_path , "xb" ) as f :
149- f .write (b"\x07 \n " )
150-
151- testcase = self .built_task .get_testcase (
152- "02" , 2 , int ("deadbeef" , 16 ), input_path , output_path
153- )
192+ self .init_testcase ("02" )
193+ self .create_contestant_file ("0\n " )
194+
154195 self .assertEqual (
155- testcase .judge (os .path .join (self .task_dir , contestant_path )),
196+ self . testcase .judge (os .path .join (self .task_dir , self . contestant_path )),
156197 OpendataVerdict (
157- Verdict .normalization_fail ,
158- "File contains non-printable character (code 7 at position 0) " ,
198+ Verdict .wrong_answer ,
199+ "Wrong answer " ,
159200 Decimal (0 ),
160201 None ,
161202 None ,
162203 ),
163204 )
164- self .assertTrue (os .path .exists (input_path ))
165- self .assertTrue (os .path .exists (output_path ))
166205
167206
168207if __name__ == "__main__" :
0 commit comments