From 4c5d193b3c2ae290bd1a014d98eca9fcb031024c Mon Sep 17 00:00:00 2001 From: Alexis Feron Date: Mon, 26 May 2025 22:27:02 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20an=20optional=20date=20to=20p?= =?UTF-8?q?ins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/dto/pin.py | 6 ++- app/models/pin.py | 4 +- app/serializers/pin_serializer.py | 3 +- requirements.txt | 2 +- tests/test_pins.py | 63 +++++++++++++++++++++++++++---- 5 files changed, 65 insertions(+), 13 deletions(-) diff --git a/app/dto/pin.py b/app/dto/pin.py index ba129d6..e0c20cc 100644 --- a/app/dto/pin.py +++ b/app/dto/pin.py @@ -1,7 +1,8 @@ from pydantic import BaseModel, Field, field_validator -from typing import List +from typing import List, Optional from bson import ObjectId import bson +from datetime import datetime class PinDTO(BaseModel): title: str @@ -9,6 +10,7 @@ class PinDTO(BaseModel): location: list files: List[str] = Field(default_factory=list) user_id: str = None + date: Optional[datetime] = None @field_validator('files') @classmethod @@ -19,6 +21,6 @@ class PinDTO(BaseModel): except bson.errors.InvalidId: raise ValueError(f"Invalid image ID format: {file_id}") return files - + class PinShareDTO(BaseModel): friend_id: str \ No newline at end of file diff --git a/app/models/pin.py b/app/models/pin.py index 9bfdadb..01a748d 100644 --- a/app/models/pin.py +++ b/app/models/pin.py @@ -1,5 +1,6 @@ from typing import Optional, List from pydantic import BaseModel +from datetime import datetime class Pin(BaseModel): id: Optional[str] @@ -7,4 +8,5 @@ class Pin(BaseModel): description: str location: list files: Optional[List[str]] = [] # Liste des IDs d'images - user_id: str \ No newline at end of file + user_id: str + date: Optional[datetime] = None \ No newline at end of file diff --git a/app/serializers/pin_serializer.py b/app/serializers/pin_serializer.py index bd6473d..e18d949 100644 --- a/app/serializers/pin_serializer.py +++ b/app/serializers/pin_serializer.py @@ -8,7 +8,8 @@ def pin_serialize(pin: list) -> Pin: "description": pin["description"], "location": pin["location"], "files": pin["files"], - "user_id": pin["user_id"] + "user_id": pin["user_id"], + "date": pin.get("date") }) def pins_serialize(pins: list) -> list: diff --git a/requirements.txt b/requirements.txt index 9502140..705264d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,5 +4,5 @@ pymongo==4.10.1 uvicorn==0.32.0 joserfc==1.0.1 python-multipart==0.0.9 -pillow==10.2.0 +pillow==11.2.1 python-magic==0.4.27 \ No newline at end of file diff --git a/tests/test_pins.py b/tests/test_pins.py index 2d9c94a..5bbfbbd 100644 --- a/tests/test_pins.py +++ b/tests/test_pins.py @@ -19,27 +19,37 @@ def add_test_image(token): ) return response.json()["id"] -def create_test_pin(token, user_id, image_id=None): +def create_test_pin(token, user_id, image_id=None, with_date=False): if image_id is None: image_id = add_test_image(token) + pin_data = { + "title": "Test Pin", + "description": "Test Description", + "location": [0,0], + "files": [image_id], + "user_id": user_id + } + + if with_date: + pin_data["date"] = "2024-03-20T12:00:00" + response = client.post( "/pin/add", - json={ - "title": "Test Pin", - "description": "Test Description", - "location": [0,0], - "files": [image_id], - "user_id": user_id - }, + json=pin_data, headers={"Authorization": f"Bearer {token}"} ) return response.json()["id"] def test_add_pin(token, user_id): + # Test sans date image_id = add_test_image(token) pin_id = create_test_pin(token, user_id, image_id) assert pin_id is not None + + # Test avec date + pin_id_with_date = create_test_pin(token, user_id, image_id, with_date=True) + assert pin_id_with_date is not None def test_add_pin_invalid_files_format(token, user_id): response = client.post( @@ -63,6 +73,7 @@ def test_list_pins(token): assert isinstance(data, list) def test_get_pin(token, user_id): + # Test sans date image_id = add_test_image(token) pin_id = create_test_pin(token, user_id, image_id) @@ -76,6 +87,17 @@ def test_get_pin(token, user_id): assert data["description"] == "Test Description" assert data["location"] == [0,0] assert image_id in data["files"] + assert "date" not in data + + # Test avec date + pin_id_with_date = create_test_pin(token, user_id, image_id, with_date=True) + response = client.get( + f"/pin/{pin_id_with_date}", + headers={"Authorization": f"Bearer {token}"} + ) + assert response.status_code == 200 + data = response.json() + assert data["date"] == "2024-03-20T12:00:00" def test_get_pin_wrong_format(token): response = client.get(f"/pin/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"}) @@ -89,6 +111,7 @@ def test_update_pin(token, user_id): image_id = add_test_image(token) pin_id = create_test_pin(token, user_id, image_id) + # Test mise à jour sans date response = client.patch( f"/pin/{pin_id}", json={ @@ -111,6 +134,30 @@ def test_update_pin(token, user_id): assert data["description"] == "Updated Description" assert data["location"] == [1,1] assert image_id in data["files"] + assert "date" not in data + + # Test mise à jour avec date + response = client.patch( + f"/pin/{pin_id}", + json={ + "title": "Updated Pin With Date", + "description": "Updated Description", + "location": [1,1], + "files": [image_id], + "user_id": user_id, + "date": "2024-03-21T12:00:00" + }, + headers={"Authorization": f"Bearer {token}"} + ) + assert response.status_code == 200 + + get_response = client.get( + f"/pin/{pin_id}", + headers={"Authorization": f"Bearer {token}"} + ) + data = get_response.json() + assert data["title"] == "Updated Pin With Date" + assert data["date"] == "2024-03-21T12:00:00" def test_update_wrong_format(token): response = client.get(f"/pin/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"})