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_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_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_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_bookmarks_in_url(self): random_bookmark = BookmarkRule(self.user).bookmark url = random_bookmark.text.url # each bookmark belongs to a random text / url so the # combo of user/url will always result in one bookmark assert 1 == len(Bookmark.find_all_for_user_and_url(self.user, url))
def create_default_exercises(): for _ in range(5): b = BookmarkRule(flask.g.user).bookmark db_session.add(b) db_session.commit() return "OK"
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_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_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 b.bad_quality_bookmark()
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().correct 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 add_bookmarks(self, bookmark_count, exercises_count=0, **kwargs): bookmark_rules = [] for _ in range(bookmark_count): bookmark_rule = BookmarkRule(self.user, **kwargs) bookmark = bookmark_rule.bookmark for i in range(0, exercises_count): random_exercise = ExerciseRule().exercise bookmark.add_new_exercise(random_exercise) bookmark_rules.append(bookmark_rule) return bookmark_rules
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( datetime.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_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_has_been_learned(self): random_bookmarks = [ BookmarkRule(self.user).bookmark for _ in range(0, 2) ] random_exercise = ExerciseRule().exercise random_exercise.outcome = OutcomeRule().too_easy random_bookmarks[0].add_new_exercise(random_exercise) result_bool, result_time = random_bookmarks[0].has_been_learned( also_return_time=True) assert result_bool and result_time == random_bookmarks[0].exercise_log[ -1].time result_bool, result_None = random_bookmarks[1].has_been_learned( also_return_time=True) assert not result_bool and result_None is None
def test_check_if_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 assert not random_bookmarks[ 0].check_if_learned_based_on_exercise_outcomes() # 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) result_bool, result_time = random_bookmarks[ 1].check_if_learned_based_on_exercise_outcomes( add_to_result_time=True) assert result_bool and result_time == random_exercise.time # Same test as above, but without a second return value assert random_bookmarks[1].check_if_learned_based_on_exercise_outcomes( ) # A bookmark with 5 correct exercises in a row returns true and the time of the last exercise for i in range(0, 5): correct_exercise = ExerciseRule().exercise correct_exercise.outcome = OutcomeRule().correct random_bookmarks[2].add_new_exercise(correct_exercise) result_bool, result_time = random_bookmarks[ 2].check_if_learned_based_on_exercise_outcomes() assert result_bool and result_time == random_bookmarks[2].exercise_log[ -1].time random_bookmarks[2].update_learned_status(self.db.session) # 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) result_bool, result_None = random_bookmarks[ 3].check_if_learned_based_on_exercise_outcomes( add_to_result_time=True) assert not result_bool and result_None is None # Same as before, but without a second return value assert not random_bookmarks[ 3].check_if_learned_based_on_exercise_outcomes()
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() random_text_long = TextRule(length=200).text random_bookmark.text = random_text_long assert not random_bookmark.content_is_not_too_long()
def setUp(self): super().setUp() self.text_rule = TextRule() self.user_rule = UserRule() self.bookmark_rule = BookmarkRule(self.user_rule.user)
def test_translation(self): random_bookmark = BookmarkRule(self.user).bookmark assert random_bookmark.translation is not None