Skip to content

Commit

Permalink
adapt some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nhoening committed Feb 17, 2021
1 parent fe4a362 commit 7d77d23
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 58 deletions.
10 changes: 5 additions & 5 deletions flexmeasures/api/v1/routes.py
Expand Up @@ -51,7 +51,7 @@ def get_meter_data():
{
"type": "GetMeterDataRequest",
"connection": "CS 1",
"connection": "cs_1",
"start": "2015-01-01T00:00:00Z",
"duration": "PT1H30M",
"unit": "MW"
Expand All @@ -66,7 +66,7 @@ def get_meter_data():
{
"type": "GetMeterDataResponse",
"connection": "CS 1",
"connection": "cs_1",
"values": [
306.66,
306.66,
Expand Down Expand Up @@ -117,8 +117,8 @@ def post_meter_data():
"groups": [
{
"connections": [
"CS 1",
"CS 3"
"cs_1",
"cs_3"
],
"values": [
306.66,
Expand All @@ -131,7 +131,7 @@ def post_meter_data():
},
{
"connections": [
"CS 2"
"cs_2"
],
"values": [
306.66,
Expand Down
8 changes: 4 additions & 4 deletions flexmeasures/api/v1/tests/conftest.py
Expand Up @@ -38,7 +38,7 @@ def setup_api_test_data(db):
test_prosumer = user_datastore.find_user(email="demo@seita.nl")
test_asset_type = AssetType(name="test-type")
db.session.add(test_asset_type)
asset_names = ["CS 0"]
asset_names = ["cs_0"]
assets: List[Asset] = []
for asset_name in asset_names:
asset = Asset(
Expand All @@ -63,7 +63,7 @@ def setup_api_test_data(db):

# Create 5 test assets for the test_prosumer user
test_prosumer = user_datastore.find_user(email="test_prosumer@seita.nl")
asset_names = ["CS 1", "CS 2", "CS 3", "CS 4", "CS 5"]
asset_names = ["cs_1", "cs_2", "cs_3", "cs_4", "cs_5"]
assets: List[Asset] = []
for asset_name in asset_names:
asset = Asset(
Expand All @@ -76,13 +76,13 @@ def setup_api_test_data(db):
unit="MW",
)
asset.owner = test_prosumer
if asset_name == "CS 4":
if asset_name == "cs_4":
asset.event_resolution = timedelta(hours=1)
assets.append(asset)
db.session.add(asset)

# Add power forecasts to one of the assets, for two sources
cs_5 = Asset.query.filter(Asset.name == "CS 5").one_or_none()
cs_5 = Asset.query.filter(Asset.name == "cs_5").one_or_none()
test_supplier = user_datastore.find_user(email="test_supplier@seita.nl")
prosumer_data_source = DataSource.query.filter(
DataSource.user == test_prosumer
Expand Down
10 changes: 5 additions & 5 deletions flexmeasures/api/v1/tests/test_api_v1.py
Expand Up @@ -196,7 +196,7 @@ def test_get_meter_data(db, app, client, message):
All data should be in the database, and currently only the Prosumer data is returned.
"""
message["connection"] = "CS 5"
message["connection"] = "cs_5"

# set up frame with expected values, and filter by source if needed
expected_values = pd.concat(
Expand Down Expand Up @@ -235,7 +235,7 @@ def test_get_meter_data(db, app, client, message):
).sort_index()

# check whether conftest.py did its job setting up the database with expected values
cs_5 = Asset.query.filter(Asset.name == "CS 5").one_or_none()
cs_5 = Asset.query.filter(Asset.name == "cs_5").one_or_none()
verify_power_in_db(message, cs_5, expected_values, db, swapped_sign=True)

# check whether the API returns the expected values (currently only the Prosumer data is returned)
Expand Down Expand Up @@ -303,7 +303,7 @@ def test_post_and_get_meter_data(db, app, client, post_message, get_message):
for job, horizon in zip(jobs, horizons):
assert job.kwargs["horizon"] == horizon
assert job.kwargs["start"] == parse_date(post_message["start"]) + horizon
for asset_name in ("CS 1", "CS 2", "CS 3"):
for asset_name in ("cs_1", "cs_2", "cs_3"):
if asset_name in str(post_message):
asset = Asset.query.filter_by(name=asset_name).one_or_none()
assert asset.id in [job.kwargs["asset_id"] for job in jobs]
Expand Down Expand Up @@ -356,6 +356,6 @@ def test_post_meter_data_to_different_resolutions(db, app, client):
"assets do not have matching resolutions"
in post_meter_data_response.json["message"]
)
assert "CS 2" in post_meter_data_response.json["message"]
assert "CS 4" in post_meter_data_response.json["message"]
assert "cs_2" in post_meter_data_response.json["message"]
assert "cs_4" in post_meter_data_response.json["message"]
assert post_meter_data_response.json["status"] == "INVALID_RESOLUTION"
8 changes: 4 additions & 4 deletions flexmeasures/api/v1/tests/utils.py
Expand Up @@ -24,7 +24,7 @@ def message_for_get_meter_data(
"type": "GetMeterDataRequest",
"start": "2015-01-01T00:00:00Z",
"duration": "PT1H30M",
"connections": ["CS 1", "CS 2", "CS 3"],
"connections": ["cs_1", "cs_2", "cs_3"],
"unit": "MW",
}
if no_connection:
Expand All @@ -35,7 +35,7 @@ def message_for_get_meter_data(
message["connection"] = message["connections"][0]
message.pop("connections", None)
elif demo_connection:
message["connection"] = "CS 0"
message["connection"] = "cs_0"
message.pop("connections", None)
if no_unit:
message.pop("unit", None)
Expand All @@ -61,13 +61,13 @@ def message_for_post_meter_data(
"type": "PostMeterDataRequest",
"groups": [
{
"connections": ["CS 1", "CS 2"],
"connections": ["cs_1", "cs_2"],
"values": (
tile([306.66, 306.66, 0, 0, 306.66, 306.66], tile_n) * sign
).tolist(),
},
{
"connection": ["CS 4" if different_target_resolutions else "CS 3"],
"connection": ["cs_4" if different_target_resolutions else "cs_3"],
"values": (
tile([306.66, 0, 0, 0, 306.66, 306.66], tile_n) * sign
).tolist(),
Expand Down
18 changes: 9 additions & 9 deletions flexmeasures/api/v1_1/routes.py
Expand Up @@ -89,10 +89,10 @@ def get_connection():
"ea1.2018-06.io.flexmeasures.company:3:1"
],
"names": [
"CS 4",
"CS 3",
"CS 2",
"CS 1"
"cs_4",
"cs_3",
"cs_2",
"cs_1"
]
}
Expand Down Expand Up @@ -289,7 +289,7 @@ def get_prognosis():
{
"type": "GetPrognosisRequest",
"connection": "CS 1",
"connection": "cs_1",
"start": "2015-01-01T00:00:00Z",
"duration": "PT1H30M",
"horizon": "PT6H",
Expand All @@ -306,7 +306,7 @@ def get_prognosis():
{
"type": "GetPrognosisResponse",
"connection": "CS 1",
"connection": "cs_1",
"values": [
306.66,
306.66,
Expand Down Expand Up @@ -357,8 +357,8 @@ def post_prognosis():
"groups": [
{
"connections": [
"CS 1",
"CS 3"
"cs_1",
"cs_3"
],
"values": [
300,
Expand All @@ -371,7 +371,7 @@ def post_prognosis():
},
{
"connections": [
"CS 2"
"cs_2"
],
"values": [
300,
Expand Down
8 changes: 4 additions & 4 deletions flexmeasures/api/v1_1/tests/conftest.py
Expand Up @@ -44,7 +44,7 @@ def setup_api_test_data(db):
test_prosumer = user_datastore.find_user(email="test_prosumer@seita.nl")
test_asset_type = AssetType(name="test-type")
db.session.add(test_asset_type)
asset_names = ["CS 1", "CS 2", "CS 3"]
asset_names = ["cs_1", "cs_2", "cs_3"]
assets: List[Asset] = []
for asset_name in asset_names:
asset = Asset(
Expand All @@ -61,9 +61,9 @@ def setup_api_test_data(db):
db.session.add(asset)

# Add power forecasts to the assets
cs_1 = Asset.query.filter(Asset.name == "CS 1").one_or_none()
cs_2 = Asset.query.filter(Asset.name == "CS 2").one_or_none()
cs_3 = Asset.query.filter(Asset.name == "CS 3").one_or_none()
cs_1 = Asset.query.filter(Asset.name == "cs_1").one_or_none()
cs_2 = Asset.query.filter(Asset.name == "cs_2").one_or_none()
cs_3 = Asset.query.filter(Asset.name == "cs_3").one_or_none()
data_source = DataSource.query.filter(
DataSource.user == test_prosumer
).one_or_none()
Expand Down
2 changes: 1 addition & 1 deletion flexmeasures/api/v1_1/tests/utils.py
Expand Up @@ -25,7 +25,7 @@ def message_for_get_prognosis(
"duration": "PT1H30M",
"horizon": "PT6H",
"resolution": "PT15M",
"connections": ["CS 1", "CS 2", "CS 3"],
"connections": ["cs_1", "cs_2", "cs_3"],
"unit": "MW",
}
if invalid_horizon:
Expand Down
2 changes: 1 addition & 1 deletion flexmeasures/api/v1_2/tests/conftest.py
Expand Up @@ -15,5 +15,5 @@ def setup_api_test_data(db):
user_datastore = SQLAlchemySessionUserDatastore(db.session, User, Role)
test_prosumer = user_datastore.find_user(email="test_prosumer@seita.nl")

battery = Asset.query.filter(Asset.name == "Test battery").one_or_none()
battery = Asset.query.filter(Asset.name == "test_battery").one_or_none()
battery.owner = test_prosumer
12 changes: 6 additions & 6 deletions flexmeasures/api/v1_2/tests/test_api_v1_2.py
Expand Up @@ -14,7 +14,7 @@

@pytest.mark.parametrize("message", [message_for_get_device_message()])
def test_get_device_message(client, message):
asset = Asset.query.filter(Asset.name == "Test battery").one_or_none()
asset = Asset.query.filter(Asset.name == "test_battery").one_or_none()
message["event"] = message["event"] % (asset.owner_id, asset.id)
auth_token = get_auth_token(client, "test_prosumer@seita.nl", "testtest")
get_device_message_response = client.get(
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_get_device_message(client, message):

@pytest.mark.parametrize("message", [message_for_get_device_message(wrong_id=True)])
def test_get_device_message_wrong_event_id(client, message):
asset = Asset.query.filter(Asset.name == "Test battery").one_or_none()
asset = Asset.query.filter(Asset.name == "test_battery").one_or_none()
message["event"] = message["event"] % (asset.owner_id, asset.id)
auth_token = get_auth_token(client, "test_prosumer@seita.nl", "testtest")
get_device_message_response = client.get(
Expand All @@ -76,7 +76,7 @@ def test_get_device_message_wrong_event_id(client, message):
)
def test_get_device_message_unknown_prices(client, message):
asset = Asset.query.filter(
Asset.name == "Test battery with no known prices"
Asset.name == "test_battery_with_no_known_prices"
).one_or_none()
message["event"] = message["event"] % (asset.owner_id, asset.id)
auth_token = get_auth_token(client, "test_prosumer@seita.nl", "testtest")
Expand All @@ -95,7 +95,7 @@ def test_get_device_message_unknown_prices(client, message):
def test_post_udi_event(app, message):
auth_token = None
with app.test_client() as client:
asset = Asset.query.filter(Asset.name == "Test battery").one_or_none()
asset = Asset.query.filter(Asset.name == "test_battery").one_or_none()
message["event"] = message["event"] % (asset.owner_id, asset.id)
auth_token = get_auth_token(client, "test_prosumer@seita.nl", "testtest")
post_udi_event_response = client.post(
Expand All @@ -110,7 +110,7 @@ def test_post_udi_event(app, message):
msg_dt = parse_datetime(message["datetime"])

# test database state
asset = Asset.query.filter(Asset.name == "Test battery").one_or_none()
asset = Asset.query.filter(Asset.name == "test_battery").one_or_none()
assert asset.soc_datetime == msg_dt
assert asset.soc_in_mwh == message["value"] / 1000
assert asset.soc_udi_event_id == 204
Expand Down Expand Up @@ -140,7 +140,7 @@ def test_post_udi_event(app, message):
assert post_udi_event_response.json["type"] == "PostUdiEventResponse"

# test database state
asset = Asset.query.filter(Asset.name == "Test battery").one_or_none()
asset = Asset.query.filter(Asset.name == "test_battery").one_or_none()
assert asset.soc_datetime == next_msg_dt
assert asset.soc_in_mwh == message["value"] / 1000
assert asset.soc_udi_event_id == 205
4 changes: 2 additions & 2 deletions flexmeasures/api/v1_3/tests/conftest.py
Expand Up @@ -15,10 +15,10 @@ def setup_api_test_data(db):
user_datastore = SQLAlchemySessionUserDatastore(db.session, User, Role)
test_prosumer = user_datastore.find_user(email="test_prosumer@seita.nl")

battery = Asset.query.filter(Asset.name == "Test battery").one_or_none()
battery = Asset.query.filter(Asset.name == "test_battery").one_or_none()
battery.owner = test_prosumer

charging_station = Asset.query.filter(
Asset.name == "Test charging station"
Asset.name == "test_charging_station"
).one_or_none()
charging_station.owner = test_prosumer
8 changes: 4 additions & 4 deletions flexmeasures/api/v1_3/tests/test_api_v1_3.py
Expand Up @@ -21,7 +21,7 @@

@pytest.mark.parametrize("message", [message_for_get_device_message(wrong_id=True)])
def test_get_device_message_wrong_event_id(client, message):
asset = Asset.query.filter(Asset.name == "Test battery").one_or_none()
asset = Asset.query.filter(Asset.name == "test_battery").one_or_none()
message["event"] = message["event"] % (asset.owner_id, asset.id)
auth_token = get_auth_token(client, "test_prosumer@seita.nl", "testtest")
get_device_message_response = client.get(
Expand All @@ -41,8 +41,8 @@ def test_get_device_message_wrong_event_id(client, message):
@pytest.mark.parametrize(
"message, asset_name",
[
(message_for_post_udi_event(), "Test battery"),
(message_for_post_udi_event(targets=True), "Test charging station"),
(message_for_post_udi_event(), "test_battery"),
(message_for_post_udi_event(targets=True), "test_charging_station"),
],
)
def test_post_udi_event_and_get_device_message(app, message, asset_name):
Expand Down Expand Up @@ -204,7 +204,7 @@ def test_post_udi_event_and_get_device_message(app, message, asset_name):
def test_post_udi_event_and_get_device_message_with_unknown_prices(app, message):
auth_token = None
with app.test_client() as client:
asset = Asset.query.filter(Asset.name == "Test battery").one_or_none()
asset = Asset.query.filter(Asset.name == "test_battery").one_or_none()
asset_id = asset.id
asset_owner_id = asset.owner_id
message["event"] = message["event"] % (asset.owner_id, asset.id)
Expand Down
4 changes: 1 addition & 3 deletions flexmeasures/api/v2_0/implementations/assets.py
Expand Up @@ -15,7 +15,7 @@
from flexmeasures.api import ma
from flexmeasures.api.common.utils.api_utils import get_form_from_request
from flexmeasures.api.common.responses import required_info_missing
from flexmeasures.utils.flexmeasures_inflection import humanize, parameterize
from flexmeasures.utils.flexmeasures_inflection import parameterize


class AssetSchema(ma.SQLAlchemySchema):
Expand Down Expand Up @@ -186,8 +186,6 @@ def patch(asset):
for k, v in asset_data.items():
if k == "name":
asset.name = parameterize(v)
elif k == "display_name":
asset.display_name = humanize(v)
else:
setattr(asset, k, v)
db.session.add(asset)
Expand Down
4 changes: 2 additions & 2 deletions flexmeasures/conftest.py
Expand Up @@ -237,7 +237,7 @@ def add_battery_assets(db: SQLAlchemy, setup_roles_users, setup_markets):
epex_da = Market.query.filter(Market.name == "epex_da").one_or_none()

battery = Asset(
name="Test battery",
name="test_battery",
asset_type_name="battery",
event_resolution=timedelta(minutes=15),
capacity_in_mw=2,
Expand All @@ -255,7 +255,7 @@ def add_battery_assets(db: SQLAlchemy, setup_roles_users, setup_markets):
db.session.add(battery)

battery = Asset(
name="Test battery with no known prices",
name="test_battery_with_no_known_prices",
asset_type_name="battery",
event_resolution=timedelta(minutes=15),
capacity_in_mw=2,
Expand Down
12 changes: 6 additions & 6 deletions flexmeasures/data/models/planning/tests/test_solver.py
Expand Up @@ -14,7 +14,7 @@

def test_battery_solver_day_1():
epex_da = Market.query.filter(Market.name == "epex_da").one_or_none()
battery = Asset.query.filter(Asset.name == "Test battery").one_or_none()
battery = Asset.query.filter(Asset.name == "test_battery").one_or_none()
start = as_server_time(datetime(2015, 1, 1))
end = as_server_time(datetime(2015, 1, 2))
resolution = timedelta(minutes=15)
Expand All @@ -35,7 +35,7 @@ def test_battery_solver_day_1():

def test_battery_solver_day_2():
epex_da = Market.query.filter(Market.name == "epex_da").one_or_none()
battery = Asset.query.filter(Asset.name == "Test battery").one_or_none()
battery = Asset.query.filter(Asset.name == "test_battery").one_or_none()
start = as_server_time(datetime(2015, 1, 2))
end = as_server_time(datetime(2015, 1, 3))
resolution = timedelta(minutes=15)
Expand Down Expand Up @@ -68,10 +68,10 @@ def test_battery_solver_day_2():
@pytest.mark.parametrize(
"target_soc, charging_station_name",
[
(1, "Test charging station"),
(5, "Test charging station"),
(0, "Test charging station (bidirectional)"),
(5, "Test charging station (bidirectional)"),
(1, "test_charging_station"),
(5, "test_charging_station"),
(0, "test_charging_station_bidirectional"),
(5, "test_charging_station_bidirectional"),
],
)
def test_charging_station_solver_day_2(target_soc, charging_station_name):
Expand Down

0 comments on commit 7d77d23

Please sign in to comment.