def check_output(self, parts_to_create_on_disk, collected_data): with using_controller(parts_to_create_on_disk, collected_data) \ as (controller, combined_info, captured): self.assertTrue(controller.dump_data()) expected = combined_info.serialise() dump = captured.getvalue() try: loaded = json.loads(dump) except json.decoder.JSONDecodeError: raise Exception(f"Could not parse {dump}") self.assertEqual(loaded, expected)
def check_submit_solution(self, year, day, part, solution, response_path, expected_result): if response_path: response = bs4.BeautifulSoup( response_path.read_text(), 'html.parser') else: response = None with using_controller([], None, interactive=False) \ as (controller, combined_info, _), \ amending_settings(challenges_boilerplate=DummyBoilerplate()), \ patch.object(WebAoc, 'submit_solution', return_value=response): self.assertEqual(controller.submit_challenge_solution( year, day, part, no_prompt=True, solution=solution), expected_result)
def check_play_challenge(self, year, day, part, force, original_replacement, expected_result): with using_controller([], None, interactive=False) \ as (controller, combined_info, _), \ amending_settings(challenges_boilerplate=DummyBoilerplate()): if original_replacement is not None: controller.add_challenge(year, day, part) part_info = combined_info.get_part(year, day, part) code = part_info.path.read_text() code = code.replace(' "FUNCTION-BODY"', original_replacement) part_info.path.write_text(code) self.assertEqual(controller.play_challenge(year, day, part, force), expected_result)
def checking_add_challenge(self, year, day, part, expected_result): with using_controller([], None) as (controller, combined_info, _), \ amending_settings(challenges_boilerplate=DummyBoilerplate()): expected_contents = {} yield controller, combined_info, expected_contents self.assertEqual(controller.add_challenge(year, day, part), expected_result) for path, expected_content in expected_contents.items(): if path.exists(): content = path.read_text() else: content = None self.assertEqual(content, expected_content)
def check_challenge_input(self, parts_to_create_on_disk, collected_data, year, day, only_if_empty, remote_input, initial_content, expected_content, expected_result): with using_controller(parts_to_create_on_disk, collected_data) \ as (controller, combined_info, _), \ patch.object(WebAoc, 'get_input_page', return_value=remote_input): challenge_input_path = combined_info\ .get_day(year, day).get_input_filename() if initial_content is not None: challenge_input_path.write_text(initial_content) self.assertEqual( controller.refresh_challenge_input( year, day, only_if_empty=only_if_empty), expected_result) if challenge_input_path.exists(): content = challenge_input_path.read_text() else: content = None self.assertEqual(content, expected_content)
def check_readme(self, parts_to_create_on_disk, collected_data, initial_content, expected_content, expected_result): if initial_content is None: readme_file = nullcontext() new_settings = {"readme_path": None} else: readme_file = tempfile.NamedTemporaryFile(mode="w") new_settings = {"readme_path": Path(readme_file.name)} with using_controller(parts_to_create_on_disk, collected_data) \ as (controller, _, _), readme_file, \ amending_settings(**new_settings): if initial_content is not None: readme_file.write(initial_content) readme_file.flush() self.assertEqual(controller.update_readme(), expected_result) readme_path = settings_proxy().readme_path if readme_path and readme_path.exists(): content = readme_path.read_text() else: content = None self.assertEqual(content, expected_content)
def check_output(self, parts_to_create_on_disk, collected_data, expected_output): with using_controller(parts_to_create_on_disk, collected_data) \ as (controller, _, captured): controller.list_years() self.assertEqual(captured.getvalue(), expected_output)