mirror of
https://gitlab.com/megazordpobeda/DataRush.git
synced 2026-05-23 03:57:09 +00:00
Merge branch 'master' of gitlab.prodcontest.ru:team-15/project
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# Generated by Django 5.1.6 on 2025-03-01 10:26
|
||||
# Generated by Django 5.1.6 on 2025-03-01 20:35
|
||||
|
||||
import apps.competition.models
|
||||
import datetime
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
@@ -11,7 +12,7 @@ class Migration(migrations.Migration):
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('user', '0001_initial'),
|
||||
('user', '0002_alter_user_email_alter_user_password_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
@@ -19,14 +20,14 @@ class Migration(migrations.Migration):
|
||||
name='Competition',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('title', models.CharField(max_length=100, verbose_name='Название')),
|
||||
('description', models.TextField(verbose_name='Описание')),
|
||||
('image_url', models.FileField(blank=True, null=True, upload_to='', verbose_name='Изображение соревнования')),
|
||||
('end_date', models.DateTimeField(blank=True, null=True, verbose_name='Дедлайн участия')),
|
||||
('start_date', models.DateTimeField(blank=True, null=True, verbose_name='Дедлайн участия')),
|
||||
('type', models.CharField(choices=[('solo', 'Solo')], max_length=10, verbose_name='Тип участия')),
|
||||
('participation_type', models.CharField(choices=[('edu', 'Edu'), ('competitive', 'Competitive')], max_length=11, verbose_name='Тип соревнования')),
|
||||
('participants', models.ManyToManyField(related_name='participants', to='user.user')),
|
||||
('title', models.CharField(max_length=100, verbose_name='название')),
|
||||
('description', models.TextField(verbose_name='описание')),
|
||||
('image_url', models.FileField(blank=True, null=True, upload_to=apps.competition.models.Competition.image_url_upload_to, verbose_name='изображение соревнования')),
|
||||
('end_date', models.DateTimeField(blank=True, null=True, verbose_name='дедлайн участия')),
|
||||
('start_date', models.DateTimeField(blank=True, null=True, verbose_name='дедлайн участия')),
|
||||
('type', models.CharField(choices=[('solo', 'Индивидуальный')], max_length=10, verbose_name='тип участия')),
|
||||
('participation_type', models.CharField(choices=[('edu', 'Образовательный'), ('competitive', 'Соревновательный')], max_length=11, verbose_name='тип соревнования')),
|
||||
('participants', models.ManyToManyField(blank=True, editable=False, related_name='participants', to='user.user')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'соревнование',
|
||||
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
# Generated by Django 5.1.6 on 2025-03-01 12:21
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('competition', '0001_initial'),
|
||||
('task', '0001_initial'),
|
||||
('user', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='competition',
|
||||
name='tasks',
|
||||
field=models.ManyToManyField(blank=True, related_name='tasks', to='task.competitiontask'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='competition',
|
||||
name='participants',
|
||||
field=models.ManyToManyField(blank=True, editable=False, related_name='participants', to='user.user'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='competition',
|
||||
name='participation_type',
|
||||
field=models.CharField(choices=[('edu', 'Образовательный'), ('competitive', 'Соревновательный')], max_length=11, verbose_name='Тип соревнования'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='competition',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('solo', 'Индивидуальный')], max_length=10, verbose_name='Тип участия'),
|
||||
),
|
||||
]
|
||||
@@ -1,17 +0,0 @@
|
||||
# Generated by Django 5.1.6 on 2025-03-01 13:49
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('competition', '0002_competition_tasks_alter_competition_participants_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='competition',
|
||||
name='tasks',
|
||||
),
|
||||
]
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
# Generated by Django 5.1.6 on 2025-03-01 14:46
|
||||
|
||||
import tinymce.models
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('competition', '0003_remove_competition_tasks'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='competition',
|
||||
name='description',
|
||||
field=tinymce.models.HTMLField(verbose_name='описание'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='competition',
|
||||
name='end_date',
|
||||
field=models.DateTimeField(blank=True, null=True, verbose_name='дедлайн участия'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='competition',
|
||||
name='image_url',
|
||||
field=models.FileField(blank=True, null=True, upload_to='', verbose_name='изображение соревнования'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='competition',
|
||||
name='participation_type',
|
||||
field=models.CharField(choices=[('edu', 'Образовательный'), ('competitive', 'Соревновательный')], max_length=11, verbose_name='тип соревнования'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='competition',
|
||||
name='start_date',
|
||||
field=models.DateTimeField(blank=True, null=True, verbose_name='дедлайн участия'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='competition',
|
||||
name='title',
|
||||
field=models.CharField(max_length=100, verbose_name='аазвание'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='competition',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('solo', 'Индивидуальный')], max_length=10, verbose_name='тип участия'),
|
||||
),
|
||||
]
|
||||
@@ -1,5 +1,8 @@
|
||||
import uuid
|
||||
from datetime import timedelta, datetime, tzinfo
|
||||
|
||||
from dateutil.parser import isoparse
|
||||
import pytz
|
||||
from django.contrib.auth.hashers import make_password
|
||||
from django.test import TestCase
|
||||
|
||||
@@ -12,14 +15,14 @@ class CompetitionEndpointTests(TestCase):
|
||||
self.user = User.objects.create(
|
||||
email="user@example.com",
|
||||
password=make_password("password123"),
|
||||
username="t1wk4"
|
||||
username="t1wk4",
|
||||
)
|
||||
|
||||
self.competition = Competition.objects.create(
|
||||
title="AI Challenge",
|
||||
description="Machine Learning Competition",
|
||||
type="solo",
|
||||
participation_type="edu"
|
||||
participation_type="edu",
|
||||
)
|
||||
|
||||
resp = self.client.post(
|
||||
@@ -29,9 +32,7 @@ class CompetitionEndpointTests(TestCase):
|
||||
).json()
|
||||
token = resp["token"]
|
||||
|
||||
self.valid_headers = {
|
||||
"HTTP_AUTHORIZATION": f"Bearer {token}"
|
||||
}
|
||||
self.valid_headers = {"HTTP_AUTHORIZATION": f"Bearer {token}"}
|
||||
|
||||
# --- Helper methods ---
|
||||
def get_url(self, competition_id):
|
||||
@@ -41,8 +42,7 @@ class CompetitionEndpointTests(TestCase):
|
||||
def test_get_competition_success(self):
|
||||
"""Authenticated user gets competition details (200 OK)"""
|
||||
response = self.client.get(
|
||||
self.get_url(self.competition.id),
|
||||
**self.valid_headers
|
||||
self.get_url(self.competition.id), **self.valid_headers
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
@@ -61,8 +61,7 @@ class CompetitionEndpointTests(TestCase):
|
||||
def test_invalid_uuid_format(self):
|
||||
"""Invalid UUID format returns 400 Bad Request"""
|
||||
response = self.client.get(
|
||||
self.get_url("invalid-id"),
|
||||
**self.valid_headers
|
||||
self.get_url("invalid-id"), **self.valid_headers
|
||||
)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
@@ -76,8 +75,7 @@ class CompetitionEndpointTests(TestCase):
|
||||
"""Valid UUID but missing competition returns 404"""
|
||||
new_uuid = uuid.uuid4()
|
||||
response = self.client.get(
|
||||
self.get_url(new_uuid),
|
||||
**self.valid_headers
|
||||
self.get_url(new_uuid), **self.valid_headers
|
||||
)
|
||||
self.assertEqual(response.status_code, 404)
|
||||
self.assertEqual(response.json()["detail"], "Not Found")
|
||||
@@ -86,7 +84,7 @@ class CompetitionEndpointTests(TestCase):
|
||||
"""Invalid token returns 401 Unauthorized"""
|
||||
response = self.client.get(
|
||||
self.get_url(self.competition.id),
|
||||
HTTP_AUTHORIZATION="Bearer invalid_token"
|
||||
HTTP_AUTHORIZATION="Bearer invalid_token",
|
||||
)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
self.assertEqual(response.json()["detail"], "Unauthorized")
|
||||
@@ -103,6 +101,161 @@ class CompetitionEndpointTests(TestCase):
|
||||
with self.subTest(header=header):
|
||||
response = self.client.get(
|
||||
self.get_url(self.competition.id),
|
||||
HTTP_AUTHORIZATION=header
|
||||
HTTP_AUTHORIZATION=header,
|
||||
)
|
||||
self.assertEqual(response.status_code, expected_status)
|
||||
|
||||
|
||||
class CompetitionsEndpointTests(TestCase):
|
||||
def setUp(self):
|
||||
self.user = User.objects.create(
|
||||
email="user@example.com",
|
||||
password=make_password("password123"),
|
||||
username="t1wk4"
|
||||
)
|
||||
|
||||
resp = self.client.post(
|
||||
"/api/v1/sign-in",
|
||||
data={"email": self.user.email, "password": "password123"},
|
||||
content_type="application/json",
|
||||
).json()
|
||||
token = resp["token"]
|
||||
|
||||
# Create test competitions
|
||||
now = datetime.now(tz=pytz.utc)
|
||||
self.competitions = []
|
||||
for i in range(1, 6):
|
||||
competition = Competition.objects.create(
|
||||
title=f"Competition {i}",
|
||||
description=f"Description {i}",
|
||||
type=Competition.CompetitionType.SOLO,
|
||||
participation_type=(
|
||||
Competition.CompetitionParticipationType.EDU if i % 2 == 0
|
||||
else Competition.CompetitionParticipationType.COMPETITIVE
|
||||
),
|
||||
start_date=(now + timedelta(days=i)).isoformat(),
|
||||
end_date=(now + timedelta(days=i + 7)).isoformat(),
|
||||
)
|
||||
if i <= 2:
|
||||
competition.participants.add(self.user)
|
||||
self.competitions.append(competition)
|
||||
|
||||
self.valid_headers = {
|
||||
"HTTP_AUTHORIZATION": f"Bearer {token}"
|
||||
}
|
||||
|
||||
def get_url(self, params=None):
|
||||
base_url = "/api/v1/competitions"
|
||||
return f"{base_url}?{params}" if params else base_url
|
||||
|
||||
def test_get_participating_competitions(self):
|
||||
"""Test filtering competitions where user is participating"""
|
||||
response = self.client.get(
|
||||
self.get_url("is_participating=true"),
|
||||
**self.valid_headers
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
data = response.json()
|
||||
self.assertEqual(len(data), 2)
|
||||
self.assertEqual(
|
||||
{item["id"] for item in data},
|
||||
{str(self.competitions[0].id), str(self.competitions[1].id)}
|
||||
)
|
||||
|
||||
def test_competition_type_values(self):
|
||||
"""Test competition type choices are respected"""
|
||||
response = self.client.get(
|
||||
self.get_url("is_participating=true"),
|
||||
**self.valid_headers
|
||||
)
|
||||
|
||||
for item in response.json():
|
||||
self.assertEqual(item["type"], "solo")
|
||||
|
||||
def test_participation_type_values(self):
|
||||
"""Test participation type alternates between edu/competitive"""
|
||||
response = self.client.get(
|
||||
self.get_url("is_participating=false"),
|
||||
**self.valid_headers
|
||||
)
|
||||
|
||||
types = [item["participation_type"] for item in response.json()]
|
||||
self.assertCountEqual(
|
||||
types,
|
||||
["competitive", "edu", "competitive"]
|
||||
)
|
||||
|
||||
def test_datetime_formatting(self):
|
||||
"""Test start/end date ISO formatting"""
|
||||
response = self.client.get(
|
||||
self.get_url("is_participating=true"),
|
||||
**self.valid_headers
|
||||
)
|
||||
|
||||
for item in response.json():
|
||||
if item["start_date"]:
|
||||
try:
|
||||
isoparse(item["start_date"])
|
||||
except ValueError:
|
||||
self.fail("Invalid start_date format")
|
||||
if item["end_date"]:
|
||||
try:
|
||||
isoparse(item["end_date"])
|
||||
except ValueError:
|
||||
self.fail("Invalid end_date format")
|
||||
|
||||
def test_competition_metadata(self):
|
||||
"""Test competition metadata fields"""
|
||||
response = self.client.get(
|
||||
self.get_url("is_participating=true"),
|
||||
**self.valid_headers
|
||||
)
|
||||
|
||||
item = response.json()[0]
|
||||
self.assertEqual(item["title"], "Competition 1")
|
||||
self.assertEqual(item["description"], "Description 1")
|
||||
self.assertEqual(item["type"], "solo")
|
||||
self.assertEqual(item["participation_type"], "competitive")
|
||||
|
||||
def test_verbose_name_consistency(self):
|
||||
"""Test model verbose names don't affect API schema"""
|
||||
response = self.client.get(
|
||||
self.get_url("is_participating=true"),
|
||||
**self.valid_headers
|
||||
)
|
||||
|
||||
item = response.json()[0]
|
||||
self.assertNotIn("название", item) # Russian verbose name
|
||||
self.assertIn("title", item) # Actual API field name
|
||||
|
||||
def test_null_dates_handling(self):
|
||||
"""Test competitions with null dates"""
|
||||
competition = Competition.objects.create(
|
||||
title="No Dates Competition",
|
||||
description="Test competition",
|
||||
type=Competition.CompetitionType.SOLO,
|
||||
participation_type=Competition.CompetitionParticipationType.EDU
|
||||
)
|
||||
|
||||
response = self.client.get(
|
||||
self.get_url("is_participating=false"),
|
||||
**self.valid_headers
|
||||
)
|
||||
|
||||
test_item = next(
|
||||
item for item in response.json()
|
||||
if item["id"] == str(competition.id)
|
||||
)
|
||||
self.assertIsNone(test_item["start_date"])
|
||||
self.assertIsNone(test_item["end_date"])
|
||||
|
||||
def test_participation_status_filtering(self):
|
||||
"""Test filtering by participation_type"""
|
||||
response = self.client.get(
|
||||
self.get_url("is_participating=false"),
|
||||
**self.valid_headers
|
||||
)
|
||||
|
||||
data = response.json()
|
||||
self.assertEqual(len(data), 3)
|
||||
|
||||
Reference in New Issue
Block a user