class WordsExerciseStatsTest(ModelTestMixIn): def setUp(self): super().setUp() self.NUM_BOOKMARKS = 5 self.user_rule = UserRule() self.user_rule.add_bookmarks(self.NUM_BOOKMARKS) self.user = self.user_rule.user self.NUM_BOOKMARKS = len(self.user.all_bookmarks_fit_for_study()) def test_no_priority_without_run_of_algorithm(self): result = self.__get_table_count(BookmarkPriorityARTS) assert (result == 0) def test_update_bookmark_priority(self): # GIVEN # WHEN arts.update_bookmark_priority(self.db, self.user) # THEN result = self.__get_table_count(BookmarkPriorityARTS) assert (self.NUM_BOOKMARKS == result), (str(self.NUM_BOOKMARKS) + ' should be == to ' + str(result)) def __get_table_count(self, cls): return self.db.session.query(cls).count()
class WatchEventTest(ModelTestMixIn): def setUp(self): super().setUp() self.user_rule = UserRule() self.user = self.user_rule.user self.wet = WatchEventTypeRule() def test_new_watch_event_type(self): result = WatchEventType.find_by_name(self.wet.watch_event_type.name) assert result is not None assert result.name == self.wet.watch_event_type.name def test_watch_event(self): # GIVEN bookmark_rules = self.user_rule.add_bookmarks(1) bookmark = bookmark_rules[0].bookmark assert len(WatchInteractionEvent.events_for_bookmark(bookmark)) == 0 # WHEN WatchInterationEventRule(bookmark) # THEN assert len(WatchInteractionEvent.events_for_bookmark(bookmark)) == 1 def test_user_activity_data(self): uad = UserActivityData(self.user, datetime.now(), "reading", "1200", "") assert uad.event == "reading"
class DomainTest(ModelTestMixIn, TestCase): def setUp(self): super().setUp() self.user_rule = UserRule() self.user_rule.add_bookmarks(random.randint(1, 5)) self.user = self.user_rule.user def test_url_domain(self): """Tests the correct retrieval of a domain from a random url e.g. 'https://google.com' should be retrieved from e.g. 'https://google.com/search' """ url_random = UrlRule().url.as_string() url_parts = url_random.split('//', 1) domain_should_be = url_parts[0] + '//' + url_parts[1].split('/', 1)[0] domain_to_check = Url(url_random, self.faker.word()).domain_name() assert domain_to_check == domain_should_be, (domain_should_be + " should be " + domain_to_check) def test_one_domain_multiple_urls(self): """ Tests that if multiple URLs are added to the database that their DomainName is not added to the database more than once """ # Create an 'original' URL, which is saved to the Database url_random_obj_origin = UrlRule().url # Create a random number of URLs, each with the same DomainName random_num = random.randint(0, 10) for _ in range(0, random_num): url_random_extended = url_random_obj_origin.as_string( ) + self.faker.word() _ = Url(url_random_extended, self.faker.word()) domain_for_query = url_random_obj_origin.domain_name() try: assert DomainName.find(domain_for_query) except NoResultFound: assert False, "No domains found in database" except MultipleResultsFound: assert False, "There were multiple DomainNames in the database"
def _create_model_object(self): user_rule = UserRule() cohort = CohortRule() user = cohort.student1 #UserRule and CohortRule give different user.id, therefore we equalize them so that all the information refers #to the same user user = user_rule.user start_time = datetime.now() - timedelta(minutes=randint(0, 7200)) bookmark_rules = user_rule.add_bookmarks(bookmark_count=3, exercises_count=3) self.user = user_rule.user self.bookmark = bookmark_rules[0].bookmark exercise_session = UserExerciseSession(user.id, start_time) return exercise_session
class BookmarkTest(ModelTestMixIn): def setUp(self): super().setUp() self.user_rule = UserRule() self.user_rule.add_bookmarks(random.randint(3, 5)) self.user = self.user_rule.user def test_add_new_exercise(self): random_bookmark = BookmarkRule(self.user).bookmark length_original_exercise_log = len(random_bookmark.exercise_log) random_exercise = ExerciseRule().exercise random_bookmark.add_new_exercise(random_exercise) length_new_exercise_log = len(random_bookmark.exercise_log) assert length_original_exercise_log < length_new_exercise_log def test_translation(self): random_bookmark = BookmarkRule(self.user).bookmark assert random_bookmark.translation is not None def test_bookmarks_in_article(self): random_bookmark = BookmarkRule(self.user).bookmark article = random_bookmark.text.article # each bookmark belongs to a random text / article so the # combo of user/article will always result in one bookmark assert 1 == len( Bookmark.find_all_for_user_and_article(self.user, article)) def test_text_is_not_too_long(self): random_bookmark = BookmarkRule(self.user).bookmark random_text_short = TextRule(length=10).text random_bookmark.text = random_text_short assert random_bookmark.content_is_not_too_long() def test_add_exercise_outcome(self): random_bookmark = BookmarkRule(self.user).bookmark random_exercise = ExerciseRule().exercise random_bookmark.add_new_exercise_result( random_exercise.source, random_exercise.outcome, random_exercise.solving_speed, ) latest_exercise = random_bookmark.exercise_log[-1] assert latest_exercise.source == random_exercise.source assert latest_exercise.outcome == random_exercise.outcome assert latest_exercise.solving_speed == random_exercise.solving_speed def test_user_bookmark_count(self): assert len(self.user.all_bookmarks()) > 0 def test_bookmark_is_serializable(self): assert self.user.all_bookmarks()[0].json_serializable_dict() def test_bad_quality_bookmark(self): random_bookmarks = [ BookmarkRule(self.user).bookmark for _ in range(0, 3) ] random_bookmarks[0].origin = random_bookmarks[0].translation random_bookmarks[1].origin.word = self.faker.sentence(nb_words=10) random_bookmarks[2].origin.word = self.faker.word()[:2] for b in random_bookmarks: assert bad_quality_bookmark(b) def test_fit_for_study(self): random_bookmarks = [ BookmarkRule(self.user).bookmark for _ in range(0, 2) ] random_exercise = ExerciseRule().exercise random_exercise.outcome = OutcomeRule().wrong random_bookmarks[0].starred = True random_bookmarks[1].starred = True random_bookmarks[1].add_new_exercise(random_exercise) for b in random_bookmarks: assert b.fit_for_study def test_add_new_exercise_result(self): random_bookmark = BookmarkRule(self.user).bookmark exercise_count_before = len(random_bookmark.exercise_log) random_bookmark.add_new_exercise_result(SourceRule().random, OutcomeRule().random, random.randint(100, 1000)) exercise_count_after = len(random_bookmark.exercise_log) assert exercise_count_after > exercise_count_before def test_find_by_specific_user(self): list_should_be = self.user.all_bookmarks() list_to_check = Bookmark.find_by_specific_user(self.user) for b in list_should_be: assert b in list_to_check def test_find_all(self): list_should_be = self.user.all_bookmarks() list_to_check = Bookmark.find_all() for b in list_should_be: assert b in list_to_check def find_all_for_user_and_text(self): bookmark_should_be = self.user.all_bookmarks()[0] bookmark_to_check = Bookmark.find_all_for_text_and_user( bookmark_should_be.text, self.user) assert bookmark_should_be in bookmark_to_check def test_find(self): bookmark_should_be = self.user.all_bookmarks()[0] bookmark_to_check = Bookmark.find(bookmark_should_be.id) assert bookmark_to_check == bookmark_should_be def test_find_all_by_user_and_word(self): bookmark_should_be = self.user.all_bookmarks()[0] bookmark_to_check = Bookmark.find_all_by_user_and_word( self.user, bookmark_should_be.origin) assert bookmark_should_be in bookmark_to_check def test_find_by_user_word_and_text(self): bookmark_should_be = self.user.all_bookmarks()[0] bookmark_to_check = Bookmark.find_by_user_word_and_text( self.user, bookmark_should_be.origin, bookmark_should_be.text) assert bookmark_to_check == bookmark_should_be def test_exists(self): random_bookmark = self.user.all_bookmarks()[0] assert Bookmark.exists(random_bookmark) def test_latest_exercise_outcome(self): random_bookmark = self.user.all_bookmarks()[0] exercise_log = SortedExerciseLog(random_bookmark) assert exercise_log.latest_exercise_outcome() is None random_exercise = ExerciseRule().exercise random_bookmark.add_new_exercise(random_exercise) assert (random_exercise.outcome == SortedExerciseLog( random_bookmark).latest_exercise_outcome()) def test_is_learned_based_on_exercise_outcomes(self): random_bookmarks = [ BookmarkRule(self.user).bookmark for _ in range(0, 4) ] # Empty exercise_log should lead to a False return learned = is_learned_based_on_exercise_outcomes( SortedExerciseLog(random_bookmarks[0])) assert not learned # An exercise with Outcome equal to TOO EASY results in True, and time of last exercise random_exercise = ExerciseRule().exercise random_exercise.outcome = OutcomeRule().too_easy random_bookmarks[1].add_new_exercise(random_exercise) learned = is_learned_based_on_exercise_outcomes( SortedExerciseLog(random_bookmarks[1])) result_time = SortedExerciseLog( random_bookmarks[1]).last_exercise_time() assert learned and result_time == random_exercise.time # Same test as above, but without a second return value learned = is_learned_based_on_exercise_outcomes( SortedExerciseLog(random_bookmarks[1])) assert learned # A bookmark with CORRECTS_IN_A_ROW_FOR_LEARNED correct exercises in a row # returns true and the time of the last exercise correct_bookmark = random_bookmarks[2] exercises = 0 distinct_dates = set() while not (exercises >= CORRECTS_IN_A_ROW_FOR_LEARNED and len(distinct_dates) >= CORRECTS_IN_DISTINCT_DAYS_FOR_LEARNED): correct_exercise = ExerciseRule().exercise correct_exercise.outcome = OutcomeRule().correct correct_bookmark.add_new_exercise(correct_exercise) exercises += 1 distinct_dates.add(correct_exercise.time.date()) correct_bookmark.update_learned_status(self.db.session) log = SortedExerciseLog(correct_bookmark) learned = is_learned_based_on_exercise_outcomes(log) result_time = log.last_exercise_time() assert learned log = SortedExerciseLog(correct_bookmark) learned_time_from_log = log.last_exercise_time() assert result_time == learned_time_from_log # A bookmark with no TOO EASY outcome or less than 5 correct exercises in a row returns False, None wrong_exercise = ExerciseRule().exercise wrong_exercise.outcome = OutcomeRule().wrong random_bookmarks[3].add_new_exercise(wrong_exercise) log = SortedExerciseLog(random_bookmarks[3]) learned = is_learned_based_on_exercise_outcomes(log) assert not learned def test_top_bookmarks(self): assert top_bookmarks(self.user)
class WordsToStudyTest(ModelTestMixIn): def setUp(self): super().setUp() self.BOOKMARK_COUNT = 20 self.user_rule = UserRule() self.user_rule.add_bookmarks(self.BOOKMARK_COUNT, exercises_count=1) self.user = self.user_rule.user def test_new_bookmark_has_the_highest_priority(self): """ Adding a new bookmark, makes it the next thing to study """ # GIVEN new_bookmark = self.user_rule.add_bookmarks(1)[0].bookmark # WHEN arts.update_bookmark_priority(zeeguu.core.db, self.user) # THEN bookmark = self.__get_bookmark_with_highest_priority() # print (bookmark) # print (new_bookmark) self.assertTrue(new_bookmark == bookmark, "The newly added bookmark has the highest priority") def test_just_finished_bookmark_has_not_the_highest_priority(self): # GIVEN ABTesting._algorithms = [ ABTesting._algorithms[random.randint( 0, len(ABTesting._algorithms) - 1)] ] arts.update_bookmark_priority(zeeguu.core.db, self.user) first_bookmark_to_study = self.__get_bookmark_with_highest_priority() # WHEN # Add an exercise exercise_rule = ExerciseRule() exercise_rule.exercise.time = datetime.now() exercise_rule.exercise.solving_speed = 100 exercise_rule.exercise.outcome = OutcomeRule().correct first_bookmark_to_study.add_new_exercise(exercise_rule.exercise) arts.update_bookmark_priority(zeeguu.core.db, self.user) # THEN bookmark = self.__get_bookmark_with_highest_priority() assert first_bookmark_to_study != bookmark def __get_bookmark_with_highest_priority(self): bookmarks_to_study = self.user.bookmarks_to_study() if not bookmarks_to_study: return None return bookmarks_to_study[0] def __get_bookmark_with_lowest_priority(self): bookmarks_to_study = self.user.bookmarks_to_study() if len(bookmarks_to_study) == 0: return None return bookmarks_to_study[-1]