-
Notifications
You must be signed in to change notification settings - Fork 213
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
feat: enhanced the get product endpoint with category filter #1222
Open
PeterOyelegbin
wants to merge
10
commits into
hngprojects:dev
Choose a base branch
from
PeterOyelegbin:feature-add-filter-to-GET-/api/v1/products
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.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d3ffdd2
enhanced the get product endpoint with category filter
PeterOyelegbin 67392e2
updated test_get_product_filer
PeterOyelegbin 58c4bbe
updated test_get_product_filter
PeterOyelegbin a54a017
Merge pull request #1 from PeterOyelegbin/dev
PeterOyelegbin 0040c9e
updated product.py
PeterOyelegbin c1f27c5
Merge pull request #2 from PeterOyelegbin/dev
PeterOyelegbin 95c51d6
Merge branch 'dev' of https://github.com/PeterOyelegbin/hng12_stage4_…
PeterOyelegbin dbd86ae
fix repo conflict
PeterOyelegbin 6ecc2e2
fix repo conflict
PeterOyelegbin 226a599
Merge pull request #3 from PeterOyelegbin/dev
PeterOyelegbin 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 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,6 +1,6 @@ | ||
from typing import Any, Dict, List, Optional | ||
from fastapi.encoders import jsonable_encoder | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy.orm import Session, Query | ||
from api.db.database import Base | ||
|
||
from api.utils.success_response import success_response | ||
|
@@ -12,6 +12,7 @@ def paginated_response( | |
skip: int, | ||
limit: int, | ||
join: Optional[Any] = None, | ||
query: Optional[Query] = None, | ||
filters: Optional[Dict[str, Any]]=None | ||
): | ||
|
||
|
@@ -24,6 +25,7 @@ def paginated_response( | |
* skip- this is the number of items to skip before fetching the next page of data. This would also | ||
be a query parameter | ||
* join- this is an optional argument to join a table to the query | ||
* query- this is an optional custom query to use instead of querying all items from the model. | ||
* filters- this is an optional dictionary of filters to apply to the query | ||
|
||
Example use: | ||
|
@@ -61,7 +63,8 @@ def paginated_response( | |
``` | ||
''' | ||
|
||
query = db.query(model) | ||
if query is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If query is none, then it gets all products; |
||
query = db.query(model) | ||
|
||
if join is not None: | ||
query = query.join(join) | ||
|
@@ -82,7 +85,8 @@ def paginated_response( | |
|
||
total = query.count() | ||
results = jsonable_encoder(query.offset(skip).limit(limit).all()) | ||
total_pages = int(total / limit) + (total % limit > 0) | ||
# total_pages = int(total / limit) + (total % limit > 0) | ||
total_pages = (total + limit - 1) // limit | ||
|
||
return success_response( | ||
status_code=200, | ||
|
This file contains 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 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,209 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
from sqlalchemy.orm import Session | ||
from unittest.mock import MagicMock | ||
from uuid_extensions import uuid7 | ||
from datetime import datetime, timezone, timedelta | ||
|
||
from api.v1.models.organisation import Organisation | ||
from api.v1.models.product import Product, ProductCategory | ||
from api.v1.models.user import User | ||
from main import app | ||
from api.v1.routes.blog import get_db | ||
from api.v1.services.user import user_service | ||
|
||
|
||
# Mock database dependency | ||
@pytest.fixture | ||
def db_session_mock(): | ||
db_session = MagicMock(spec=Session) | ||
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 = {} | ||
|
||
|
||
# Mock user service dependency | ||
|
||
user_id = uuid7() | ||
org_id = uuid7() | ||
product_id = uuid7() | ||
category_id = uuid7() | ||
timezone_offset = -8.0 | ||
tzinfo = timezone(timedelta(hours=timezone_offset)) | ||
timeinfo = datetime.now(tzinfo) | ||
created_at = timeinfo | ||
updated_at = timeinfo | ||
access_token = user_service.create_access_token(str(user_id)) | ||
access_token2 = user_service.create_access_token(str(uuid7())) | ||
|
||
# Create test user | ||
|
||
user = User( | ||
id=str(user_id), | ||
email="[email protected]", | ||
password="password123", | ||
created_at=created_at, | ||
updated_at=updated_at, | ||
) | ||
|
||
# Create test organisation | ||
|
||
org = Organisation( | ||
id=str(org_id), | ||
name="hng", | ||
email=None, | ||
industry=None, | ||
type=None, | ||
country=None, | ||
state=None, | ||
address=None, | ||
description=None, | ||
created_at=created_at, | ||
updated_at=updated_at, | ||
) | ||
|
||
# Create test category | ||
|
||
category = ProductCategory(id=category_id, name="Electronics") | ||
|
||
# Create test product | ||
|
||
product = Product( | ||
id=str(product_id), | ||
name="prod one", | ||
description="Test product", | ||
price=125.55, | ||
org_id=str(org_id), | ||
quantity=50, | ||
image_url="http://img", | ||
category_id=str(category_id), | ||
status="in_stock", | ||
archived=False, | ||
) | ||
|
||
|
||
# Mock data for multiple products | ||
products = [ | ||
Product( | ||
id=str(uuid7()), | ||
name="Smartphone", | ||
description="A smartphone", | ||
price=500.00, | ||
org_id=str(org_id), | ||
quantity=10, | ||
image_url="http://img1", | ||
category_id=str(category_id), | ||
status="in_stock", | ||
archived=False, | ||
), | ||
Product( | ||
id=str(uuid7()), | ||
name="Laptop", | ||
description="A laptop", | ||
price=1200.00, | ||
org_id=str(org_id), | ||
quantity=5, | ||
image_url="http://img2", | ||
category_id=str(category_id), | ||
status="in_stock", | ||
archived=False, | ||
), | ||
Product( | ||
id=str(uuid7()), | ||
name="T-Shirt", | ||
description="A T-Shirt", | ||
price=20.00, | ||
org_id=str(org_id), | ||
quantity=100, | ||
image_url="http://img3", | ||
category_id=str(uuid7()), # Different category | ||
status="in_stock", | ||
archived=False, | ||
), | ||
] | ||
|
||
|
||
def test_get_products_filtered_by_category(client, db_session_mock): | ||
# Mock the database query to return filtered products | ||
db_session_mock.query().join().filter().offset().limit().all.return_value = [ | ||
products[0], products[1]] | ||
db_session_mock.query().join().filter().count.return_value = 2 # Return an integer | ||
|
||
headers = {"authorization": f"Bearer {access_token}"} | ||
response = client.get( | ||
"/api/v1/products?category=Electronics", | ||
headers=headers | ||
) | ||
|
||
assert response.status_code == 200 | ||
data = response.json() | ||
assert data["success"] is True | ||
assert len(data["data"]["items"]) == 2 | ||
|
||
|
||
def test_get_all_products_without_filter(client, db_session_mock): | ||
# Mock the database query to return all products | ||
db_session_mock.query().offset().limit().all.return_value = products | ||
db_session_mock.query().count.return_value = 3 | ||
|
||
headers = {"authorization": f"Bearer {access_token}"} | ||
response = client.get( | ||
"/api/v1/products", | ||
headers=headers | ||
) | ||
|
||
assert response.status_code == 200 | ||
data = response.json() | ||
assert data["success"] is True | ||
assert len(data["data"]["items"]) == 3 | ||
|
||
|
||
def test_unauthorized_access(client, db_session_mock): | ||
# Test unauthorized access (missing or invalid token) | ||
response = client.get("/api/v1/products") | ||
assert response.status_code == 401 | ||
assert response.json() == { | ||
"status": False, | ||
"status_code": 401, | ||
"message": "Not authenticated" | ||
} | ||
|
||
|
||
def test_invalid_category_name(client, db_session_mock): | ||
# Mock the database query to return no products for an invalid category | ||
db_session_mock.query().join().filter().offset().limit().all.return_value = [] | ||
db_session_mock.query().join().filter().count.return_value = 0 # Return an integer | ||
|
||
headers = {"authorization": f"Bearer {access_token}"} | ||
response = client.get( | ||
"/api/v1/products?category=InvalidCategory", | ||
headers=headers | ||
) | ||
|
||
assert response.status_code == 200 | ||
data = response.json() | ||
assert data["success"] is True | ||
assert len(data["data"]["items"]) == 0 | ||
|
||
|
||
def test_empty_results_for_valid_category(client, db_session_mock): | ||
# Mock the database query to return no products for a valid but unused category | ||
db_session_mock.query().join().filter().offset().limit().all.return_value = [] | ||
db_session_mock.query().join().filter().count.return_value = 0 # Return an integer | ||
|
||
headers = {"authorization": f"Bearer {access_token}"} | ||
response = client.get( | ||
"/api/v1/products?category=Furniture", | ||
headers=headers | ||
) | ||
|
||
assert response.status_code == 200 | ||
data = response.json() | ||
assert data["success"] is True | ||
assert len(data["data"]["items"]) == 0 |
Oops, something went wrong.
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.
when you set this condition what happens is query is not None?
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.
Ohkay, your test fails