def test_getklassen(self): jsonstr = get_json_resource('getklassen_mock.json') schoolyear_id = 123 def getKlassen(url, jsondata, headers): if len(methods['getKlassen'].calls) == 2: self.assertEqual(jsondata['params']['schoolyearId'], schoolyear_id) self.assertFalse(len(methods['getKlassen'].calls) > 2) return {'result': jsonstr} methods = {'getKlassen': getKlassen} with mock_results(methods): klassen = self.session.klassen() for klasse_raw, klasse in raw_vs_object(jsonstr, klassen): self.assertEqual(klasse_raw['id'], klasse.id) self.assertEqual(klasse_raw['name'], klasse.name) self.assertEqual(klasse_raw['longName'], klasse.long_name) self.assertEqual(klassen.filter(id=129)[0].id, 129) self.assertEqual( [129, 130, 137], sorted( kl.id for kl in klassen.filter(id=set([129, 130, 137])) ) ) self.assertTrue({'id': 129} in klassen) self.assertTrue({'name': '6A'} in klassen) # calling a second time for validating the schoolyear self.session.klassen(schoolyear=schoolyear_id)
def test_getrooms(self): jsonstr = get_json_resource('getrooms_mock.json') def getRooms(url, jsondata, headers): return {'result': jsonstr} methods = {'getRooms': getRooms} with mock_results(methods): for room_raw, room in raw_vs_object(jsonstr, self.session.rooms()): self.assertEqual(room_raw['longName'], room.long_name) self.assertEqual(room_raw['name'], room.name) self.assertEqual(room_raw['id'], room.id)
def test_requestcaching_no_io_on_second_time(self): jsonstr = get_json_resource('getklassen_mock.json') def getKlassen(url, jsondata, headers): return {'result': jsonstr} methods = {'getKlassen': getKlassen} with mock_results(methods): self.session.klassen(from_cache=True) self.session.klassen(from_cache=True) self.assertEqual(len(getKlassen.calls), 1)
def test_getteachers(self): jsonstr = get_json_resource('getteachers_mock.json') def getTeachers(url, jsondata, headers): return {'result': jsonstr} methods = {'getTeachers': getTeachers} with mock_results(methods): for t_raw, t in raw_vs_object(jsonstr, self.session.teachers()): self.assertEqual(t_raw['longName'], t.long_name) self.assertEqual(t_raw['longName'], t.surname) self.assertEqual(t_raw['foreName'], t.fore_name) self.assertEqual(t_raw['name'], t.name)
def test_getsubjects(self): jsonstr = get_json_resource('getsubjects_mock.json') def getSubjects(url, jsondata, headers): return {'result': jsonstr} methods = {'getSubjects': getSubjects} with mock_results(methods): for subj_raw, subj in raw_vs_object( jsonstr, self.session.subjects()): self.assertEqual(subj_raw['id'], subj.id) self.assertEqual(subj_raw['name'], subj.name) self.assertEqual(subj_raw['longName'], subj.long_name)
def test_getdepartments(self): jsonstr = get_json_resource('getdepartments_mock.json') def getDepartments(url, jsondata, headers): return {'result': jsonstr} methods = {'getDepartments': getDepartments} with mock_results(methods): for dep_raw, dep in raw_vs_object( jsonstr, self.session.departments()): self.assertEqual(dep_raw['id'], dep.id) self.assertEqual(dep_raw['longName'], dep.long_name) self.assertEqual(dep_raw['name'], dep.name)
def test_requestcaching_no_caching_without_being_told_so(self): jsonstr = get_json_resource('getklassen_mock.json') def getKlassen(url, jsondata, headers): return {'result': jsonstr} class ThisVeryCustomException(Exception): pass def blow_up(*args, **kwargs): raise ThisVeryCustomException methods = {'getKlassen': getKlassen} failing_methods = {'getKlassen': blow_up} with mock_results(methods): self.session.klassen() self.session.klassen() self.assertEqual(len(getKlassen.calls), 2) with mock_results(failing_methods): self.assertRaises(ThisVeryCustomException, self.session.klassen)
def test_no_cache_argument_in_caching_key(self): '''Any result wrapper method takes a ``cache`` arg which activates the cache if True. This test ensures that this arg doesn't show up in the cache dict key at the end.''' jsonstr = get_json_resource('getklassen_mock.json') def getKlassen(url, jsondata, headers): return {'result': jsonstr} with mock_results({'getKlassen': getKlassen}): self.session.klassen(from_cache='LELE') # should be True-ish self.session.klassen(from_cache='YESPLS') # should be too self.assertEqual(len(getKlassen.calls), 1) self.assertEqual(len(self.session.cache), 1)
def test_gettimegrid(self): jsonstr = get_json_resource('gettimegrid_mock.json') def getTimegridUnits(url, jsondata, headers): return {'result': jsonstr} methods = {'getTimegridUnits': getTimegridUnits} with mock_results(methods): for t_raw, t in zip(jsonstr, self.session.timegrid()): self.assertEqual(t_raw['day'], t.day) for t2_raw, t2 in zip(t_raw['timeUnits'], t.times): self.assertEqual(t2_raw['startTime'], int(t2[0].strftime('%H%M'))) self.assertEqual(t2_raw['endTime'], int(t2[1].strftime('%H%M')))
def test_dont_cache_the_same_thing(self): jsonstr = get_json_resource('gettimetables_mock.json') today = datetime.datetime.now() today2 = datetime.datetime.now() self.assertNotEqual(today, today2) def getTimetable(url, jsondata, headers): return {'result': jsonstr} with mock_results({'getTimetable': getTimetable}): self.session.timetable(start=today, end=today, klasse=123, from_cache=True) self.assertEqual(len(getTimetable.calls), 1) self.session.timetable(start=today2, end=today2, klasse=123, from_cache=True) self.assertEqual(len(getTimetable.calls), 1)
def test_login_repeat_invalid_session(self): retry_amount = 5 calls = [] # This produces a list of 5 * ['getCurrentSchoolyear'] with ['logout', # 'authenticate'] between each. expected_calls = ( ['getCurrentSchoolyear', 'logout', 'authenticate'] * (retry_amount + 1) )[:-2] def authenticate(url, jsondata, headers): calls.append(jsondata['method']) return { 'result': {'sessionId': 'Foobar_session_' + jsondata['id']} } def getCurrentSchoolyear(url, jsondata, headers): calls.append(jsondata['method']) return { 'error': {'code': -8520, 'message': 'Not Logged In!'} } def logout(url, jsondata, headers): calls.append(jsondata['method']) return { 'result': {'bla': 'blub'} } methods = { 'authenticate': authenticate, 'getCurrentSchoolyear': getCurrentSchoolyear, 'logout': logout } with mock_results(methods): with mock.patch.dict( self.session.config, {'login_repeat': retry_amount} ): self.assertRaises( webuntis.errors.NotLoggedInError, self.session._request, 'getCurrentSchoolyear') self.assertEqual(calls, expected_calls)
def test_login_repeat_not_logged_in(self): retry_amount = 1 calls = [] # This produces a list of 5 * ['getCurrentSchoolyear'] with ['logout', # 'authenticate'] between each. expected_calls = ['authenticate', 'getCurrentSchoolyear'] def authenticate(url, jsondata, headers): calls.append(jsondata['method']) return { 'id': jsondata['id'], 'result': {'sessionId': 'Foobar_session_' + jsondata['id']} } def _nope(url, jsondata, headers): calls.append(jsondata['method']) return { 'id': jsondata['id'], 'error': {'code': -8520, 'message': 'Not Logged In!'} } methods = { 'authenticate': authenticate, 'getCurrentSchoolyear': _nope, 'logout': _nope } with mock_results(methods): with mock.patch.dict( self.session.config, {'login_repeat': retry_amount, 'jsessionid': None} ): self.assertRaises( webuntis.errors.NotLoggedInError, self.session._request, 'getCurrentSchoolyear') self.assertEqual(calls, expected_calls)
def test_getholidays(self): jsonstr = get_json_resource('getholidays_mock.json') def getHolidays(url, jsondata, headers): return {'result': jsonstr} methods = {'getHolidays': getHolidays} with mock_results(methods): for holiday_raw, holiday in raw_vs_object( jsonstr, self.session.holidays()): self.assertEqual(holiday_raw['id'], holiday.id) self.assertEqual(holiday_raw['name'], holiday.short_name) self.assertEqual(holiday_raw['longName'], holiday.name) self.assertEqual( holiday_raw['startDate'], int(holiday.start.strftime('%Y%m%d')) ) self.assertEqual( holiday_raw['endDate'], int(holiday.end.strftime('%Y%m%d')) )
def test_getschoolyears(self): jsonstr = get_json_resource('getschoolyears_mock.json') current_json = jsonstr[3] def getSchoolyears(url, jsondata, headers): return {'result': jsonstr} def getCurrentSchoolyear(url, jsondata, headers): return {'result': current_json} methods = { 'getSchoolyears': getSchoolyears, 'getCurrentSchoolyear': getCurrentSchoolyear } with mock_results(methods): schoolyears = self.session.schoolyears() self.assertEqual(current_json['id'], schoolyears.current.id) self.assertTrue(schoolyears.current.is_current) current_count = 0 for year_raw, year in raw_vs_object(jsonstr, schoolyears): self.assertEqual(year_raw['id'], year.id) self.assertEqual(year_raw['name'], year.name) self.assertEqual( year_raw['startDate'], int(year.start.strftime('%Y%m%d')) ) self.assertEqual( year_raw['endDate'], int(year.end.strftime('%Y%m%d')) ) if year.is_current: self.assertEqual(year, schoolyears.current) current_count += 1 self.assertEqual(current_count, 1)
def test_getstatusdata(self): jsonstr = get_json_resource('getstatusdata_mock.json') def getStatusData(url, jsondata, headers): return {'result': jsonstr} methods = {'getStatusData': getStatusData} def validate_statusdata(raw, processed): name = list(raw.items())[0][0] colors = raw[name] self.assertEqual(name, processed.name) self.assertEqual(colors['foreColor'], processed.forecolor) self.assertEqual(colors['backColor'], processed.backcolor) with mock_results(methods): statusdata = self.session.statusdata() for lstype_raw, lstype in zip( jsonstr['lstypes'], statusdata.lesson_types): validate_statusdata(lstype_raw, lstype) for code_raw, code in zip( jsonstr['codes'], statusdata.period_codes): validate_statusdata(code_raw, code)
def test_gettimetables(self): jsonstr = get_json_resource('gettimetables_mock.json') jsonstr_kl = get_json_resource('getklassen_mock.json') jsonstr_te = get_json_resource('getteachers_mock.json') jsonstr_su = get_json_resource('getsubjects_mock.json') jsonstr_ro = get_json_resource('getrooms_mock.json') def getTimetable(url, jsondata, headers): return {'result': jsonstr} def getKlassen(url, jsondata, headers): return {'result': jsonstr_kl} def getTeachers(url, jsondata, headers): return {'result': jsonstr_te} def getSubjects(url, jsondata, headers): return {'result': jsonstr_su} def getRooms(url, jsondata, headers): return {'result': jsonstr_ro} methods = { 'getTimetable': getTimetable, 'getKlassen': getKlassen, 'getTeachers': getTeachers, 'getSubjects': getSubjects, 'getRooms': getRooms } with mock_results(methods): tt = self.session.timetable(klasse=114, start='20120301', end='20120301') for period_raw, period in raw_vs_object(jsonstr, tt): self.assertEqual( int(period.start.strftime('%H%M')), period_raw['startTime'] ) self.assertEqual( int(period.start.strftime('%Y%m%d')), period_raw['date'] ) self.assertEqual( int(period.end.strftime('%H%M')), period_raw['endTime'] ) self.assertEqual( int(period.end.strftime('%H%M')), period_raw['endTime'] ) if 'code' in period_raw: self.assertEqual(period_raw['code'], period.code) else: self.assertEqual(period.code, None) if 'lstype' in period_raw: self.assertEqual(period_raw['lstype'], period.type) else: self.assertEqual(period.type, 'ls') self.assertEqual(len(period_raw['kl']), len(period.klassen)) for klasse_raw, klasse in raw_vs_object( period_raw['kl'], period.klassen): self.assertEqual(klasse.id, klasse_raw['id']) self.assertEqual(len(period_raw['te']), len(period.teachers)) for teacher_raw, teacher in raw_vs_object( period_raw['te'], period.teachers): self.assertEqual(teacher.id, teacher_raw['id']) self.assertEqual(len(period_raw['su']), len(period.subjects)) for subject_raw, subject in raw_vs_object( period_raw['su'], period.subjects): self.assertEqual(subject.id, subject_raw['id']) self.assertEqual(len(period_raw['ro']), len(period.rooms)) for room_raw, room in raw_vs_object( period_raw['ro'], period.rooms): self.assertEqual(room.id, room_raw['id']) def validate_table(periods, width=None): counter = 0 table = periods.to_table(width=width) for time, row in table: if width is not None: row = list(row) self.assertEqual(len(row), width) for date, cell in row: for hour in cell: counter += 1 self.assertEqual(counter, len(periods)) validate_table(tt) validate_table(tt, width=999) self.assertRaises(ValueError, validate_table, tt, width=0) self.assertEqual(len(webuntis.utils.timetable_utils.table([])), 0)