Exemplo n.º 1
0
 def setUp(self):
     self.c1 = MainCategory.objects.create(name='Tank')
     self.sub1 = SubCategory.objects.create(name='Unit', meta_category=self.c1)
     # create two different challenges
     self.chal = Challenge.objects.create(name='Hello', difficulty=1, score=200, test_case_count=5, category=self.sub1,
                       description=ChallengeDescFactory(), test_file_name='tank')
     self.chal2 = Challenge.objects.create(name='Hello2', difficulty=1, score=200, test_case_count=5, category=self.sub1,
                       description=ChallengeDescFactory(), test_file_name='tank2')
     self.max_challenge_score = 400
     self.starter_prof = Proficiency.objects.create(name='starter', needed_percentage=0)
     self.mid_prof = Proficiency.objects.create(name='mid', needed_percentage=50)
     self.mid_prof_award = SubcategoryProficiencyAward.objects.create(subcategory=self.sub1, proficiency=self.mid_prof,
                                                                 xp_reward=1000)
     self.top_prof = Proficiency.objects.create(name='top', needed_percentage=100)
     self.user: User = UserFactory()
     self.user.save()
Exemplo n.º 2
0
    def base_set_up(self, create_user=True):
        """
        Since a lot of tests use the same setUp code, namely creating:
            - A user
            - A submission
            - A challenge (and everything associated with it, subcategory, proficiency, etc)
        This function encapsulates it
        """
        self.sample_desc = ChallengeDescFactory()
        self.python_language = Language.objects.create(name="Python")
        challenge_cat = MainCategory.objects.create(name='Tests')
        self.sub_cat = SubCategory.objects.create(name='tests',
                                                  meta_category=challenge_cat)
        Proficiency.objects.create(name='starter', needed_percentage=0)

        self.challenge = Challenge.objects.create(name='Hello',
                                                  difficulty=5,
                                                  score=10,
                                                  description=self.sample_desc,
                                                  test_case_count=2,
                                                  category=self.sub_cat)
        self.challenge.supported_languages.add(self.python_language)
        self.challenge.save()
        self.challenge_name = self.challenge.name
        if create_user:
            self.create_user_and_auth_token()
        self.subcategory_progress = UserSubcategoryProficiency.objects.filter(
            subcategory=self.sub_cat, user=self.auth_user).first()
        self.submission = Submission.objects.create(
            language=self.python_language,
            challenge=self.challenge,
            author=self.auth_user,
            code="")
Exemplo n.º 3
0
 def setUp(self):
     self.c1 = MainCategory.objects.create(name='Test')
     self.sub1 = SubCategory.objects.create(name='Unit', meta_category=self.c1)
     self.sub2 = SubCategory.objects.create(name='Mock', meta_category=self.c1)
     self.sub3 = SubCategory.objects.create(name='Patch', meta_category=self.c1)
     Proficiency.objects.create(name='starter', needed_percentage=0)
     self.create_user_and_auth_token()
     self.sample_desc = ChallengeDescFactory()
Exemplo n.º 4
0
 def setUp(self):
     self.create_user_and_auth_token()
     challenge_cat = MainCategory.objects.create(name='Tests')
     self.sample_desc = ChallengeDescFactory()
     self.sub_cat = SubCategory.objects.create(name='tests', meta_category=challenge_cat)
     self.rust_lang = Language.objects.create(name='Rust')
     self.python_lang = Language.objects.create(name='Python')
     self.c_lang = Language.objects.create(name='C')
Exemplo n.º 5
0
 def setUp(self):
     self.c1 = MainCategory.objects.create(name='Test')
     self.sub1 = SubCategory.objects.create(name='Unit', meta_category=self.c1)
     self.first_prof = Proficiency.objects.create(name='starter', needed_percentage=0)
     self.second_prof = Proficiency.objects.create(name='master', needed_percentage=90)
     self.create_user_and_auth_token()
     self.sample_desc = ChallengeDescFactory()
     self.req_mock = MagicMock(user=self.auth_user)
     self.subcategory_progress = UserSubcategoryProficiency.objects.filter(subcategory=self.sub1,
                                                                           user=self.auth_user).first()
Exemplo n.º 6
0
    def test_subcategory_max_score_is_updated(self):
        """
        Test if the SubCategory's max score is updated on Challenge creation
        This is done to capture the fact that sometimes we'll have new challenges added or removed and
        it needs to reflex the max score in a subcategory
        """
        c1 = Challenge(name='Sub1', difficulty=5, score=200, description=ChallengeDescFactory(),
                       test_case_count=5, category=self.sub1)
        c2 = Challenge(name='Sub1_2', difficulty=5, score=200, description=ChallengeDescFactory(),
                       test_case_count=5, category=self.sub1)
        c3 = Challenge(name='Sub2', difficulty=5, score=200, description=ChallengeDescFactory(),
                       test_case_count=5, category=self.sub2)
        c1.save(); c2.save(); c3.save()

        self.sub1.refresh_from_db()
        self.sub2.refresh_from_db()
        self.sub3.refresh_from_db()
        self.assertEqual(self.sub1.max_score, 400)
        self.assertEqual(self.sub2.max_score, 200)
        self.assertEqual(self.sub3.max_score, 0)
