Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
fix: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
srwang committed Jul 12, 2023
1 parent 539a657 commit 33819b3
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions tubular/tests/test_segment_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ def json(self):
def raise_for_status(self):
raise requests.exceptions.HTTPError("", response=self)

class Fake400Response:
"""
Fakes an error response for the segment track call, which returns 400 if a request is poorly formatted. Otherwise it returns 200 which does not guarantee that segment actually tracked your event.
"""
status_code = 400
text="{'error': 'Bad request'}"

def json(self):
"""
Returns fake Segment retirement response error in the correct format
"""
return json.loads(self.text)

def raise_for_status(self):
raise requests.exceptions.HTTPError("", response=self)




@pytest.fixture
def setup_regulation_api():
Expand Down Expand Up @@ -168,7 +186,10 @@ def test_bulk_unsuppress_error(setup_regulation_api, caplog): # pylint: disable
assert "Unsuppress" in caplog.text
assert "Test error message" in caplog.text

def test_send_event_to_segment_success(setup_regulation_api):
def test_send_event_to_segment_success(setup_regulation_api): # pylint: disable=redefined-outer-name
"""
Test simple success case
"""
mock_post, segment = setup_regulation_api
mock_post.return_value = FakeResponse()

Expand All @@ -178,11 +199,24 @@ def test_send_event_to_segment_success(setup_regulation_api):

fake_json = {
"event": "test.event",
"userId": "edx-tubular-script"
"userId": "edx_tubular_script"
}

url = TEST_SEGMENT_CONFIG['fake_base_url'] + POST_EVENT_URL
mock_post.assert_any_call(
url, json=fake_json, headers=TEST_SEGMENT_CONFIG['headers']
)

def test_send_event_to_segment_error(setup_regulation_api): # pylint: disable=redefined-outer-name
"""
Test simple error case
"""
mock_post, segment = setup_regulation_api
mock_post.return_value = FakeErrorResponse()

with pytest.raises(Exception):
segment.send_event_to_segment('test.event')

assert mock_call.call_count = 1
assert "400 Bad Request" in caplog.text

0 comments on commit 33819b3

Please sign in to comment.