Exemplo n.º 1
0
    def test_validate_timestamp_raises_wrong_type(self):
        with self.assertRaises(TypeError) as context:
            ProblemLogCreator.validate_or_create_timestamp(
                ts='2021-01-03')  # noqa

        self.assertEqual(
            str(context.exception),
            "timestamp should be of type dt.datetime, but is <class 'str'>!")
    def setUp(self):
        self.time_1 = dt.datetime(2021, 1, 10, 1)
        self.time_2 = dt.datetime(2021, 1, 10, 5)

        self.tag_1 = TagCreator.create('tag_1')
        self.tag_2 = TagCreator.create('tag_2')

        self.problem_log_1 = ProblemLogCreator.create(
            comment='problem_log_1 comment',
            problem_id=1,
            result=Result.NO_IDEA,
            tags=[self.tag_1, self.tag_2],
            timestamp=self.time_1)

        self.problem_log_2 = ProblemLogCreator.create(
            problem_id=1,
            result=Result.SOLVED_OPTIMALLY_IN_UNDER_25,
            tags=[self.tag_2],
            timestamp=self.time_2)

        self.plg = ProblemLogGetter(db_gateway=Mock(), presenter=Mock())
        self.plg.repo.get_problem_logs.return_value = [
            self.problem_log_1, self.problem_log_2
        ]

        self.prob1_tag1_ts1_data = {
            'comment': 'problem_log_1 comment',
            'ease': 2.5,
            'interval': 3,
            'problem_id': 1,
            'result': Result.NO_IDEA,
            'tag': 'tag_1',
            'ts_logged': self.time_1
        }
        self.prob1_tag2_ts1_data = {
            'comment': 'problem_log_1 comment',
            'ease': 2.5,
            'interval': 3,
            'problem_id': 1,
            'result': Result.NO_IDEA,
            'tag': 'tag_2',
            'ts_logged': self.time_1
        }
        self.prob1_tag2_ts2_data = {
            'comment': '',
            'ease': 2.5,
            'interval': 14,
            'problem_id': 1,
            'result': Result.SOLVED_OPTIMALLY_IN_UNDER_25,
            'tag': 'tag_2',
            'ts_logged': self.time_2
        }
Exemplo n.º 3
0
    def test_create_timestamp(self):
        ts_before = dt.datetime.now(tz=gettz('UTC'))
        ts = ProblemLogCreator.validate_or_create_timestamp(ts=None)
        ts_after = dt.datetime.now(tz=gettz('UTC'))

        self.assertGreaterEqual(ts, ts_before)
        self.assertLessEqual(ts, ts_after)
 def setUp(self) -> None:
     self.tag_1 = TagCreator.create(name='tag_1')
     self.tag_2 = TagCreator.create(name='tag_2')
     self.problem = ProblemCreator.create(name='problem_1',
                                          difficulty=Difficulty.MEDIUM,
                                          problem_id=1,
                                          tags=[self.tag_1])
     self.pl_1 = ProblemLogCreator.create(
         problem_id=self.problem.problem_id,
         result=Result.NO_IDEA,
         tags=[self.tag_1],
         timestamp=dt.datetime(2021, 3, 19))
     self.pl_2 = ProblemLogCreator.create(
         problem_id=self.problem.problem_id,
         result=Result.NO_IDEA,
         tags=[self.tag_2],
         timestamp=dt.datetime(2021, 3, 20))
    def setUp(self):
        self.tag_1 = TagCreator.create('tag_1')
        self.tag_2 = TagCreator.create('tag_2')

        self.problem_log_1 = ProblemLogCreator.create(
            comment='problem_log_1 comment',
            problem_id=1,
            result=Result.NO_IDEA,
            tags=[self.tag_1, self.tag_2],
            timestamp=dt.datetime(2021, 1, 10, 1))

        problem_logs = [
            self.problem_log_1,
            ProblemLogCreator.create(problem_id=2,
                                     result=Result.SOLVED_OPTIMALLY_WITH_HINT,
                                     tags=[self.tag_2],
                                     timestamp=dt.datetime(2021, 1, 10, 5))
        ]

        self.plg = ProblemLogGetter(db_gateway=Mock(), presenter=Mock())
        self.plg.repo.get_problem_logs.return_value = problem_logs
