Skip to content

Commit

Permalink
Merge pull request #89 from quintel/create-context-2023
Browse files Browse the repository at this point in the history
Mocks and quick fixes for `convert_with_context`
  • Loading branch information
redekok authored Mar 22, 2023
2 parents d700f1b + c4c4784 commit 254e5d6
Show file tree
Hide file tree
Showing 4 changed files with 15,667 additions and 10 deletions.
9 changes: 8 additions & 1 deletion app/models/parsers/rooftop_pv.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ def __set_production(self):
assets_generator = self.energy_system.get_all_instances_of_type_by_name('PVInstallation')

# Assuming there is only one port and one profile
self.production = sum((asset.port[0].profile[0].value for asset in assets_generator))
self.production = sum((self.__value_mocked_for_influx_db(asset) for asset in assets_generator))


def __value_mocked_for_influx_db(self, asset):
'''If value is not found (like for influx db) return 0'''
try:
return asset.port[0].profile[0].value
except AttributeError:
return 0

def __set_percentage_used(self):
"""
Based on rooftop PV potential and production, determine which
Expand Down
27 changes: 18 additions & 9 deletions tests/api/v1/test_create_with_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@
API_URL = '/api/v1/create_with_context/'

@pytest.fixture
def energy_system_hengelo():
def energy_system_string(esdl_file_name):
'''String version of the valid Hengelo fixture'''
with open('tests/fixtures/valid_Hengelo_with_kpis.esdl') as file:
with open(f'tests/fixtures/{esdl_file_name}.esdl') as file:
esdl_string = file.read()
return esdl_string

def test_start_situation(client, energy_system_hengelo):

@pytest.mark.parametrize(
'esdl_file_name',
['pand_huidig', 'pand_scenario', 'valid_Hengelo_with_kpis']
)
def test_start_situation(client, energy_system_string):
"""Test caching of the start situation"""
data = {
'energy_system_start_situation': energy_system_hengelo,
'energy_system_end_situation': energy_system_hengelo
'energy_system_start_situation': energy_system_string,
'energy_system_end_situation': energy_system_string
}

esdl_id = re.search(r'(?<=id\=\")\w+(-\w+)+', energy_system_hengelo.split('\n', 2)[1]).group(0)
esdl_id = re.search(r'(?<=id\=\")\w+(-\w+)+', energy_system_string.split('\n', 2)[1]).group(0)

client.post(API_URL, data=data)
assert cache.get(esdl_id) is not None
Expand All @@ -30,18 +35,22 @@ def test_start_situation(client, energy_system_hengelo):

# TODO: test without scenario id

def test_with_missing_situation(client, energy_system_hengelo):
@pytest.mark.parametrize(
'esdl_file_name',
['valid_Hengelo_with_kpis']
)
def test_with_missing_situation(client, energy_system_string):
# Only start situation
data = {
'energy_system_start_situation': energy_system_hengelo
'energy_system_start_situation': energy_system_string
}
resp = client.post(API_URL, data=data)

assert resp.status_code == 400

# Only end situation
data = {
'energy_system_end_situation': energy_system_hengelo
'energy_system_end_situation': energy_system_string
}
resp = client.post(API_URL, data=data)

Expand Down
Loading

0 comments on commit 254e5d6

Please sign in to comment.