def test_set_duration(self): '''Simple coverage test to check the set_duration works under the following scenarios: - end_time > event['time'] and (end_time - event['time']) < MAX_DURATION_MINUTES - end_time > event['time'] and (end_time - event['time']) = MAX_DURATION_MINTUES - end_time > event['time'] and (end_time - event['time']) > MAX_DURATION_MINUTES - end_time = event['time'] ''' raw_event = {'_id': 'a', 'time': '2013-08-13T19:47:47.451372'} genformatting.TIMESTAMP_FORMAT = [ '%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S' ] genformatting.parse_timestamp(raw_event) start_time = raw_event['time'] event = events.Event(raw_event) end_time = start_time + datetime.timedelta( minutes=(max(0, events.MAX_DURATION_MINUTES - 5))) duration = (end_time - raw_event['time']).seconds / 60 event.set_duration(end_time) self.assertEqual(duration, event.duration) end_time = start_time + datetime.timedelta( minutes=events.MAX_DURATION_MINUTES) event.set_duration(end_time) self.assertEqual(events.MAX_DURATION_MINUTES, event.duration) end_time = start_time + datetime.timedelta( minutes=events.MAX_DURATION_MINUTES + 5) event.set_duration(end_time) self.assertEqual(events.DEFAULT_DURATION_MINUTES, event.duration) event.set_duration(start_time) self.assertEqual(0, event.duration)
def test_stage_event(self): '''Simple coverage test to check that event staging is correct. In particular, check that the duration calculation is as expected. ''' manager = eventmanager.EventManager() raw_events = [{ '_id': '1', 'anon_screen_name': 'A', 'time': '2013-11-10 06:00:00' }, { '_id': '2', 'anon_screen_name': 'B', 'time': '2013-11-10 06:00:00' }, { '_id': '3', 'anon_screen_name': 'A', 'time': '2013-11-10 06:05:00' }, { '_id': '4', 'anon_screen_name': 'B', 'time': '2013-11-10 06:10:00' }, { '_id': '5', 'anon_screen_name': 'A', 'time': '2013-11-10 06:10:00' }] expected_answers = { '1': { 'anon_screen_name': 'A', 'duration': '5' }, '2': { 'anon_screen_name': 'B', 'duration': '10' }, '3': { 'anon_screen_name': 'A', 'duration': '5' } } for raw_event in raw_events: genformatting.TIMESTAMP_FORMAT = [ '%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S' ] genformatting.parse_timestamp(raw_event) event = Event(raw_event) ending_event = manager.stage_event(event) if ending_event: self.assertEqual( expected_answers[ending_event['_id']]['anon_screen_name'], ending_event['anon_screen_name']) self.assertEqual( expected_answers[ending_event['_id']]['duration'], str(ending_event.duration))
def test_get_assessment_row(self): '''Simple coverage test for the get_assessment_row method ''' raw_event = { '_id': 'a', 'time': '2013-08-13T19:47:47.451372', 'correctness': 'correct' } genformatting.TIMESTAMP_FORMAT = [ '%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S' ] genformatting.parse_timestamp(raw_event) event = events.ProblemInteraction(raw_event) assessment_row = { 'assessment_grader_id': 'automatic', 'assessment_timestamp': '2013-08-13 19:47:47.451372', 'assessment_grade': 1, 'submission_id': 'a', 'assessment_id': 'a' } self.assertEqual(assessment_row, event.get_assessment_row())
def test_parse_timestamp(self): '''Simple test coverage of timestamp parser. ''' test_datetimes = { '2013-09-11T13:25:44.876729+00:00': datetime.datetime(2013, 9, 11, 13, 25, 44, 876729), '2013-02-22T13:13:31.323646': datetime.datetime(2013, 2, 22, 13, 13, 31, 323646), '2013-04-15T12:59:10': datetime.datetime(2013, 4, 15, 12, 59, 10), '2013-04-16 02:35:54': datetime.datetime(2013, 4, 16, 2, 35, 54) } for timestamp_string, datetime_answer in test_datetimes.iteritems(): raw_event = {'time': timestamp_string} genformatting.TIMESTAMP_FORMAT = [ '%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S' ] genformatting.parse_timestamp(raw_event) self.assertEqual(datetime_answer, raw_event['time'])
def test_get_submission_row(self): '''Simple coverage test for the get_submission_row method ''' raw_event = { '_id': 'a', 'event_type': 'problem_check', 'anon_screen_name': 'b', 'problem_id': 'c', 'time': '2013-08-13T19:47:47.451372', 'attempts': '3', 'ip': 'USA', 'os': 'TempleOS', 'agent': 'Netscape', 'answer': 'd', } genformatting.TIMESTAMP_FORMAT = [ '%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S' ] genformatting.parse_timestamp(raw_event) event = events.ProblemInteraction(raw_event) submission_row = { 'submission_id': 'a', 'user_id': 'b', 'problem_id': 'c', 'submission_timestamp': '2013-08-13 19:47:47.451372', 'submission_attempt_number': '3', 'submission_ip': 'USA', 'submission_os': 'TempleOS', 'submission_agent': 'Netscape', 'submission_answer': 'd', 'submission_is_submitted': 1, 'validity': 1 } self.assertEqual(submission_row, event.get_submission_row()) raw_event = { '_id': 'a', 'event_type': 'problem_check', 'anon_screen_name': 'b', 'problem_id': 'c', 'time': '2013-08-13T19:47:47.451372', 'attempts': '', 'ip': 'USA', 'os': 'TempleOS', 'agent': 'Netscape', 'answer': 'd', } genformatting.TIMESTAMP_FORMAT = [ '%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S' ] genformatting.parse_timestamp(raw_event) event = events.ProblemInteraction(raw_event) submission_row = { 'submission_id': 'a', 'user_id': 'b', 'problem_id': 'c', 'submission_timestamp': '2013-08-13 19:47:47.451372', 'submission_attempt_number': '', 'submission_ip': 'USA', 'submission_os': 'TempleOS', 'submission_agent': 'Netscape', 'submission_answer': 'd', 'submission_is_submitted': 1, 'validity': 0 } self.assertEqual( submission_row, event.get_submission_row(), "{} != {}".format(submission_row, event.get_submission_row()))