79203922

Date: 2024-11-19 15:02:22
Score: 1.5
Natty:
Report link

Asked for help on the streamlit forums and got a great answer: https://discuss.streamlit.io/t/mocking-out-a-postgres-connection-in-session-state-with-pytest-monkeypatch/85907/9

The gist is to organize the session into multiple classes instead of a nested one.

class FakeSession:
    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        return None

    def execute(self, statement):
        return [[100]]

    def commit(self):
        pass


class FakeConnection:
    session = FakeSession()


class FakeSessionState:
    conn = FakeConnection()


class Test_Burger:
    def test_flip_burger(self, monkeypatch):
        fake_session_state = FakeSessionState()
        monkeypatch.setattr(st, "session_state", fake_session_state)
        borger = Burger()
        assert borger.flip_burger() == ["100"]
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: lunchbox7804