From ae43bd3ea2f436ab84b9fd784a64d2354d903345 Mon Sep 17 00:00:00 2001 From: Marcel Wilson Date: Mon, 25 Sep 2023 12:32:56 -0500 Subject: [PATCH] adding logging tests for `See` --- tests/test_actions.py | 52 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/tests/test_actions.py b/tests/test_actions.py index 39279f9..6d0737b 100644 --- a/tests/test_actions.py +++ b/tests/test_actions.py @@ -134,7 +134,6 @@ def test_describe(self) -> None: class TestEventually: - settings_path = "screenpy.actions.eventually.settings" def test_can_be_instantiated(self) -> None: @@ -542,6 +541,36 @@ def test_describe(self) -> None: == "See if can you speak is only this sentence." ) + def test_log_passes(self, Tester, caplog) -> None: + with caplog.at_level(logging.INFO): + See(SimpleQuestion(), IsEqualTo(True)).perform_as(Tester) + + assert [r.msg for r in caplog.records] == [ + "Tester sees if simpleQuestion is equal to .", + " Tester examines SimpleQuestion", + " => ", + " ... hoping it's equal to .", + " => ", + ] + + def test_log_fails(self, Tester, caplog) -> None: + with caplog.at_level(logging.INFO), pytest.raises(AssertionError): + See(SimpleQuestion(), IsEqualTo(False)).perform_as(Tester) + + assert [r.msg for r in caplog.records] == [ + "Tester sees if simpleQuestion is equal to .", + " Tester examines SimpleQuestion", + " => ", + " ... hoping it's equal to .", + " => ", + # don't be fooled! the next few lines do not have commas on purpose + " ***ERROR***\n" + "\n" + "AssertionError: \n" + "Expected: \n" + " but: was \n", + ] + class TestSeeAllOf: def test_can_be_instantiated(self) -> None: @@ -970,6 +999,7 @@ class Action3: The results of this test show the strange behavior. """ + @beat("{} tries to Action3") def perform_as(self, the_actor: Actor) -> None: settings.UNABRIDGED_NARRATION = True @@ -1009,6 +1039,7 @@ class Action4: The results of this test show the strange behavior. """ + @beat("{} tries to Action4") def perform_as(self, the_actor: Actor) -> None: settings.UNABRIDGED_NARRATION = True @@ -1050,9 +1081,9 @@ def test_describe(self) -> None: mock_action2 = FakeAction() mock_action2.describe.return_value = "produce stuff!" - + t = Either(mock_action1).or_(mock_action2) - assert (t.describe() == "Either do thing or produce stuff") + assert t.describe() == "Either do thing or produce stuff" def test_multi_action_describe(self) -> None: mock_action1 = FakeAction() @@ -1065,13 +1096,13 @@ def test_multi_action_describe(self) -> None: mock_action4.describe.return_value = "PerformBar." t = Either(mock_action1, mock_action2).or_(mock_action3, mock_action4) - assert (t.describe() == "Either doThing, doStuff or performFoo, performBar") + assert t.describe() == "Either doThing, doStuff or performFoo, performBar" def test_first_action_passes(self, Tester, mocker: MockerFixture) -> None: mock_clear = mocker.spy(the_narrator, "clear_backup") mock_flush = mocker.spy(the_narrator, "flush_backup") mock_kink = mocker.spy(the_narrator, "mic_cable_kinked") - + action1 = FakeAction() action2 = FakeAction() Either(action1).or_(action2).perform_as(Tester) @@ -1086,12 +1117,12 @@ def test_first_action_fails(self, Tester, mocker: MockerFixture) -> None: mock_clear = mocker.spy(the_narrator, "clear_backup") mock_flush = mocker.spy(the_narrator, "flush_backup") mock_kink = mocker.spy(the_narrator, "mic_cable_kinked") - + exc = AssertionError("Wrong!") action1 = FakeAction() action2 = FakeAction() action1.perform_as.side_effect = exc - + Either(action1).or_(action2).perform_as(Tester) assert action1.perform_as.call_count == 1 @@ -1100,7 +1131,9 @@ def test_first_action_fails(self, Tester, mocker: MockerFixture) -> None: assert mock_clear.call_count == 2 assert mock_flush.call_count == 1 - def test_first_action_fails_with_custom_exception(self, Tester, mocker: MockerFixture) -> None: + def test_first_action_fails_with_custom_exception( + self, Tester, mocker: MockerFixture + ) -> None: mock_clear = mocker.spy(the_narrator, "clear_backup") mock_flush = mocker.spy(the_narrator, "flush_backup") mock_kink = mocker.spy(the_narrator, "mic_cable_kinked") @@ -1122,7 +1155,6 @@ class CustomException(Exception): assert mock_flush.call_count == 1 def test_output_first_fails(self, Tester, caplog): - class FakeActionFail(Performable): @beat("{} tries to FakeActionFail") def perform_as(self, actor: Actor): @@ -1151,7 +1183,7 @@ def perform_as(self, actor: Actor): caplog.set_level(logging.INFO) mock_settings = ScreenPySettings(UNABRIDGED_NARRATION=True) - + with mock.patch(self.settings_path, mock_settings): Either(FakeActionFail()).or_(FakeActionPass()).perform_as(Tester)