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
Make test suite run faster #115
Merged
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6c7ad68
Avoid redundant queries in test suite
Flix6x 4591349
Separate data source setup
Flix6x 30b3831
Reduce the use of autouse=True
Flix6x 1bcf16e
Less queries
Flix6x 788c129
Typo
Flix6x a83ac8b
Fix test
Flix6x d512afc
Separate setup of inactive user
Flix6x fc08c60
Clean redis only in API and data package
Flix6x f7a8bff
Less setup of market prices
Flix6x 81dfdf9
Add type annotations for return values
Flix6x 2a8449b
Set up charging stations only when tests need them
Flix6x 2622d28
Set up assets only when tests need them (stop autouse), and split off…
Flix6x e530550
Create draft PR for #114
Flix6x 31f4613
Setup database with module scope
Flix6x 407da74
Refactor test db setup at different scopes
Flix6x efd12c7
Merge branch 'speed-up-tests' into issue-114-Make_test_suite_run_faster
Flix6x 71a947f
Refactor other main conftest setups at different scopes
Flix6x 5edbf4f
Rename test modules using fresh db
Flix6x 8dba854
Rename test modules using fresh db
Flix6x d8da752
Update developer documentation in main conftest
Flix6x 4bf5462
Rename fixtures using fresh test db according to a single rule (appen…
Flix6x 983d20b
Rename fresh_test_db to fresh_db
Flix6x 8005ec0
Merge remote-tracking branch 'origin/main' into issue-114-Make_test_s…
Flix6x f931915
Changelog entry
Flix6x 3acbd1c
Reformulate changelog entry
Flix6x File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say "Re-use the database between the automated tests, if possible. This shaves 2/3rd off the time ..."