class JudgeTest(TestCase): def setUp(self): self.contest = Contest() self.contest.name = 'Test Contest' self.contest.players_count = 1 self.contest.save() self.judge = Judge() self.judge.path = 'some path' self.judge.was_default_judge = True self.judge.contest = self.contest self.judge.save() self.match = Match() self.match.judge = self.judge self.match.contest = self.contest self.match.save() self.user = GladosUser.objects.create_user("test_user") self.program = Program() self.program.user = self.user self.program.contest = self.contest self.program.name = 'test program' self.program.save() self.match_log = MatchLog() self.match_log.body = 'some random text' self.match_log.match = self.match self.match_log.save()
class ParserTest(TestCase): def setUp(self): self.judge = Judge() self.judge.path = '/example/judge' self.judge.save() self.contest1 = Contest() self.contest1.name = "test_contest_1" self.contest1.players_count = 1 self.contest1.default_judge = self.judge self.contest1.save() self.contest2 = Contest() self.contest2.name = "test_contest_2" self.contest2.players_count = 2 self.contest2.save() User = get_user_model() self.user1 = User.objects.create_user("user1", password="******") self.user2 = User.objects.create_user("user2", password="******") self.client_thread = FakeClient() self.match_manager = MatchManager() def test_parse_join(self): parser = InitParser(connection=self.client_thread, match_manager=self.match_manager) reply, new_parser = parser.parse('LOGIN {} PASSWORD {}'.format(quote('user1'), quote('passwd1'))) self.assertRegexpMatches(reply, "OK\n") def test_parse_join_invalid_passwd(self): parser = InitParser(connection=self.client_thread, match_manager=self.match_manager) reply, new_parser = parser.parse('LOGIN {} PASSWORD {}'.format(quote('user1'), quote('random_password'))) self.assertRegexpMatches(reply, "FAIL INVALID_PASSWORD\n") self.assertEqual(parser, new_parser) def test_parse_join_invalid_contest(self): parser = InitParser(connection=self.client_thread, match_manager=self.match_manager) reply, parser = parser.parse('LOGIN {} PASSWORD {}'.format(quote('user1'), quote('passwd1'))) self.assertRegexpMatches(reply, "OK\n") reply, new_parser = parser.parse('JOIN {}'.format(1234567890)) self.assertRegexpMatches(reply, "FAIL INVALID_CONTEST\n") self.assertEqual(parser, new_parser) def test_parse_join_ugly_password(self): parser = InitParser(connection=self.client_thread, match_manager=self.match_manager) reply, parser = parser.parse('LOGIN {} PASSWORD {}'.format(quote('user1'), quote('passwd1'))) self.assertRegexpMatches(reply, "OK\n") reply, parser = parser.parse('JOIN {}'.format(self.contest1.id)) self.assertRegexpMatches(reply, "OK\n") def test_parse_join_contest_without_default_judge(self): parser = InitParser(connection=self.client_thread, match_manager=self.match_manager) reply, parser = parser.parse('LOGIN {} PASSWORD {}'.format(quote('user1'), quote('passwd1'))) self.assertRegexpMatches(reply, "OK\n") reply, new_parser = parser.parse('JOIN {}'.format(self.contest2.id)) self.assertRegexpMatches(reply, "FAIL INVALID_CONTEST\n") self.assertEqual(parser, new_parser)
class ContestTest(TestCase): def setUp(self): self.contest = Contest() self.contest.name = 'Test Contest' self.contest.players_count = 1 self.contest.save() self.judge = Judge() self.judge.path = 'some path' self.judge.was_default_judge = True self.judge.contest = self.contest self.judge.save() self.match = Match() self.match.judge = self.judge self.match.contest = self.contest self.match.save() self.user = GladosUser.objects.create_user("test_user") self.program = Program() self.program.user = self.user self.program.contest = self.contest self.program.name = 'test program' self.program.save() self.match_log = MatchLog() self.match_log.body = 'some random text' self.match_log.match = self.match self.match_log.save() def test_contest_clean_start_end_time_invalid(self): self.contest.end = timezone.now() self.contest.start = self.contest.end + timezone.timedelta(days=1) exception = None try: self.contest.save() except ValidationError as e: exception = e self.assertTrue(exception, "contest start can't occur after contest end") def test_contest_clean_start_end_time_correct(self): self.contest.end = timezone.now() self.contest.start = self.contest.end - timezone.timedelta(days=1) exception = None try: self.contest.save() except ValidationError as e: exception = e self.assertFalse(exception, "contest start can occur after contest end: {}".format(exception))
def test_was_default_auto_update(self): judge = Judge() judge.path = '/example/judge' judge.save() self.assertFalse(judge.was_default_judge) contest1 = Contest() contest1.name = "test_contest_1" contest1.players_count = 1 contest1.default_judge = judge contest1.save() self.assertTrue(judge.was_default_judge)
class ProgramMatchTest(TestCase): def setUp(self): self.contest = Contest() self.contest.name = 'Test Contest' self.contest.players_count = 1 self.contest.save() self.judge = Judge() self.judge.path = 'some path' self.judge.was_default_judge = True self.judge.contest = self.contest self.judge.save() self.match = Match() self.match.judge = self.judge self.match.contest = self.contest self.match.save() self.user = GladosUser.objects.create_user("test_user") self.program = Program() self.program.user = self.user self.program.contest = self.contest self.program.name = 'test program' self.program.save() self.match_log = MatchLog() self.match_log.body = 'some random text' self.match_log.match = self.match self.match_log.save() def test_program_match_uniqueness(self): pm = ProgramMatch() pm.program = self.program pm.match = self.match pm.save() pm = ProgramMatch() pm.program = self.program pm.match = self.match exception = None try: pm.save() except IntegrityError as e: exception = e self.assertTrue(exception, "(program match) pair is not unique")
def setUp(self): self.judge = Judge() self.judge.path = '/example/judge' self.judge.save() self.contest1 = Contest() self.contest1.name = "test_contest_1" self.contest1.players_count = 1 self.contest1.default_judge = self.judge self.contest1.save() self.contest2 = Contest() self.contest2.name = "test_contest_2" self.contest2.players_count = 2 self.contest2.save() User = get_user_model() self.user1 = User.objects.create_user("user1", password="******") self.user2 = User.objects.create_user("user2", password="******") self.client_thread = FakeClient() self.match_manager = MatchManager()
def judge_create(request): redirect_url = request.GET.get('next', '/') error = "" if 'create' in request.POST: judge = Judge() judge.name = request.POST.get('judge_name', '') judge.description = request.POST.get('judge_description', '') judge.path = request.POST.get('judge_path', '') judge.init_parameters = request.POST.get('judge_init_parameters', '') try: judge.save() except ValidationError as e: error += str(e) + "\n" except IntegrityError as e: error += str(e) + "\n" if error == "": return redirect(redirect_url) return render(request, 'web/judge_create.html', { 'error': error })
class MatchLogTest(TestCase): def setUp(self): self.contest = Contest() self.contest.name = 'Test Contest' self.contest.players_count = 1 self.contest.save() self.judge = Judge() self.judge.path = 'some path' self.judge.was_default_judge = True self.judge.contest = self.contest self.judge.save() self.match = Match() self.match.judge = self.judge self.match.contest = self.contest self.match.save() self.user = GladosUser.objects.create_user("test_user") self.program = Program() self.program.user = self.user self.program.contest = self.contest self.program.name = 'test program' self.program.save() self.match_log = MatchLog() self.match_log.body = 'some random text' self.match_log.match = self.match self.match_log.save() def test_no_logs_before_match_start_invalid(self): self.match.start = timezone.now() self.match.save() self.match_log.match = self.match self.match_log.time = self.match.start - timezone.timedelta(days=1) exception = None try: self.match_log.save() except ValidationError as e: exception = e self.assertTrue(exception, "log with earlier date than match start") def test_no_logs_before_match_start_correct(self): self.match.start = timezone.now() self.match.save() self.match_log.match = self.match self.match_log.time = self.match.start + timezone.timedelta(days=1) exception = None try: self.match_log.save() except ValidationError as e: print e exception = e self.assertFalse(exception, "log with earlier date than match start: {}".format(exception))
class MatchTest(TestCase): def setUp(self): self.contest = Contest() self.contest.name = 'Test Contest' self.contest.players_count = 1 self.contest.save() self.judge = Judge() self.judge.path = 'some path' self.judge.was_default_judge = True self.judge.contest = self.contest self.judge.save() self.match = Match() self.match.judge = self.judge self.match.contest = self.contest self.match.save() self.user = GladosUser.objects.create_user("test_user") self.program = Program() self.program.user = self.user self.program.contest = self.contest self.program.name = 'test program' self.program.save() self.match_log = MatchLog() self.match_log.body = 'some random text' self.match_log.match = self.match self.match_log.save() def test_matches_judge_was_default_invalid(self): self.judge.was_default_judge = False self.match.judge = self.judge exception = None try: self.judge.save() self.match.save() except ValidationError as e: exception = e self.assertTrue(exception, "especially for Mr. Maciek") def test_matches_judge_was_default_correct(self): self.judge.was_default_judge = True self.match.judge = self.judge exception = None try: self.judge.save() self.match.save() except ValidationError as e: exception = e self.assertFalse(exception, "especially for Mr. Maciek: {}".format(exception)) def test_match_starts_after_contest_invalid(self): self.contest.start = timezone.now() self.match.start = self.contest.start - timezone.timedelta(days=1) exception = None try: self.contest.save() self.match.save() except ValidationError as e: exception = e self.assertTrue(exception, "does not throw exception when match starts before the contest starts") def test_match_starts_after_contest_correct(self): self.contest.start = timezone.now() self.match.start = self.contest.start + timezone.timedelta(days=1) exception = None try: self.contest.save() self.match.save() except ValidationError as e: exception = e self.assertFalse(exception, "does throw exception even when match starts correctly after contest starts")