def test_happy_path(self): dirpath = MockDir( {"module.json": b"", "module.py": b"", "module.html": b"", "module.js": b""} ) module_files = ModuleFiles.load_from_dirpath(dirpath) self.assertEqual(module_files.spec.name, "module.json") self.assertEqual(module_files.code.name, "module.py") self.assertEqual(module_files.html.name, "module.html") self.assertEqual(module_files.javascript.name, "module.js")
def test_lesson_missing_title(self): dirpath = MockDir({ "index.yaml": textwrap.dedent("""\ introduction_html: <p>hi</p> lessons: [] """).encode("utf-8") }) with self.assertRaisesRegex(KeyError, "title"): Course.load_from_path(dirpath / "index.yaml")
def test_validate_max_1_html(self): dirpath = MockDir( { "module.json": b"", "module.py": b"", "module.html": b"", "extra.html": b"", } ) with self.assertRaisesRegex(ValueError, "Multiple.*html.*files"): ModuleFiles.load_from_dirpath(dirpath)
def test_ignore_package_json(self): dirpath = MockDir( { "module.json": b"", "module.py": b"", "package.json": b"", "package-lock.json": b"", } ) module_files = ModuleFiles.load_from_dirpath(dirpath) self.assertEqual(module_files.spec.name, "module.json")
def test_lesson_file_not_found(self): dirpath = MockDir({ "index.yaml": textwrap.dedent("""\ title: Title introduction_html: <p>hi</p> lessons: - lesson-x """).encode("utf-8") }) with self.assertRaisesRegex(FileNotFoundError, "lesson-x.html"): Course.load_from_path(dirpath / "index.yaml")
def test_validate_detect_python_syntax_errors(self): test_dir = MockDir({ "badpy.json": json.dumps( dict( name="Syntax-error Python", id_name="badpy", category="Clean", parameters=[], )).encode("utf-8"), "badpy.py": b'def render(table, params):\n cols = split(","', }) with self.assertRaises(ModuleCompileError): import_module_from_directory("123456", test_dir)
def test_happy_path(self): dirpath = MockDir({ "en/a-course/index.yaml": textwrap.dedent("""\ title: Title introduction_html: |- <p>Hi</p> <p>Bye</p> lessons: - lesson-1 - lesson-2 """).encode("utf-8"), "en/a-course/lesson-1.html": (b"<header><h1>L1</h1><p>HP1</p></header>" b"<footer><h2>F1</h2><p>foot</p></footer>"), "en/a-course/lesson-2.html": (b"<header><h1>L2</h1><p>HP2</p></header>" b"<footer><h2>F2</h2><p>foot</p></footer>"), }) course = Course.load_from_path(dirpath / "en" / "a-course" / "index.yaml") self.assertEqual( course, Course( slug="a-course", title="Title", locale_id="en", introduction_html="<p>Hi</p>\n<p>Bye</p>", lessons={ "lesson-1": Lesson( course, "lesson-1", "en", header=LessonHeader("L1", "<p>HP1</p>"), footer=LessonFooter("F1", "<p>foot</p>"), ), "lesson-2": Lesson( course, "lesson-2", "en", header=LessonHeader("L2", "<p>HP2</p>"), footer=LessonFooter("F2", "<p>foot</p>"), ), }, ), )
def test_ignore_test_py(self): dirpath = MockDir({"module.json": b"", "module.py": b"", "test_module.py": b""}) module_files = ModuleFiles.load_from_dirpath(dirpath) self.assertEqual(module_files.code.name, "module.py")
def test_validate_missing_py(self): dirpath = MockDir({"module.json": b""}) with self.assertRaisesRegex(ValueError, 'Missing ".py" module-code file'): ModuleFiles.load_from_dirpath(dirpath)
def test_validate_missing_json(self): dirpath = MockDir({"module.py": b""}) with self.assertRaisesRegex( ValueError, 'Missing ".json" or ".yaml" module-spec file' ): ModuleFiles.load_from_dirpath(dirpath)
def test_validate_extra_py(self): dirpath = MockDir({"module.json": b"{}", "module.py": b"", "module2.py": b""}) with self.assertRaisesRegex(ValueError, "Multiple.*py.*files"): ModuleFiles.load_from_dirpath(dirpath)
def test_lesson_missing_index(self): dirpath = MockDir({}) with self.assertRaisesRegex(FileNotFoundError, "index.yaml"): Course.load_from_path(dirpath / "index.yaml")
def test_lesson_invalid_yaml(self): dirpath = MockDir({"index.yaml": b"{"}) with self.assertRaises(yaml.YAMLError): Course.load_from_path(dirpath / "index.yaml")