Esempio n. 1
0
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
        AlgoService.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()
Esempio n. 2
0
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_user_recently_visited_domains(self):
        assert len(recent_domains_with_times(self.user)) != 0

    # TODO: Discuss the necessity of this test
    def test_user_recently_visited_domains_does_not_include_android(self):
        assert not(any("android" in dom[0] for dom in recent_domains_with_times(self.user)))

    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"
Esempio n. 3
0
class WordsToStudyTest(ModelTestMixIn):
    def setUp(self):
        super().setUp()

        BOOKMARK_COUNT = 10

        self.user_rule = UserRule()
        self.user_rule.add_bookmarks(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
        AlgoService.update_bookmark_priority(zeeguu.db, self.user)

        # THEN
        bookmark = self.__get_bookmark_with_highest_priority()

        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
        AlgoService.update_bookmark_priority(zeeguu.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)

        AlgoService.update_bookmark_priority(zeeguu.db, self.user)

        # THEN
        bookmark = self.__get_bookmark_with_lowest_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()
        return bookmarks_to_study[-1]
class FeedTest(ModelTestMixIn):
    def setUp(self):
        super().setUp()
        self.user_rule = UserRule()
        self.user = self.user_rule.user

    def test_words_being_learned(self):
        """Tests whether user bookmarks with exercises in their logs are
        flagged as 'learned'
        """
        length_should_be = random.randint(2, 5)
        exercises_amount = random.randint(2, 5)
        self.user_rule.add_bookmarks(length_should_be,
                                     exercises_count=exercises_amount)

        # Check whether exercises are flagged as 'learned'
        est = SimpleKnowledgeEstimator(self.user)
        length_to_check = len(est.words_being_learned())

        assert length_should_be == length_should_be, (str(length_to_check) +
                                                      " should be " +
                                                      str(length_should_be))

    def test_get_known_bookmarks(self):
        """Tests whether bookmarks with exercises are flagged as 'known'
        when their Outcome was 'too easy'
        """
        # Add a random number of bookmarks with the same learned language id
        # as the user to the user
        count_bookmarks = random.randint(2, 5)
        origin_word = UserWordRule(self.faker.word(),
                                   self.user.learned_language).user_word
        self.user_rule.add_bookmarks(count_bookmarks, origin=origin_word)
        est = SimpleKnowledgeEstimator(self.user)

        # Get how many bookmarks are 'known'.
        before = est.get_known_bookmarks()
        assert len(before) == 0, "No Bookmark should be known at this point"

        # Add an exercise with Outcome TOO_EASY to random number of bookmarks
        user_bookmarks = self.user.all_bookmarks()
        for i in range(0, random.randint(1, count_bookmarks)):
            bookmark = user_bookmarks[i]
            random_exercise = ExerciseRule().exercise
            random_exercise.outcome = OutcomeRule().too_easy
            bookmark.add_new_exercise(random_exercise)

        after = est.get_known_bookmarks()

        assert len(after) > len(before), (str(len(after)) +
                                          " should be larger than " +
                                          str(len(before)))
Esempio n. 5
0
class BookmarkTest(ModelTestMixIn):
    def setUp(self):
        super().setUp()

        self.user_rule = UserRule()
        self.user_rule.add_bookmarks(random.randint(1, 3))
        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_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 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()
Esempio n. 6
0
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"