-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Add search functionality to items endpoint with tests #1715
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
Draft
yaswanthrajeev
wants to merge
3
commits into
fastapi:master
Choose a base branch
from
yaswanthrajeev:feature/items-search
base: master
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.
Draft
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,3 +162,102 @@ def test_delete_item_not_enough_permissions( | |
assert response.status_code == 400 | ||
content = response.json() | ||
assert content["detail"] == "Not enough permissions" | ||
def test_search_items_by_title( | ||
client: TestClient, superuser_token_headers: dict[str, str], db: Session | ||
) -> None: | ||
# Create test items using the existing function | ||
item1 = create_random_item(db) | ||
item2 = create_random_item(db) | ||
|
||
# Update the first item to have a specific title for testing | ||
item1.title = "Alpha Item" | ||
item1.description = "First item" | ||
db.add(item1) | ||
db.commit() | ||
|
||
# Test search by title | ||
response = client.get( | ||
f"{settings.API_V1_STR}/items/?search=Alpha", | ||
headers=superuser_token_headers, | ||
) | ||
assert response.status_code == 200 | ||
content = response.json() | ||
assert content["count"] == 1 | ||
assert content["data"][0]["title"] == "Alpha Item" | ||
|
||
|
||
def test_search_items_by_description( | ||
client: TestClient, superuser_token_headers: dict[str, str], db: Session | ||
) -> None: | ||
# Create test items | ||
item1 = create_random_item(db) | ||
item2 = create_random_item(db) | ||
|
||
# Update the first item to have a specific description for testing | ||
item1.title = "First" | ||
item1.description = "UniqueAlphaDescription" # More unique | ||
db.add(item1) | ||
db.commit() | ||
|
||
# Test search by description | ||
response = client.get( | ||
f"{settings.API_V1_STR}/items/?search=UniqueAlpha", # More specific search | ||
headers=superuser_token_headers, | ||
) | ||
assert response.status_code == 200 | ||
content = response.json() | ||
assert content["count"] == 1 | ||
assert content["data"][0]["description"] == "UniqueAlphaDescription" | ||
|
||
|
||
def test_search_items_no_results( | ||
client: TestClient, superuser_token_headers: dict[str, str], db: Session | ||
) -> None: | ||
# Create test items | ||
item = create_random_item(db) | ||
|
||
# Update item to have specific content | ||
item.title = "Alpha" | ||
item.description = "First item" | ||
db.add(item) | ||
db.commit() | ||
|
||
# Test search with no matches | ||
response = client.get( | ||
f"{settings.API_V1_STR}/items/?search=NotFound", | ||
headers=superuser_token_headers, | ||
) | ||
assert response.status_code == 200 | ||
content = response.json() | ||
assert content["count"] == 0 | ||
assert content["data"] == [] | ||
|
||
|
||
def test_search_items_with_pagination( | ||
client: TestClient, superuser_token_headers: dict[str, str], db: Session | ||
) -> None: | ||
# Create multiple test items | ||
item1 = create_random_item(db) | ||
item2 = create_random_item(db) | ||
item3 = create_random_item(db) | ||
|
||
# Update items to have specific content for testing | ||
item1.title = "Test Item 1" | ||
item1.description = "First test" | ||
item2.title = "Test Item 2" | ||
item2.description = "Second test" | ||
item3.title = "Other Item" | ||
item3.description = "Not matching" | ||
|
||
db.add_all([item1, item2, item3]) | ||
db.commit() | ||
|
||
# Test search with pagination | ||
response = client.get( | ||
f"{settings.API_V1_STR}/items/?search=Test&skip=0&limit=2", | ||
headers=superuser_token_headers, | ||
) | ||
assert response.status_code == 200 | ||
content = response.json() | ||
assert content["count"] == 2 | ||
assert len(content["data"]) == 2 | ||
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. Maybe also assert the titles of the returned items (e.g., "Test Item 1" and "Test Item 2") so the test is stronger and doesn’t rely on implicit ordering. |
Empty file.
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
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.
Maybe also assert that
item2
is NOT returned, just to be explicit about the filtering.