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 LanguageTest(ModelTestMixIn, TestCase): def setUp(self): super().setUp() self.user = UserRule().user def test_languages_exists(self): language_should_be = LanguageRule().random try: language_to_check = Language.find(language_should_be.code) except NoResultFound: assert False, "No Language found in database" assert language_should_be.code == language_to_check.code \ and language_should_be.name == language_to_check.name def test_get_all_languages(self): languages = LanguageRule.languages for lan in languages: assert LanguageRule.get_or_create_language(lan) def test_user_set_language(self): language_should_be = LanguageRule().random self.user.set_learned_language(language_should_be.code, session) assert self.user.learned_language.id == language_should_be.id def test_native_language(self): language_should_be = LanguageRule().random self.user.set_native_language(language_should_be.code) assert self.user.native_language.id == language_should_be.id
def setUp(self): super().setUp() self.user_rule = UserRule() self.user = self.user_rule.user self.wet = WatchEventTypeRule()
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 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())
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 test_create_anonymous(self): self.user = UserRule().user new_password = self.faker.password() self.user.update_password(new_password) user_to_check = User.create_anonymous( str(self.user.id), new_password, self.user.learned_language.code, self.user.native_language.code, ) assert user_to_check.email == str( self.user.id) + User.ANONYMOUS_EMAIL_DOMAIN assert user_to_check.name == str(self.user.id) assert user_to_check.learned_language == self.user.learned_language assert user_to_check.native_language == self.user.native_language
def __init__(self): super().__init__() self.cohort = self._create_model_object() self.save(self.cohort) self.teacher = UserRule().user self.save(self.teacher) teacher_role = TeacherCohortMap(self.teacher, self.cohort) self.save(teacher_role) self.student1 = UserRule().user self.student1.cohort = self.cohort self.save(self.student1) student2 = UserRule().user student2.cohort = self.cohort self.save(student2)
def _create_model_object(self): user = UserRule().user article = ArticleRule().article user_article = UserArticle(user, article) if self._exists_in_db(user_article): return self._create_model_object() return user_article
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
def setUp(self): super().setUp() self.user_rule = UserRule() self.user = self.user_rule.user self.db.session.add(self.user) self.db.session.commit() self.random_origin_word = self.faker.word() self.random_origin_language = LanguageRule().random self.user_word_rule = UserWordRule(self.random_origin_word, self.random_origin_language) self.text = "This sentence, taken as a reading passage unto itself, is being used to prove a point." self.english = LanguageRule().get_or_create_language("en")
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 UserTest(ModelTestMixIn): def setUp(self): super().setUp() self.user = UserRule().user def test_create_anonymous(self): self.user = UserRule().user new_password = self.faker.password() self.user.update_password(new_password) user_to_check = User.create_anonymous( str(self.user.id), new_password, self.user.learned_language.code, self.user.native_language.code, ) assert user_to_check.email == str( self.user.id) + User.ANONYMOUS_EMAIL_DOMAIN assert user_to_check.name == str(self.user.id) assert user_to_check.learned_language == self.user.learned_language assert user_to_check.native_language == self.user.native_language def test_date_of_last_bookmark(self): random_bookmarks = [ BookmarkRule(self.user).bookmark for _ in range(random.randint(2, 5)) ] random_bookmarks[-1].time = datetime.now() last_bookmark_time = self.user.date_of_last_bookmark() assert (last_bookmark_time == random_bookmarks[-1].time ), "{0} should be {1}".format(last_bookmark_time, random_bookmarks[-1].time) def test_active_during_recent(self): # User has no bookmarks, so he/she was not active recently assert not self.user.active_during_recent() random_bookmark = BookmarkRule(self.user).bookmark random_bookmark.time = datetime.now() assert self.user.active_during_recent() def test_validate_email(self): random_email = self.faker.email() assert User.validate_email("", random_email) def test_validate_password(self): random_password = self.faker.password() assert User.validate_password("", random_password) def test_validate_name(self): random_name = self.faker.name() assert User.validate_name("", random_name) def test_update_password(self): password_before = self.user.password password_after = self.user.update_password(self.faker.password()) assert password_before != password_after def test_all_bookmarks(self): random_bookmarks = [ BookmarkRule(self.user).bookmark for _ in range(random.randint(2, 5)) ] bookmarks_retrieved = self.user.all_bookmarks() assert all([b in bookmarks_retrieved for b in random_bookmarks]) def test_bookmarks_chronologically(self): random_bookmarks = [ BookmarkRule(self.user).bookmark for _ in range(random.randint(2, 5)) ] random_bookmarks_ordered = sorted(random_bookmarks, key=lambda b: b.time, reverse=True) assert self.user.bookmarks_chronologically( ) == random_bookmarks_ordered def test_bookmarks_by_date(self): random_bookmarks = [ BookmarkRule(self.user).bookmark for _ in range(random.randint(2, 5)) ] random_bookmarks_dates = [ self.__truncate_time_from_date(b.time) for b in random_bookmarks ] random_bookmarks_dates_count = dict(Counter(random_bookmarks_dates)) result_bookmarks, result_dates_sorted = self.user.bookmarks_by_date() for day, bookmarks in result_bookmarks.items(): assert len(bookmarks) == random_bookmarks_dates_count[day] def test_bookmark_counts_by_date(self): date_bookmark_pair = [] for i in range(random.randint(5, 10)): today_without_time = self.__truncate_time_from_date(today()) random_date = today_without_time - timedelta(random.randint( 1, 364)) random_bookmark = BookmarkRule(self.user).bookmark random_bookmark.time = random_date date_bookmark_pair.append(random_date) date_bookmark_count_pair = dict(Counter(date_bookmark_pair)) counts_by_date = json.loads(self.user.bookmark_counts_by_date()) for pair in counts_by_date: result_date = datetime.strptime(pair["date"], "%Y-%m-%d") result_count = pair["count"] assert result_date in date_bookmark_count_pair assert result_count == date_bookmark_count_pair[result_date] def test_exists(self): assert User.exists(self.user) def test_authorize(self): new_password = self.faker.password() self.user.update_password(new_password) result = User.authorize(self.user.email, new_password) assert result is not None and result == self.user def test_authorize_anonymous(self): random_uuid = str(uuid.uuid4()) new_password = self.faker.password() anonymous_user = User.create_anonymous(random_uuid, new_password) self.db.session.add(anonymous_user) self.db.session.commit() result = User.authorize_anonymous(random_uuid, new_password) assert result is not None and result == anonymous_user def __truncate_time_from_date(self, date_with_time): return datetime(date_with_time.year, date_with_time.month, date_with_time.day, 0, 0, 0)
def setUp(self): super().setUp() self.lan = LanguageRule().de self.user = UserRule().user
def setUp(self): super().setUp() self.user = UserRule().user
def setUp(self): super().setUp() self.text_rule = TextRule() self.user_rule = UserRule() self.bookmark_rule = BookmarkRule(self.user_rule.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]
def setUp(self): super().setUp() self.user_rule = UserRule() self.user_rule.add_bookmarks(random.randint(1, 5)) self.user = self.user_rule.user