-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tests): add test_export_course_skills.py (#1992)
* refactor(tests): add test_export_course_skills.py * fix(tests): add get_fake_skill * fix(tests): remove unused imports * refactor(tests): move get_fake_skill() to fakes * refactor(tests): add test_export_skill.py * fix: remove fakes. * fix: remove fakes. * fix(tests): remove unused import * fix(tests): use proper import order * fix(tests): use caplog * fix(tests): use proper order of entries in caplog.record_tuples * refactor(tests): add test_export_course_data.py * refactor(tests): add test_export_course.py Co-authored-by: Daniel Kantor <github@daniel-kantor.com>
- Loading branch information
Showing
7 changed files
with
229 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
58 changes: 58 additions & 0 deletions
58
apps/librelingo_json_export/tests/test_export_course_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
) |
48 changes: 48 additions & 0 deletions
48
apps/librelingo_json_export/tests/test_export_course_skills.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import pytest | ||
|
||
from librelingo_json_export.export import _export_course_skills | ||
from librelingo_types import Module | ||
from librelingo_fakes import fakes | ||
|
||
|
||
@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 = fakes.get_fake_skill() | ||
_, fake_skill_2 = fakes.get_fake_skill() | ||
_, fake_skill_3 = fakes.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, | ||
) |
Oops, something went wrong.