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

WIP: fix emissions as int and added test cases #899

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
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
11 changes: 7 additions & 4 deletions .github/workflows/global-api-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ jobs:
run: |
docker run --name github_action_postgresql -d -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_PASSWORD="" postgres
sleep 10
createuser -w -h localhost -p 5432 -U postgres citycatalyst
createdb -w -h localhost -p 5432 -U postgres citycatalyst -O citycatalyst
ALEMBIC_URL=postgresql://citycatalyst:@localhost/citycatalyst alembic upgrade head
createuser -w -h localhost -p 5432 -U postgres ccglobal
createdb -w -h localhost -p 5432 -U postgres ccglobal -O ccglobal
ALEMBIC_URL=postgresql://ccglobal:@localhost/ccglobal alembic upgrade head
cp sample.env .env

- name: Run tests
run: pytest

- name: Build coverage file
run: |
pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered --cov=app tests/ | tee pytest-coverage.txt
Expand Down Expand Up @@ -115,4 +118,4 @@ jobs:
run: |
kubectl create -f k8s/cc-global-api-migrate.yml -n default
kubectl apply -f k8s/cc-global-api-deploy.yml -n default
kubectl rollout restart deployment cc-global-api-deploy -n default
kubectl rollout restart deployment cc-global-api-deploy -n default
20 changes: 10 additions & 10 deletions global-api/routes/ghgi_emissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def db_query_total(datasource_name, spatial_granularity, actor_id, gpc_reference
query = text(
f"""
SELECT upper(e.gas_name) as gas_name,
sum(e.emissions_value) as emissions_value,
COALESCE(max(gwp.{gwp}),0) as gwp_100yr,
COALESCE(sum(e.emissions_value * gwp.{gwp}),0) as emissions_value_100yr,
COALESCE(sum(e.emissions_value * gwp2.{gwp}),0) as emissions_value_20yr
sum(e.emissions_value)::int as emissions_value,
COALESCE(max(gwp.{gwp}),0)::int as gwp_100yr,
COALESCE(sum(e.emissions_value * gwp.{gwp}),0)::int as emissions_value_100yr,
COALESCE(sum(e.emissions_value * gwp2.{gwp}),0)::int as emissions_value_20yr
FROM modelled.emissions e
LEFT JOIN modelled.emissions_factor ef
ON e.emissionfactor_id = ef.emissionfactor_id
Expand Down Expand Up @@ -64,8 +64,8 @@ def db_query_eq_total(datasource_name, spatial_granularity, actor_id, gpc_refere

query = text(
f"""
SELECT COALESCE(sum(e.emissions_value * gwp.{gwp}),0) as emissions_value_100yr,
COALESCE(sum(e.emissions_value * gwp2.{gwp}),0) as emissions_value_20yr
SELECT COALESCE(sum(e.emissions_value * gwp.{gwp}),0)::int as emissions_value_100yr,
COALESCE(sum(e.emissions_value * gwp2.{gwp}),0)::int as emissions_value_20yr
FROM modelled.emissions e
LEFT JOIN modelled.emissions_factor ef
ON e.emissionfactor_id = ef.emissionfactor_id
Expand Down Expand Up @@ -141,7 +141,7 @@ def db_query(datasource_name, spatial_granularity, actor_id, gpc_reference_numbe

query = text(
f"""
SELECT e.emissions_value,
SELECT e.emissions_value::int as emissions_value,
e.gas_name,
ef.emissionfactor_value,
a.activity_name,
Expand All @@ -150,9 +150,9 @@ def db_query(datasource_name, spatial_granularity, actor_id, gpc_reference_numbe
a.activity_subcategory_type,
m.methodology_name,
e.geometry as emissions_geometry,
COALESCE(gwp.{gwp},0) as gwp_100yr,
COALESCE(e.emissions_value * gwp.{gwp},0) as emissions_value_100yr,
COALESCE(e.emissions_value * gwp2.{gwp},0) as emissions_value_20yr
COALESCE(gwp.{gwp},0)::int as gwp_100yr,
COALESCE(e.emissions_value * gwp.{gwp},0)::int as emissions_value_100yr,
COALESCE(e.emissions_value * gwp2.{gwp},0)::int as emissions_value_20yr
FROM modelled.emissions e
LEFT JOIN modelled.emissions_factor ef
ON e.emissionfactor_id = ef.emissionfactor_id
Expand Down
2 changes: 1 addition & 1 deletion global-api/tests/test_ccra_assessment.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_get_ccra_cities():
assert response.status_code == 200
assert response.json() == [
{
"cityName": "Test City",
"cityname": "Test City",
"region": "TEST",
"actor_id": "CITY1",
"osm_id": "R12345"
Expand Down
67 changes: 67 additions & 0 deletions global-api/tests/test_ghgi_emissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from fastapi.testclient import TestClient
import pytest
from main import app # Replace 'yourmodule' with the actual module name where your APIRouter is defined

client = TestClient(app)

# Mocking the database session
@pytest.fixture
def mock_db_session(mocker):
# You can use mocker to stub out the methods of the database session
mock_session = mocker.patch('db.database.SessionLocal')
return mock_session

def test_get_emissions_by_city_and_year(mock_db_session):
# Given: Setting up expected data and response
# Mock the database query results
mock_db_session.return_value.__enter__.return_value.execute.return_value.fetchall.return_value = [
("CO2", 1000, 288, 288000, 288000)
]
mock_db_session.return_value.__enter__.return_value.execute.return_value.fetchone.return_value = (
288000, 288000
)

# Additionally mock any other necessary database operations or fixture set up
# Mocking data quality
mock_db_session.return_value.__enter__.return_value.execute.return_value.fetchone.side_effect = [
('High',), # return for dq quality
(288000, 288000) # return for emissions_value_20yr and emissions_value_100yr
]

# When: Sending a GET request to the API
response = client.get("/api/v1/source/test_source/test_granularity/test_actor/2021/test_gpc", params={"gwp": "ar5"})

# Then: Verifying the response
assert response.status_code == 200
assert response.json() == {
"totals": {
"emissions": {
"co2_mass": "1000",
"co2_co2eq": "288000",
"ch4_mass": "0",
"ch4_co2eq_100yr": "0",
"ch4_co2eq_20yr": "0",
"n2o_mass": "0",
"n2o_co2eq_100yr": "0",
"n2o_co2eq_20yr": "0",
"co2eq_100yr": "288000",
"co2eq_20yr": "288000",
"gpc_quality": "High",
}
},
"records": [
{
"emissions_value": "1000",
"gas_name": "CO2",
"emissionfactor_value": "None",
"activity_name": "None",
"activity_value": "None",
"activity_units": "None",
"activity_subcategory_type": None,
"methodology_name": "None",
"emissions_geometry": "None",
"gwp_100yr": "288",
"emissions_value_100yr": "288000"
}
]
}
Loading