diff --git a/backend/app/core/db.py b/backend/app/core/db.py index d5337b4..b25f9c1 100644 --- a/backend/app/core/db.py +++ b/backend/app/core/db.py @@ -135,6 +135,7 @@ def init_db(session: Session) -> None: if not team: team_in = TeamCreate( theme_name="Laaiend vuur 熾熱的火 🔥", + short_name="1", event_id=event.id, ) team = Team.create(session=session, create_obj=team_in) diff --git a/backend/app/models/mixin.py b/backend/app/models/mixin.py index 93029de..c0b38c8 100644 --- a/backend/app/models/mixin.py +++ b/backend/app/models/mixin.py @@ -25,6 +25,14 @@ class ThemeNameUpdate(ThemeName): theme_name: str | None = Field(default=None, max_length=255) +class ShortName(BaseModel): + short_name: str = Field(index=True, max_length=8) + + +class ShortNameUpdate(ShortName): + short_name: str | None = Field(default=None, max_length=8) + + class Contact(BaseModel): contact: str | None = Field(default=None, nullable=True, max_length=255) diff --git a/backend/app/models/team.py b/backend/app/models/team.py index 9710550..ac56896 100644 --- a/backend/app/models/team.py +++ b/backend/app/models/team.py @@ -20,6 +20,7 @@ if TYPE_CHECKING: class TeamBase( mixin.ThemeName, + mixin.ShortName, mixin.CheckInCheckOut, mixin.Canceled, BaseSQLModel @@ -38,7 +39,7 @@ class TeamCreate(TeamBase): # Properties to receive via API on update, all are optional -class TeamUpdate(mixin.ThemeNameUpdate, TeamBase): +class TeamUpdate(mixin.ThemeNameUpdate, mixin.ShortNameUpdate, TeamBase): event_id: RowId | None = Field(default=None) diff --git a/backend/app/tests/api/routes/test_teams.py b/backend/app/tests/api/routes/test_teams.py index 46d5165..c55b6c1 100644 --- a/backend/app/tests/api/routes/test_teams.py +++ b/backend/app/tests/api/routes/test_teams.py @@ -17,6 +17,7 @@ def test_create_team(client: TestClient, superuser_token_headers: dict[str, str] event = create_random_event(db) data = { "theme_name": "Foo", + "short_name": "1", "event_id": str(event.id), } response = client.post( @@ -27,12 +28,14 @@ def test_create_team(client: TestClient, superuser_token_headers: dict[str, str] assert response.status_code == status.HTTP_200_OK content = response.json() assert content["theme_name"] == data["theme_name"] + assert content["short_name"] == data["short_name"] assert content["event_id"] == str(event.id) assert "id" in content def test_create_team_without_event(client: TestClient, superuser_token_headers: dict[str, str]) -> None: data = { "theme_name": "No Event Team", + "short_name": "2", } response = client.post( f"{settings.API_V1_STR}/teams/", @@ -46,6 +49,7 @@ def test_create_team_without_event(client: TestClient, superuser_token_headers: def test_create_team_with_incorrect_event(client: TestClient, superuser_token_headers: dict[str, str]) -> None: data = { "theme_name": "No Event Team", + "short_name": "3", "event_id": str(uuid.uuid4()), # Non-existent event } response = client.post( @@ -63,6 +67,7 @@ def test_create_team_with_normal_user( event = create_random_event(db) data = { "theme_name": "Normal user", + "short_name": "4", "event_id": str(event.id), } response = client.post( @@ -80,6 +85,7 @@ def test_create_team_with_event_user( event = event_user_token_headers.event data = { "theme_name": "Event user", + "short_name": "5", "event_id": str(event.id), } response = client.post( @@ -90,6 +96,7 @@ def test_create_team_with_event_user( assert response.status_code == status.HTTP_200_OK content = response.json() assert content["theme_name"] == data["theme_name"] + assert content["short_name"] == data["short_name"] assert content["event_id"] == str(event.id) @@ -99,6 +106,7 @@ def test_create_team_for_event_user( event = create_random_event(db) data = { "theme_name": "Other event user", + "short_name": "6", "event_id": str(event.id), } response = client.post( @@ -120,6 +128,7 @@ def test_read_team(client: TestClient, superuser_token_headers: dict[str, str], content = response.json() assert content["id"] == str(team.id) assert content["theme_name"] == team.theme_name + assert content["short_name"] == team.short_name assert content["event_id"] == str(team.event_id) def test_read_team_not_found(client: TestClient, superuser_token_headers: dict[str, str]) -> None: @@ -153,6 +162,7 @@ def test_read_team_with_event_user(client: TestClient, event_user_token_headers: content = response.json() assert content["id"] == str(team.id) assert content["theme_name"] == team.theme_name + assert content["short_name"] == team.short_name assert content["event_id"] == str(event_user_token_headers.event.id) @@ -230,7 +240,10 @@ def test_read_teams_with_event_user_team_manager(client: TestClient, db: Session def test_update_team_name(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None: team = create_random_team(db) - data = {"theme_name": "Updated Team Name"} + data = { + "theme_name": "Updated Team Name", + "short_name": "7", + } response = client.put( f"{settings.API_V1_STR}/teams/{team.id}", headers=superuser_token_headers, @@ -240,11 +253,15 @@ def test_update_team_name(client: TestClient, superuser_token_headers: dict[str, content = response.json() assert content["id"] == str(team.id) assert content["theme_name"] == data["theme_name"] + assert content["short_name"] == data["short_name"] assert content["event_id"] == str(team.event_id) def test_update_team_not_found(client: TestClient, superuser_token_headers: dict[str, str]) -> None: - data = {"theme_name": "Non-existent team"} + data = { + "theme_name": "Non-existent team", + "short_name": "8", + } response = client.put( f"{settings.API_V1_STR}/teams/{uuid.uuid4()}", headers=superuser_token_headers, @@ -256,7 +273,10 @@ def test_update_team_not_found(client: TestClient, superuser_token_headers: dict def test_update_team_not_enough_permissions(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None: team = create_random_team(db) - data = {"theme_name": "Not enough permissions team"} + data = { + "theme_name": "Not enough permissions team", + "short_name": "9", + } response = client.put( f"{settings.API_V1_STR}/teams/{team.id}", headers=normal_user_token_headers, @@ -268,7 +288,10 @@ def test_update_team_not_enough_permissions(client: TestClient, normal_user_toke def test_update_team_name_with_event_permissions(client: TestClient, event_user_token_headers: EventUserHeader, db: Session) -> None: team = create_random_team(db, event=event_user_token_headers.event) - data = {"theme_name": "Updated Team Name with Event permissions"} + data = { + "theme_name": "Updated Team Name with Event permissions", + "short_name": "10", + } response = client.put( f"{settings.API_V1_STR}/teams/{team.id}", headers=event_user_token_headers.headers, @@ -278,6 +301,7 @@ def test_update_team_name_with_event_permissions(client: TestClient, event_user_ content = response.json() assert content["id"] == str(team.id) assert content["theme_name"] == data["theme_name"] + assert content["short_name"] == data["short_name"] assert content["event_id"] == str(event_user_token_headers.event.id) @@ -328,6 +352,7 @@ def test_update_team_event_with_event_user(client: TestClient, event_user_token_ content = response.json() assert content["id"] == str(team.id) assert content["theme_name"] == team.theme_name + assert content["short_name"] == team.short_name assert content["event_id"] == str(new_event.id) diff --git a/backend/app/tests/utils/team.py b/backend/app/tests/utils/team.py index 4b8a916..55ca922 100644 --- a/backend/app/tests/utils/team.py +++ b/backend/app/tests/utils/team.py @@ -1,3 +1,5 @@ +import random + from sqlmodel import Session from app.models.event import Event @@ -7,12 +9,16 @@ from app.tests.utils.event import create_random_event from app.tests.utils.utils import random_lower_string +def random_short_name() -> str: + return str(random.Random().randrange(1, 200)) + def create_random_team(db: Session, event: Event | None = None) -> Team: name = random_lower_string() + short_name = random_short_name() if not event: event = create_random_event(db) - team_in = TeamCreate(theme_name=name, event_id=event.id) + team_in = TeamCreate(theme_name=name, short_name=short_name, event_id=event.id) team = Team.create(session=db, create_obj=team_in) return team