Skip to content
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

fix: fix transaction unit test #427

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions stac_api/runtime/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import pytest
from httpx import ASGITransport, AsyncClient

from stac_fastapi.pgstac.db import close_db_connection, connect_to_db

VALID_COLLECTION = {
"id": "CMIP245-winter-median-pr",
"type": "Collection",
Expand Down Expand Up @@ -208,7 +210,7 @@
}


@pytest.fixture
@pytest.fixture(autouse=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using autouse=True will make sure the fixture is ran for each test

def test_environ():
"""
Set up the test environment with mocked AWS and PostgreSQL credentials.
Expand Down Expand Up @@ -250,7 +252,7 @@ def override_validated_token():


@pytest.fixture
def app(test_environ):
async def app():
"""
Fixture to initialize the FastAPI application.

Expand All @@ -265,7 +267,9 @@ def app(test_environ):
"""
from src.app import app

return app
await connect_to_db(app)
yield app
await close_db_connection(app)


@pytest.fixture(scope="function")
Expand Down
24 changes: 12 additions & 12 deletions stac_api/runtime/tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,59 +53,59 @@ def setup(
self.invalid_stac_collection = invalid_stac_collection
self.invalid_stac_item = invalid_stac_item

def test_post_invalid_collection(self):
async def test_post_invalid_collection(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function weren't async while the api_client fixture is async

"""
Test the API's response to posting an invalid STAC collection.

Asserts that the response status code is 422 and the detail
is "Validation Error".
"""
response = self.api_client.post(
response = await self.api_client.post(
collections_endpoint, json=self.invalid_stac_collection
)
assert response.json()["detail"] == "Validation Error"
assert response.status_code == 422

def test_post_valid_collection(self):
async def test_post_valid_collection(self):
"""
Test the API's response to posting a valid STAC collection.

Asserts that the response status code is 200.
"""
response = self.api_client.post(
response = await self.api_client.post(
collections_endpoint, json=self.valid_stac_collection
)
# assert response.json() == {}
assert response.status_code == 200

def test_post_invalid_item(self):
async def test_post_invalid_item(self):
"""
Test the API's response to posting an invalid STAC item.

Asserts that the response status code is 422 and the detail
is "Validation Error".
"""
response = self.api_client.post(
response = await self.api_client.post(
items_endpoint.format(self.invalid_stac_item["collection"]),
json=self.invalid_stac_item,
)
assert response.json()["detail"] == "Validation Error"
assert response.status_code == 422

def test_post_valid_item(self):
async def test_post_valid_item(self):
"""
Test the API's response to posting a valid STAC item.

Asserts that the response status code is 200.
"""
response = self.api_client.post(
response = await self.api_client.post(
items_endpoint.format(self.valid_stac_item["collection"]),
json=self.valid_stac_item,
)
# assert response.json() == {}
assert response.status_code == 200

def test_post_invalid_bulk_items(self):
async def test_post_invalid_bulk_items(self):
"""
Test the API's response to posting invalid bulk STAC items.

Expand All @@ -117,12 +117,12 @@ def test_post_invalid_bulk_items(self):
"items": {item_id: self.invalid_stac_item},
"method": "upsert",
}
response = self.api_client.post(
response = await self.api_client.post(
bulk_endpoint.format(collection_id), json=invalid_request
)
assert response.status_code == 422

def test_post_valid_bulk_items(self):
async def test_post_valid_bulk_items(self):
"""
Test the API's response to posting valid bulk STAC items.

Expand All @@ -131,7 +131,7 @@ def test_post_valid_bulk_items(self):
item_id = self.valid_stac_item["id"]
collection_id = self.valid_stac_item["collection"]
valid_request = {"items": {item_id: self.valid_stac_item}, "method": "upsert"}
response = self.api_client.post(
response = await self.api_client.post(
bulk_endpoint.format(collection_id), json=valid_request
)
assert response.status_code == 200