def test_get_problem_logs(self, mock_query_problem_logs, mock_format_problem_logs): mock_query_problem_logs.return_value = 'fake_problems' DjangoGateway.get_problem_logs() mock_query_problem_logs.assert_called_once_with(problem_ids=None) mock_format_problem_logs.assert_called_once_with( problem_log_qs='fake_problems')
def test_create_problem(self): dgw = DjangoGateway() problem = dgw.create_problem(problem=self.problem) self.assertIsInstance(problem, Problem) self.assertEqual(problem.name, 'test_problem') orm_problems = OrmProblem.objects.all() self.assertEqual(orm_problems.count(), 1) orm_problem = orm_problems.first() self.assertEqual(orm_problem.name, 'test_problem') self.assertNotEqual(orm_problem.pk, 99) # pk is independent of problem_id self.assertEqual(orm_problem.url, 'https://testurl.com') self.assertEqual([t.name for t in orm_problem.tags.all()], ['tag1'])
def test_create_tag(self): tag = DjangoGateway.create_tag(Tag(name='tag1')) self.assertIsInstance(tag, Tag) self.assertEqual(tag.name, 'tag1') tags = OrmTag.objects.all() self.assertEqual(tags.count(), 1) self.assertEqual(tags[0].name, 'tag1')
def test_query_filter_for_tags_all(self): required_tags = ['tag1', 'tag2'] res = DjangoGateway._query_problems(tags_all=required_tags) self.assertEqual(len(res), 3) self.assertEqual(sorted([prob.name for prob in res]), ['many_tags', 'prob_1', 'prob_2'])
def test_query_filter_for_tags_any(self): tags = ['tag3', 'tag4'] res = DjangoGateway._query_problems(tags_any=tags) self.assertEqual(len(res), 2) self.assertEqual(sorted([prob.name for prob in res]), ['many_tags', 'single_tag'])
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_format_tag(self): tags = DjangoGateway._format_tags(tags=OrmTag.objects.all()) self.assertEqual(len(tags), 3) self.assertIsInstance(tags, list) self.assertIsInstance(tags[0], Tag) self.assertEqual([t.name for t in tags], ['tag2', 'Tag1', 't3']) self.assertEqual([t.tag_id for t in tags], [OrmTag.objects.get(name=t.name).pk for t in tags])
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 test_get_problems_filter_by_empty_params(self): params = [ ({ 'name': '' }, []), ] for kwargs, expected_res in params: with self.subTest(kwargs=kwargs, expected_res=expected_res): res = DjangoGateway.get_problems(**kwargs) self.assertEqual(res, expected_res)
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_create_problem_log(self): dgw = DjangoGateway() ts = dt.datetime(2021, 3, 6, 10, 0, tzinfo=tzlocal()) log = ProblemLogCreator.create( comment='test comment', problem_id=1, result=Result.SOLVED_OPTIMALLY_IN_UNDER_25, tags=[TagCreator.create(name=self.tag.name)], timestamp=ts) dgw.create_problem_log(problem_log=log) self.assertEqual(OrmProblemLog.objects.count(), 1) orm_log = OrmProblemLog.objects.first() self.assertEqual(orm_log.problem.name, 'testname') self.assertEqual(orm_log.result, Result.SOLVED_OPTIMALLY_IN_UNDER_25.value) self.assertEqual(orm_log.tags.count(), 1) self.assertEqual(orm_log.tags.first().name, 'test-tag') self.assertEqual(orm_log.timestamp, ts) self.assertEqual(orm_log.comment, 'test comment')
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 _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 test_format_problems(self): problems = DjangoGateway._format_problems(OrmProblem.objects.all()) tag_1 = TagCreator.create(name='tag1', tag_id=self.tag_1.pk) tag_2 = TagCreator.create(name='tag2', tag_id=self.tag_2.pk) self.assertIsInstance(problems, list) self.assertEqual(2, len(problems)) self.assertIsInstance(problems[0], Problem) self.assertEqual(problems[0].name, 'name2') self.assertEqual(problems[0].difficulty, Difficulty.EASY) self.assertEqual(problems[0].problem_id, OrmProblem.objects.first().pk) self.assertEqual(problems[0].tags, [tag_1, tag_2]) self.assertEqual(problems[0].url, 'www.test_url.com')
def test_format_problem_logs(self): expected_res = [ ProblemLogCreator.create( comment='test comment 123!', problem_id=self.prob.pk, result=Result.SOLVED_SUBOPTIMALLY, tags=[TagCreator.create(name='tag_1', tag_id=self.tag.pk)], timestamp=dt.datetime(2021, 1, 10, 10, tzinfo=gettz('UTC'))) ] problem_log_qs = OrmProblemLog.objects.filter(pk=self.problem_log.pk) res = DjangoGateway._format_problem_logs(problem_log_qs=problem_log_qs) self.assertEqual(expected_res, res)
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_query_all(self): self.assertEqual(4, len(DjangoGateway._query_problems(name=None)))
def test_query_tags(self): tags = DjangoGateway._query_tags(names=None, sub_str=None) self.assertIsInstance(tags[0], OrmTag) self.assertEqual([t.name for t in tags], ['tag2', 'Tag1', 't3'])
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_exists_raises_when_all_inputs(self): with self.assertRaises(ValueError) as context: DjangoGateway.problem_exists(name='test', problem_id=1) self.assertEqual(str(context.exception), "Supply exactly one of 'problem_id' or 'name'!")
def test_query_tag_filter_sub_str(self): tags = DjangoGateway._query_tags(names=None, sub_str='ag') self.assertEqual([t.name for t in tags], ['tag2', 'Tag1'])
def test_query_filter_by_substring(self): self.assertEqual(2, len(DjangoGateway._query_problems(name_substr='pro'))) self.assertEqual(1, len(DjangoGateway._query_problems(name_substr='_1')))
def test_query_tag_filter_names(self): tags = DjangoGateway._query_tags(names=['Tag1', 't3'], sub_str=None) self.assertEqual([t.name for t in tags], ['Tag1', 't3'])
def test_query_for_problem(self): self.assertEqual( 2, len(DjangoGateway._query_problem_logs(problem_ids=[self.prob.pk])))
def test_problem_does_not_exist_problem_id(self): self.assertFalse(DjangoGateway.problem_exists(problem_id=99999999))
def test_tag_does_not_exist(self): self.assertFalse(DjangoGateway.tag_exists(name='not there'))
def test_tag_exists(self): self.assertTrue(DjangoGateway.tag_exists(name='Tag1'))
def test_query_all(self): self.assertEqual(3, len(DjangoGateway._query_problem_logs()))
def test_get_tags(self): tags = DjangoGateway.get_tags() self.assertIsInstance(tags[0], Tag) self.assertEqual(len(tags), 3)
def test_query_filter_by_name(self): self.assertEqual(1, len(DjangoGateway._query_problems(name='prob_1')))