def test_lessons_loader_translation_missing_programming_challenges(self):
        config_file = "translation-missing-pcd.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        lesson = Lesson.objects.get(slug="lesson-complex-translation")

        self.assertEqual(lesson.name, "Complex Translated Lesson English")
        self.assertIn(
            "English description of lesson programming challenges.",
            lesson.programming_challenges_description
        )

        # 'de' should still be an available language as programming challenge description is optional
        self.assertIn("de", lesson.languages)
        with translation.override("de"):
            self.assertEqual(lesson.name, "Complex Translated Lesson German")
            # accessing the untranslated field should not default back to english
            self.assertEqual("", lesson.programming_challenges_description)
    def test_lessons_loader_translation_missing_ct_links(self):
        config_file = "translation-missing-ct-links.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        lesson = Lesson.objects.get(slug="lesson-complex-translation")

        self.assertEqual(lesson.name, "Complex Translated Lesson English")
        self.assertIn(
            "English text for Computational Thinking links.",
            lesson.computational_thinking_links
        )

        # 'de' should still be an available language as ct_links is optional
        self.assertIn("de", lesson.languages)
        with translation.override("de"):
            self.assertEqual(lesson.name, "Complex Translated Lesson German")
            # accessing the untranslated field should not default back to english
            self.assertEqual("", lesson.computational_thinking_links)
    def test_lesson_loader_optional_programming_challenges_set_correctly(self):
        config_file = os.path.join(self.loader_name, "programming-challenges.yaml")
        lessons_structure = os.path.join(self.test_data.LOADER_ASSET_PATH, config_file)

        topic = self.test_data.create_topic(1)
        unit_plan = self.test_data.create_unit_plan(topic, 1)
        difficulty = self.test_data.create_difficulty_level(1)
        self.test_data.create_programming_challenge(topic, 1, difficulty)
        self.test_data.create_programming_challenge(topic, 2, difficulty)

        lesson_loader = LessonsLoader(
            lessons_structure,
            topic,
            unit_plan,
            self.test_data.LOADER_ASSET_PATH
        )
        lesson_loader.load()
        self.assertQuerysetEqual(
            Lesson.objects.get(slug="lesson-1").programming_challenges.all(),
            [
                "<ProgrammingChallenge: Challenge 1.1: 1>",
                "<ProgrammingChallenge: Challenge 1.1: 2>",
            ],
            ordered=False,
        )
    def test_lesson_loader_optional_generated_resources_set_correctly(self):
        config_file = "generated-resources.yaml"

        topic = self.test_data.create_topic(1)
        unit_plan = self.test_data.create_unit_plan(topic, 1)
        self.resource_test_data.create_resource(
            "grid",
            "Grid",
            "Grid description",
            "GridResourceGenerator",
        )
        self.resource_test_data.create_resource(
            "arrows",
            "Arrows",
            "Arrows description",
            "ArrowsResourceGenerator",
        )
        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        self.assertQuerysetEqual(
            Lesson.objects.get(slug="lesson-1").generated_resources.order_by("name"),
            [
                "<Resource: Arrows>",
                "<Resource: Grid>",
            ],
        )
 def test_lesson_loader_optional_programming_challenges_empty(self):
     config_file = "programming-challenges-empty.yaml"
     topic = self.test_data.create_topic(1)
     unit_plan = self.test_data.create_unit_plan(topic, 1)
     lesson_loader = LessonsLoader(
         topic,
         unit_plan,
         structure_filename=config_file,
         base_path=self.base_path
     )
     lesson_loader.load()
 def test_lesson_loader_optional_generated_resources_empty(self):
     config_file = "generated-resources-empty.yaml"
     topic = self.test_data.create_topic(1)
     unit_plan = self.test_data.create_unit_plan(topic, 1)
     lesson_loader = LessonsLoader(
         topic,
         unit_plan,
         structure_filename=config_file,
         base_path=self.base_path
     )
     lesson_loader.load()
     lesson = Lesson.objects.get(slug="lesson-1")
     self.assertFalse(lesson.generated_resources.exists())
    def test_lesson_loader_missing_programming_challenges_description(self):
        config_file = "basic-config.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        self.assertEqual("", Lesson.objects.get(slug="lesson-1").programming_challenges_description)
    def test_lesson_loader_missing_computational_thinking_content(self):
        config_file = "basic-config.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        self.assertEqual("", Lesson.objects.get(slug="lesson-1").computational_thinking_links)
    def test_lesson_loader_no_heading_tree(self):
        config_file = os.path.join(self.loader_name, "basic-config.yaml")
        lessons_structure = os.path.join(self.test_data.LOADER_ASSET_PATH, config_file)

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            lessons_structure,
            topic,
            unit_plan,
            self.test_data.LOADER_ASSET_PATH
        )
        lesson_loader.load()
        self.assertIsNone(Lesson.objects.get(slug="lesson-1").heading_tree)
    def test_lesson_loader_missing_programming_challenges_description(self):
        config_file = os.path.join(self.loader_name, "basic-config.yaml")
        lessons_structure = os.path.join(self.test_data.LOADER_ASSET_PATH, config_file)

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            lessons_structure,
            topic,
            unit_plan,
            self.test_data.LOADER_ASSET_PATH
        )
        lesson_loader.load()
        self.assertIsNone(Lesson.objects.get(slug="lesson-1").programming_challenges_description)
    def test_lesson_loader_no_heading_tree(self):
        config_file = "basic-config.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        # Empty list will evaluate to false
        self.assertFalse(Lesson.objects.get(slug="lesson-1").heading_tree)
    def test_lesson_loader_valid_computational_thinking_content(self):
        config_file = "ct-links.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        self.assertEquals(
            Lesson.objects.get(slug="ct-links").computational_thinking_links,
            "<p>Example text for Computational Thinking links.</p>",
        )
 def test_lesson_loader_optional_generated_resources_description_missing(self):
     config_file = "generated-resources-description-missing.yaml"
     topic = self.test_data.create_topic(1)
     unit_plan = self.test_data.create_unit_plan(topic, 1)
     self.resource_test_data.create_resource(
         "grid",
         "Grid",
         "Grid description",
         "GridResourceGenerator",
     )
     lesson_loader = LessonsLoader(
         topic,
         unit_plan,
         structure_filename=config_file,
         base_path=self.base_path
     )
     lesson_loader.load()
    def test_lesson_loader_optional_classroom_resources_set_correctly_when_omitted(self):
        config_file = "basic-config.yaml"

        topic = self.test_data.create_topic(1)
        unit_plan = self.test_data.create_unit_plan(topic, 1)

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        self.assertQuerysetEqual(
            Lesson.objects.get(slug="lesson-1").classroom_resources.all(),
            [],
        )
    def test_lesson_loader_valid_programming_challenges_description(self):
        config_file = "programming-challenges-description.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        self.assertEquals(
            Lesson.objects.get(slug="programming-challenges-description").programming_challenges_description,
            "<p>Description of lesson programming challenges.</p>",
        )
    def test_lessons_loader_translation_missing_lesson_file(self):
        config_file = "basic-config.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        lesson = Lesson.objects.get(slug="lesson-1")

        # 'de' should not be stored as available language
        self.assertSetEqual(set(["en"]), set(lesson.languages))
    def test_lesson_loader_content_set_correctly(self):
        config_file = "basic-config.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        self.assertEquals(
            Lesson.objects.get(slug="lesson-1").content,
            "<p>Etiam in massa. Nam ut metus. In rhoncus venenatis tellus.</p>",
        )
    def test_lesson_loader_slug_set_correctly(self):
        config_file = "basic-config.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        self.assertEquals(
            Lesson.objects.get(slug="lesson-1").slug,
            "lesson-1",
        )
    def test_lesson_loader_valid_computational_thinking_content(self):
        config_file = os.path.join(self.loader_name, "ct-links.yaml")
        lessons_structure = os.path.join(self.test_data.LOADER_ASSET_PATH, config_file)

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            lessons_structure,
            topic,
            unit_plan,
            self.test_data.LOADER_ASSET_PATH
        )
        lesson_loader.load()
        self.assertEquals(
            Lesson.objects.get(slug="ct-links").computational_thinking_links,
            "<p>Example text for Computational Thinking links.</p>",
        )
    def test_lesson_loader_content_set_correctly(self):
        config_file = os.path.join(self.loader_name, "basic-config.yaml")
        lessons_structure = os.path.join(self.test_data.LOADER_ASSET_PATH, config_file)

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            lessons_structure,
            topic,
            unit_plan,
            self.test_data.LOADER_ASSET_PATH
        )
        lesson_loader.load()
        self.assertEquals(
            Lesson.objects.get(slug="lesson-1").content,
            "<p>Etiam in massa. Nam ut metus. In rhoncus venenatis tellus.</p>",
        )
    def test_basic_lesson_loader_configuration(self):
        config_file = "basic-config.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()

        self.assertQuerysetEqual(
            Lesson.objects.all(),
            ["<Lesson: Lesson 1>"]
        )
 def test_lessons_loader_translation_basic(self):
     config_file = "basic-translation.yaml"
     topic = self.test_data.create_topic("1")
     unit_plan = self.test_data.create_unit_plan(topic, "1")
     lesson_loader = LessonsLoader(
         topic,
         unit_plan,
         structure_filename=config_file,
         base_path=self.base_path
     )
     lesson_loader.load()
     lesson = Lesson.objects.get(slug="lesson-basic-translation")
     self.assertSetEqual(set(["en", "de"]), set(lesson.languages))
     self.assertEqual(lesson.name, "Basic Translated Lesson English")
     self.assertIn("English lesson content", lesson.content)
     with translation.override("de"):
         self.assertEqual(lesson.name, "Basic Translated Lesson German")
         self.assertIn("German lesson content", lesson.content)
    def test_lesson_loader_slug_set_correctly(self):
        config_file = os.path.join(self.loader_name, "basic-config.yaml")
        lessons_structure = os.path.join(self.test_data.LOADER_ASSET_PATH, config_file)

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            lessons_structure,
            topic,
            unit_plan,
            self.test_data.LOADER_ASSET_PATH
        )
        lesson_loader.load()
        self.assertEquals(
            Lesson.objects.get(slug="lesson-1").slug,
            "lesson-1",
        )
    def test_lesson_loader_optional_classroom_resources_set_correctly_when_omitted(self):
        config_file = os.path.join(self.loader_name, "basic-config.yaml")
        lessons_structure = os.path.join(self.test_data.LOADER_ASSET_PATH, config_file)

        topic = self.test_data.create_topic(1)
        unit_plan = self.test_data.create_unit_plan(topic, 1)

        lesson_loader = LessonsLoader(
            lessons_structure,
            topic,
            unit_plan,
            self.test_data.LOADER_ASSET_PATH
        )
        lesson_loader.load()
        self.assertEqual(
            Lesson.objects.get(slug="lesson-1").classroom_resources,
            None,
        )
    def test_lesson_loader_valid_programming_challenges_description(self):
        config_file = os.path.join(self.loader_name, "programming-challenges-description.yaml")
        lessons_structure = os.path.join(self.test_data.LOADER_ASSET_PATH, config_file)

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            lessons_structure,
            topic,
            unit_plan,
            self.test_data.LOADER_ASSET_PATH
        )
        lesson_loader.load()
        self.assertEquals(
            Lesson.objects.get(slug="programming-challenges-description").programming_challenges_description,
            "<p>Description of lesson programming challenges.</p>",
        )
    def test_basic_lesson_loader_configuration(self):
        config_file = os.path.join(self.loader_name, "basic-config.yaml")
        lessons_structure = os.path.join(self.test_data.LOADER_ASSET_PATH, config_file)

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            lessons_structure,
            topic,
            unit_plan,
            self.test_data.LOADER_ASSET_PATH
        )
        lesson_loader.load()

        self.assertQuerysetEqual(
            Lesson.objects.all(),
            ["<Lesson: Lesson 1>"]
        )
    def test_lessons_loader_translation_complex(self):
        config_file = "complex-translation.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()

        lesson = Lesson.objects.get(slug="lesson-complex-translation")

        self.assertSetEqual(set(["en", "de"]), set(lesson.languages))

        self.assertEqual(lesson.name, "Complex Translated Lesson English")
        self.assertIn("English lesson content", lesson.content)
        self.assertIn("Another English header", str(lesson.heading_tree))
        self.assertIn(
            "English text for Computational Thinking links.",
            lesson.computational_thinking_links
        )
        self.assertIn(
            "English description of lesson programming challenges.",
            lesson.programming_challenges_description
        )

        with translation.override("de"):
            self.assertEqual(lesson.name, "Complex Translated Lesson German")
            self.assertIn("German lesson content", lesson.content)
            self.assertIn("Another German header", str(lesson.heading_tree))
            self.assertIn(
                "German text for Computational Thinking links.",
                lesson.computational_thinking_links
            )
            self.assertIn(
                "German description of lesson programming challenges.",
                lesson.programming_challenges_description
            )
    def test_lesson_loader_optional_learning_outcomes_set_correctly_when_omitted(self):
        config_file = os.path.join(self.loader_name, "basic-config.yaml")
        lessons_structure = os.path.join(self.test_data.LOADER_ASSET_PATH, config_file)

        topic = self.test_data.create_topic(1)
        unit_plan = self.test_data.create_unit_plan(topic, 1)
        self.test_data.create_learning_outcome(1)
        self.test_data.create_learning_outcome(2)

        lesson_loader = LessonsLoader(
            lessons_structure,
            topic,
            unit_plan,
            self.test_data.LOADER_ASSET_PATH
        )
        lesson_loader.load()
        self.assertQuerysetEqual(
            Lesson.objects.get(slug="lesson-1").learning_outcomes.all(),
            [],
        )
    def test_lesson_loader_optional_programming_challenges_set_correctly_when_omitted(self):
        config_file = "basic-config.yaml"

        topic = self.test_data.create_topic(1)
        unit_plan = self.test_data.create_unit_plan(topic, 1)
        difficulty = self.test_data.create_difficulty_level(1)
        self.test_data.create_programming_challenge(topic, 1, difficulty)
        self.test_data.create_programming_challenge(topic, 2, difficulty)

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        self.assertQuerysetEqual(
            Lesson.objects.get(slug="lesson-1").programming_challenges.all(),
            [],
        )
    def test_lesson_loader_multiple_lessons(self):
        config_file = "multiple-lessons.yaml"

        topic = self.test_data.create_topic("1")
        unit_plan = self.test_data.create_unit_plan(topic, "1")

        lesson_loader = LessonsLoader(
            topic,
            unit_plan,
            structure_filename=config_file,
            base_path=self.base_path
        )
        lesson_loader.load()
        self.assertQuerysetEqual(
            Lesson.objects.all(),
            [
                "<Lesson: Lesson 1>",
                "<Lesson: Lesson 2>",
                "<Lesson: Lesson 3>",
            ],
            ordered=False,
        )