def test_serialize_with_empty_schedules(self): class_item = Class( name="Class", schedule_items=[], lecturer=["John", "Troy"], ) serialized_class = class_item.serialize() assert serialized_class["name"] == class_item.name assert serialized_class["schedule_items"] == [] assert serialized_class["lecturer"] == class_item.lecturer
def test_serialize_contains_required_keys(self): class_item = Class( name="Class", schedule_items=[self.generate_random_schedule_item()], lecturer=["John", "Troy"], ) serialized_class_item = class_item.serialize() self.assert_serialization_contains_keys( keys=["name", "lecturer", "schedule_items"], serialized_object=serialized_class_item, )
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
def test_serialize_with_schedules(self): class_item = Class( name="Class", schedule_items=[self.generate_random_schedule_item() for _ in range(10)], lecturer=["John", "Troy"], ) serialized_class = class_item.serialize() assert serialized_class["name"] == class_item.name assert len(serialized_class["schedule_items"]) == len(class_item.schedule_items) for i in range(len(serialized_class["schedule_items"])): assert ( serialized_class["schedule_items"][i] == class_item.schedule_items[i].serialize() ) assert serialized_class["lecturer"] == class_item.lecturer
def generate_random_class_item(cls): return Class( name=cls.get_random_string(128), schedule_items=[ ScheduleItem( day=cls.get_random_string(6), start=cls.get_random_string(6), end=cls.get_random_string(6), room=cls.get_random_string(4), ) ], lecturer=[cls.get_random_string(5)], )
def test_class_fields_validation(self): test_cases = [ { "name": "Name" * 40, "schedule_items": [self.generate_random_schedule_item()], "lecturer": ["Smith"], }, {"name": "Name" * 40, "schedule_items": 1, "lecturer": ["Smith"]}, {"name": "Name" * 40, "schedule_items": 1, "lecturer": 2}, ] for case in test_cases: with pytest.raises(ValidationError): Class( name=case["name"], schedule_items=case["schedule_items"], lecturer=case["lecturer"], ).validate()
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