Exemplo n.º 6
0
    def setUp(self) -> None:
        self.tag = TagCreator.create(name='test-tag')

        self.problem = ProblemCreator.create(name='testname',
                                             difficulty=Difficulty(1),
                                             problem_id=1,
                                             tags=[self.tag],
                                             url='test-url')

        self.problem_log = ProblemLogCreator.create(
            problem_id=1,
            result=Result.SOLVED_OPTIMALLY_IN_UNDER_25,
            tags=[TagCreator.create('test-tag')])
    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)
Exemplo n.º 8
0
    def _format_problem_logs(problem_log_qs: QuerySet) -> List[ProblemLog]:
        res = []
        for p_l in problem_log_qs:
            res.append(
                ProblemLogCreator.create(comment=p_l.comment,
                                         problem_id=p_l.problem.pk,
                                         result=Result(p_l.result),
                                         tags=[
                                             TagCreator.create(name=tag.name,
                                                               tag_id=tag.pk)
                                             for tag in p_l.tags.all()
                                         ],
                                         timestamp=p_l.timestamp))

        return res
Exemplo n.º 9
0
 def test_create_all_validators_called(
         self,
         mock_val_tags,
         mock_val_ts,  # pylint: disable=too-many-arguments
         mock_val_result,
         mock_val_problem_id,
         mock_val_comment):
     p_l = ProblemLogCreator.create(comment='test-comment',
                                    problem_id=1,
                                    result=Result.NO_IDEA,
                                    tags=[self.tag])
     self.assertIsInstance(p_l, ProblemLog)
     mock_val_tags.assert_called_once_with([self.tag])
     mock_val_ts.assert_called_once()
     mock_val_result.assert_called_once()
     mock_val_problem_id.assert_called_once()
     mock_val_comment.assert_called_once()
Exemplo n.º 10
0
    def log_problem(self, comment: str, problem_name: str, result: Result,
                    tags: List[str]):
        try:
            problem = self.repo.get_problems(name=problem_name)[0]
        except IndexError:
            raise ValueError(
                f"Problem with name '{problem_name}' does not exist, "
                "try searching for similar problems.")

        tag_getter = TagGetter(db_gateway=self.repo, presenter=self.presenter)

        problem_log = ProblemLogCreator.create(
            comment=comment,
            problem_id=problem.problem_id,
            result=result,
            tags=tag_getter.get_existing_tags(names=tags))

        self.repo.create_problem_log(problem_log=problem_log)

        self.presenter.confirm_problem_logged(problem=problem,
                                              problem_log=problem_log)
    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')
Exemplo n.º 12
0
    def test_validate_comment_raises_too_long(self):
        with self.assertRaises(ValueError) as context:
            ProblemLogCreator.validate_comment(comment='a' * 256)

        self.assertEqual(str(context.exception),
                         "Comment too long, max length = 255 chars.")
Exemplo n.º 13
0
 def test_validate_timestamp(self):
     ts = dt.datetime(2021, 3, 10)
     self.assertEqual(ts,
                      ProblemLogCreator.validate_or_create_timestamp(ts=ts))
Exemplo n.º 14
0
    def test_validate_result_raises_wrong_type(self):
        with self.assertRaises(TypeError) as context:
            ProblemLogCreator.validate_result(result=1)  # noqa

        self.assertEqual(str(context.exception),
                         "result should be of type 'Result'!")
Exemplo n.º 15
0
 def test_validate_result(self):
     self.assertEqual(
         Result.NO_IDEA,
         ProblemLogCreator.validate_result(result=Result.NO_IDEA))
Exemplo n.º 16
0
    def test_validate_problem_id_raises_wrong_type(self):
        with self.assertRaises(TypeError) as context:
            ProblemLogCreator.validate_problem_id(problem_id='1')  # noqa

        self.assertEqual(str(context.exception),
                         "problem_id should be of type 'int'!")
Exemplo n.º 17
0
 def test_validate_problem_id(self):
     self.assertEqual(1,
                      ProblemLogCreator.validate_problem_id(problem_id=1))