Skip to content

Commit

Permalink
Fix test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
RishiDiwanTT committed Nov 9, 2023
1 parent 61c5d03 commit 5a4e0f9
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions tests/util/test_xray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pytest import MonkeyPatch

from app import app
from tests.fixtures.controller import ControllerSetupFixture
from util.xray import PalaceXrayMiddleware, PalaceXrayUtils


Expand Down Expand Up @@ -41,7 +41,7 @@ def test_configure_app(self, monkeypatch):
monkeypatch.setattr("util.xray.PalaceXrayMiddleware", mock_middleware)

# Nothing happens if env isn't set
monkeypatch.delenv(PalaceXrayUtils.XRAY_ENV_ENABLE)
monkeypatch.delenv(PalaceXrayUtils.XRAY_ENV_ENABLE, raising=False)
PalaceXrayUtils.configure_app(mock_app)
assert PalaceXrayUtils.setup_xray.called is False
assert mock_middleware.called is False
Expand All @@ -60,21 +60,24 @@ def test_configure_app(self, monkeypatch):


class TestPalaceXrayMiddleware:
def test_before_request(self, monkeypatch: MonkeyPatch):
def test_before_request(
self, monkeypatch: MonkeyPatch, controller_setup_fixture: ControllerSetupFixture
):
mock_app = MagicMock()
mock_app._xray_first_request_done = None
mock_recorder = MagicMock()
xray = PalaceXrayMiddleware(mock_app, mock_recorder)

# First request does additional setup
with app.test_request_context("/") as ctx:
xray._before_request()
assert mock_app._xray_first_request_done == True
assert mock_recorder.current_segment().put_annotation.call_count == 2
assert ctx.request._palace_first_request == True
with controller_setup_fixture.setup() as fixture:
# First request does additional setup
with fixture.app.test_request_context("/") as ctx:
xray._before_request()
assert mock_app._xray_first_request_done == True
assert mock_recorder.current_segment().put_annotation.call_count == 2
assert ctx.request._palace_first_request == True

# Second request only calls put_annotation once
with app.test_request_context("/") as ctx:
xray._before_request()
assert getattr(ctx.request, "_palace_first_request", None) is None
assert mock_recorder.current_segment().put_annotation.call_count == 3
# Second request only calls put_annotation once
with fixture.app.test_request_context("/") as ctx:
xray._before_request()
assert getattr(ctx.request, "_palace_first_request", None) is None
assert mock_recorder.current_segment().put_annotation.call_count == 3

0 comments on commit 5a4e0f9

Please sign in to comment.