示例#1
0
    def test_serialization_with_empty_classes(self):
        course = Course(
            name="Course",
            credit=4,
            term=1,
            classes=[],
        )

        serialized_course = course.serialize()

        assert serialized_course["name"] == course.name
        assert serialized_course["credit"] == course.credit
        assert serialized_course["term"] == course.term
        assert serialized_course["classes"] == []
示例#2
0
    def test_serialization_contains_required_keys(self):
        course = Course(
            name="Course",
            credit=4,
            term=1,
            classes=[self.generate_random_class_item()],
        )

        serialized_course = course.serialize()

        self.assert_serialization_contains_keys(
            keys=["name", "credit", "term", "classes"],
            serialized_object=serialized_course,
        )
示例#3
0
    def test_serialization_with_classes(self):
        course = Course(
            name="Course",
            credit=4,
            term=1,
            classes=[self.generate_random_class_item() for _ in range(5)],
        )

        serialized_course = course.serialize()

        assert serialized_course["name"] == course.name
        assert serialized_course["credit"] == course.credit
        assert serialized_course["term"] == course.term
        for i in range(len(serialized_course["classes"])):
            assert serialized_course["classes"][i] == course["classes"][i].serialize()
示例#4
0
 def generate_random_course_item(cls):
     return Course(
         name=cls.get_random_string(128),
         credit=random.randint(0, 10),
         term=random.randint(0, 10),
         classes=[cls.generate_random_class_item() for _ in range(5)],
     )
示例#5
0
    def test_course_fields_validation(self):
        test_cases = [
            {
                "name": "Name" * 40,
                "credit": 3,
                "term": 2,
                "classes": [self.generate_random_class_item()],
            },
            {
                "name": "Name" * 40,
                "credit": "3",
                "term": 2,
                "classes": [self.generate_random_class_item()],
            },
            {
                "name": "Name" * 40,
                "credit": "3",
                "term": "2",
                "classes": [self.generate_random_class_item()],
            },
            {"name": "Name" * 40, "credit": "3", "term": "2", "classes": 1},
        ]

        for case in test_cases:
            with pytest.raises(ValidationError):
                Course(
                    name=case["name"],
                    credit=case["credit"],
                    term=case["term"],
                    classes=case["classes"],
                ).validate()
示例#6
0
    def create_period(self, major_id):
        """Create dummy period with its course and class"""

        class_obj = Class(
            name='Anum - A',
            schedule_items=[
                ScheduleItem(
                    day='Senin',
                    start='09.40',
                    end='08.00',
                    room='A6.09 (Ged Baru)',
                ),
            ],
            lecturer=['Nama Dosen 1', 'Nama Dosen 2'],
        )

        course = Course(
            name=self.COURSE['name'],
            credit=self.COURSE['credit'],
            term=self.COURSE['term'],
            classes=[class_obj],
        )

        period = Period.objects().create(
            major_id=major_id,
            name=app.config["ACTIVE_PERIOD"],
            is_detail=True,
            courses=[course],
        )

        return period
示例#7
0
def create_courses(html, is_detail=False):
    soup = BeautifulSoup(html, 'html.parser')
    if is_detail:
        classes = soup.find_all('th', class_='sub border2 pad2')
    else:
        classes = soup.find_all('th', class_='sub border2 pad1')

    courses = []
    for class_ in classes:
        course_name = class_.strong.text
        m = re.search('([0-9]+) SKS, Term ([0-9]+)', class_.text)
        if m:
            credit, term = m.group().split(' SKS, Term ')

        classes = []
        for sib in class_.parent.find_next_siblings('tr'):
            if (sib.get('class') == None):
                break

            class_name = sib.a.text
            try:
                schedules = str(sib.contents[9]).split('<br/>')
                schedules[0] = schedules[0].replace('<td nowrap="">', '')
                schedules[-1] = schedules[-1].replace('</td>', '')

                rooms = str(sib.contents[11]).split('<br/>')
                if is_detail:
                    rooms[0] = rooms[0].replace('<td>', '')
                else:
                    rooms[0] = rooms[0].replace('<td class="ce">', '')
                rooms[-1] = rooms[-1].replace('</td>', '')

                lecturers = str(sib.contents[13]).split('<br/>')
                lecturers[0] = lecturers[0].replace('<td>', '')
                lecturers[-1] = lecturers[-1].replace('</td>', '')
                lecturers = [l.lstrip('-') for l in lecturers]

                result = []
                schedules = zip(schedules, rooms)
                for schedule, room in schedules:
                    day, time = schedule.split(', ')
                    start, end = time.split('-')
                    result.append(
                        ScheduleItem(day=day, start=start, end=end, room=room))

                classes.append(
                    Class(name=class_name,
                          schedule_items=result,
                          lecturer=lecturers))

            except (IndexError, ValueError) as e:
                pass

        if classes:
            courses.append(
                Course(name=course_name,
                       credit=credit,
                       term=term,
                       classes=classes))

    return courses