Skip to content

Commit

Permalink
Added API tests (#132)
Browse files Browse the repository at this point in the history
* added test container

* updated init.sql, added tests for addresses and seismic

* updated env file ci workflow

* updated test db url

* added soft story tests

* added all api tests

* changed the order of coordinates

* update ci tests
  • Loading branch information
agennadi authored Dec 24, 2024
1 parent 84f6e80 commit c76f2b5
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 244 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
run: docker compose up -d

- name: Run Backend Tests
run: docker compose run backend pytest backend/database/tests
run: docker compose run backend pytest backend

- name: Run Frontend Tests
run: docker compose run frontend npm test
Expand Down
8 changes: 4 additions & 4 deletions backend/api/models/landslide_zones.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class LandslideZone(Base):
identifier: Mapped[int] = mapped_column(Integer, primary_key=True)
geometry: Mapped[Geometry] = mapped_column(Geometry("MULTIPOLYGON", srid=4326))
gridcode: Mapped[int] = mapped_column(Integer)
sum_shape: Mapped[str] = mapped_column(Float)
shape_length: Mapped[str] = mapped_column(Float)
shape_length_1: Mapped[str] = mapped_column(Float)
shape_area: Mapped[str] = mapped_column(Float)
sum_shape: Mapped[float] = mapped_column(Float)
shape_length: Mapped[float] = mapped_column(Float)
shape_length_1: Mapped[float] = mapped_column(Float)
shape_area: Mapped[float] = mapped_column(Float)
update_timestamp: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
Expand Down
2 changes: 1 addition & 1 deletion backend/api/models/tsunami.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TsunamiZone(Base):
identifier: Mapped[int] = mapped_column(Integer, primary_key=True)
evacuate: Mapped[str] = mapped_column(String(_STRING_LENGTH), nullable=False)
county: Mapped[str] = mapped_column(String(_STRING_LENGTH), nullable=False)
globalID: Mapped[str] = mapped_column(String(_STRING_LENGTH), nullable=False)
global_id: Mapped[str] = mapped_column(String(_STRING_LENGTH), nullable=False)
shape_length: Mapped[float] = mapped_column(Float, nullable=True)
shape_area: Mapped[float] = mapped_column(Float, nullable=True)
geometry: Mapped[Geometry] = mapped_column(
Expand Down
2 changes: 1 addition & 1 deletion backend/api/routers/landslide_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from backend.api.models.landslide_zones import LandslideZone

router = APIRouter(
prefix="/landslides",
prefix="/landslide-zones",
tags=[Tags.LANDSLIDE],
)

Expand Down
2 changes: 1 addition & 1 deletion backend/api/routers/tsunami_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


router = APIRouter(
prefix="/tsunami",
prefix="/tsunami-zones",
tags=[Tags.TSUNAMI],
)

Expand Down
2 changes: 1 addition & 1 deletion backend/api/tests/test_addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def test_get_addresses(client):
response = client.get(f"/addresses/")
response_dict = response.json()
assert response.status_code == 200
assert len(response_dict) == 2
assert len(response_dict["features"]) == 2
49 changes: 0 additions & 49 deletions backend/api/tests/test_combined_risks.py

This file was deleted.

10 changes: 10 additions & 0 deletions backend/api/tests/test_landslide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest
from backend.api.tests.test_session_config import test_engine, test_session, client


def test_get_landslide_zones(client):
response = client.get(f"/landslide-zones/")
response_dict = response.json()
print(response_dict["features"])
assert response.status_code == 200
assert len(response_dict["features"]) == 2
9 changes: 9 additions & 0 deletions backend/api/tests/test_liquefaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest
from backend.api.tests.test_session_config import test_engine, test_session, client


def test_get_liquefaction_zones(client):
response = client.get(f"/liquefaction-zones/")
response_dict = response.json()
assert response.status_code == 200
assert len(response_dict["features"]) == 3
53 changes: 0 additions & 53 deletions backend/api/tests/test_polygons.py

This file was deleted.

3 changes: 1 addition & 2 deletions backend/api/tests/test_seismic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
def test_get_seismic_hazard_zones(client):
response = client.get(f"/seismic-zones/")
response_dict = response.json()
print(response_dict)
assert response.status_code == 200
assert len(response_dict) == 2
assert len(response_dict["features"]) == 2
44 changes: 5 additions & 39 deletions backend/api/tests/test_soft_story.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
"""
Test the API of soft_story.py.
"""

import pytest
from fastapi.testclient import TestClient

# Will the .. be stable?
from ..main import app
from ..schemas.geo import Polygon


@pytest.fixture
def client():
return TestClient(app)


def test_delete_soft_story(client):
response = client.delete("/api/soft-story/address")
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False


def test_put_soft_story(client):
response = client.put("/api/soft-story/address?soft-story=true")
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False


def test_post_soft_story(client):
response = client.put("/api/soft-story/address?soft-story=true")
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False
from backend.api.tests.test_session_config import test_engine, test_session, client


def test_get_soft_story(client):
response = client.get("/api/soft-story/address")
def test_get_soft_stories(client):
response = client.get(f"/soft-stories/")
response_dict = response.json()
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False
assert len(response_dict["features"]) == 6
55 changes: 5 additions & 50 deletions backend/api/tests/test_tsunami.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,9 @@
"""
Test the API of tsunami.py.
"""

import pytest
from fastapi.testclient import TestClient

# Will the .. be stable?
from ..main import app
from ..schemas.geo import Polygon


@pytest.fixture
def client():
return TestClient(app)


def test_delete_tsunami_polygon(client):
response = client.delete("/api/polygons/1?table_name=tsunami")
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False


def test_put_tsunami_polygon(client):
response = client.put(
"/api/polygons/1?table_name=tsunami", json=Polygon().model_dump()
)
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False


def test_post_tsunami_polygon(client):
response = client.put(
"/api/polygons/1?table_name=tsunami", json=Polygon().model_dump()
)
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False


def test_get_tsunami_polygon(client):
response = client.get("/api/polygons/1?table_name=tsunami")
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False
from backend.api.tests.test_session_config import test_engine, test_session, client


def test_get_tsunami_risk(client):
response = client.get("/api/tsunami-risk/addresss")
def test_get_tsunami_zones(client):
response = client.get(f"/tsunami-zones/")
response_dict = response.json()
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False
assert len(response_dict["features"]) == 1
Loading

0 comments on commit c76f2b5

Please sign in to comment.