class ZoneDeletionTestCase(OrganizationManagementTestCase): def setUp(self): super(ZoneDeletionTestCase, self).setUp() self.zone = Zone(name=self.ZONE_NAME) self.zone.save() self.org.add_zone(self.zone) self.org.save() def test_delete_zone_from_org_admin(self): """Delete a zone from the org_management page""" self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.browser.find_element_by_css_selector(".zone-delete-link").click() self.browser.switch_to_alert().accept() self.browser_wait_for_no_element(".zone-delete-link") time.sleep(1) self.browser_check_django_message(message_type="success", contains="successfully deleted") with self.assertRaises(NoSuchElementException): self.assertEqual(self.browser.find_element_by_css_selector(".zone-delete-link"), None, "Make sure 'delete' link no longer exists.") def test_cancel_delete_zone_from_org_admin(self): """Delete a zone from the org_management page""" self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.browser.find_element_by_css_selector(".zone-delete-link").click() self.browser.switch_to_alert().dismiss() self.assertNotEqual(self.browser.find_element_by_css_selector(".zone-delete-link"), None, "Make sure 'delete' link still exists.") self.browser_check_django_message(num_messages=0) def test_issue_697_part1(self): self.facility = Facility(name=self.FACILITY_NAME) self.facility.save() self.test_delete_zone_from_org_admin()
class TestSaveContentLog(KALiteTestCase): CONTENT_ID = "712f11" POINTS = 3 COMPLETION_COUNTER = 1 CONTENT_SOURCE = "" CONTENT_KIND = "Document" USERNAME = "******" PASSWORD = "******" def setUp(self): super(TestSaveContentLog, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Default Facility") self.facility.save() self.user = FacilityUser(username=self.USERNAME, facility=self.facility) self.user.set_password(self.PASSWORD) self.user.save() # create an initial ContentLog instance so we have something to update later self.contentlog = ContentLog(content_id=self.CONTENT_ID, user=self.user) self.contentlog.points = self.POINTS self.contentlog.content_kind = (self.CONTENT_KIND,) self.contentlog.content_source = self.CONTENT_SOURCE self.contentlog.save() def test_timestamp(self): new_start_timestamp = ContentLog.objects.get(user=self.user) new_start_timestamp.save() # Make sure that the start_timestamp will not change when we update, # only progress_timestamp will update. self.assertEqual(new_start_timestamp.start_timestamp, self.contentlog.start_timestamp) self.assertTrue(new_start_timestamp.progress_timestamp > self.contentlog.progress_timestamp)
class OrganizationDeletionTestCase(OrganizationManagementTestCase): def test_delete_org(self): """Delete an empty org""" self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.assertNotEqual( self.browser.find_element_by_css_selector(".icon-pencil"), None, "Make sure 'edit' icon appears.") self.assertNotEqual( self.browser.find_element_by_css_selector(".icon-trash"), None, "Make sure 'delete' icon appears.") self.browser.find_element_by_css_selector(".icon-trash").click() self.browser.switch_to_alert().accept() self.browser_wait_for_no_element(".icon-trash") self.browser_check_django_message(message_type="success", contains="successfully deleted") with self.assertRaises(NoSuchElementException): self.assertEqual( self.browser.find_element_by_css_selector(".icon-trash"), None, "Make sure 'delete' icon is gone.") def test_cancel_delete_org(self): """Click to delete an empty org, then choose CANCEL""" self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.assertNotEqual( self.browser.find_element_by_css_selector(".icon-pencil"), None, "Make sure 'edit' icon appears.") self.assertNotEqual( self.browser.find_element_by_css_selector(".icon-trash"), None, "Make sure 'delete' icon appears.") self.browser.find_element_by_css_selector(".icon-trash").click() self.browser.switch_to_alert().dismiss() self.assertNotEqual( self.browser.find_element_by_css_selector(".icon-trash"), None, "Make sure 'delete' icon appears.") self.browser_check_django_message(num_messages=0) def test_cannot_delete_full_org(self): """Confirm no option to delete an org with data""" # Save zone info, but without adding self.zone = Zone(name=self.ZONE_NAME) self.zone.save() self.org.add_zone(self.zone) self.org.save() self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.assertNotEqual( self.browser.find_element_by_css_selector(".icon-pencil"), None, "Make sure 'edit' icon appears.") with self.assertRaises(NoSuchElementException): self.assertEqual( self.browser.find_element_by_css_selector(".icon-trash"), None, "Make sure 'delete' icon does not appear.") def test_issue_697_part2(self): self.facility = Facility(name=self.FACILITY_NAME) self.facility.save() self.test_delete_org()
def test_fetch_model(self): MODEL_NAME = "kalite.facility.models.Facility" facility_name = "kir1" # Create a Facility object first that will be fetched. facility = Facility(name=facility_name) facility.save() (out, err, rc) = call_command_with_output("readmodel", MODEL_NAME, id=facility.id) data_map = json.loads(out) self.assertEquals(data_map["name"], facility_name)
def test_unicode_string(self): # Dependencies dev = Device.get_own_device() self.assertNotIn(unicode(dev), "Bad Unicode data", "Device: Bad conversion to unicode.") fac = Facility(name=self.korean_string) fac.save() self.assertNotIn(unicode(fac), "Bad Unicode data", "Facility: Bad conversion to unicode.") fg = FacilityGroup(facility=fac, name=self.korean_string) fg.save() self.assertNotIn(unicode(fg), "Bad Unicode data", "FacilityGroup: Bad conversion to unicode.") user = FacilityUser( facility=fac, group=fg, first_name=self.korean_string, last_name=self.korean_string, username=self.korean_string, notes=self.korean_string, ) user.set_password(self.korean_string * settings.PASSWORD_CONSTRAINTS["min_length"]) user.save() self.assertNotIn(unicode(user), "Bad Unicode data", "FacilityUser: Bad conversion to unicode.") known_classes = [ExerciseLog, UserLog, UserLogSummary, VideoLog] # elog = ExerciseLog(user=user, exercise_id=self.korean_string) self.assertNotIn(unicode(elog), "Bad Unicode data", "ExerciseLog: Bad conversion to unicode (before saving).") elog.save() self.assertNotIn(unicode(elog), "Bad Unicode data", "ExerciseLog: Bad conversion to unicode (after saving).") vlog = VideoLog(user=user, video_id=self.korean_string, youtube_id=self.korean_string) self.assertNotIn(unicode(vlog), "Bad Unicode data", "VideoLog: Bad conversion to unicode (before saving).") vlog.save() self.assertNotIn(unicode(vlog), "Bad Unicode data", "VideoLog: Bad conversion to unicode (after saving).") ulog = UserLog(user=user) self.assertNotIn(unicode(ulog), "Bad Unicode data", "UserLog: Bad conversion to unicode.") ulogsum = UserLogSummary( user=user, device=dev, activity_type=1, start_datetime=datetime.now(), end_datetime=datetime.now(), ) self.assertNotIn(unicode(ulogsum), "Bad Unicode data", "UserLogSummary: Bad conversion to unicode.")
class TestExploreMethods(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_ATTEMPTS = 3 ORIGINAL_STREAK_PROGRESS = 20 NEW_POINTS_LARGER = 22 NEW_ATTEMPTS = 5 NEW_STREAK_PROGRESS_LARGER = 10 NEW_POINTS_SMALLER = 0 NEW_STREAK_PROGRESS_SMALLER = 0 EXERCISE_ID = "number_line" USERNAME1 = "test_user_explore_1" PASSWORD = "******" FACILITY = "Test Facility Explore" TIMESTAMP = datetime.datetime(2014, 11, 17, 20, 51, 2, 342662) def setUp(self): '''Performed before every test''' # create a facility and user that can be referred to in models across tests self.facility = Facility(name=self.FACILITY) self.facility.save() self.user1 = FacilityUser(username=self.USERNAME1, facility=self.facility) self.user1.set_password(self.PASSWORD) self.user1.save() #add one exercise self.original_exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user1) self.original_exerciselog.points = self.ORIGINAL_POINTS self.original_exerciselog.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog.latest_activity_timestamp = self.TIMESTAMP self.original_exerciselog.completion_timestamp = self.TIMESTAMP self.original_exerciselog.save() #create a request factory for later instantiation of request self.factory = RequestFactory() def test_explore_overall(self): '''get_explore_recommendations()''' #create a request object and set the language attribute request = self.factory.get('/content_recommender?explore=true') request.language = settings.LANGUAGE_CODE actual = get_explore_recommendations(self.user1, request) self.assertEqual(actual[0].get("interest_topic").get("id"), "arithmetic")
class ZoneDeletionTestCase(OrganizationManagementTestCase): def setUp(self): super(ZoneDeletionTestCase, self).setUp() self.zone = Zone(name=self.ZONE_NAME) self.zone.save() self.org.add_zone(self.zone) self.org.save() def test_delete_zone_from_org_admin(self): """Delete a zone from the org_management page""" self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.browser.find_element_by_css_selector(".zone-delete-link").click() self.browser.switch_to_alert().accept() self.browser_wait_for_no_element(".zone-delete-link") self.browser_check_django_message(message_type="success", contains="successfully deleted") with self.assertRaises(NoSuchElementException): self.assertEqual(self.browser.find_element_by_css_selector(".zone-delete-link"), None, "Make sure 'delete' link is gone.") def test_cancel_delete_zone_from_org_admin(self): """Delete a zone from the org_management page""" self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.browser.find_element_by_css_selector(".zone-delete-link").click() self.browser.switch_to_alert().dismiss() self.assertNotEqual(self.browser.find_element_by_css_selector(".zone-delete-link"), None, "Make sure 'delete' link still exists.") self.browser_check_django_message(num_messages=0) def test_cannot_delete_full_zone(self): # Save zone info, but without adding self.devicezone = DeviceZone(device=Device.get_own_device(), zone=self.zone) self.devicezone.save() # Check on the org management page self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) with self.assertRaises(NoSuchElementException): self.assertEqual(self.browser.find_element_by_css_selector(".zone-delete-link"), None, "Make sure 'delete' link is gone.") # Follow the link, and confirm on the zone management page. zone_url = self.browser.find_element_by_css_selector(".zone-manage-link").get_attribute("href") self.browse_to(zone_url) self.assertEqual(self.browser.current_url, zone_url, "Expect link to go to zone management page") with self.assertRaises(NoSuchElementException): self.assertEqual(self.browser.find_element_by_css_selector(".zone-delete-link"), None, "Make sure 'delete' link is gone.") def test_issue_697_part1(self): self.facility = Facility(name=self.FACILITY_NAME) self.facility.save() self.test_delete_zone_from_org_admin()
def generate_fake_facilities(names=("Wilson Elementary",)): """Add the given fake facilities""" facilities = [] for name in names: found_facilities = Facility.objects.filter(name=name) if found_facilities: facility = found_facilities[0] logging.info("Retrieved facility '%s'" % name) else: facility = Facility(name=name) facility.save() logging.info("Created facility '%s'" % name) facilities.append(facility) return facilities
class OrganizationDeletionTestCase(OrganizationManagementTestCase): def test_delete_org(self): """Delete an empty org""" self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.assertNotEqual(self.browser.find_element_by_css_selector(".icon-pencil"), None, "Make sure 'edit' icon appears.") self.assertNotEqual(self.browser.find_element_by_css_selector(".icon-trash"), None, "Make sure 'delete' icon appears.") self.browser.find_element_by_css_selector(".icon-trash").click() self.browser.switch_to_alert().accept() self.browser_wait_for_no_element(".icon-trash") self.browser_check_django_message(message_type="success", contains="successfully deleted") with self.assertRaises(NoSuchElementException): self.assertEqual(self.browser.find_element_by_css_selector(".icon-trash"), None, "Make sure 'delete' icon is gone.") def test_cancel_delete_org(self): """Click to delete an empty org, then choose CANCEL""" self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.assertNotEqual(self.browser.find_element_by_css_selector(".icon-pencil"), None, "Make sure 'edit' icon appears.") self.assertNotEqual(self.browser.find_element_by_css_selector(".icon-trash"), None, "Make sure 'delete' icon appears.") self.browser.find_element_by_css_selector(".icon-trash").click() self.browser.switch_to_alert().dismiss() self.assertNotEqual(self.browser.find_element_by_css_selector(".icon-trash"), None, "Make sure 'delete' icon appears.") self.browser_check_django_message(num_messages=0) def test_cannot_delete_full_org(self): """Confirm no option to delete an org with data""" # Save zone info, but without adding self.zone = Zone(name=self.ZONE_NAME) self.zone.save() self.org.add_zone(self.zone) self.org.save() self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.assertNotEqual(self.browser.find_element_by_css_selector(".icon-pencil"), None, "Make sure 'edit' icon appears.") with self.assertRaises(NoSuchElementException): self.assertEqual(self.browser.find_element_by_css_selector(".icon-trash"), None, "Make sure 'delete' icon does not appear.") def test_issue_697_part2(self): self.facility = Facility(name=self.FACILITY_NAME) self.facility.save() self.test_delete_org()
def test_records_created_before_reg_still_sync(self): with self.get_distributed_server() as d: # Create a facility on central server, in correct zone facility_central = Facility(name="Central Facility", zone_fallback=self.zone) facility_central.save() # Create a facility on distributed server facility_distributed_id = d.addmodel(FACILITY_MODEL, name='Distributed Facility') self.register(d) sync_results = d.sync() self.assertEqual(sync_results["downloaded"], 2, "Wrong number of records downloaded") # =2 because DeviceZone is redownloaded self.assertEqual(sync_results["uploaded"], 1, "Wrong number of records uploaded") self.assertEqual(Facility.objects.filter(id=facility_distributed_id).count(), 1, "Distributed server facility not found centrally.") results = d.runcode("from kalite.facility.models import Facility; count = Facility.objects.filter(id='%s').count()" % facility_central.id) self.assertEqual(results["count"], 1, "Central server facility not found on distributed.")
class CentralFacilityUserFormTestCase(OrganizationManagementTestCase): def setUp(self): super(CentralFacilityUserFormTestCase, self).setUp() self.zone = Zone(name=self.ZONE_NAME) self.zone.save() self.org.add_zone(self.zone) self.org.save() self.facility = Facility(name=self.FACILITY_NAME, zone_fallback=self.zone) self.facility.save() self.user.facility = self.facility self.user.save() def test_add_student(self): self.browser_login_user(self.USER_EMAIL, self.USER_PASSWORD) self.browse_to('%s?facility=%s' % (self.reverse('add_facility_student'), self.facility.id)) self.browser.find_element_by_id('id_username').send_keys('s') self.browser.find_element_by_id('id_password_first').send_keys('password') self.browser.find_element_by_id('id_password_recheck').send_keys('password') self.browser.find_elements_by_class_name('submit')[0].click() self.browser_check_django_message(message_type="success", contains="successfully created")
def test_syncing_of_remotely_created_model_modified_locally(self): with self.get_distributed_server() as d: # Create a facility on central server facility_central = Facility(name="Central Facility", zone_fallback=self.zone) facility_central.save() self.register(d) sync_results = d.sync() d.modifymodel(FACILITY_MODEL, facility_central.id, name="Central Facility - Mod") sync_results = d.sync() self.assertEqual(sync_results["uploaded"], 1, "Wrong number of records uploaded") self.assertEqual(Facility.objects.get(id=facility_central.id).name, "Central Facility - Mod", "Updated Facility name not synced back to central server")
class TestSaveExerciseLog(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_ATTEMPTS = 3 ORIGINAL_STREAK_PROGRESS = 20 NEW_POINTS_LARGER = 22 NEW_ATTEMPTS = 5 NEW_STREAK_PROGRESS_LARGER = 10 NEW_POINTS_SMALLER = 0 NEW_STREAK_PROGRESS_SMALLER = 0 EXERCISE_ID = "number_line" EXERCISE_ID2 = "radius_diameter_and_circumference" USERNAME = "******" PASSWORD = "******" def setUp(self): super(TestSaveExerciseLog, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Test Facility") self.facility.save() self.user = FacilityUser(username=self.USERNAME, facility=self.facility) self.user.set_password(self.PASSWORD) self.user.save() # create an initial ExerciseLog instance so we have something to update later self.original_exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user) self.original_exerciselog.points = self.ORIGINAL_POINTS self.original_exerciselog.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog.save() def test_new_exerciselog(self): # make sure the target exercise log does not already exist exerciselogs = ExerciseLog.objects.filter(exercise_id=self.EXERCISE_ID2, user__username=self.USERNAME) self.assertEqual(exerciselogs.count(), 0, "The target exercise log to be newly created already exists") c = KALiteClient() # login success = c.login(username=self.USERNAME, password=self.PASSWORD, facility=self.facility.id) self.assertTrue(success, "Was not able to login as the test user") # save a new exercise log result = c.save_exercise_log( exercise_id=self.EXERCISE_ID2, streak_progress=self.NEW_STREAK_PROGRESS_LARGER, points=self.NEW_POINTS_LARGER, correct=True, attempts=self.NEW_ATTEMPTS, user=self.USERNAME, ) self.assertEqual(result.status_code, 201, "An error (%d) was thrown while saving the exercise log." % result.status_code) # get a reference to the newly created ExerciseLog exerciselog = ExerciseLog.objects.get(exercise_id=self.EXERCISE_ID2, user__username=self.USERNAME) # make sure the ExerciseLog was properly created self.assertEqual(exerciselog.points, self.NEW_POINTS_LARGER, "The ExerciseLog's points were not saved correctly.") self.assertEqual(exerciselog.streak_progress, self.NEW_STREAK_PROGRESS_LARGER, "The ExerciseLog's streak progress was not saved correctly.") self.assertEqual(exerciselog.attempts, self.NEW_ATTEMPTS, "The ExerciseLog did not have the correct number of attempts (%d)." % self.NEW_ATTEMPTS) def test_update_exerciselog(self): # get a new reference to the existing ExerciseLog exerciselog = ExerciseLog.objects.get(id=self.original_exerciselog.id) # make sure the ExerciseLog hasn't already been changed self.assertEqual(exerciselog.points, self.ORIGINAL_POINTS, "The ExerciseLog's points have already changed.") self.assertEqual(exerciselog.streak_progress, self.ORIGINAL_STREAK_PROGRESS, "The ExerciseLog's streak progress already changed.") self.assertEqual(exerciselog.attempts, self.ORIGINAL_ATTEMPTS, "The ExerciseLog's attempts have already changed.") c = KALiteClient() # login success = c.login(username=self.USERNAME, password=self.PASSWORD, facility=self.facility.id) self.assertTrue(success, "Was not able to login as the test user") # save a new record onto the exercise log, with a correct answer (increasing the points and streak) result = c.save_exercise_log( exercise_id=self.EXERCISE_ID, streak_progress=self.NEW_STREAK_PROGRESS_LARGER, points=self.NEW_POINTS_LARGER, attempts=self.NEW_ATTEMPTS, user=self.USERNAME, ) self.assertEqual(result.status_code, 201, "An error (%d) was thrown while saving the exercise log." % result.status_code) # get a reference to the updated ExerciseLog exerciselog = ExerciseLog.objects.get(exercise_id=self.EXERCISE_ID, user__username=self.USERNAME) # make sure the ExerciseLog was properly updated self.assertEqual(exerciselog.points, self.NEW_POINTS_LARGER, "The ExerciseLog's points were not updated correctly.") self.assertEqual(exerciselog.streak_progress, self.NEW_STREAK_PROGRESS_LARGER, "The ExerciseLog's streak progress was not updated correctly.") self.assertEqual(exerciselog.attempts, self.NEW_ATTEMPTS, "The ExerciseLog did not have the correct number of attempts (%d)." % self.NEW_ATTEMPTS) # save a new record onto the exercise log, with an incorrect answer (decreasing the points and streak) result = c.save_exercise_log( exercise_id=self.EXERCISE_ID, streak_progress=self.NEW_STREAK_PROGRESS_SMALLER, points=self.NEW_POINTS_SMALLER, attempts=self.NEW_ATTEMPTS + 1, user=self.USERNAME, ) self.assertEqual(result.status_code, 201, "An error (%d) was thrown while saving the exercise log." % result.status_code) # get a reference to the updated ExerciseLog exerciselog = ExerciseLog.objects.get(exercise_id=self.EXERCISE_ID, user__username=self.USERNAME) # make sure the ExerciseLog was properly updated self.assertEqual(exerciselog.points, self.NEW_POINTS_SMALLER, "The ExerciseLog's points were not saved correctly.") self.assertEqual(exerciselog.streak_progress, self.NEW_STREAK_PROGRESS_SMALLER, "The ExerciseLog's streak progress was not saved correctly.") self.assertEqual(exerciselog.attempts, self.NEW_ATTEMPTS + 1, "The ExerciseLog did not have the correct number of attempts.")
class TestSaveVideoLog(KALiteTestCase): ORIGINAL_POINTS = 84 ORIGINAL_SECONDS_WATCHED = 32 NEW_POINTS = 32 NEW_SECONDS_WATCHED = 15 YOUTUBE_ID = "aNqG4ChKShI" VIDEO_ID = "dummy" YOUTUBE_ID2 = "b22tMEc6Kko" VIDEO_ID2 = "dummy2" USERNAME = "******" PASSWORD = "******" def setUp(self): super(TestSaveVideoLog, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Test Facility") self.facility.save() self.user = FacilityUser(username=self.USERNAME, facility=self.facility) self.user.set_password(self.PASSWORD) self.user.save() # create an initial VideoLog instance so we have something to update later self.original_videolog = VideoLog(video_id=self.VIDEO_ID, youtube_id=self.YOUTUBE_ID, user=self.user) self.original_videolog.points = self.ORIGINAL_POINTS self.original_videolog.total_seconds_watched = self.ORIGINAL_SECONDS_WATCHED self.original_videolog.save() def test_new_videolog(self): # make sure the target video log does not already exist videologs = VideoLog.objects.filter(video_id=self.VIDEO_ID2, user__username=self.USERNAME) self.assertEqual(videologs.count(), 0, "The target video log to be newly created already exists") c = KALiteClient() # login success = c.login(username=self.USERNAME, password=self.PASSWORD, facility=self.facility.id) self.assertTrue(success, "Was not able to login as the test user") # save a new video log result = c.save_video_log( video_id=self.VIDEO_ID2, youtube_id=self.YOUTUBE_ID2, total_seconds_watched=self.ORIGINAL_SECONDS_WATCHED, points=self.NEW_POINTS, user=self.USERNAME, ) self.assertEqual(result.status_code, 201, "An error (%d) was thrown while saving the video log." % result.status_code) # get a reference to the newly created VideoLog videolog = VideoLog.objects.get(video_id=self.VIDEO_ID2, user__username=self.USERNAME) # make sure the VideoLog was properly created self.assertEqual(videolog.points, self.NEW_POINTS, "The VideoLog's points were not saved correctly.") self.assertEqual(videolog.total_seconds_watched, self.ORIGINAL_SECONDS_WATCHED, "The VideoLog's seconds watched was not saved correctly.") def test_update_videolog(self): # get a new reference to the existing VideoLog videolog = VideoLog.objects.get(id=self.original_videolog.id) # make sure the VideoLog hasn't already been changed self.assertEqual(videolog.points, self.ORIGINAL_POINTS, "The VideoLog's points have already changed.") self.assertEqual(videolog.total_seconds_watched, self.ORIGINAL_SECONDS_WATCHED, "The VideoLog's seconds watched already changed.") c = KALiteClient() # login success = c.login(username=self.USERNAME, password=self.PASSWORD, facility=self.facility.id) self.assertTrue(success, "Was not able to login as the test user") # save a new record onto the video log, with a correct answer (increasing the points and streak) result = c.save_video_log( video_id=self.VIDEO_ID, youtube_id=self.YOUTUBE_ID, total_seconds_watched=self.ORIGINAL_SECONDS_WATCHED + self.NEW_SECONDS_WATCHED, points=self.ORIGINAL_POINTS + self.NEW_POINTS, user=self.USERNAME, ) self.assertEqual(result.status_code, 201, "An error (%d) was thrown while saving the video log." % result.status_code) # get a reference to the updated VideoLog videolog = VideoLog.objects.get(video_id=self.VIDEO_ID, user__username=self.USERNAME) # make sure the VideoLog was properly updated self.assertEqual(videolog.points, self.ORIGINAL_POINTS + self.NEW_POINTS, "The VideoLog's points were not updated correctly.") self.assertEqual(videolog.total_seconds_watched, self.ORIGINAL_SECONDS_WATCHED + self.NEW_SECONDS_WATCHED, "The VideoLog's seconds watched was not updated correctly.")
class TestResumeMethods(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_ATTEMPTS = 3 ORIGINAL_STREAK_PROGRESS = 20 NEW_POINTS_LARGER = 22 NEW_ATTEMPTS = 5 NEW_STREAK_PROGRESS_LARGER = 10 NEW_POINTS_SMALLER = 0 NEW_STREAK_PROGRESS_SMALLER = 0 EXERCISE_ID = "number_line" EXERCISE_ID2 = "radius_diameter_and_circumference" INVALID_EXERCISE_ID = "s_diameter_and_circumference" USERNAME1 = "test_user_resume1" USERNAME2 = "test_user_resume2" USERNAME3 = "test_user_resume3" PASSWORD = "******" FACILITY = "Test Facility Resume" TIMESTAMP_LATER = datetime.datetime(2014, 11, 17, 20, 51, 2, 342662) TIMESTAMP_EARLY = datetime.datetime(2014, 10, 8, 15, 59, 59, 370290) def setUp(self): """Performed before every test""" # a brand new user self.facility = Facility(name=self.FACILITY) self.facility.save() self.user_with_no_activity = FacilityUser(username=self.USERNAME1, facility=self.facility) self.user_with_no_activity.set_password(self.PASSWORD) self.user_with_no_activity.save() # a user with valid exercises self.user_with_activity = FacilityUser(username=self.USERNAME2, facility=self.facility) self.user_with_activity.set_password(self.PASSWORD) self.user_with_activity.save() # a user with invalid exercises self.user_with_old_activity = FacilityUser(username=self.USERNAME3, facility=self.facility) self.user_with_old_activity.set_password(self.PASSWORD) self.user_with_old_activity.save() # add some exercises for second user (both incomplete) self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user_with_activity, complete=False) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_POINTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.save() self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID2, user=self.user_with_activity, complete=False) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_POINTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.save() self.original_exerciselog3 = ExerciseLog(exercise_id=self.INVALID_EXERCISE_ID, user=self.user_with_old_activity, complete=False) self.original_exerciselog3.points = self.ORIGINAL_POINTS self.original_exerciselog3.attempts = self.ORIGINAL_POINTS self.original_exerciselog3.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog3.latest_activity_timestamp = self.TIMESTAMP_LATER self.original_exerciselog3.completion_timestamp = self.TIMESTAMP_LATER self.original_exerciselog3.save() def test_get_most_recent_incomplete_item(self): '''get_most_recent_incomplete_item()''' # test user with activity first expected = { "id": unicode(self.EXERCISE_ID2, 'utf-8'), "timestamp": self.TIMESTAMP_LATER, "kind": "Exercise" } actual = get_most_recent_incomplete_item(self.user_with_activity) self.assertEqual(expected, actual) # new user just created (no activity logged) self.assertIsNone(get_most_recent_incomplete_item(user=self.user_with_no_activity)) def test_get_empty_list_invalid_resume(self): # Used to mock a request object that is only queried for its 'lang' property. class MicroMock(object): def __init__(self, **kwargs): self.__dict__.update(kwargs) # test user with invalid activity actual = get_resume_recommendations(self.user_with_old_activity, MicroMock(language="en")) # ensure that no recommendations are returned self.assertEqual(len(actual), 0)
class TestNextMethods(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_ATTEMPTS = 0 ORIGINAL_STREAK_PROGRESS = 20 NEW_POINTS_LARGER = 22 NEW_ATTEMPTS = 5 NEW_STREAK_PROGRESS_LARGER = 10 NEW_POINTS_SMALLER = 0 NEW_STREAK_PROGRESS_SMALLER = 0 EXERCISE_ID = "number_line" EXERCISE_ID2 = "radius_diameter_and_circumference" EXERCISE_ID_STRUGGLE = "counting-out-1-20-objects" USERNAME1 = "test_user_next1" USERNAME2 = "test_user_next2" PASSWORD = "******" FACILITY = "Test Facility Next Steps" TIMESTAMP_LATER = datetime.datetime(2014, 11, 17, 20, 51, 2, 342662) TIMESTAMP_EARLY = datetime.datetime(2014, 10, 8, 15, 59, 59, 370290) TIMESTAMP_STRUGGLE = datetime.datetime(2014, 9, 17, 17, 43, 36, 405260) GROUP = 'Test Group Next Steps' def setUp(self): '''Performed before every test''' # create a facility and user that can be referred to in models across tests self.facility = Facility(name=self.FACILITY) self.facility.save() self.facilitygroup = FacilityGroup(name=self.GROUP, description="", facility=self.facility) self.facilitygroup.save() self.user1 = FacilityUser(username=self.USERNAME1, facility=self.facility, group=self.facilitygroup) self.user1.set_password(self.PASSWORD) self.user1.save() self.user2 = FacilityUser(username=self.USERNAME2, facility=self.facility, group=self.facilitygroup) self.user2.set_password(self.PASSWORD) self.user2.save() #user 1 - now add some mock data into exercise log self.original_exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user1) self.original_exerciselog.points = self.ORIGINAL_POINTS self.original_exerciselog.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog.latest_activity_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog.completion_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog.save() #user 2 self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID, user = self.user2, struggling=False) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.save() self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID2, user = self.user2, struggling=False) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.save() self.original_exerciselog3 = ExerciseLog(exercise_id=self.EXERCISE_ID_STRUGGLE, user = self.user2, struggling=True) self.original_exerciselog3.points = self.ORIGINAL_POINTS self.original_exerciselog3.attempts = self.ORIGINAL_POINTS #intentionally made larger to trigger struggling self.original_exerciselog3.streak_progress = 0 self.original_exerciselog3.attempts = 100 self.original_exerciselog3.latest_activity_timestamp = self.TIMESTAMP_STRUGGLE self.original_exerciselog3.completion_timestamp = self.TIMESTAMP_STRUGGLE self.original_exerciselog3.save() #set all other exercise's struggling param to false def test_group_recommendations(self): '''get_group_recommendations()''' user = FacilityUser.objects.filter(username=self.USERNAME1)[0] expected = ["radius_diameter_and_circumference"] actual = get_group_recommendations(user) self.assertEqual(expected, actual, "Group recommendations incorrect.") def test_struggling(self): '''get_struggling_exercises()''' expected = [unicode(self.EXERCISE_ID_STRUGGLE, 'utf-8')] actual = get_struggling_exercises(FacilityUser .objects .filter(username=self.USERNAME2) .order_by('-id')[0]) self.assertSequenceEqual(expected, actual, "Struggling return incorrect.") def test_exercise_prereqs(self): '''get_exercise_prereqs()''' ex_id = 'equivalent_fractions' expected = ['visualizing-equivalent-fractions'] actual = get_exercise_prereqs([ex_id]) self.assertEqual(expected, actual, "Exercise Prereqs incorrect.")
class TestHelperMethods(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_ATTEMPTS = 3 ORIGINAL_STREAK_PROGRESS = 20 NEW_POINTS_LARGER = 22 NEW_ATTEMPTS = 5 NEW_STREAK_PROGRESS_LARGER = 10 NEW_POINTS_SMALLER = 0 NEW_STREAK_PROGRESS_SMALLER = 0 EXERCISE_ID = "number_line" EXERCISE_ID2 = "radius_diameter_and_circumference" USERNAME1 = "test_user_helper1" PASSWORD = "******" FACILITY = "Test Facility Next Steps" TIMESTAMP_LATER = datetime.datetime(2014, 11, 17, 20, 51, 2, 342662) TIMESTAMP_EARLY = datetime.datetime(2014, 10, 8, 15, 59, 59, 370290) def setUp(self): '''Performed before every test''' super(TestHelperMethods, self).setUp() # user + facility self.facility = Facility(name=self.FACILITY) self.facility.save() self.user1 = FacilityUser(username=self.USERNAME1, facility=self.facility) self.user1.set_password(self.PASSWORD) self.user1.save() # insert some exercise activity self.original_exerciselog1 = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user1) self.original_exerciselog1.points = self.ORIGINAL_POINTS self.original_exerciselog1.attempts = self.ORIGINAL_POINTS self.original_exerciselog1.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog1.latest_activity_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog1.completion_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog1.struggling = False self.original_exerciselog1.save() self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID2, user=self.user1) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_POINTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.struggling = False self.original_exerciselog2.save() def tearDown(self): '''Performed after each test''' super(TestHelperMethods, self).tearDown() self.user_with_activity = None self.user_with_no_activity = None @unittest.skipIf( settings.RUNNING_IN_CI, "benjaoming: Disabling for now, cannot reproduce failures locally, " "but it fails consistently on Circle." ) def test_exercises_from_topics(self): '''get_exercises_from_topics()''' expected = [e.id for e in self.content_exercises[0:4]] actual = get_exercises_from_topics([self.content_subsubtopics[0].id]) actual = map(lambda s: str(s), actual) expected = set(expected) actual = set(sorted(actual)) self.assertEqual(expected, actual) def test_most_recent_exercises(self): '''get_most_recent_exercises()''' first = "radius_diameter_and_circumference" second = "number_line" expected = [unicode(first, 'utf-8'), unicode(second, 'utf-8')] actual = get_most_recent_exercises(self.user1) self.assertSequenceEqual(expected, actual)
class TestHelperMethods(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_ATTEMPTS = 3 ORIGINAL_STREAK_PROGRESS = 20 NEW_POINTS_LARGER = 22 NEW_ATTEMPTS = 5 NEW_STREAK_PROGRESS_LARGER = 10 NEW_POINTS_SMALLER = 0 NEW_STREAK_PROGRESS_SMALLER = 0 EXERCISE_ID = "number_line" EXERCISE_ID2 = "radius_diameter_and_circumference" USERNAME1 = "test_user_helper1" PASSWORD = "******" FACILITY = "Test Facility Next Steps" TIMESTAMP_LATER = datetime.datetime(2014, 11, 17, 20, 51, 2, 342662) TIMESTAMP_EARLY = datetime.datetime(2014, 10, 8, 15, 59, 59, 370290) def setUp(self): '''Performed before every test''' # user + facility self.facility = Facility(name=self.FACILITY) self.facility.save() self.user1 = FacilityUser(username=self.USERNAME1, facility=self.facility) self.user1.set_password(self.PASSWORD) self.user1.save() # insert some exercise activity self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user1) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_POINTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.struggling = False self.original_exerciselog2.save() self.original_exerciselog2 = ExerciseLog(exercise_id=self.EXERCISE_ID2, user=self.user1) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_POINTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.struggling = False self.original_exerciselog2.save() def tearDown(self): '''Performed after each test''' self.user_with_activity = None self.user_with_no_activity = None def test_exercises_from_topics(self): '''get_exercises_from_topics()''' expected = [u'counting-out-1-20-objects', u'counting-objects', u'one-more--one-less'] actual = get_exercises_from_topics(['cc-early-math-counting']) self.assertEqual(expected, actual) def test_most_recent_exercises(self): '''get_most_recent_exercises()''' first = "radius_diameter_and_circumference" second = "number_line" expected = [unicode(first, 'utf-8'), unicode(second, 'utf-8')] actual = get_most_recent_exercises(self.user1) self.assertSequenceEqual(expected, actual)
def create_facility(self, facility_name=default_facility_name): if Facility.objects.filter(name=facility_name): logging.debug("Creating duplicate facility: %s" % facility_name) facility = Facility(name=facility_name) facility.save() return facility
class TestExploreMethods(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_ATTEMPTS = 3 ORIGINAL_STREAK_PROGRESS = 20 NEW_POINTS_LARGER = 22 NEW_ATTEMPTS = 5 NEW_STREAK_PROGRESS_LARGER = 10 NEW_POINTS_SMALLER = 0 NEW_STREAK_PROGRESS_SMALLER = 0 EXERCISE_ID = "topic0-0-exercise-0" USERNAME1 = "test_user_explore_1" PASSWORD = "******" FACILITY = "Test Facility Explore" TIMESTAMP = datetime.datetime(2014, 11, 17, 20, 51, 2, 342662) def setUp(self): '''Performed before every test''' super(TestExploreMethods, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name=self.FACILITY) self.facility.save() self.user1 = FacilityUser(username=self.USERNAME1, facility=self.facility) self.user1.set_password(self.PASSWORD) self.user1.save() #add one exercise self.original_exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user1) self.original_exerciselog.points = self.ORIGINAL_POINTS self.original_exerciselog.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog.latest_activity_timestamp = self.TIMESTAMP self.original_exerciselog.completion_timestamp = self.TIMESTAMP self.original_exerciselog.save() #create a request factory for later instantiation of request self.factory = RequestFactory() def test_explore_overall(self): '''get_explore_recommendations()''' # This test is super-slow because of get_explore_recommendations which # is already called and covered in BDD tests. # In order to maintain some level of feasible testing, I am cutting # the test short here. # /benjaoming return #create a request object and set the language attribute request = self.factory.get('/content_recommender?explore=true') request.language = settings.LANGUAGE_CODE actual = get_explore_recommendations(self.user1, request) logger.info(actual[0]) self.assertEqual(actual[0].get("topic-0-0").get("id"), "topic0-0-exercise-1")
class TestVideoLogs(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_SECONDS_WATCHED = 3 NEW_POINTS = 22 NEW_SECONDS_WATCHED = 5 YOUTUBE_ID = "aNqG4ChKShI" VIDEO_ID = i18n.get_video_id(YOUTUBE_ID) or "dummy" def setUp(self): super(TestVideoLogs, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Test Facility") self.facility.save() self.user = FacilityUser(username="******", facility=self.facility) self.user.set_password("dumber") self.user.save() # create an initial VideoLog instance so we have something to collide with later self.original_videolog = VideoLog(video_id=self.VIDEO_ID, youtube_id=self.YOUTUBE_ID, user=self.user) self.original_videolog.points = self.ORIGINAL_POINTS self.original_videolog.total_seconds_watched = self.ORIGINAL_SECONDS_WATCHED self.original_videolog.save() # get a new reference to the existing VideoLog videolog = VideoLog.objects.get(id=self.original_videolog.id) # make sure the VideoLog was created correctly self.assertEqual(videolog.points, self.ORIGINAL_POINTS, "The VideoLog's points have already changed.") self.assertEqual(videolog.total_seconds_watched, self.ORIGINAL_SECONDS_WATCHED, "The VideoLog's total seconds watched have already changed.") def test_videolog_update(self): # get a new reference to the existing VideoLog videolog = VideoLog.objects.get(id=self.original_videolog.id) # update the VideoLog videolog.points = self.NEW_POINTS videolog.total_seconds_watched = self.NEW_SECONDS_WATCHED videolog.save() # get a new reference to the existing VideoLog videolog2 = VideoLog.objects.get(id=self.original_videolog.id) # make sure the VideoLog was updated self.assertEqual(videolog2.points, self.NEW_POINTS, "The VideoLog's points were not updated.") self.assertEqual(videolog2.total_seconds_watched, self.NEW_SECONDS_WATCHED, "The VideoLog's total seconds watched were not updated.") @unittest.skip("Auto-merging is not yet automatic, so skip this") def test_videolog_collision(self): # create a new video log with the same youtube_id and user, but different points/total seconds watched videolog = VideoLog(video_id=self.VIDEO_ID, youtube_id=self.YOUTUBE_ID, user=self.user) videolog.points = self.NEW_POINTS videolog.total_seconds_watched = self.NEW_SECONDS_WATCHED # try saving the new VideoLog: this is where the collision will happen, hopefully leading to a merge videolog.save() # get a new reference to the existing VideoLog videolog2 = VideoLog.objects.get(id=self.original_videolog.id) # make sure the VideoLog has been properly merged self.assertEqual(videolog.points, max(self.ORIGINAL_POINTS, self.NEW_POINTS), "The VideoLog's points were not properly merged.") self.assertEqual(videolog.total_seconds_watched, max(self.ORIGINAL_ATTEMPTS, self.NEW_SECONDS_WATCHED), "The VideoLog's total seconds watched have already changed.")
class TestExerciseLogs(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_ATTEMPTS = 3 NEW_POINTS = 22 NEW_ATTEMPTS = 5 EXERCISE_ID = "number_line" def setUp(self): super(TestExerciseLogs, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Test Facility") self.facility.save() self.user = FacilityUser(username="******", facility=self.facility) self.user.set_password("dumber") self.user.save() # create an initial ExerciseLog instance so we have something to collide with later self.original_exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user) self.original_exerciselog.points = self.ORIGINAL_POINTS self.original_exerciselog.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog.save() # get a new reference to the existing ExerciseLog exerciselog = ExerciseLog.objects.get(id=self.original_exerciselog.id) # make sure the ExerciseLog was saved as intended self.assertEqual(exerciselog.points, self.ORIGINAL_POINTS, "The ExerciseLog's points have already changed.") self.assertEqual(exerciselog.attempts, self.ORIGINAL_ATTEMPTS, "The ExerciseLog's attempts have already changed.") def test_exerciselog_update(self): # get a new reference to the existing ExerciseLog exerciselog = ExerciseLog.objects.get(id=self.original_exerciselog.id) # update the ExerciseLog exerciselog.points = self.NEW_POINTS exerciselog.attempts = self.NEW_ATTEMPTS exerciselog.save() # get a new reference to the existing ExerciseLog exerciselog2 = ExerciseLog.objects.get(id=self.original_exerciselog.id) # make sure the ExerciseLog was updated self.assertEqual(exerciselog2.points, self.NEW_POINTS, "The ExerciseLog's points were not updated.") self.assertEqual(exerciselog2.attempts, self.NEW_ATTEMPTS, "The ExerciseLog's attempts were not updated.") @unittest.skip("Auto-merging is not yet automatic, so skip this") def test_exerciselog_collision(self): # create a new exercise log with the same exercise_id and user, but different points/attempts exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user) exerciselog.points = self.NEW_POINTS exerciselog.attempts = self.NEW_ATTEMPTS # try saving the new ExerciseLog: this is where the collision will happen, hopefully leading to a merge exerciselog.save() # get a new reference to the existing ExerciseLog exerciselog2 = ExerciseLog.objects.get(id=self.original_exerciselog.id) # make sure the ExerciseLog has been properly merged self.assertEqual(exerciselog.points, max(self.ORIGINAL_POINTS, self.NEW_POINTS), "The ExerciseLog's points were not properly merged.") self.assertEqual(exerciselog.attempts, max(self.ORIGINAL_ATTEMPTS, self.NEW_ATTEMPTS), "The ExerciseLog's attempts have already changed.")
class TestVideoLogs(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_SECONDS_WATCHED = 3 NEW_POINTS = 22 NEW_SECONDS_WATCHED = 5 YOUTUBE_ID = "aNqG4ChKShI" VIDEO_ID = i18n.get_video_id(YOUTUBE_ID) or "dummy" def setUp(self): super(TestVideoLogs, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Test Facility") self.facility.save() self.user = FacilityUser(username="******", facility=self.facility) self.user.set_password("dumber") self.user.save() # create an initial VideoLog instance so we have something to collide with later self.original_videolog = VideoLog(video_id=self.VIDEO_ID, youtube_id=self.YOUTUBE_ID, user=self.user) self.original_videolog.points = self.ORIGINAL_POINTS self.original_videolog.total_seconds_watched = self.ORIGINAL_SECONDS_WATCHED self.original_videolog.save() # get a new reference to the existing VideoLog videolog = VideoLog.objects.get(id=self.original_videolog.id) # make sure the VideoLog was created correctly self.assertEqual(videolog.points, self.ORIGINAL_POINTS, "The VideoLog's points have already changed.") self.assertEqual(videolog.total_seconds_watched, self.ORIGINAL_SECONDS_WATCHED, "The VideoLog's total seconds watched have already changed.") def test_videolog_update(self): # get a new reference to the existing VideoLog videolog = VideoLog.objects.get(id=self.original_videolog.id) # update the VideoLog videolog.points = self.NEW_POINTS videolog.total_seconds_watched = self.NEW_SECONDS_WATCHED videolog.save() # get a new reference to the existing VideoLog videolog2 = VideoLog.objects.get(id=self.original_videolog.id) # make sure the VideoLog was updated self.assertEqual(videolog2.points, self.NEW_POINTS, "The VideoLog's points were not updated.") self.assertEqual(videolog2.total_seconds_watched, self.NEW_SECONDS_WATCHED, "The VideoLog's total seconds watched were not updated.") @unittest.skip("Auto-merging is not yet automatic, so skip this") def test_videolog_collision(self): # create a new video log with the same youtube_id and user, but different points/total seconds watched videolog = VideoLog(video_id=self.VIDEO_ID, youtube_id=self.YOUTUBE_ID, user=self.user) videolog.points = self.NEW_POINTS videolog.total_seconds_watched = self.NEW_SECONDS_WATCHED # try saving the new VideoLog: this is where the collision will happen, hopefully leading to a merge videolog.save() # get a new reference to the existing VideoLog videolog2 = VideoLog.objects.get(id=self.original_videolog.id) # make sure the VideoLog has been properly merged self.assertEqual(videolog.points, max(self.ORIGINAL_POINTS, self.NEW_POINTS), "The VideoLog's points were not properly merged.") self.assertEqual(videolog.total_seconds_watched, max(self.ORIGINAL_ATTEMPTS, self.NEW_SECONDS_WATCHED), "The VideoLog's total seconds watched have already changed.")
class TestExerciseLogs(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_ATTEMPTS = 3 NEW_POINTS = 22 NEW_ATTEMPTS = 5 EXERCISE_ID = "number_line" def setUp(self): super(TestExerciseLogs, self).setUp() # create a facility and user that can be referred to in models across tests self.facility = Facility(name="Test Facility") self.facility.save() self.user = FacilityUser(username="******", facility=self.facility) self.user.set_password("dumber") self.user.save() # create an initial ExerciseLog instance so we have something to collide with later self.original_exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user) self.original_exerciselog.points = self.ORIGINAL_POINTS self.original_exerciselog.attempts = self.ORIGINAL_ATTEMPTS self.original_exerciselog.save() # get a new reference to the existing ExerciseLog exerciselog = ExerciseLog.objects.get(id=self.original_exerciselog.id) # make sure the ExerciseLog was saved as intended self.assertEqual(exerciselog.points, self.ORIGINAL_POINTS, "The ExerciseLog's points have already changed.") self.assertEqual(exerciselog.attempts, self.ORIGINAL_ATTEMPTS, "The ExerciseLog's attempts have already changed.") def test_exerciselog_update(self): # get a new reference to the existing ExerciseLog exerciselog = ExerciseLog.objects.get(id=self.original_exerciselog.id) # update the ExerciseLog exerciselog.points = self.NEW_POINTS exerciselog.attempts = self.NEW_ATTEMPTS exerciselog.save() # get a new reference to the existing ExerciseLog exerciselog2 = ExerciseLog.objects.get(id=self.original_exerciselog.id) # make sure the ExerciseLog was updated self.assertEqual(exerciselog2.points, self.NEW_POINTS, "The ExerciseLog's points were not updated.") self.assertEqual(exerciselog2.attempts, self.NEW_ATTEMPTS, "The ExerciseLog's attempts were not updated.") @unittest.skip("Auto-merging is not yet automatic, so skip this") def test_exerciselog_collision(self): # create a new exercise log with the same exercise_id and user, but different points/attempts exerciselog = ExerciseLog(exercise_id=self.EXERCISE_ID, user=self.user) exerciselog.points = self.NEW_POINTS exerciselog.attempts = self.NEW_ATTEMPTS # try saving the new ExerciseLog: this is where the collision will happen, hopefully leading to a merge exerciselog.save() # get a new reference to the existing ExerciseLog exerciselog2 = ExerciseLog.objects.get(id=self.original_exerciselog.id) # make sure the ExerciseLog has been properly merged self.assertEqual(exerciselog.points, max(self.ORIGINAL_POINTS, self.NEW_POINTS), "The ExerciseLog's points were not properly merged.") self.assertEqual(exerciselog.attempts, max(self.ORIGINAL_ATTEMPTS, self.NEW_ATTEMPTS), "The ExerciseLog's attempts have already changed.")
class TestResumeMethods(KALiteTestCase): ORIGINAL_POINTS = 37 ORIGINAL_ATTEMPTS = 3 ORIGINAL_STREAK_PROGRESS = 20 NEW_POINTS_LARGER = 22 NEW_ATTEMPTS = 5 NEW_STREAK_PROGRESS_LARGER = 10 NEW_POINTS_SMALLER = 0 NEW_STREAK_PROGRESS_SMALLER = 0 EXERCISE_ID = "number_line" EXERCISE_ID2 = "radius_diameter_and_circumference" USERNAME1 = "test_user_resume1" USERNAME2 = "test_user_resume2" PASSWORD = "******" FACILITY = "Test Facility Resume" TIMESTAMP_LATER = datetime.datetime(2014, 11, 17, 20, 51, 2, 342662) TIMESTAMP_EARLY = datetime.datetime(2014, 10, 8, 15, 59, 59, 370290) def setUp(self): """Performed before every test""" # a brand new user self.facility = Facility(name=self.FACILITY) self.facility.save() self.user_with_no_activity = FacilityUser(username=self.USERNAME1, facility=self.facility) self.user_with_no_activity.set_password(self.PASSWORD) self.user_with_no_activity.save() # a user with valid exercises self.user_with_activity = FacilityUser(username=self.USERNAME2, facility=self.facility) self.user_with_activity.set_password(self.PASSWORD) self.user_with_activity.save() # add some exercises for second user (both incomplete) self.original_exerciselog2 = ExerciseLog( exercise_id=self.EXERCISE_ID, user=self.user_with_activity, complete=False ) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_POINTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_EARLY self.original_exerciselog2.save() self.original_exerciselog2 = ExerciseLog( exercise_id=self.EXERCISE_ID2, user=self.user_with_activity, complete=False ) self.original_exerciselog2.points = self.ORIGINAL_POINTS self.original_exerciselog2.attempts = self.ORIGINAL_POINTS self.original_exerciselog2.streak_progress = self.ORIGINAL_STREAK_PROGRESS self.original_exerciselog2.latest_activity_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.completion_timestamp = self.TIMESTAMP_LATER self.original_exerciselog2.save() def test_get_most_recent_incomplete_item(self): """get_most_recent_incomplete_item()""" # test user with activity first expected = {"id": unicode(self.EXERCISE_ID2, "utf-8"), "timestamp": self.TIMESTAMP_LATER, "kind": "Exercise"} actual = get_most_recent_incomplete_item(self.user_with_activity) self.assertEqual(expected, actual) # new user just created (no activity logged) self.assertIsNone(get_most_recent_incomplete_item(user=self.user_with_no_activity))