def test_ranking_language(self): """Test to check ranking language page""" client = Client() collection = create_collection('Colleccion de prueba XYZ') user = create_user('5555', 'pepe') client.login(username='******', password='******') group_a = create_group('1A') group_a.user_set.add(user) client.cookies.load({settings.LANGUAGE_COOKIE_NAME: 'en'}) ranking_url = reverse('judge:result', args=[collection.pk]) response = client.get(ranking_url, follow=True) self.assertIn('Group', response.content.decode('utf-8')) self.assertIn('User', response.content.decode('utf-8')) self.assertIn('Score', response.content.decode('utf-8')) self.assertIn('LEGEND', response.content.decode('utf-8')) self.assertIn('Solved', response.content.decode('utf-8')) self.assertIn('Sum of the first accepted submission of each exercise', response.content.decode('utf-8')) client.cookies.load({settings.LANGUAGE_COOKIE_NAME: 'es'}) ranking_url = reverse('judge:result', args=[collection.pk]) response = client.get(ranking_url, follow=True) self.assertIn('Grupo', response.content.decode('utf-8')) self.assertIn('Usuario', response.content.decode('utf-8')) self.assertIn('Puntuación', response.content.decode('utf-8')) self.assertIn('LEYENDA', response.content.decode('utf-8')) self.assertIn('Resueltos', response.content.decode('utf-8')) self.assertIn('Suma del primer envío aceptado de cada ejercicio', response.content.decode('utf-8'))
def test_ranking_achievements(self): """Test if we can see the number of achievements that have an user at ranking""" client = Client() user = create_user('passwordmichu', 'michu') client.login(username='******', password='******') coll = create_collection('Coleccion de cartas') problem = create_select_problem(coll, 'Problema') create_an_achievement_of_each(coll) submit_select_url = reverse('judge:submit', args=[problem.pk]) client.post(submit_select_url, {'code': problem.solution}, follow=True) ranking_url = reverse('judge:result', args=[coll.pk]) group_a = create_group('1A') group_a.user_set.add(user) response = client.get(ranking_url, follow=True) self.assertIn('x5', response.content.decode('utf-8'))
def test_participation(self): """ Test the count of participating users in a group """ group = create_group('Grupo test') user1 = create_user(username='******', passwd='1111') user2 = create_user(username='******', passwd='1111') user3 = create_user(username='******', passwd='1111') user4 = create_user(username='******', passwd='1111') collection = create_collection('Test for statistics') problem = create_select_problem(collection, 'Dummy for statistics') for user in [user1, user2, user3, user4]: group.user_set.add(user) subs = [ Submission(verdict_code=VerdictCode.AC, user=user1, problem=problem), Submission(verdict_code=VerdictCode.WA, user=user2, problem=problem), Submission(verdict_code=VerdictCode.RE, user=user2, problem=problem), Submission(verdict_code=VerdictCode.TLE, user=user2, problem=problem), Submission(verdict_code=VerdictCode.VE, user=user1, problem=problem), Submission(verdict_code=VerdictCode.AC, user=user4, problem=problem), ] for sub in subs: sub.save() data = participation_per_group() expected = { 'Grupo test': { 'all': 4, 'acc': 2, 'participating': 3, 'avg': 2, 'stdev': 1.0, 'quantiles': '1 - 1.0 - 2.0 - 3.0 - 3', } } self.assertDictEqual(data, expected)
def test_download_ranking(self): """ Test to download excel of results """ client = Client() user = create_user('2222', 'tamara') teacher = create_superuser('1111', 'teacher') group_a = create_group('1A') group_a.user_set.add(user) start = first_day_of_course(datetime(2020, 9, 1)) end = datetime(2021, 3, 7).strftime('%Y-%m-%d') collection = create_collection('Coleccion 1') select_problem = create_select_problem(collection, 'SelectProblem ABC DEF') sub = Submission.objects.create(code='SELECT * FROM test where n = 1000', user=user, veredict_code=VeredictCode.WA, problem=select_problem) sub.save() Submission.objects.filter(id=sub.id).update(creation_date=datetime(2021, 3, 5)) client.login(username=teacher.username, password='******') url = reverse('judge:download_ranking', args=[collection.pk]) response = client.get(url, {'group': group_a.id, 'start': start, 'end': end}, follow=True) # Teacher download ranking self.assertEqual( response.get('Content-Disposition'), "attachment; filename=ranking.xlsx", ) self.assertEqual( response.get('Content-Type'), "application/xlsx" ) file = tempfile.NamedTemporaryFile(mode='w+b', buffering=-1, suffix='.xlsx') cont = response.content file.write(cont) work = openpyxl.load_workbook(file) book = work.active self.assertIn("Colección: " + collection.name_md, book.cell(row=1, column=1).value) self.assertIn("1A", book.cell(row=3, column=1).value) self.assertIn("1", book.cell(row=5, column=1).value) self.assertIn(user.username, book.cell(row=5, column=2).value) self.assertIn("0/1 (1)", book.cell(row=5, column=3).value) self.assertIn("0", book.cell(row=5, column=4).value) self.assertIn("0", book.cell(row=5, column=5).value) file.close() # Date or group invalid response = client.get(url, { 'group': group_a.id, 'start': start, 'end': ''}, follow=True) self.assertIn("Este campo es obligatorio", response.content.decode('utf-8')) response = client.get(url, { 'group': group_a.id, 'start': 'eee', 'end': end}, follow=True) self.assertIn("Introduzca una fecha válida", response.content.decode('utf-8')) response = client.get(url, { 'group': group_a.id, 'end': end}, follow=True) self.assertIn("Este campo es obligatorio", response.content.decode('utf-8')) response = client.get(url, {'group': '1A', 'start': start, 'end': end}, follow=True) self.assertIn('Introduzca un número entero', response.content.decode('utf-8')) # User can't download ranking client.logout() client.login(username=user.username, password='******') response = client.get(url, {'group': group_a.id, 'start': start, 'end': end}, follow=True) self.assertIn('Forbidden', response.content.decode('utf-8'))
def test_download_ranking(self): """ Test to download excel of results """ client = Client() user = create_user('2222', 'tamara') teacher = create_superuser('1111', 'teacher') group_a = create_group('1A') group_a.user_set.add(user) start = first_day_of_course(datetime(2020, 9, 1)).strftime('%Y-%m-%d') end = datetime(2021, 3, 7).strftime('%Y-%m-%d') collection = create_collection('Coleccion 1') select_problem = create_select_problem(collection, 'SelectProblem1') select_problem_2 = create_select_problem(collection, 'SelectProblem2') sub1 = Submission.objects.create( code='SELECT * FROM test where n = 1000', user=user, verdict_code=VerdictCode.WA, problem=select_problem) Submission.objects.filter(id=sub1.id).update( creation_date=datetime(2021, 3, 1)) sub2 = Submission.objects.create( code='SELECT * FROM test where n = 1000', user=user, verdict_code=VerdictCode.AC, problem=select_problem_2) Submission.objects.filter(id=sub2.id).update( creation_date=datetime(2021, 3, 2)) sub3 = Submission.objects.create( code='SELECT * FROM test where n = 1000', user=user, verdict_code=VerdictCode.RE, problem=select_problem_2) Submission.objects.filter(id=sub3.id).update( creation_date=datetime(2021, 3, 4)) client.login(username=teacher.username, password='******') url = reverse('judge:download_ranking', args=[collection.pk]) response = client.get(url, { 'group': group_a.id, 'start': start, 'end': end }, follow=True) # Teacher download ranking self.assertEqual( response.get('Content-Disposition'), 'attachment; filename="ranking.ods"', ) self.assertEqual(response.get('Content-Type'), "application/vnd.oasis.opendocument.spreadsheet") cont = response.getvalue() buffer = io.BytesIO(cont) buffer.seek(0) data = get_data(buffer) self.assertIn(collection.name_md, data['1A'][0][1]) self.assertEqual("1A", data['1A'][1][1]) self.assertEqual(1, data['1A'][6][0]) self.assertEqual(user.username, data['1A'][6][1]) self.assertEqual("0/1", data['1A'][6][2]) # SelectProblem1 self.assertEqual("1/2 (1)", data['1A'][6][3]) # SelectProblem2 self.assertEqual(1, data['1A'][6][4]) # Score self.assertEqual(1, data['1A'][6][5]) # Solved # Date or group invalid response = client.get(url, { 'group': group_a.id, 'start': end, 'end': start }, follow=True) self.assertIn("La fecha inicial no puede ser mayor que la fecha final", response.content.decode('utf-8')) response = client.get(url, { 'group': group_a.id, 'start': start, 'end': datetime(4000, 7, 7).strftime('%Y-%m-%d') }, follow=True) self.assertIn("La fecha final no puede ser mayor que la fecha de hoy", response.content.decode('utf-8')) response = client.get(url, { 'group': group_a.id, 'start': start, 'end': '' }, follow=True) self.assertIn("Este campo es obligatorio", response.content.decode('utf-8')) response = client.get(url, { 'group': group_a.id, 'start': 'eee', 'end': end }, follow=True) self.assertIn("Introduzca una fecha válida", response.content.decode('utf-8')) response = client.get(url, { 'group': group_a.id, 'end': end }, follow=True) self.assertIn("Este campo es obligatorio", response.content.decode('utf-8')) response = client.get(url, { 'group': '1A', 'start': start, 'end': end }, follow=True) self.assertIn('Introduzca un número entero', response.content.decode('utf-8')) # Students cannot download ranking client.logout() client.login(username=user.username, password='******') response = client.get(url, { 'group': group_a.id, 'start': start, 'end': end }, follow=True) self.assertIn('pero no está autorizado a acceder a esta página', response.content.decode('utf-8'))