Exemplo n.º 7
0
    def setUp(self):
        self.sample_desc = ChallengeDescFactory()
        self.python_language = Language.objects.create(name="Python")
        self.rust_language = Language.objects.create(name="Rust")
        challenge_cat = MainCategory.objects.create(name='Tests')
        self.sub_cat = SubCategory.objects.create(name='tests', meta_category=challenge_cat)
        Proficiency.objects.create(name='starter', needed_percentage=0)
        self.challenge = Challenge.objects.create(name='Hello', difficulty=5, score=10, description=self.sample_desc,
                                                  test_case_count=3, category=self.sub_cat)
        self.challenge_name = self.challenge.name

        self.create_user_and_auth_token()
Exemplo n.º 8
0
    def test_deserialize_ignores_read_only_fields(self):
        new_user = UserFactory(); new_user.save()
        desc = ChallengeDescFactory()
        new_c = Challenge.objects.create(name='Stay Callin', difficulty=5, score=10, description=desc,
                                                  test_case_count=2, category=self.sub_cat)
        challenge_comment = ChallengeComment.objects.create(challenge=self.challenge,
                                                            author=self.auth_user, content='Hello World')
        ser = ChallengeCommentSerializer(data={'id': 2014, 'content': 'change', 'author_id': new_user.id, 'parent_id': challenge_comment.id,
                                               'challenge_id': new_c.id})
        self.assertTrue(ser.is_valid())
        new_comment = ser.save(author=self.auth_user, challenge=self.challenge)

        self.assertNotEqual(new_comment.id, 2014)
        self.assertEqual(new_comment.author, self.auth_user)
        self.assertIsNone(new_comment.parent)
        self.assertEqual(new_comment.content, 'change')
        self.assertEqual(new_comment.challenge, self.challenge)
Exemplo n.º 9
0
    def test_submission_new_challenge_should_update(self):
        """ Since the user does not have a submission for this challenge, it should update his score """
        sample_desc2 = ChallengeDescFactory()
        new_challenge = Challenge.objects.create(name='NEW MAN', description=sample_desc2,
                                                 difficulty=10, score=100, test_file_name='smth',
                                                 test_case_count=3, category=self.sub_cat)
        new_submission = Submission.objects.create(language=self.python_language, challenge=new_challenge, author=self.auth_user,
                                                   code='hack you', task_id='123', result_score=100)
        old_sc_progress = self.subcategory_progress.user_score

        result = update_user_score(user=self.auth_user, submission=new_submission)

        # should also update his subcategory_progress by 100
        self.subcategory_progress.refresh_from_db()
        self.assertEqual(self.subcategory_progress.user_score, old_sc_progress + 100)
        self.assertTrue(result)
        self.auth_user.refresh_from_db()
        self.assertEqual(self.auth_user.score, 120)  # should have updated it
Exemplo n.º 10
0
    def create_challenge(self) -> Challenge:
        """
        Creates a Challenge object and returns it
        """
        from random import randint
        if not SubCategory.objects.exists():
            # create a subcategory
            if not MainCategory.objects.exists():
                main_cat = MainCategory.objects.create(
                    name=f'Tests{randint(1, 100)}')
            else:
                main_cat = MainCategory.objects.first()
            sub_cat = SubCategory.objects.create(name='tests',
                                                 meta_category=main_cat)
        else:
            sub_cat = SubCategory.objects.first()

        return Challenge.objects.create(
            name=f'Sample Challenge{randint(1, 100)}',
            difficulty=randint(1, 10),
            score=randint(1, 100),
            description=ChallengeDescFactory(),
            test_case_count=randint(1, 20),
            category=sub_cat)
Exemplo n.º 11
0
 def setUp(self):
     self.d: ChallengeDescription = ChallengeDescFactory()
Exemplo n.º 12
0
 def test_cannot_have_duplicate_descriptions(self):
     desc = ChallengeDescFactory()
     ChallengeFactory(description=desc)
     with self.assertRaises(IntegrityError):
         ChallengeFactory(description=desc)
Exemplo n.º 13
0
 def setUp(self):
     challenge_cat = MainCategory.objects.create(name='Tests')
     self.sample_desc = ChallengeDescFactory()
     self.sub_cat = SubCategory.objects.create(name='tests',
                                               meta_category=challenge_cat)
Exemplo n.º 14
0
    def test_serializes_as_expected(self):
        # create three challenges for said category
        for i in range(3):
            Challenge.objects.create(name=f'Hello{i}', difficulty=5, score=10, description=ChallengeDescFactory(),
                                     test_case_count=3, category=self.sub1)

        expected_data = {'name': self.sub1.name,
                         'proficiency': {'name': self.subcategory_progress.proficiency.name,
                                         'user_score': self.subcategory_progress.user_score},
                         'next_proficiency': {'name': self.second_prof.name,
                                              'needed_percentage': self.second_prof.needed_percentage},
                         'max_score': self.sub1.max_score, 'challenge_count': 3, 'solved_challenges_count': 0}
        received_data = LimitedSubCategorySerializer(self.sub1, context={'request': self.req_mock}).data
        self.assertEqual(expected_data, received_data)