Skip to content
Snippets Groups Projects
Commit a540348a authored by Saket Agrahari's avatar Saket Agrahari
Browse files

test case for budget and funding source

parent 7317ec8e
No related branches found
No related tags found
No related merge requests found
......@@ -34,7 +34,7 @@ BUDGET_RESPONSE_SCHEMA = {
'properties': {
'id': {'type': 'number'},
'NREN': {'type': 'string'},
'BUDGET': {'type': 'string'},
'BUDGET': {'type': 'number'},
'BUDGET_YEAR': {'type': 'integer'},
},
'required': ['id'],
......@@ -56,7 +56,7 @@ def budget_view() -> Any:
response will be formatted as:
.. asjson::
compendium_v2.routes.data_entry.BUDGET_RESPONSE_SCHEMA
compendium_v2.routes.budget.BUDGET_RESPONSE_SCHEMA
:return:
"""
......@@ -65,7 +65,7 @@ def budget_view() -> Any:
return {
'id': entry.id,
'NREN': entry.nren,
'BUDGET': entry.budget,
'BUDGET': float(entry.budget),
'BUDGET_YEAR': entry.year,
}
......
......@@ -27,14 +27,14 @@ FUNDING_RESPONSE_SCHEMA = {
'properties': {
'id': {'type': 'number'},
'NREN': {'type': 'string'},
'YEAR': {'type': 'string'},
'CLIENT_INSTITUTIONS': {'type': 'string'},
'EUROPEAN_FUNDING': {'type': 'string'},
'GOV_PUBLIC_BODIES': {'type': 'string'},
'COMMERCIAL': {'type': 'string'},
'OTHER': {'type': 'string'}
'YEAR': {'type': 'integer'},
'CLIENT_INSTITUTIONS': {'type': 'number'},
'EUROPEAN_FUNDING': {'type': 'number'},
'GOV_PUBLIC_BODIES': {'type': 'number'},
'COMMERCIAL': {'type': 'number'},
'OTHER': {'type': 'number'}
},
'required': ['id'],
'required': ['id', 'NREN', 'YEAR'],
'additionalProperties': False
}
},
......@@ -46,14 +46,14 @@ FUNDING_RESPONSE_SCHEMA = {
@routes.route('/', methods=['GET'])
@common.require_accepts_json
def budget_view() -> Any:
def funding_source_view() -> Any:
"""
handler for /api/funding/ requests
response will be formatted as:
.. asjson::
compendium_v2.routes.data_entry.BUDGET_RESPONSE_SCHEMA
compendium_v2.routes.funding.FUNDING_RESPONSE_SCHEMA
:return:
"""
......@@ -62,17 +62,16 @@ def budget_view() -> Any:
return {
'id': entry.id,
'NREN': entry.nren,
'YEAR': entry.year,
'CLIENT_INSTITUTIONS': entry.client_institutions,
'EUROPEAN_FUNDING': entry.european_funding,
'GOV_PUBLIC_BODIES': entry.gov_public_bodies,
'COMMERCIAL': entry.commercial,
'OTHER': entry.other
'YEAR': int(entry.year),
'CLIENT_INSTITUTIONS': float(entry.client_institutions),
'EUROPEAN_FUNDING': float(entry.european_funding),
'GOV_PUBLIC_BODIES': float(entry.gov_public_bodies),
'COMMERCIAL': float(entry.commercial),
'OTHER': float(entry.other)
}
with db.session_scope() as session:
entries = sorted([_extract_data(entry)
for entry in session.query(model.FundingSource)],
key=lambda d: (d['NREN'], d['YEAR']))
dict_obj = {"data": entries}
return jsonify(dict_obj)
return jsonify(entries)
......@@ -12,6 +12,16 @@ from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
import csv
def _test_data_csv(filename):
data_filename = os.path.join(
os.path.dirname(__file__),
'data',
filename)
yield from csv.DictReader(open(data_filename, "r"))
@pytest.fixture
def dummy_config():
......@@ -58,14 +68,45 @@ def mocked_db(mocker):
@pytest.fixture
def create_test_presentation_data():
def test_budget_data():
with db.session_scope() as session:
data = _test_data_csv("BudgetTestData.csv")
for index, row in enumerate(data):
nren = row["nren"]
budget = row["budget"]
year = row["year"]
session.add(
model.BudgetEntry(
id=1,
nren='testnren',
budget=123,
year=2023,)
id=index + 1,
nren=nren,
budget=float(budget),
year=int(year))
)
@pytest.fixture
def test_funding_source_data():
with db.session_scope() as session:
data = _test_data_csv("FundingSourceTestData.csv")
for index, row in enumerate(data):
nren = row["nren"]
year = row["year"]
client = row["client"]
european = row["european"]
publicbodies = row["publicbodies"]
commercial = row["commercial"]
other = row["other"]
session.add(
model.FundingSource(
id=index + 1,
nren=nren, year=year,
client_institutions=client,
european_funding=european,
gov_public_bodies=publicbodies,
commercial=commercial,
other=other)
)
......
nren,budget,year
ACOnet,6.4,2021
ACOnet,6.4,2020
ACOnet,6.1,2019
......
nren,year,client,european,publicbodies,commercial,other
ACOnet,2020,100.0,0.0,0.0,0.0,0.0
AMRES,2020,0.0,0.0,100.0,0.0,0.0
ANA,2020,13.0,1.0,1.0,1.0,0.0
......
import json
import jsonschema
from compendium_v2.routes.budget import BUDGET_RESPONSE_SCHEMA
def test_budget_response(client, test_budget_data):
rv = client.get(
'/api/budget/',
headers={'Accept': ['application/json']})
assert rv.status_code == 200
result = json.loads(rv.data.decode('utf-8'))
jsonschema.validate(result, BUDGET_RESPONSE_SCHEMA)
assert result
import json
import jsonschema
from compendium_v2.routes.funding import FUNDING_RESPONSE_SCHEMA
def test_funding_source_response(client, test_funding_source_data):
rv = client.get(
'/api/funding/',
headers={'Accept': ['application/json']})
assert rv.status_code == 200
result = json.loads(rv.data.decode('utf-8'))
jsonschema.validate(result, FUNDING_RESPONSE_SCHEMA)
assert result
......@@ -3,7 +3,6 @@ import json
import jsonschema
import pytest
from compendium_v2.routes.budget import BUDGET_RESPONSE_SCHEMA
from compendium_v2.routes.default import VERSION_SCHEMA
......@@ -24,12 +23,3 @@ def test_version_request(client):
assert rv.status_code == 200
result = json.loads(rv.data.decode('utf-8'))
jsonschema.validate(result, VERSION_SCHEMA)
def test_budget_response(client, create_test_presentation_data):
rv = client.get(
'/api/budget/',
headers={'Accept': ['application/json']})
assert rv.status_code == 200
result = json.loads(rv.data.decode('utf-8'))
jsonschema.validate(result, BUDGET_RESPONSE_SCHEMA)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment