Example #1
0
    def test_list_problem_tag_combos(self, mock_filter_problems,
                                     mock_filter_tags,
                                     mock_get_knowledge_status):
        mock_get_knowledge_status.return_value = self.problem_tag_combo_df
        mock_filter_tags.return_value = 'fake_tag_df'
        mock_filter_problems.return_value = 'fake_res_prob'

        mock_presenter = Mock()
        p_g = ProblemGetter(db_gateway=Mock(), presenter=mock_presenter)

        # call
        p_g.list_problem_tag_combos(tag_substr='tag_substr',
                                    problem_substr='prob_substr')

        mock_get_knowledge_status.assert_called_once()

        tag_filter_call_df = mock_filter_tags.call_args[1]['df']
        tag_filter_str = mock_filter_tags.call_args[1]['tag_substr']
        assert_frame_equal(tag_filter_call_df,
                           self.problem_tag_combo_df.sort_values('KS'))
        self.assertEqual(tag_filter_str, 'tag_substr')

        prob_filter_call_df = mock_filter_problems.call_args[1]['df']
        self.assertEqual(prob_filter_call_df, 'fake_tag_df')
        prob_filter_str = mock_filter_problems.call_args[1]['problem_substr']
        self.assertEqual(prob_filter_str, 'prob_substr')

        mock_presenter.list_problem_tag_combos.assert_called_once_with(
            'fake_res_prob')
Example #2
0
    def test_aggregate_problems(self):
        knowledge_status = pd.DataFrame(
            data={
                'difficulty': [Difficulty.EASY] * 4,
                'problem': [
                    'problem_1', 'problem_1', 'unlogged_problem',
                    'unlogged_problem_with_logged_tag'
                ],
                'problem_id': [1, 1, 2, 3],
                'url': ['some_url.com'] * 4,
                'ease': [1, 1, np.nan, np.nan],
                'interval': [2, 2, np.nan, np.nan],
                'KS': [5, 5, np.nan, np.nan],
                'result': ['KNEW_BY_HEART', 'KNEW_BY_HEART', np.nan, np.nan],
                'RF': [1, 1, np.nan, np.nan],
                'tag': ['tag_1', 'tag_2', 'unlogged_tag', 'tag_1'],
                'ts_logged': [self.time_1, self.time_1, np.nan, np.nan]
            })

        expected_result = pd.DataFrame(data={
            'problem_id': [1, 2, 3],
            'KS': [5., 0., 0.],
            'RF': [1, np.nan, np.nan]
        })

        p_g = ProblemGetter(db_gateway=Mock(), presenter=Mock())

        # call
        res = p_g.aggregate_problems(knowledge_status)

        assert_frame_equal(expected_result, res, check_like=True)
Example #3
0
 def _show_problem_history(cls, _):
     problem_name = cls._get_problem_name()
     prob_getter = ProblemGetter(db_gateway=DjangoGateway(),
                                 presenter=CliPresenter())
     try:
         prob_getter.show_problem_history(name=problem_name)
     except ValueError as err:
         print(err)
Example #4
0
    def test_get_problems_none_found(self):
        p_g = ProblemGetter(db_gateway=Mock(), presenter=Mock())
        p_g.repo.get_problems.return_value = []

        expected_df = add_missing_columns(
            df=pd.DataFrame(), required_columns=self.problem_columns)

        res = p_g._get_problems()

        assert_frame_equal(expected_df, res)
Example #5
0
 def _list_problem_tag_combos(args):
     kwargs = {}
     if args.sort_by:
         kwargs['sorted_by'] = args.sort_by
     if args.filter_tags:
         kwargs['tag_substr'] = args.filter_tags
     if args.filter_problems:
         kwargs['problem_substr'] = args.filter_problems
     prob_getter = ProblemGetter(db_gateway=DjangoGateway(),
                                 presenter=CliPresenter())
     prob_getter.list_problem_tag_combos(**kwargs)
Example #6
0
    def test_get_knowledge_status(self, mock_log_data_getter,
                                  mock_get_problems):
        """ test that all data is retrieved, including unlogged problems """
        problem_df = pd.DataFrame(
            data={
                'difficulty': [Difficulty.EASY] * 3,
                'problem': [
                    'problem_1', 'unlogged_problem',
                    'unlogged_problem_with_logged_tag'
                ],
                'problem_id': [1, 2, 3],
                'tags': ['tag_1, tag_2', 'unlogged_tag', 'tag_1'],
                'url': ['some_url.com'] * 3
            })

        # problem 1 has been logged with 2 tags
        log_df = pd.DataFrame(
            data={
                'ease': [1] * 2,
                'interval': [2] * 2,
                'KS': [5] * 2,
                'problem_id': [1] * 2,
                'result': ['KNEW_BY_HEART'] * 2,
                'RF': [1] * 2,
                'tag': ['tag_1', 'tag_2'],
                'ts_logged': [self.time_1] * 2
            })

        expected_res = pd.DataFrame(
            data={
                'difficulty': [Difficulty.EASY] * 4,
                'problem': [
                    'problem_1', 'problem_1', 'unlogged_problem',
                    'unlogged_problem_with_logged_tag'
                ],
                'problem_id': [1, 1, 2, 3],
                'url': ['some_url.com'] * 4,
                'ease': [1, 1, np.nan, np.nan],
                'interval': [2, 2, np.nan, np.nan],
                'KS': [5, 5, np.nan, np.nan],
                'result': ['KNEW_BY_HEART', 'KNEW_BY_HEART', np.nan, np.nan],
                'RF': [1, 1, np.nan, np.nan],
                'tag': ['tag_1', 'tag_2', 'unlogged_tag', 'tag_1'],
                'ts_logged': [self.time_1, self.time_1, np.nan, np.nan]
            })

        mock_get_problems.return_value = problem_df
        mock_log_data_getter.return_value = log_df
        p_g = ProblemGetter(db_gateway=Mock(), presenter=Mock())

        # call
        res = p_g.get_knowledge_status()

        assert_frame_equal(expected_res, res, check_like=True)
