Skip to content

Commit

Permalink
adding logging tests for See
Browse files Browse the repository at this point in the history
  • Loading branch information
bandophahita committed Sep 25, 2023
1 parent 9d2056e commit ae43bd3
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def test_describe(self) -> None:


class TestEventually:

settings_path = "screenpy.actions.eventually.settings"

def test_can_be_instantiated(self) -> None:
Expand Down Expand Up @@ -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 <True>.",
" Tester examines SimpleQuestion",
" => <True>",
" ... hoping it's equal to <True>.",
" => <True>",
]

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 <False>.",
" Tester examines SimpleQuestion",
" => <True>",
" ... hoping it's equal to <False>.",
" => <False>",
# don't be fooled! the next few lines do not have commas on purpose
" ***ERROR***\n"
"\n"
"AssertionError: \n"
"Expected: <False>\n"
" but: was <True>\n",
]


class TestSeeAllOf:
def test_can_be_instantiated(self) -> None:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit ae43bd3

Please sign in to comment.