forked from openedx/cc2olx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Tests are updated for refactored code
- Loading branch information
1 parent
01f21c5
commit de68907
Showing
14 changed files
with
310 additions
and
202 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 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
Empty file.
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,128 @@ | ||
from pathlib import Path | ||
from unittest.mock import MagicMock, Mock, patch | ||
|
||
import pytest | ||
|
||
from cc2olx.content_parsers import HtmlContentParser | ||
|
||
|
||
class TestHtmlContentParser: | ||
def test_parse_content_returns_default_content_if_there_is_no_resource_identifier(self): | ||
parser = HtmlContentParser(Mock()) | ||
expected_content = {"html": "<p>MISSING CONTENT</p>"} | ||
|
||
actual_content = parser._parse_content(None) | ||
|
||
assert actual_content == expected_content | ||
|
||
def test_parse_content_returns_default_content_if_the_resource_is_missed_in_cartridge(self): | ||
cartridge_mock = Mock(define_resource=Mock(return_value=None)) | ||
parser = HtmlContentParser(cartridge_mock) | ||
expected_content = {"html": "<p>MISSING CONTENT</p>"} | ||
|
||
actual_content = parser._parse_content(Mock()) | ||
|
||
assert actual_content == expected_content | ||
|
||
@patch("cc2olx.content_parsers.html.logger") | ||
def test_parse_content_logs_missing_resource(self, logger_mock): | ||
cartridge_mock = Mock(define_resource=Mock(return_value=None)) | ||
parser = HtmlContentParser(cartridge_mock) | ||
idref_mock = Mock() | ||
|
||
parser._parse_content(idref_mock) | ||
|
||
logger_mock.info.assert_called_once_with("Missing resource: %s", idref_mock) | ||
|
||
@pytest.mark.parametrize( | ||
"resource_type", | ||
[ | ||
"imsbasiclti_xmlv1p2", | ||
"imsbasiclti_xmlv1p3", | ||
"imsqti_xmlv1p3/imscc_xmlv1p1/assessment", | ||
"imsqti_xmlv1p3/imscc_xmlv1p3/assessment", | ||
"imsdt_xmlv1p2", | ||
"imsdt_xmlv1p3", | ||
], | ||
) | ||
@patch("cc2olx.content_parsers.html.HtmlContentParser._parse_web_link_content", Mock(return_value=None)) | ||
def test_parse_content_returns_default_content_for_some_other_cc_resource_types(self, resource_type): | ||
cartridge_mock = Mock(define_resource=Mock(return_value={"type": resource_type})) | ||
parser = HtmlContentParser(cartridge_mock) | ||
expected_content = {"html": "<p>MISSING CONTENT</p>"} | ||
|
||
actual_content = parser._parse_content(Mock()) | ||
|
||
assert actual_content == expected_content | ||
|
||
@pytest.mark.parametrize( | ||
"resource_type", | ||
[ | ||
"unsupported_resource_type", | ||
"chess_game_xmlv1p1", | ||
"drag_and_drop_xmlv1p1", | ||
"imsab_xmlv1p2" | ||
], | ||
) | ||
@patch("cc2olx.content_parsers.html.HtmlContentParser._parse_web_link_content", Mock(return_value=None)) | ||
@patch("cc2olx.content_parsers.html.HtmlContentParser._parse_not_imported_content") | ||
def test_parse_content_parses_not_imported_content(self, parse_not_imported_content_mock, resource_type): | ||
cartridge_mock = Mock(define_resource=Mock(return_value={"type": "imsqti_xmlv1p2"})) | ||
parser = HtmlContentParser(cartridge_mock) | ||
|
||
actual_content = parser._parse_content(Mock()) | ||
|
||
assert actual_content == parse_not_imported_content_mock.return_value | ||
|
||
@patch("cc2olx.content_parsers.html.imghdr.what", Mock(return_value=None)) | ||
def test_parse_webcontent_returns_default_content_for_unknown_webcontent_type_from_web_resources_dir(self): | ||
parser = HtmlContentParser( | ||
Mock(build_res_file_path=Mock(return_value=Path("web_resources/unknown/path/to/file.ext"))) | ||
) | ||
expected_content = {"html": "<p>MISSING CONTENT</p>"} | ||
|
||
actual_content = parser._parse_webcontent(Mock(), MagicMock()) | ||
|
||
assert actual_content == expected_content | ||
|
||
@patch("cc2olx.content_parsers.html.logger") | ||
@patch("cc2olx.content_parsers.html.imghdr.what", Mock(return_value=None)) | ||
def test_parse_webcontent_logs_skipping_webcontent(self, logger_mock): | ||
res_file_path = Path("web_resources/unknown/path/to/file.ext") | ||
parser = HtmlContentParser(Mock(build_res_file_path=Mock(return_value=res_file_path))) | ||
|
||
parser._parse_webcontent(Mock(), MagicMock()) | ||
|
||
logger_mock.info.assert_called_once_with("Skipping webcontent: %s", res_file_path) | ||
|
||
@patch("cc2olx.content_parsers.html.logger") | ||
@patch("cc2olx.content_parsers.html.open", Mock(side_effect=FileNotFoundError)) | ||
def test_webcontent_html_file_reading_failure_is_logged(self, logger_mock): | ||
parser = HtmlContentParser(Mock()) | ||
idref_mock = Mock() | ||
res_file_path_mock = Mock() | ||
|
||
with pytest.raises(FileNotFoundError): | ||
parser._parse_webcontent_html_file(idref_mock, res_file_path_mock) | ||
|
||
logger_mock.error.assert_called_once_with("Failure reading %s from id %s", res_file_path_mock, idref_mock) | ||
|
||
@pytest.mark.parametrize( | ||
"resource,message", | ||
[ | ||
( | ||
{"type": "some_type_mock", "href": "https://example.com/some/type/link/"}, | ||
"Not imported content: type = 'some_type_mock', href = 'https://example.com/some/type/link/'", | ||
), | ||
({"type": "some_type_mock"}, "Not imported content: type = 'some_type_mock'"), | ||
], | ||
) | ||
@patch("cc2olx.content_parsers.html.logger") | ||
def test_not_imported_content_parsing_with_href_in_resource(self, logger_mock, resource, message): | ||
parser = HtmlContentParser(Mock()) | ||
expected_content = {"html": message} | ||
|
||
actual_content = parser._parse_not_imported_content(resource) | ||
|
||
logger_mock.info.assert_called_once_with("%s", message) | ||
assert actual_content == expected_content |
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,44 @@ | ||
from unittest.mock import MagicMock, Mock, PropertyMock, call, patch | ||
|
||
import pytest | ||
|
||
from cc2olx.content_parsers import QtiContentParser | ||
from cc2olx.exceptions import QtiError | ||
|
||
|
||
class TestQtiContentParser: | ||
@pytest.mark.parametrize("cc_profile", ["unknown_profile", "cc.chess.v0p1", "cc.drag_and_drop.v0p1", "123"]) | ||
def test_parse_problem_raises_qti_error_if_cc_profile_is_unknown(self, cc_profile): | ||
parser = QtiContentParser(Mock()) | ||
|
||
with patch("cc2olx.content_parsers.qti.QtiContentParser._parse_problem_profile", return_value=cc_profile): | ||
with pytest.raises(QtiError) as exc_info: | ||
parser._parse_problem(MagicMock(), Mock(), Mock()) | ||
|
||
assert str(exc_info.value) == f'Unknown cc_profile: "{cc_profile}"' | ||
|
||
@patch("cc2olx.content_parsers.qti.logger") | ||
@patch("cc2olx.content_parsers.qti.QtiContentParser._parse_problem_profile") | ||
def test_parse_problem_logs_inability_to_process_problem(self, cc_profile_mock, logger_mock): | ||
parser = QtiContentParser(Mock()) | ||
ident_mock = MagicMock() | ||
res_file_path_mock = Mock() | ||
problem_mock = Mock(attrib={"ident": ident_mock}) | ||
expected_logger_info_call_args_list = [ | ||
call("Problem with ID %s can't be converted.", ident_mock), | ||
call(" Profile %s is not supported.", cc_profile_mock.return_value), | ||
call(" At file %s.", res_file_path_mock), | ||
] | ||
|
||
with patch( | ||
"cc2olx.content_parsers.qti.QtiContentParser._problem_parsers_map", | ||
new_callable=PropertyMock, | ||
) as problem_parsers_map_mock: | ||
problem_parsers_map_mock.return_value = { | ||
cc_profile_mock.return_value: Mock(side_effect=NotImplementedError) | ||
} | ||
|
||
parser._parse_problem(problem_mock, Mock(), res_file_path_mock) | ||
|
||
assert logger_mock.info.call_count == 3 | ||
assert logger_mock.info.call_args_list == expected_logger_info_call_args_list |
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,24 @@ | ||
from unittest.mock import Mock, patch | ||
|
||
from cc2olx.content_parsers import VideoContentParser | ||
|
||
|
||
class TestVideoContentParser: | ||
def test_parse_content_returns_none_if_there_is_no_resource_identifier(self): | ||
parser = VideoContentParser(Mock()) | ||
|
||
actual_content = parser._parse_content(None) | ||
|
||
assert actual_content is None | ||
|
||
@patch( | ||
"cc2olx.content_parsers.video.VideoContentParser._parse_web_link_content", | ||
Mock(return_value={"href": "youtube.com/watch?v=ABCDeF12345"}), | ||
) | ||
def test_parse_content_parses_youtube_link(self): | ||
parser = VideoContentParser(Mock()) | ||
expected_content = {"youtube": "ABCDeF12345"} | ||
|
||
actual_content = parser._parse_content(Mock()) | ||
|
||
assert actual_content == expected_content |
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
Oops, something went wrong.