-
Notifications
You must be signed in to change notification settings - Fork 217
feat: add testimonial update endpoint #1216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Samuelhetty
wants to merge
16
commits into
hngprojects:dev
Choose a base branch
from
Samuelhetty:feat/Testimonials_update
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−4
Open
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2ef6744
feat: added testimonial update endpoint and schema
2d9e106
fix(tests): Ensure updated content is retrieved correctly in testimon…
99f4ece
feat: Created TestimonialService update method
280a9dc
fix(tests): Ensure mock user attributes are valid in testimonial upda…
Samuelhetty 28dcdc4
Merge branch 'dev' into feat/Testimonials_update
Samuelhetty 7d816a8
Improved test reliability and consistency with dependency overrides
Samuelhetty 70b5cfd
Merge branch 'dev' into feat/Testimonials_update
Samuelhetty d941dd6
Merge branch 'dev' into feat/Testimonials_update
Samuelhetty 5fbd5ca
removed database url from alembic ini
Samuelhetty 85ed8ac
Removed venv from repo
Samuelhetty ae54baf
Removed venv from repo and added to .gitignore
Samuelhetty 7dcb4da
Merge branch 'dev' into feat/Testimonials_update
Samuelhetty 5da7cbe
Merge branch 'dev' into feat/Testimonials_update
Samuelhetty c7776fc
reverted alembic ini file
Samuelhetty 61b6772
Moved line 48-50 outside the loop
Samuelhetty fe95f8b
Merge branch 'dev' into feat/Testimonials_update
Samuelhetty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| from pydantic import BaseModel | ||
| from typing import Optional | ||
|
|
||
| class CreateTestimonial(BaseModel): | ||
| content: str | ||
| ratings: float = 0 | ||
| ratings: float = 0 | ||
|
|
||
|
|
||
| class UpdateTestimonial(BaseModel): | ||
| content: Optional[str] = None | ||
| ratings: Optional[float] = None | ||
| client_name: Optional[str] = None | ||
| client_designation: Optional[str] = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
| from api.v1.models.testimonial import Testimonial | ||
| from api.v1.models.user import User | ||
| from api.v1.schemas.testimonial import CreateTestimonial | ||
| from api.v1.schemas.testimonial import UpdateTestimonial | ||
|
|
||
|
|
||
|
|
||
| class TestimonialService(Service): | ||
|
|
@@ -34,9 +36,19 @@ def fetch(self, db: Session, id: str): | |
|
|
||
| return check_model_existence(db, Testimonial, id) | ||
|
|
||
| def update(self, db: Session, id: str, schema): | ||
| def update(self, db: Session, id: str, schema: UpdateTestimonial): | ||
| """Updates a testimonial""" | ||
| pass | ||
| testimonial = self.fetch(db, id) | ||
| if not testimonial: | ||
| return None # Or raise an HTTPException(status_code=404, detail="Testimonial not found") | ||
| # Update the fields | ||
| for key, value in schema.dict(exclude_unset=True).items(): | ||
| setattr(testimonial, key, value) | ||
|
|
||
| db.commit() | ||
| db.refresh(testimonial) | ||
| return testimonial | ||
|
||
|
|
||
|
|
||
| def delete(self, db: Session, id: str): | ||
| """Deletes a specific testimonial""" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
| from unittest.mock import MagicMock, patch | ||
| from uuid_extensions import uuid7 | ||
| from datetime import datetime, timezone | ||
| from faker import Faker | ||
| from main import app | ||
| from api.db.database import get_db | ||
| from api.v1.models.user import User | ||
| from api.v1.models.testimonial import Testimonial | ||
| from api.v1.services.user import user_service | ||
| from fastapi import status | ||
|
|
||
| fake = Faker() | ||
| client = TestClient(app) | ||
|
|
||
| # Fixtures | ||
| @pytest.fixture | ||
| def db_session_mock(): | ||
| db_session = MagicMock() | ||
| return db_session | ||
|
|
||
| @pytest.fixture | ||
| def client(db_session_mock): | ||
| app.dependency_overrides[get_db] = lambda: db_session_mock | ||
| client = TestClient(app) | ||
| yield client | ||
| app.dependency_overrides = {} | ||
|
|
||
|
|
||
| def mock_get_current_user(): | ||
| return User( | ||
| id=str(uuid7()), | ||
| email=fake.email(), | ||
| password=user_service.hash_password("Testpassword@123"), | ||
| first_name="Test", | ||
| last_name="User", | ||
| is_active=True, | ||
| is_superadmin=False, | ||
| created_at=datetime.now(timezone.utc), | ||
| updated_at=datetime.now(timezone.utc) | ||
| ) | ||
|
|
||
| def mock_testimonial(user_id): | ||
| return Testimonial( | ||
| id=str(uuid7()), | ||
| content="Original content", | ||
| author_id=user_id, | ||
| client_name="Client 1", | ||
| client_designation="Client Designation", | ||
| comments="Testimonial comments", | ||
| ratings=4.5 | ||
| ) | ||
|
|
||
|
|
||
| def test_update_testimonial_success(client, db_session_mock): | ||
| '''Test successful update of a testimonial''' | ||
|
|
||
| mock_user = mock_get_current_user() | ||
| app.dependency_overrides[user_service.get_current_user] = lambda: mock_user | ||
|
|
||
| mock_testimonial_obj = mock_testimonial(mock_user.id) | ||
| db_session_mock.get.return_value = mock_testimonial_obj | ||
|
|
||
| update_data = {"content": "Updated content"} | ||
| response = client.put( | ||
| f'/api/v1/testimonials/{mock_testimonial_obj.id}', | ||
| json=update_data, | ||
| headers={'Authorization': 'Bearer token'} | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.json()["message"] == "Testimonial updated successfully" | ||
|
|
||
|
|
||
| def test_update_testimonial_not_found(client, db_session_mock): | ||
| '''Test updating a non-existing testimonial''' | ||
|
|
||
| app.dependency_overrides[user_service.get_current_user] = lambda: mock_get_current_user() | ||
|
|
||
| db_session_mock.get.return_value = None | ||
|
|
||
| update_data = {"content": "Updated content"} | ||
| testimonial_id = str(uuid7()) | ||
| response = client.put( | ||
| f'/api/v1/testimonials/{testimonial_id}', | ||
| json=update_data, | ||
| headers={'Authorization': 'Bearer token'} | ||
| ) | ||
|
|
||
| assert response.status_code == 404 | ||
| assert response.json()["message"] in ["Testimonial not found", "Testimonial does not exist"] | ||
|
|
||
| def test_update_testimonial_unauthorized(client): | ||
| '''Test updating a testimonial without authentication''' | ||
|
|
||
| testimonial_id = str(uuid7()) | ||
| update_data = {"content": "Updated content"} | ||
|
|
||
| response = client.put(f'/api/v1/testimonials/{testimonial_id}', json=update_data) | ||
|
|
||
| assert response.status_code == 401 | ||
| assert response.json()["message"] == "Not authenticated" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you touch this line of code?
How is it related to your issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It gave some errors while testing and a mentor said i could adjust it. Reverted back now.