def test_list_problems(self, mock_stdout): # pylint: disable=line-too-long expected_output = \ "| problem_id | problem | difficulty | KS | RF | url |\n" \ "|--------------|-----------|--------------|------|------|--------------|\n" \ "| 5 | test-prob | MEDIUM | 2 | 0.7 | www.test.com |\n" CliPresenter.list_problems(problems=self.problem_df) self.assertEqual(expected_output, mock_stdout.getvalue())
def test_problem_log_confirmation(self): expected_txt = \ "Logged execution of Problem 'testname' with result " \ "'SOLVED_OPTIMALLY_IN_UNDER_25' at " with patch('sys.stdout', new_callable=io.StringIO) as mock_stdout: CliPresenter.confirm_problem_logged(problem=self.problem, problem_log=self.problem_log) res = mock_stdout.getvalue() self.assertTrue(res.startswith(expected_txt))
def test_list_problem_tag_combos(self, mock_stdout): # pylint: disable=line-too-long expected_output = \ "| tag | problem | problem_id | difficulty | last_access | last_result | KS | RF | url | ease | interval |\n" \ "|----------|-----------|--------------|--------------|------------------|---------------|------|------|--------------|--------|------------|\n" \ "| test-tag | name | 5 | MEDIUM | 2021-01-10 08:10 | NO_IDEA | 2 | 0.7 | www.test.com | 2.5 | 10 |\n" CliPresenter.list_problem_tag_combos( problem_tag_combos=self.problem_tag_combo_df) self.assertEqual(expected_output, mock_stdout.getvalue())
def test_show_problem_history(self, mock_stdout): expected_output = "History for problem 'test_problem':\n" expected_output += \ "| | ts_logged | result | comment | tags |\n" \ "|----|------------------|----------|-----------------------|--------|\n" \ "| 0 | 2021-01-10 08:10 | NO_IDEA | problem_log_1 comment | tag_1 |\n" CliPresenter.show_problem_history( problem=self.problem, problem_log_info=self.problem_log_info) self.assertEqual(expected_output, mock_stdout.getvalue())
def test_format_timestamp(self): timestamp = pd.Series({1: dt.datetime(2021, 1, 15, 10, 23, 45, 124)}) expected_res = pd.Series({1: '2021-01-15 10:23'}) res = CliPresenter._format_timestamp(ts=timestamp) assert_series_equal(expected_res, res)
def test_format_difficulty(self): difficulty = pd.Series({1: Difficulty.MEDIUM}) expected_res = pd.Series({1: Difficulty.MEDIUM.name}) res = CliPresenter._format_difficulty(difficulty=difficulty) assert_series_equal(expected_res, res)
def test_format_result(self): result = pd.Series({1: Result.KNEW_BY_HEART}) expected_res = pd.Series({1: Result.KNEW_BY_HEART.name}) res = CliPresenter._format_result(result=result) assert_series_equal(expected_res, res)
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)
def test_list_tags(self, mock_stdout): # pylint: disable=line-too-long test_df = pd.DataFrame(data=[{ 'tag': 'test-tag', 'experience': 0.8, 'KS (weighted avg)': 5.0, 'priority': 4.0, 'num_problems': 4 }]) expected_output = \ "| tag | priority | KS (weighted avg) | experience | num_problems |\n" \ "|----------|------------|---------------------|--------------|----------------|\n" \ "| test-tag | 4 | 5 | 0.8 | 4 |\n" CliPresenter.list_tags(tags=test_df) self.assertEqual(expected_output, mock_stdout.getvalue())
def _list_tags(args): kwargs = {} if args.filter: kwargs['sub_str'] = args.filter if args.sort_by: kwargs['sorted_by'] = args.sort_by tag_getter = TagGetter(db_gateway=DjangoGateway(), presenter=CliPresenter()) tag_getter.list_tags(**kwargs)
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)
def test_format_tag_df_empty(self): order = [ 'tag', 'priority', 'KS (weighted avg)', 'experience', 'num_problems' ] expected_res = pd.DataFrame(columns=order).set_index('tag') assert_frame_equal(expected_res, CliPresenter.format_tag_df(pd.DataFrame()), check_dtype=False, check_index_type=False)
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)
def _add_problem(cls, _): """Record a new problem""" prob_adder = ProblemAdder(db_gateway=DjangoGateway(), presenter=CliPresenter()) user_input = cls._record_problem_data() try: prob_adder.add_problem(difficulty=user_input['difficulty'], url=user_input['url'], name=user_input['name'], tags=cls._get_tags_from_user()) except ValueError as err: print(err) return
def _add_problem_log(cls, _): """Log the execution of a problem""" problem_name = cls._get_problem_name() try: result = cls._get_user_input_result() except ValueError as err: print(f"\nSupplied invalid Result!\n{err}") return comment = cls._get_comment() prob_logger = ProblemLogger(db_gateway=DjangoGateway(), presenter=CliPresenter()) try: prob_logger.log_problem(comment=comment, problem_name=problem_name, result=result, tags=cls._get_tags_from_user()) except ValueError as err: print(err)
def test_format_df(self): expected_df = pd.DataFrame(data=[{ 'difficulty': 'MEDIUM', 'ease': 2.5, 'interval': 10, 'problem_id': 5, 'KS': 2.0, 'last_access': '2021-01-10 08:10', 'problem': 'name', 'last_result': Result.NO_IDEA.name, 'RF': 0.7, 'tag': 'test-tag', 'url': 'www.test.com'}]) \ .set_index('tag') \ .reindex(columns=self.post_format_cols) formatted_df = CliPresenter.format_df( self.problem_tag_combo_df, ordered_cols=self.pre_format_cols, index_col='tag') assert_frame_equal(expected_df, formatted_df)
def test_format_problem_df_missing_cols(self): data_df = self.problem_tag_combo_df \ .loc[:, ['problem', 'problem_id', 'ts_logged']] \ .copy() expected_df = pd.DataFrame(data=[{ 'difficulty': np.nan, 'ease': np.nan, 'interval': np.nan, 'problem_id': 5, 'KS': np.nan, 'last_access': '2021-01-10 08:10', 'problem': 'name', 'last_result': np.nan, 'RF': np.nan, 'tag': np.nan, 'url': np.nan}]) \ .set_index('tag') \ .reindex(columns=self.post_format_cols) formatted_df = CliPresenter.format_df( data_df, ordered_cols=self.pre_format_cols, index_col='tag') assert_frame_equal(expected_df, formatted_df)
def test_format_tag_df(self): test_df = pd.DataFrame(data=[{ 'tags': 'test-tag', 'experience': 0.8, 'KS (weighted avg)': 5.0, 'priority': 4.0, 'num_problems': 4, 'surplus_col': 'not displayed' }]) formatted_df = CliPresenter.format_tag_df(test_df) expected_df = pd.DataFrame( data=[ {'tags': 'test-tag', 'experience': 0.8, 'KS (weighted avg)': 5.0, 'priority': 4.0, 'num_problems': 4}], columns=['tag', 'priority', 'KS (weighted avg)', 'experience', 'num_problems']) \ .set_index('tag') assert_frame_equal(formatted_df, expected_df)
def test_format_problem_df_empty_smoke_test(self): """ Too much trouble with getting the dtypes right; smoke test suffices for now """ CliPresenter.format_df(pd.DataFrame(), ordered_cols=self.pre_format_cols, index_col='tag')
def _add_tag(cls, _): """Create new Tag""" tag_adder = TagAdder(db_gateway=DjangoGateway(), presenter=CliPresenter()) tag_adder.add_tag(name=cls._clean_input(input('Tag name: ')))
def test_problem_confirmation_txt(self): expected_txt = "Created Problem 'testname' with id '1' " \ "(difficulty 'EASY', tags: test-tag)" self.assertEqual( expected_txt, CliPresenter._problem_confirmation_txt(problem=self.problem))