Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alukach committed Sep 4, 2024
1 parent eb9f91f commit d08b6c7
Show file tree
Hide file tree
Showing 6 changed files with 490 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ jobs:
DB_NAME: mydatabase
DB_USER: myuser
DB_PASSWORD: mypassword
DB_TABLE_NAME: space2stats
DB_TABLE_NAME: space2stats
S3_BUCKET_NAME: test-bucket
439 changes: 438 additions & 1 deletion space2stats_api/src/poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions space2stats_api/src/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typing_extensions = "*"
starlette-cramjam = ">=0.3,<0.4"
mangum = "*"
asgi-s3-response-middleware = "^0.0.1"
boto3 = "^1.35.11"

[tool.poetry.group.lambda.dependencies]
mangum = "*"
Expand All @@ -32,6 +33,7 @@ pre-commit = "*"
pytest = "*"
pytest-cov = "*"
pytest-postgresql = "*"
moto = "^5.0.13"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
2 changes: 1 addition & 1 deletion space2stats_api/src/space2stats/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def lifespan(app: FastAPI):
app.add_middleware(CompressionMiddleware)
app.add_middleware(
S3ResponseMiddleware,
bucket_name=settings.S3_BUCKET_NAME,
s3_bucket_name=settings.S3_BUCKET_NAME,
s3_client=s3_client,
)

Expand Down
37 changes: 37 additions & 0 deletions space2stats_api/src/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os

import boto3
import pytest
from moto import mock_aws


@pytest.fixture()
def aws_credentials():
"""Mocked AWS Credentials for moto."""
os.environ["AWS_ACCESS_KEY_ID"] = "testing"
os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
os.environ["AWS_SECURITY_TOKEN"] = "testing"
os.environ["AWS_SESSION_TOKEN"] = "testing"
os.environ["AWS_DEFAULT_REGION"] = "us-east-1"


@pytest.fixture()
def s3_client(aws_credentials):
"""
Return a mocked S3 client
"""
with mock_aws():
yield boto3.client("s3", region_name="us-east-1")


@pytest.fixture
def test_bucket(s3_client) -> str:
bucket_name = "test-bucket"
s3_client.create_bucket(Bucket=bucket_name)

return bucket_name


@pytest.fixture(autouse=True)
def set_bucket_name(monkeypatch, test_bucket):
monkeypatch.setenv("S3_BUCKET_NAME", test_bucket)
17 changes: 10 additions & 7 deletions space2stats_api/src/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"properties": {},
}


@pytest.fixture(scope="session")
def database(postgresql_proc):
"""Fake Database."""
Expand All @@ -38,7 +39,7 @@ def database(postgresql_proc):


@pytest.fixture(autouse=True)
def client(monkeypatch, database):
def client(monkeypatch, database, test_bucket):
monkeypatch.setenv("DB_HOST", database.host)
monkeypatch.setenv("DB_PORT", str(database.port))
monkeypatch.setenv("DB_NAME", database.dbname)
Expand All @@ -62,7 +63,7 @@ def test_read_root(client):
def test_get_summary(mock_get_summaries, client):
mock_get_summaries.return_value = (
[("hex_1", 100, 200)],
["hex_id", "sum_pop_2020", "sum_pop_f_10_2020"]
["hex_id", "sum_pop_2020", "sum_pop_f_10_2020"],
)

request_payload = {
Expand All @@ -89,7 +90,7 @@ def test_get_summary(mock_get_summaries, client):
def test_get_summary_with_geometry_polygon(mock_get_summaries, client):
mock_get_summaries.return_value = (
[("hex_1", 100, 200)],
["hex_id", "sum_pop_2020", "sum_pop_f_10_2020"]
["hex_id", "sum_pop_2020", "sum_pop_f_10_2020"],
)

request_payload = {
Expand Down Expand Up @@ -119,7 +120,7 @@ def test_get_summary_with_geometry_polygon(mock_get_summaries, client):
def test_get_summary_with_geometry_point(mock_get_summaries, client):
mock_get_summaries.return_value = (
[("hex_1", 100, 200)],
["hex_id", "sum_pop_2020", "sum_pop_f_10_2020"]
["hex_id", "sum_pop_2020", "sum_pop_f_10_2020"],
)

request_payload = {
Expand Down Expand Up @@ -147,9 +148,11 @@ def test_get_summary_with_geometry_point(mock_get_summaries, client):

@patch("space2stats.app.get_available_fields")
def test_get_fields(mock_get_available_fields, client):
mock_get_available_fields.return_value = ["sum_pop_2020",
"sum_pop_f_10_2020",
"field3"]
mock_get_available_fields.return_value = [
"sum_pop_2020",
"sum_pop_f_10_2020",
"field3",
]

response = client.get("/fields")

Expand Down

0 comments on commit d08b6c7

Please sign in to comment.