From 997a74c8a26a4e2528e337c31bd44e82ec209d3e Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 18:26:24 +0100 Subject: [PATCH 01/13] refactor(tests): add test_export_course_skills.py --- .../tests/test_export.py | 40 --------------- .../tests/test_export_course_skills.py | 50 +++++++++++++++++++ 2 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 apps/librelingo_json_export/tests/test_export_course_skills.py diff --git a/apps/librelingo_json_export/tests/test_export.py b/apps/librelingo_json_export/tests/test_export.py index e6b6fa6de84c..84601a01763b 100644 --- a/apps/librelingo_json_export/tests/test_export.py +++ b/apps/librelingo_json_export/tests/test_export.py @@ -7,7 +7,6 @@ from librelingo_fakes import fakes from librelingo_types import Module, Language from librelingo_json_export.export import ( - _export_course_skills, _export_skill, _export_course_data, export_course, @@ -23,45 +22,6 @@ def get_fake_skill(introduction=None): ) -class TestExportCourseSkills(FakeFsTestCase): - def setUp(self): - self.setUpPyfakefs() - self.export_path = fakes.path() - - @patch("librelingo_json_export.export._export_skill") - def test_exports_all_skills(self, _export_skill): - _, fake_skill_1 = get_fake_skill() - _, fake_skill_2 = get_fake_skill() - _, fake_skill_3 = get_fake_skill() - fake_module_1 = Module( - title="", - filename="", - skills=[ - fake_skill_1, - fake_skill_2, - ], - ) - fake_module_2 = Module( - title="", - filename="", - skills=[ - fake_skill_3, - ], - ) - fake_course = fakes.customize( - fakes.course1, modules=[fake_module_1, fake_module_2] - ) - _export_course_skills(self.export_path, fake_course) - _export_skill.assert_has_calls( - [ - call(self.export_path, fake_skill_1, fake_course, None), - call(self.export_path, fake_skill_2, fake_course, None), - call(self.export_path, fake_skill_3, fake_course, None), - ], - any_order=True, - ) - - class TestExportSkill(FakeFsTestCase): def setUp(self): self.setUpPyfakefs() diff --git a/apps/librelingo_json_export/tests/test_export_course_skills.py b/apps/librelingo_json_export/tests/test_export_course_skills.py new file mode 100644 index 000000000000..149be58e4285 --- /dev/null +++ b/apps/librelingo_json_export/tests/test_export_course_skills.py @@ -0,0 +1,50 @@ +import pytest + +from librelingo_json_export.export import _export_course_skills +from librelingo_types import Module +from librelingo_fakes import fakes + +from . import test_export as te + + +@pytest.fixture +def export_path(): + return fakes.path() + + +@pytest.fixture +def mock_export_skill(mocker): + return mocker.patch("librelingo_json_export.export._export_skill") + + +def test_exports_all_skills( + mocker, fs, export_path, mock_export_skill +): # pylint:disable=invalid-name + _, fake_skill_1 = te.get_fake_skill() + _, fake_skill_2 = te.get_fake_skill() + _, fake_skill_3 = te.get_fake_skill() + fake_module_1 = Module( + title="", + filename="", + skills=[ + fake_skill_1, + fake_skill_2, + ], + ) + fake_module_2 = Module( + title="", + filename="", + skills=[ + fake_skill_3, + ], + ) + fake_course = fakes.customize(fakes.course1, modules=[fake_module_1, fake_module_2]) + _export_course_skills(export_path, fake_course) + mock_export_skill.assert_has_calls( + [ + mocker.call(export_path, fake_skill_1, fake_course, None), + mocker.call(export_path, fake_skill_2, fake_course, None), + mocker.call(export_path, fake_skill_3, fake_course, None), + ], + any_order=True, + ) From f291ee938dd96e4acac7c2d17e2f8562686e5af5 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 18:42:35 +0100 Subject: [PATCH 02/13] fix(tests): add get_fake_skill --- .../tests/test_export_course_skills.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/apps/librelingo_json_export/tests/test_export_course_skills.py b/apps/librelingo_json_export/tests/test_export_course_skills.py index 149be58e4285..16ad37ee2e59 100644 --- a/apps/librelingo_json_export/tests/test_export_course_skills.py +++ b/apps/librelingo_json_export/tests/test_export_course_skills.py @@ -4,7 +4,14 @@ from librelingo_types import Module from librelingo_fakes import fakes -from . import test_export as te + +def get_fake_skill(introduction=None): + random_word = "quiz" + return random_word, fakes.customize( + fakes.skillWithPhraseAndWord, + name=f"Animals {random_word}", + introduction=introduction, + ) @pytest.fixture @@ -20,9 +27,9 @@ def mock_export_skill(mocker): def test_exports_all_skills( mocker, fs, export_path, mock_export_skill ): # pylint:disable=invalid-name - _, fake_skill_1 = te.get_fake_skill() - _, fake_skill_2 = te.get_fake_skill() - _, fake_skill_3 = te.get_fake_skill() + _, fake_skill_1 = get_fake_skill() + _, fake_skill_2 = get_fake_skill() + _, fake_skill_3 = get_fake_skill() fake_module_1 = Module( title="", filename="", From 60af910830943f0aa39cdac0f11b0cf7dc121043 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 18:51:58 +0100 Subject: [PATCH 03/13] fix(tests): remove unused imports --- apps/librelingo_json_export/tests/test_export.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/librelingo_json_export/tests/test_export.py b/apps/librelingo_json_export/tests/test_export.py index 84601a01763b..064bc23ebdc8 100644 --- a/apps/librelingo_json_export/tests/test_export.py +++ b/apps/librelingo_json_export/tests/test_export.py @@ -1,11 +1,11 @@ -from unittest.mock import patch, call +from unittest.mock import patch import json import os from pyfakefs.fake_filesystem_unittest import TestCase as FakeFsTestCase # type: ignore from librelingo_fakes import fakes -from librelingo_types import Module, Language +from librelingo_types import Language from librelingo_json_export.export import ( _export_skill, _export_course_data, From 7783853a7d9690911080633c48f061d4b5a522a8 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 19:02:14 +0100 Subject: [PATCH 04/13] refactor(tests): move get_fake_skill() to fakes --- apps/librelingo_fakes/librelingo_fakes/fakes.py | 9 +++++++++ .../tests/test_export_course_skills.py | 15 +++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/apps/librelingo_fakes/librelingo_fakes/fakes.py b/apps/librelingo_fakes/librelingo_fakes/fakes.py index 3565a31d1d59..401a0d0e3280 100644 --- a/apps/librelingo_fakes/librelingo_fakes/fakes.py +++ b/apps/librelingo_fakes/librelingo_fakes/fakes.py @@ -280,3 +280,12 @@ def customize(fake, **kwargs): def path(): return Path(f"./path{random.randint(0, 5000)}") + + +def get_fake_skill(introduction=None): + random_word = "quiz" + return random_word, fakes.customize( + fakes.skillWithPhraseAndWord, + name=f"Animals {random_word}", + introduction=introduction, + ) diff --git a/apps/librelingo_json_export/tests/test_export_course_skills.py b/apps/librelingo_json_export/tests/test_export_course_skills.py index 16ad37ee2e59..e63f2942e7a2 100644 --- a/apps/librelingo_json_export/tests/test_export_course_skills.py +++ b/apps/librelingo_json_export/tests/test_export_course_skills.py @@ -5,15 +5,6 @@ from librelingo_fakes import fakes -def get_fake_skill(introduction=None): - random_word = "quiz" - return random_word, fakes.customize( - fakes.skillWithPhraseAndWord, - name=f"Animals {random_word}", - introduction=introduction, - ) - - @pytest.fixture def export_path(): return fakes.path() @@ -27,9 +18,9 @@ def mock_export_skill(mocker): def test_exports_all_skills( mocker, fs, export_path, mock_export_skill ): # pylint:disable=invalid-name - _, fake_skill_1 = get_fake_skill() - _, fake_skill_2 = get_fake_skill() - _, fake_skill_3 = get_fake_skill() + _, fake_skill_1 = fakes.get_fake_skill() + _, fake_skill_2 = fakes.get_fake_skill() + _, fake_skill_3 = fakes.get_fake_skill() fake_module_1 = Module( title="", filename="", From b6e0dc0a74a8fe10585401fcf945662fb49d0355 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 19:39:44 +0100 Subject: [PATCH 05/13] refactor(tests): add test_export_skill.py --- .../tests/test_export.py | 67 ----------------- .../tests/test_export_skill.py | 74 +++++++++++++++++++ 2 files changed, 74 insertions(+), 67 deletions(-) create mode 100644 apps/librelingo_json_export/tests/test_export_skill.py diff --git a/apps/librelingo_json_export/tests/test_export.py b/apps/librelingo_json_export/tests/test_export.py index 064bc23ebdc8..d6c50a3b88c2 100644 --- a/apps/librelingo_json_export/tests/test_export.py +++ b/apps/librelingo_json_export/tests/test_export.py @@ -13,73 +13,6 @@ ) -def get_fake_skill(introduction=None): - random_word = "quiz" - return random_word, fakes.customize( - fakes.skillWithPhraseAndWord, - name=f"Animals {random_word}", - introduction=introduction, - ) - - -class TestExportSkill(FakeFsTestCase): - def setUp(self): - self.setUpPyfakefs() - self.export_path = fakes.path() - - def test_creates_the_challenges_file(self): - random_name, fake_skill = get_fake_skill() - _export_skill(self.export_path, fake_skill, fakes.course1) - self.assertTrue( - os.path.exists( - self.export_path / "challenges" / f"animals-{random_name}.json" - ) - ) - - def test_creates_the_introduction_file(self): - fake_name = str(fakes.fake_value()) - introduction = f"# *Hello* (https://example.com)[_{fake_name}_]!" - random_name, fake_skill = get_fake_skill(introduction=introduction) - _export_skill(self.export_path, fake_skill, fakes.course1) - with open( - self.export_path / "introduction" / f"animals-{random_name}.md" - ) as animals_file: - introduction_file_content = animals_file.read() - self.assertEqual(introduction_file_content, introduction) - - def test_does_not_create_an_introduction_file_if_theres_no_introduction(self): - random_name, fake_skill = get_fake_skill() - _export_skill(self.export_path, fake_skill, fakes.course1) - self.assertFalse( - os.path.exists( - self.export_path / "introduction" / f"animals-{random_name}.md" - ) - ) - - @patch("librelingo_json_export.export._get_skill_data") - def test_calls__get_skill_data_with_correct_value(self, _get_skill_data): - _get_skill_data.return_value = [] - _export_skill(self.export_path, fakes.skillWithPhraseAndWord, fakes.course1) - _get_skill_data.assert_called_with(fakes.skillWithPhraseAndWord, fakes.course1) - - @patch("librelingo_json_export.export._get_skill_data") - def test_writes_correct_value_into_json_file(self, _get_skill_data): - fake_skill_data = {"fake_skill_data": 1000} - _get_skill_data.return_value = fake_skill_data - _export_skill(self.export_path, fakes.skillWithPhraseAndWord, fakes.course1) - with open(self.export_path / "challenges" / "masculine.json") as masculine_file: - self.assertEqual(json.loads(masculine_file.read()), fake_skill_data) - - def test_assert_logs_correctly(self): - with self.assertLogs("librelingo_json_export", level="INFO") as log: - _, fake_skill = get_fake_skill() - _export_skill(self.export_path, fake_skill, fakes.course1) - self.assertEqual( - log.output[0], - f"INFO:librelingo_json_export:Writing skill '{fake_skill.name}'", - ) - - class TestExportCourseData(FakeFsTestCase): def setUp(self): self.setUpPyfakefs() diff --git a/apps/librelingo_json_export/tests/test_export_skill.py b/apps/librelingo_json_export/tests/test_export_skill.py new file mode 100644 index 000000000000..191a1e2b16b0 --- /dev/null +++ b/apps/librelingo_json_export/tests/test_export_skill.py @@ -0,0 +1,74 @@ +import os +import json +import pytest +import logging + +from librelingo_json_export.export import _export_skill + +from librelingo_fakes import fakes + + +@pytest.fixture +def export_path(): + return fakes.path() + + +def test_creates_the_challenges_file(fs, export_path): # pylint:disable=invalid-name + random_name, fake_skill = fakes.get_fake_skill() + _export_skill(export_path, fake_skill, fakes.course1) + assert os.path.exists(export_path / "challenges" / f"animals-{random_name}.json") + + +def test_creates_the_introduction_file(fs, export_path): # pylint:disable=invalid-name + fake_name = str(fakes.fake_value()) + introduction = f"# *Hello* (https://example.com)[_{fake_name}_]!" + random_name, fake_skill = fakes.get_fake_skill(introduction=introduction) + _export_skill(export_path, fake_skill, fakes.course1) + with open( + export_path / "introduction" / f"animals-{random_name}.md" + ) as animals_file: + introduction_file_content = animals_file.read() + assert introduction_file_content == introduction + + +def test_does_not_create_an_introduction_file_if_theres_no_introduction( + fs, export_path +): # pylint:disable=invalid-name + random_name, fake_skill = fakes.get_fake_skill() + _export_skill(export_path, fake_skill, fakes.course1) + assert not os.path.exists( + export_path / "introduction" / f"animals-{random_name}.md" + ) + + +@pytest.fixture +def mock_get_skill_data(mocker): + return mocker.patch("librelingo_json_export.export._get_skill_data") + + +def test_calls__get_skill_data_with_correct_value( + fs, export_path, mock_get_skill_data +): # pylint:disable=invalid-name + mock_get_skill_data.return_value = [] + _export_skill(export_path, fakes.skillWithPhraseAndWord, fakes.course1) + mock_get_skill_data.assert_called_with(fakes.skillWithPhraseAndWord, fakes.course1) + + +def test_writes_correct_value_into_json_file( + fs, export_path, mock_get_skill_data +): # pylint:disable=invalid-name + fake_skill_data = {"fake_skill_data": 1000} + mock_get_skill_data.return_value = fake_skill_data + _export_skill(export_path, fakes.skillWithPhraseAndWord, fakes.course1) + with open(export_path / "challenges" / "masculine.json") as masculine_file: + assert json.loads(masculine_file.read()) == fake_skill_data + + +def test_assert_logs_correctly(fs, export_path): # pylint:disable=invalid-name + with logging.getLogger("librelingo_json_export").setLevel(logging.INFO) as log: + _, fake_skill = fakes.get_fake_skill() + _export_skill(export_path, fake_skill, fakes.course1) + assert ( + log.output[0] + == f"INFO:librelingo_json_export:Writing skill '{fake_skill.name}'" + ) From dcc05dbd10ca9416ad106d789f59ed12a97946da Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 19:44:40 +0100 Subject: [PATCH 06/13] fix: remove fakes. --- apps/librelingo_fakes/librelingo_fakes/fakes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/librelingo_fakes/librelingo_fakes/fakes.py b/apps/librelingo_fakes/librelingo_fakes/fakes.py index 401a0d0e3280..985c97ac1ad0 100644 --- a/apps/librelingo_fakes/librelingo_fakes/fakes.py +++ b/apps/librelingo_fakes/librelingo_fakes/fakes.py @@ -284,7 +284,7 @@ def path(): def get_fake_skill(introduction=None): random_word = "quiz" - return random_word, fakes.customize( + return random_word, customize( fakes.skillWithPhraseAndWord, name=f"Animals {random_word}", introduction=introduction, From cbd8fe334a65b1f7660d24131f46541de6d968fd Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 19:53:37 +0100 Subject: [PATCH 07/13] fix: remove fakes. --- apps/librelingo_fakes/librelingo_fakes/fakes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/librelingo_fakes/librelingo_fakes/fakes.py b/apps/librelingo_fakes/librelingo_fakes/fakes.py index 985c97ac1ad0..026c9462e53c 100644 --- a/apps/librelingo_fakes/librelingo_fakes/fakes.py +++ b/apps/librelingo_fakes/librelingo_fakes/fakes.py @@ -285,7 +285,7 @@ def path(): def get_fake_skill(introduction=None): random_word = "quiz" return random_word, customize( - fakes.skillWithPhraseAndWord, + skillWithPhraseAndWord, name=f"Animals {random_word}", introduction=introduction, ) From 959f2e5ed2347d98123656674d8cddecf5546c76 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 19:56:28 +0100 Subject: [PATCH 08/13] fix(tests): remove unused import --- apps/librelingo_json_export/tests/test_export.py | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/librelingo_json_export/tests/test_export.py b/apps/librelingo_json_export/tests/test_export.py index d6c50a3b88c2..ab5026d25e35 100644 --- a/apps/librelingo_json_export/tests/test_export.py +++ b/apps/librelingo_json_export/tests/test_export.py @@ -7,7 +7,6 @@ from librelingo_fakes import fakes from librelingo_types import Language from librelingo_json_export.export import ( - _export_skill, _export_course_data, export_course, ) From 0daa32700ec725e6638304fbf9036f6774eda171 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 19:57:38 +0100 Subject: [PATCH 09/13] fix(tests): use proper import order --- apps/librelingo_json_export/tests/test_export_skill.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/librelingo_json_export/tests/test_export_skill.py b/apps/librelingo_json_export/tests/test_export_skill.py index 191a1e2b16b0..1ba3bbfe98c7 100644 --- a/apps/librelingo_json_export/tests/test_export_skill.py +++ b/apps/librelingo_json_export/tests/test_export_skill.py @@ -1,7 +1,7 @@ import os import json -import pytest import logging +import pytest from librelingo_json_export.export import _export_skill From 94b9828d1d21f2e147f370c258c5177500bf988c Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 21:00:23 +0100 Subject: [PATCH 10/13] fix(tests): use caplog --- .../librelingo_json_export/tests/test_export_skill.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/apps/librelingo_json_export/tests/test_export_skill.py b/apps/librelingo_json_export/tests/test_export_skill.py index 1ba3bbfe98c7..0cdbfcdf9185 100644 --- a/apps/librelingo_json_export/tests/test_export_skill.py +++ b/apps/librelingo_json_export/tests/test_export_skill.py @@ -64,11 +64,12 @@ def test_writes_correct_value_into_json_file( assert json.loads(masculine_file.read()) == fake_skill_data -def test_assert_logs_correctly(fs, export_path): # pylint:disable=invalid-name - with logging.getLogger("librelingo_json_export").setLevel(logging.INFO) as log: +def test_assert_logs_correctly(fs, caplog, export_path): # pylint:disable=invalid-name + with caplog.at_level(logging.INFO, logger="librelingo_json_export"): _, fake_skill = fakes.get_fake_skill() _export_skill(export_path, fake_skill, fakes.course1) - assert ( - log.output[0] - == f"INFO:librelingo_json_export:Writing skill '{fake_skill.name}'" + assert caplog.record_tuples[0] == ( + logging.INFO, + "librelingo_json_export", + f"Writing skill '{fake_skill.name}'", ) From 850fd6df45ed093d4781d2e2f8efa8cb1bd53193 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 21:06:50 +0100 Subject: [PATCH 11/13] fix(tests): use proper order of entries in caplog.record_tuples --- apps/librelingo_json_export/tests/test_export_skill.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/librelingo_json_export/tests/test_export_skill.py b/apps/librelingo_json_export/tests/test_export_skill.py index 0cdbfcdf9185..130c9415e57e 100644 --- a/apps/librelingo_json_export/tests/test_export_skill.py +++ b/apps/librelingo_json_export/tests/test_export_skill.py @@ -69,7 +69,7 @@ def test_assert_logs_correctly(fs, caplog, export_path): # pylint:disable=inval _, fake_skill = fakes.get_fake_skill() _export_skill(export_path, fake_skill, fakes.course1) assert caplog.record_tuples[0] == ( - logging.INFO, "librelingo_json_export", + logging.INFO, f"Writing skill '{fake_skill.name}'", ) From 3de40baea15c7d3ab8e35c54d1fdece011b1e658 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 21:25:21 +0100 Subject: [PATCH 12/13] refactor(tests): add test_export_course_data.py --- .../tests/test_export.py | 46 +-------------- .../tests/test_export_course_data.py | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 44 deletions(-) create mode 100644 apps/librelingo_json_export/tests/test_export_course_data.py diff --git a/apps/librelingo_json_export/tests/test_export.py b/apps/librelingo_json_export/tests/test_export.py index ab5026d25e35..478201b2dc1b 100644 --- a/apps/librelingo_json_export/tests/test_export.py +++ b/apps/librelingo_json_export/tests/test_export.py @@ -1,56 +1,14 @@ from unittest.mock import patch -import json -import os - from pyfakefs.fake_filesystem_unittest import TestCase as FakeFsTestCase # type: ignore from librelingo_fakes import fakes -from librelingo_types import Language + +# from librelingo_types import Language from librelingo_json_export.export import ( - _export_course_data, export_course, ) -class TestExportCourseData(FakeFsTestCase): - def setUp(self): - self.setUpPyfakefs() - self.export_path = fakes.path() - - def test_creates_the_correct_file(self): - _export_course_data(self.export_path, fakes.course1) - self.assertTrue(os.path.exists(self.export_path / "courseData.json")) - - @patch("librelingo_json_export.export._get_course_data") - def test_calls__get_course_data_with_correct_value(self, _get_course_data): - _get_course_data.return_value = [] - _export_course_data(self.export_path, fakes.course1) - _get_course_data.assert_called_with(fakes.course1) - - @patch("librelingo_json_export.export._get_course_data") - def test_writes_correct_value_into_json_file(self, _get_course_data): - fake_course_data = {"fake_course_data": 1000} - _get_course_data.return_value = fake_course_data - _export_course_data(self.export_path, fakes.course1) - with open(self.export_path / "courseData.json") as f: - self.assertEqual(json.loads(f.read()), fake_course_data) - - def test_assert_logs_correctly(self): - with self.assertLogs("librelingo_json_export", level="INFO") as log: - course_name = "Animals" - target_name = "English" - fake_course = fakes.customize( - fakes.course1, - target_language=Language(name=course_name, code=""), - source_language=Language(name=target_name, code=""), - ) - _export_course_data(self.export_path, fake_course) - self.assertEqual( - log.output[0], - f"INFO:librelingo_json_export:Writing course {course_name} for {target_name} speakers", - ) - - class TestExportCourse(FakeFsTestCase): def setUp(self): self.setUpPyfakefs() diff --git a/apps/librelingo_json_export/tests/test_export_course_data.py b/apps/librelingo_json_export/tests/test_export_course_data.py new file mode 100644 index 000000000000..b25d85e10696 --- /dev/null +++ b/apps/librelingo_json_export/tests/test_export_course_data.py @@ -0,0 +1,58 @@ +import os +import json +import logging +import pytest + +from librelingo_json_export.export import _export_course_data +from librelingo_types import Language +from librelingo_fakes import fakes + + +@pytest.fixture +def export_path(): + return fakes.path() + + +def test_creates_the_correct_file(fs, export_path): # pylint:disable=invalid-name + _export_course_data(export_path, fakes.course1) + assert os.path.exists(export_path / "courseData.json") + + +@pytest.fixture +def mock_get_course_data(mocker): + return mocker.patch("librelingo_json_export.export._get_course_data") + + +def test_calls__get_course_data_with_correct_value( + fs, export_path, mock_get_course_data +): # pylint:disable=invalid-name + mock_get_course_data.return_value = [] + _export_course_data(export_path, fakes.course1) + mock_get_course_data.assert_called_with(fakes.course1) + + +def test_writes_correct_value_into_json_file( + fs, export_path, mock_get_course_data +): # pylint:disable=invalid-name + fake_course_data = {"fake_course_data": 1000} + mock_get_course_data.return_value = fake_course_data + _export_course_data(export_path, fakes.course1) + with open(export_path / "courseData.json") as f: + assert json.loads(f.read()) == fake_course_data + + +def test_assert_logs_correctly(caplog, fs, export_path): # pylint:disable=invalid-name + with caplog.at_level(logging.INFO, logger="librelingo_json_export"): + course_name = "Animals" + target_name = "English" + fake_course = fakes.customize( + fakes.course1, + target_language=Language(name=course_name, code=""), + source_language=Language(name=target_name, code=""), + ) + _export_course_data(export_path, fake_course) + assert caplog.record_tuples[0] == ( + "librelingo_json_export", + logging.INFO, + f"Writing course {course_name} for {target_name} speakers", + ) From 0085769033624e89930b0fe6843fd7fdf3421387 Mon Sep 17 00:00:00 2001 From: Piotr Idzik Date: Wed, 12 Jan 2022 21:33:12 +0100 Subject: [PATCH 13/13] refactor(tests): add test_export_course.py --- .../tests/test_export.py | 27 --------------- .../tests/test_export_course.py | 33 +++++++++++++++++++ 2 files changed, 33 insertions(+), 27 deletions(-) delete mode 100644 apps/librelingo_json_export/tests/test_export.py create mode 100644 apps/librelingo_json_export/tests/test_export_course.py diff --git a/apps/librelingo_json_export/tests/test_export.py b/apps/librelingo_json_export/tests/test_export.py deleted file mode 100644 index 478201b2dc1b..000000000000 --- a/apps/librelingo_json_export/tests/test_export.py +++ /dev/null @@ -1,27 +0,0 @@ -from unittest.mock import patch - -from pyfakefs.fake_filesystem_unittest import TestCase as FakeFsTestCase # type: ignore -from librelingo_fakes import fakes - -# from librelingo_types import Language -from librelingo_json_export.export import ( - export_course, -) - - -class TestExportCourse(FakeFsTestCase): - def setUp(self): - self.setUpPyfakefs() - self.export_path = fakes.path() - - @patch("librelingo_json_export.export._export_course_data") - def test_calls__export_course_data_with_correct_value(self, _export_course_data): - export_course(self.export_path, fakes.course1) - _export_course_data.assert_called_with(self.export_path, fakes.course1, None) - - @patch("librelingo_json_export.export._export_course_skills") - def test_calls__export_course_skills_with_correct_value( - self, _export_course_skills - ): - export_course(self.export_path, fakes.course1) - _export_course_skills.assert_called_with(self.export_path, fakes.course1, None) diff --git a/apps/librelingo_json_export/tests/test_export_course.py b/apps/librelingo_json_export/tests/test_export_course.py new file mode 100644 index 000000000000..5d97fe44b17d --- /dev/null +++ b/apps/librelingo_json_export/tests/test_export_course.py @@ -0,0 +1,33 @@ +import pytest + +from librelingo_json_export.export import export_course +from librelingo_fakes import fakes + + +@pytest.fixture +def export_path(): + return fakes.path() + + +@pytest.fixture +def mock_export_course_data(mocker): + return mocker.patch("librelingo_json_export.export._export_course_data") + + +def test_calls__export_course_data_with_correct_value( + fs, export_path, mock_export_course_data +): # pylint:disable=invalid-name + export_course(export_path, fakes.course1) + mock_export_course_data.assert_called_with(export_path, fakes.course1, None) + + +@pytest.fixture +def mock_export_course_skills(mocker): + return mocker.patch("librelingo_json_export.export._export_course_skills") + + +def test_calls__export_course_skills_with_correct_value( + fs, export_path, mock_export_course_skills +): # pylint:disable=invalid-name + export_course(export_path, fakes.course1) + mock_export_course_skills.assert_called_with(export_path, fakes.course1, None)