def test_no_session_id(test_token, capsys): test_token.unlink() with pytest.raises(AocdError("Missing session ID")): default_user() out, err = capsys.readouterr() assert out == "" assert "ERROR: AoC session ID is needed to get your puzzle data!" in err
def test_server_error(requests_mock, freezer): freezer.move_to("2018-12-01 12:00:00Z") requests_mock.post(url="https://adventofcode.com/2018/day/1/answer", status_code=500) with pytest.raises( AocdError("Non-200 response for POST: <Response [500]>")): submit(1234, part="a")
def test_solve_no_plugs(mocker): mock = mocker.patch("pkg_resources.iter_entry_points", return_value=iter([])) puzzle = Puzzle(year=2018, day=1) expected = AocdError("Puzzle.solve is only available with unique entry point") with pytest.raises(expected): puzzle.solve() mock.assert_called_once_with(group="adventofcode.user")
def test_submit_correct_part_a_answer_for_part_b_blocked(requests_mock, tmpdir): requests_mock.get( url="https://adventofcode.com/2018/day/1", text="<h2>Day 1: Yo Dawg</h2> <p>Your puzzle answer was <code>1234</code></p>", ) requests_mock.post( url="https://adventofcode.com/2018/day/1/answer", text="<article>That's the right answer</article>", ) with pytest.raises(AocdError("cowardly refusing to re-submit answer_a (1234) for part b")): submit(1234, part="b", day=1, year=2018, session="whatever", reopen=False)
def test_solve_for_unfound_user(aocd_dir, mocker): input_path = aocd_dir / "thetesttoken" / "2018_01_input.txt" input_path.write_text("someinput") other_plug = mocker.Mock() other_plug.name = "otherplugin" mocker.patch("pkg_resources.iter_entry_points", return_value=iter([other_plug])) puzzle = Puzzle(year=2018, day=1) with pytest.raises(AocdError("No entry point found for 'myplugin'")): puzzle.solve_for("myplugin") other_plug.load.assert_not_called() other_plug.load.return_value.assert_not_called()
def test_server_error(requests_mock, caplog): mock = requests_mock.get( url="https://adventofcode.com/2101/day/1/input", text="Not Found", status_code=404, ) with pytest.raises(AocdError("Unexpected response")): aocd.get_data(year=2101, day=1) assert mock.called assert mock.call_count == 1 assert caplog.record_tuples == [ ("aocd.models", logging.ERROR, "got 404 status code token=...oken"), ("aocd.models", logging.ERROR, "Not Found"), ]
def test_answered(aocd_dir): answer_a_path = aocd_dir / "thetesttoken" / "2016_07a_answer.txt" answer_b_path = aocd_dir / "thetesttoken" / "2016_07b_answer.txt" puzzle = Puzzle(year=2016, day=7) answer_a_path.write_text("foo") answer_b_path.write_text("") assert puzzle.answered_a is True assert puzzle.answered("a") is True assert puzzle.answered_b is False assert puzzle.answered("b") is False answer_a_path.write_text("") answer_b_path.write_text("bar") assert puzzle.answered_a is False assert puzzle.answered("a") is False assert puzzle.answered_b is True assert puzzle.answered("b") is True expected = AocdError('part must be "a" or "b"') with pytest.raises(expected): puzzle.answered("c")
def test_submit_bogus_part(): with pytest.raises(AocdError('part must be "a" or "b"')): submit(1234, part="c")
def test_will_not_submit_null(): with pytest.raises( AocdError("cowardly refusing to submit non-answer: None")): submit(None, part="a")
def test_year_out_of_range(freezer): freezer.move_to("2015-11-11") with pytest.raises(AocdError("Time travel not supported yet")): most_recent_year()
def test_get_day_and_year_fail_no_filename_on_stack(): with pytest.raises(AocdError("Failed introspection of filename")): get_day_and_year()
def test_day_is_invalid(mocker): fake_stack = [("~/2016/q27.py", )] mocker.patch("aocd.get.traceback.extract_stack", return_value=fake_stack) with pytest.raises(AocdError("Failed introspection of day")): get_day_and_year()
def test_year_is_ambiguous(mocker): fake_stack = [("~/2016/2017_q01.py", )] mocker.patch("aocd.get.traceback.extract_stack", return_value=fake_stack) with pytest.raises(AocdError("Failed introspection of year")): get_day_and_year()