示例#1
0
    def test_fetch_does_not_accept_non_https_aoc_urls(self):
        with self.assertRaises(Exception):
            WebAoc('test-session')\
                .fetch(requests.get, 'http://adventofcode.com/')
        with self.assertRaises(Exception):
            WebAoc('test-session')\
                .fetch(requests.get, 'https://adventofcodes.com/')

        with self.assertRaises(Exception):
            WebAoc('test-session')\
                .fetch(requests.get, 'https://adventofcode.com.de/')
示例#2
0
    def test_get_events_page(self):
        responses.add(
            responses.GET,
            'https://adventofcode.com/events',
            body=web_fixtures.joinpath('events.html').read_bytes(),
            status=200,
        )

        events_page = WebAoc('test-session').get_events_page()

        self.assertEqual(len(responses.calls), 1)
        self.assertIsNotNone(events_page)
        self.assertEqual(events_page.title.text,
                         'Events - Advent of Code 2020')
        self.assertEqual(events_page.select('.star-count')[-1].text, '39*')
示例#3
0
    def refresh_challenge_input(self, year, day, only_if_empty=True):
        """Refresh a challenge's input from the AOC website"""
        input_path = settings_proxy().challenges_boilerplate\
            .get_day_input_filename(year, day)
        if only_if_empty and input_path.exists() and input_path.lstat(
        ).st_size:
            return False

        _input = WebAoc().get_input_page(year, day)
        if not _input:
            click.echo(
                f"Could not update input for {e_error(f'{year} {day}')}")
            return False

        if input_path.exists() and _input == input_path.read_text():
            click.echo(f"Input did not change for {e_warn(f'{year} {day}')} "
                       f"({e_value(f'{len(_input)} bytes')})")
            return False

        input_path.write_text(_input)

        click.echo(f"Updated input for {e_success(f'{year} {day}')} "
                   f"({e_value(f'{len(_input)} bytes')}) at "
                   f"{e_value(str(input_path))}")

        return True
示例#4
0
    def test_get_year_page(self):
        responses.add(
            responses.GET,
            'https://adventofcode.com/2020',
            body=web_fixtures.joinpath('2020.html').read_bytes(),
            status=200,
        )

        events_page = WebAoc('test-session').get_year_page(2020)

        self.assertEqual(len(responses.calls), 1)
        self.assertIsNotNone(events_page)
        self.assertEqual(events_page.title.text, 'Advent of Code 2020')
        self.assertEqual(
            events_page.select('.calendar-day1')[0].text,
            '              ..........|..........                 1 **')
示例#5
0
    def test_get_input_page(self):
        responses.add(
            responses.GET,
            'https://adventofcode.com/2020/day/5/input',
            body=web_fixtures.joinpath('2020_day_5_input.txt').read_bytes(),
            status=200,
        )

        day_5_input = WebAoc('test-session').get_input_page(2020, 5)

        self.assertEqual(day_5_input, 'Day 5 Input\n')
示例#6
0
    def test_collect_data(self):
        responses.add(
            responses.GET,
            'https://adventofcode.com/events',
            body=web_fixtures.joinpath('events.html').read_bytes(),
            status=200,
        )
        responses.add(
            responses.GET,
            'https://adventofcode.com/2020',
            body=web_fixtures.joinpath('2020.html').read_bytes(),
            status=200,
        )

        scraper = AccountScraper(WebAoc('test-session'))
        # noinspection DuplicatedCode
        self.assertEqual(
            scraper.collect_data(), {
                "username": "******",
                "total_stars": 39,
                "years": {
                    2020: {
                        "year": 2020,
                        "stars": 39,
                        "days": {
                            1: 2,
                            2: 2,
                            3: 2,
                            4: 2,
                            5: 2,
                            6: 2,
                            7: 2,
                            8: 2,
                            9: 2,
                            10: 2,
                            11: 2,
                            12: 2,
                            13: 2,
                            14: 1,
                            15: 2,
                            16: 2,
                            17: 2,
                            18: 0,
                            19: 2,
                            20: 2,
                            21: 1,
                            22: 0,
                            23: 0,
                            24: 0,
                            25: 0,
                        },
                    }
                },
            })
示例#7
0
    def get_and_submit_challenge_solution(self, year, day, part, force,
                                          no_prompt, solution):
        attempted, submitted, success = False, False, False

        if not WebAoc().is_configured():
            click.echo(f"You haven't set {e_error('AOC_SESSION_ID')} in "
                       f"{e_error('user_settings.py')}")
            return attempted, submitted, success

        solution = self.get_solution(year, day, part, force, no_prompt,
                                     solution)
        if solution is None:
            return attempted, submitted, success

        return self.submit_challenge_solution(year, day, part, no_prompt,
                                              solution)
示例#8
0
    def test_submit_solution(self):
        def submit_callback(request):
            self.assertIsNotNone(request.body)
            query = parse_qs(request.body)
            self.assertEqual(query, {'level': ['1'], 'answer': ['Solution']})

            return 200, {}, '<html><body><article>Correct!'

        responses.add_callback(
            responses.POST,
            'https://adventofcode.com/2020/day/5/answer',
            callback=submit_callback,
            content_type='text/html',
        )

        answer_page = WebAoc('test-session')\
            .submit_solution(2020, 5, 'a', 'Solution')
        self.assertEqual(answer_page.article.text, 'Correct!')
示例#9
0
    def submit_challenge_solution(self, year, day, part, no_prompt, solution):
        attempted, submitted, success = False, False, False

        part_info = self.combined_info\
            .get_part(year, day, part)
        if not part_info:
            return attempted, submitted, success

        attempted = True
        answer_page = WebAoc().submit_solution(year, day, part, solution)
        if not answer_page:
            return attempted, submitted, success

        submitted = True
        message = answer_page.article.text
        is_final_part = part_info.is_final_part
        success, result = self.get_submission_result(is_final_part, message)
        self.echo_submission_result(solution, message, result)

        if success:
            self.fetch_and_update_readme_if_agreed(no_prompt)

        return attempted, submitted, success
示例#10
0
 def test_fetch_doesnt_do_anything_without_session_id(self):
     self.assertIsNone(WebAoc(None).fetch(None, None))
示例#11
0
 def get_day_url(self) -> str:
     return WebAoc().get_day_url(self.year, self.day)
示例#12
0
 def get_year_url(self) -> str:
     return WebAoc().get_year_url(self.year)