Skip to content

Commit

Permalink
refactor(tests): add test_export_course_skills.py (#1992)
Browse files Browse the repository at this point in the history
* 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
vil02 and kantord committed Jan 22, 2022
1 parent 20eea84 commit 353f234
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 178 deletions.
11 changes: 10 additions & 1 deletion apps/librelingo_fakes/librelingo_fakes/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,13 @@ def customize(fake, **kwargs):


def path():
return Path(f"./path{number(max_value=5000)}")
return Path(f"./path{random.randint(0, 5000)}")


def get_fake_skill(introduction=None):
random_word = "quiz"
return random_word, customize(
skillWithPhraseAndWord,
name=f"Animals {random_word}",
introduction=introduction,
)
177 changes: 0 additions & 177 deletions apps/librelingo_json_export/tests/test_export.py

This file was deleted.

33 changes: 33 additions & 0 deletions apps/librelingo_json_export/tests/test_export_course.py
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 apps/librelingo_json_export/tests/test_export_course_data.py
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 apps/librelingo_json_export/tests/test_export_course_skills.py
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,
)
Loading

0 comments on commit 353f234

Please sign in to comment.