Skip to content

Commit

Permalink
THEME-KIT #20 | update test
Browse files Browse the repository at this point in the history
  • Loading branch information
gorsutlame committed Mar 22, 2024
1 parent a50692f commit 6f7f5df
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
5 changes: 1 addition & 4 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,10 @@ def test_watch_command_should_call_gateway_with_correct_arguments_belong_to_file
theme_id=1234, template_name='layout/base.html')
self.assertIn(expected_call_deleted, self.mock_gateway.mock_calls)

@patch("ntk.command.time", autospec=True)
@patch("ntk.command.Command._get_accept_files", autospec=True)
@patch("builtins.open", autospec=True)
def test_watch_command_with_create_image_file_should_call_gateway_with_correct_arguments(
self, mock_open_file, mock_get_accept_file, mock_time
self, mock_open_file, mock_get_accept_file
):
mock_get_accept_file.return_value = [
f'{os.getcwd()}/assets/image.jpg',
Expand All @@ -372,8 +371,6 @@ def test_watch_command_with_create_image_file_should_call_gateway_with_correct_a
)
self.assertIn(expected_call_added, self.mock_gateway.mock_calls)

mock_time.sleep.assert_called_once_with(0.07)

@patch("ntk.command.Command._get_accept_files", autospec=True)
@patch("ntk.command.Command._compile_sass", autospec=True)
def test_watch_command_with_sass_directory_should_call_compile_sass(
Expand Down
55 changes: 49 additions & 6 deletions tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,56 @@ def test_request(self, mock_request):

self.gateway._request(request_type, url, apikey=self.apikey, payload=payload, files=files)

expected_calls = [call(
'POST', 'http://simple.com/api/admin/themes/5/templates/',
headers={'Authorization': 'Bearer apikey'},
data={'name': 'assets/base.html', 'content': '{% load i18n %}\n\n<div class="mt-2">My home page</div>'},
files=files)
expected_calls = [
call(
'POST', 'http://simple.com/api/admin/themes/5/templates/',
headers={'Authorization': 'Bearer apikey'},
data={
'name': 'assets/base.html', 'content': '{% load i18n %}\n\n<div class="mt-2">My home page</div>'},
files=files),
call().status_code.__eq__(429)
]
self.assertEqual(mock_request.mock_calls, expected_calls)
assert mock_request.mock_calls == expected_calls

@patch('ntk.gateway.requests.request', autospec=True)
def test_request_with_rate_limit_should_retry(self, mock_request):
mock_response_429 = MagicMock()
mock_response_429.status_code = 429
mock_response_429.content.decode.return_value = "throttled"

# Mock the response for the second call with status code 200
mock_response_200 = MagicMock()
mock_response_200.status_code = 200

mock_request.side_effect = [mock_response_429, mock_response_200]

request_type = 'POST'
url = 'http://simple.com/api/admin/themes/5/templates/'
payload = {
'name': 'assets/base.html',
'content': '{% load i18n %}\n\n<div class="mt-2">My home page</div>'
}
files = {'file': ('assets/image.jpg', self.mock_img_file)}

self.gateway._request(request_type, url, apikey=self.apikey, payload=payload, files=files)

assert mock_request.call_count == 2

expected_calls = [
call(
'POST', 'http://simple.com/api/admin/themes/5/templates/',
headers={'Authorization': 'Bearer apikey'},
data={
'name': 'assets/base.html', 'content': '{% load i18n %}\n\n<div class="mt-2">My home page</div>'
}, files=files),
call(
'POST', 'http://simple.com/api/admin/themes/5/templates/',
headers={'Authorization': 'Bearer apikey'},
data={
'name': 'assets/base.html', 'content': '{% load i18n %}\n\n<div class="mt-2">My home page</div>'
}, files=files)
]
assert mock_request.mock_calls == expected_calls

#####
# get_themes
Expand Down

0 comments on commit 6f7f5df

Please sign in to comment.