Skip to content

Commit 7437511

Browse files
committed
improve tests
1 parent bd26e6e commit 7437511

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

ntk/command.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
level=logging.INFO,
2121
datefmt='%Y-%m-%d %H:%M:%S'
2222
)
23+
logging.getLogger('watchfiles').setLevel(logging.WARNING)
2324

2425

2526
class Command:
@@ -45,10 +46,10 @@ def _handle_files_change(self, changes):
4546
for event_type, pathfile in changes:
4647
template_name = get_template_name(pathfile)
4748
if event_type in [Change.added, Change.modified]:
48-
logging.info(f'[{self.config.env}] {str(event_type)} {template_name}')
49+
logging.info(f'[{self.config.env}] {event_type.name.title()} {template_name}')
4950
self._push_templates([template_name], compile_sass=True)
5051
elif event_type == Change.deleted:
51-
logging.info(f'[{self.config.env}] {str(event_type)} {template_name}')
52+
logging.info(f'[{self.config.env}] {event_type.name.title()} {template_name}')
5253
self._delete_templates([template_name])
5354

5455
def _push_templates(self, template_names, compile_sass=False):

tests/test_command.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,62 @@ def test_pull_command_with_configs_and_filenames_should_be_download_only_file_in
310310

311311
mock_write_config.assert_not_called()
312312

313+
#####
314+
# push
315+
#####
316+
def test_push_command_without_config_file_should_be_required_api_key_store_and_theme_id(self):
317+
with self.assertRaises(TypeError) as error:
318+
self.parser.apikey = None
319+
self.parser.store = None
320+
self.parser.theme_id = None
321+
self.command.push(self.parser)
322+
self.assertEqual(
323+
str(error.exception), '[development] argument -a/--apikey, -s/--store, -t/--theme_id are required.')
324+
325+
@patch("ntk.command.Command._get_accept_files", autospec=True)
326+
def test_push_command_with_configs_and_without_filenames_should_upload_all_files(
327+
self, mock_get_accept_files
328+
):
329+
mock_get_accept_files.return_value = [
330+
f'{os.getcwd()}/layout/base.html',
331+
]
332+
self.mock_gateway.return_value.create_or_update_template.return_value.ok = True
333+
self.mock_gateway.return_value.create_or_update_template.return_value.headers = {
334+
'content-type': 'application/json; charset=utf-8'}
335+
self.command.config.parser_config(self.parser)
336+
self.parser.filenames = None
337+
with patch("builtins.open", self.mock_file):
338+
self.command.push(self.parser)
339+
expected_call = call().create_or_update_template(
340+
theme_id=1234,
341+
template_name='layout/base.html',
342+
content='{% load i18n %}\n\n<div class="mt-2">My home page</div>',
343+
files={}
344+
)
345+
self.assertIn(expected_call, self.mock_gateway.mock_calls)
346+
347+
@patch("ntk.command.Command._get_accept_files", autospec=True)
348+
def test_push_command_with_filenames_should_upload_only_specified_files(
349+
self, mock_get_accept_files
350+
):
351+
mock_get_accept_files.return_value = [
352+
f'{os.getcwd()}/layout/base.html',
353+
]
354+
self.mock_gateway.return_value.create_or_update_template.return_value.ok = True
355+
self.mock_gateway.return_value.create_or_update_template.return_value.headers = {
356+
'content-type': 'application/json; charset=utf-8'}
357+
self.command.config.parser_config(self.parser)
358+
self.parser.filenames = ['layout/base.html']
359+
with patch("builtins.open", self.mock_file):
360+
self.command.push(self.parser)
361+
expected_call = call().create_or_update_template(
362+
theme_id=1234,
363+
template_name='layout/base.html',
364+
content='{% load i18n %}\n\n<div class="mt-2">My home page</div>',
365+
files={}
366+
)
367+
self.assertIn(expected_call, self.mock_gateway.mock_calls)
368+
313369
#####
314370
# watch (_handle_files_change)
315371
#####

0 commit comments

Comments
 (0)