Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-use the database between automated tests, if possible. This shaves 2/3rd off of the time it takes for the FlexMeasures test suite to run. * Avoid redundant queries in test suite * Separate data source setup * Reduce the use of autouse=True * Less queries * Typo * Fix test * Separate setup of inactive user * Clean redis only in API and data package * Less setup of market prices * Add type annotations for return values * Set up charging stations only when tests need them * Set up assets only when tests need them (stop autouse), and split off setup of asset types * Create draft PR for #114 * Setup database with module scope * Refactor test db setup at different scopes * Refactor other main conftest setups at different scopes * Rename test modules using fresh db * Rename test modules using fresh db * Update developer documentation in main conftest * Rename fixtures using fresh test db according to a single rule (append _fresh_db) * Rename fresh_test_db to fresh_db Co-authored-by: F.N. Claessen <felix@seita.nl> Co-authored-by: Flix6x <Flix6x@users.noreply.github.com>
- Loading branch information
1 parent
8e84f04
commit 6eeefa6
Showing
32 changed files
with
988 additions
and
707 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from datetime import timedelta | ||
|
||
import pytest | ||
from flask import url_for | ||
from iso8601 import parse_date | ||
from numpy import repeat | ||
|
||
from flexmeasures.api.common.utils.api_utils import message_replace_name_with_ea | ||
from flexmeasures.api.tests.utils import get_auth_token | ||
from flexmeasures.api.v1.tests.utils import ( | ||
message_for_post_meter_data, | ||
message_for_get_meter_data, | ||
count_connections_in_post_message, | ||
) | ||
from flexmeasures.data.models.assets import Asset | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"post_message", | ||
[ | ||
message_for_post_meter_data(), | ||
message_for_post_meter_data(single_connection=True), | ||
message_for_post_meter_data(single_connection_group=True), | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"get_message", | ||
[ | ||
message_for_get_meter_data(), | ||
message_for_get_meter_data(single_connection=False), | ||
message_for_get_meter_data(resolution="PT30M"), | ||
], | ||
) | ||
def test_post_and_get_meter_data( | ||
setup_fresh_api_test_data, app, clean_redis, client, post_message, get_message | ||
): | ||
""" | ||
Tries to post meter data as a logged-in test user with the MDC role, which should succeed. | ||
There should be some ForecastingJobs waiting now. | ||
Then tries to get meter data, which should succeed, and should return the same meter data as was posted, | ||
or a downsampled version, if that was requested. | ||
""" | ||
|
||
# post meter data | ||
auth_token = get_auth_token(client, "test_prosumer@seita.nl", "testtest") | ||
post_meter_data_response = client.post( | ||
url_for("flexmeasures_api_v1.post_meter_data"), | ||
json=message_replace_name_with_ea(post_message), | ||
headers={"Authorization": auth_token}, | ||
) | ||
print("Server responded with:\n%s" % post_meter_data_response.json) | ||
assert post_meter_data_response.status_code == 200 | ||
assert post_meter_data_response.json["type"] == "PostMeterDataResponse" | ||
|
||
# look for Forecasting jobs | ||
expected_connections = count_connections_in_post_message(post_message) | ||
assert ( | ||
len(app.queues["forecasting"]) == 4 * expected_connections | ||
) # four horizons times the number of assets | ||
horizons = repeat( | ||
[ | ||
timedelta(hours=1), | ||
timedelta(hours=6), | ||
timedelta(hours=24), | ||
timedelta(hours=48), | ||
], | ||
expected_connections, | ||
) | ||
jobs = sorted(app.queues["forecasting"].jobs, key=lambda x: x.kwargs["horizon"]) | ||
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"): | ||
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] | ||
|
||
# get meter data | ||
get_meter_data_response = client.get( | ||
url_for("flexmeasures_api_v1.get_meter_data"), | ||
query_string=message_replace_name_with_ea(get_message), | ||
headers={"Authorization": auth_token}, | ||
) | ||
print("Server responded with:\n%s" % get_meter_data_response.json) | ||
assert get_meter_data_response.status_code == 200 | ||
assert get_meter_data_response.json["type"] == "GetMeterDataResponse" | ||
if "groups" in post_message: | ||
posted_values = post_message["groups"][0]["values"] | ||
else: | ||
posted_values = post_message["values"] | ||
if "groups" in get_meter_data_response.json: | ||
gotten_values = get_meter_data_response.json["groups"][0]["values"] | ||
else: | ||
gotten_values = get_meter_data_response.json["values"] | ||
|
||
if "resolution" not in get_message or get_message["resolution"] == "": | ||
assert gotten_values == posted_values | ||
else: | ||
# We used a target resolution of 30 minutes, so double of 15 minutes. | ||
# Six values went in, three come out. | ||
if posted_values[1] > 0: # see utils.py:message_for_post_meter_data | ||
assert gotten_values == [306.66, -0.0, 306.66] | ||
else: | ||
assert gotten_values == [153.33, 0, 306.66] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.