Example #7
0
 def _list_problems(args):
     prob_getter = ProblemGetter(db_gateway=DjangoGateway(),
                                 presenter=CliPresenter())
     kwargs = {}
     if args.filter_name:
         kwargs['name_substr'] = args.filter_name
     if args.filter_tags_all:
         kwargs['tags_all'] = args.filter_tags_all
     if args.filter_tags_any:
         kwargs['tags_any'] = args.filter_tags_any
     if args.sort_by:
         kwargs['sorted_by'] = args.sort_by
     prob_getter.list_problems(**kwargs)
Example #8
0
    def test_get_knowledge_status(self, mock_get_knowledge_status,
                                  mock_get_problems):
        """ test that all data is retrieved, including unlogged problems """
        problem_df = pd.DataFrame(
            data={
                'difficulty': [Difficulty.EASY] * 2,
                'problem': ['problem_1', 'unlogged_problem'],
                'problem_id': [1, 2],
                'tags': ['tag_1, tag_2', 'unlogged_tag'],
                'url': ['some_url.com'] * 2
            })

        knowledge_status = pd.DataFrame(
            data={
                'difficulty': [Difficulty.EASY] * 3,
                'problem': ['problem_1', 'problem_1', 'unlogged_problem'],
                'problem_id': [1, 1, 2],
                'url': ['some_url.com'] * 3,
                'ease': [1, 1, np.nan],
                'interval': [2, 2, np.nan],
                'KS': [5, 5, np.nan],
                'result': ['KNEW_BY_HEART', 'KNEW_BY_HEART', np.nan],
                'RF': [1, 1, np.nan],
                'tag': ['tag_1', 'tag_2', 'unlogged_tag'],
                'ts_logged': [self.time_1, self.time_1, np.nan]
            })

        expected_res = pd.DataFrame(
            data={
                'difficulty': [Difficulty.EASY] * 2,
                'problem': ['problem_1', 'unlogged_problem'],
                'problem_id': [1, 2],
                'url': ['some_url.com'] * 2,
                'KS': [5., 0.],
                'RF': [1, np.nan],
                'tags': ['tag_1, tag_2', 'unlogged_tag']
            })

        mock_get_problems.return_value = problem_df
        mock_get_knowledge_status.return_value = knowledge_status
        p_g = ProblemGetter(db_gateway=Mock(), presenter=Mock())

        # call
        p_g.list_problems()

        res = p_g.presenter.list_problems.call_args[0][0]

        assert_frame_equal(expected_res, res, check_like=True)
Example #9
0
    def test_get_problems(self):
        mock_repo = Mock()
        mock_repo.get_problems.return_value = [self.problem]
        p_g = ProblemGetter(db_gateway=mock_repo, presenter=Mock())

        problem_df = p_g._get_problems(name_substr='aa',
                                       tags_any=['bb'],
                                       tags_all=['cc'])

        expected_result = self.problem_df

        mock_repo.get_problems.assert_called_once_with(name_substr='aa',
                                                       tags_any=['bb'],
                                                       tags_all=['cc'])

        assert_frame_equal(expected_result, problem_df,
                           check_like=True)  # no particular column order
Example #10
0
    def test_filter_problems(self):
        input_df = pd.DataFrame(data={
            'problem': ['hello', 'nope'],
            'other_col': ['one', 'two']
        })

        expected_result = pd.DataFrame(data={
            'problem': ['hello'],
            'other_col': ['one']
        })

        res = ProblemGetter._filter_problems(input_df, problem_substr='ello')

        assert_frame_equal(expected_result, res)
Example #11
0
    def test_merge_problem_with_unlogged_problems(self):
        prob_df = pd.DataFrame({
            'problem': ['problem'] * 2,
            'tag': ['tag_1', 'tag_2'],
            'problem_id': [1] * 2
        })
        log_df = pd.DataFrame({
            'problem_id': [1],
            'tag': ['tag_1'],
            'result': ['result_1']
        })
        expected_res = pd.DataFrame({
            'problem': ['problem'] * 2,
            'tag': ['tag_1', 'tag_2'],
            'problem_id': [1] * 2,
            'result': ['result_1', np.nan]
        })

        res = ProblemGetter._merge_problem_and_log_data(problem_data=prob_df,
                                                        log_data=log_df)

        assert_frame_equal(expected_res, res)
Example #12
0
    def test_merge_problem_and_log_data_one_to_many(self):
        prob_df = pd.DataFrame({
            'problem': ['problem'],
            'tag': ['tag_1'],
            'problem_id': [1]
        })
        log_df = pd.DataFrame({
            'problem_id': [1] * 2,
            'tag': ['tag_1', 'tag_1'],
            'result': ['result_1', 'result_2']
        })
        expected_res = pd.DataFrame({
            'problem': ['problem'] * 2,
            'tag': ['tag_1', 'tag_1'],
            'problem_id': [1] * 2,
            'result': ['result_1', 'result_2']
        })

        res = ProblemGetter._merge_problem_and_log_data(problem_data=prob_df,
                                                        log_data=log_df)

        assert_frame_equal(expected_res, res)