Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce annotations on the level of an organisation account (which is a much better place for organisation holidays, for example), and adds a CLI command to create a single user annotation. We'll now have three levels of annotations: - Accounts - Assets (GenericAssets) - Sensors Also adds unique constraints on the many-to-many relationships involving annotations and roles. * Allow annotations to be attached to an organisation account Signed-off-by: F.N. Claessen <felix@seita.nl> * Add CLI command for adding a single user annotation Signed-off-by: F.N. Claessen <felix@seita.nl> * Rename Annotation.name to Annotation.content Signed-off-by: F.N. Claessen <felix@seita.nl> * Update downgrade Signed-off-by: F.N. Claessen <felix@seita.nl> * Fix repr Signed-off-by: F.N. Claessen <felix@seita.nl> * Rename UniqueConstraint Signed-off-by: F.N. Claessen <felix@seita.nl> * Add annotations_accounts to downgrade prompt Signed-off-by: F.N. Claessen <felix@seita.nl> * Add unique constraints to relationships Signed-off-by: F.N. Claessen <felix@seita.nl> * Fix print statement Signed-off-by: F.N. Claessen <felix@seita.nl> * Fix queries Signed-off-by: F.N. Claessen <felix@seita.nl> * Reuse possibly existing Annotation when adding it to another account, asset and/or sensor Signed-off-by: F.N. Claessen <felix@seita.nl> * black Signed-off-by: F.N. Claessen <felix@seita.nl> * Add UniqueConstraints on roles_accounts and roles_users tables Signed-off-by: F.N. Claessen <felix@seita.nl> * Start CLI tests Signed-off-by: F.N. Claessen <felix@seita.nl> * black Signed-off-by: F.N. Claessen <felix@seita.nl> * Add test for get_or_create_annotation Signed-off-by: F.N. Claessen <felix@seita.nl> * Add test fixtures Signed-off-by: F.N. Claessen <felix@seita.nl> * Check database entries Signed-off-by: F.N. Claessen <felix@seita.nl> * Check whether annotation ended up in the database session and got a non-None id. Signed-off-by: F.N. Claessen <felix@seita.nl>
- Loading branch information
Showing
8 changed files
with
431 additions
and
25 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
Empty file.
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,73 @@ | ||
from flexmeasures.cli.tests.utils import to_flags | ||
from flexmeasures.data.models.annotations import ( | ||
Annotation, | ||
AccountAnnotationRelationship, | ||
) | ||
from flexmeasures.data.models.data_sources import DataSource | ||
|
||
|
||
def test_add_annotation(app, db, setup_roles_users): | ||
from flexmeasures.cli.data_add import add_annotation | ||
|
||
cli_input = { | ||
"content": "Company founding day", | ||
"at": "2016-05-11T00:00+02:00", | ||
"account-id": 1, | ||
"user-id": 1, | ||
} | ||
runner = app.test_cli_runner() | ||
result = runner.invoke(add_annotation, to_flags(cli_input)) | ||
|
||
# Check result for success | ||
assert "Successfully added annotation" in result.output | ||
|
||
# Check database for annotation entry | ||
assert ( | ||
Annotation.query.filter( | ||
Annotation.content == cli_input["content"], | ||
Annotation.start == cli_input["at"], | ||
) | ||
.join(AccountAnnotationRelationship) | ||
.filter( | ||
AccountAnnotationRelationship.account_id == cli_input["account-id"], | ||
AccountAnnotationRelationship.annotation_id == Annotation.id, | ||
) | ||
.join(DataSource) | ||
.filter( | ||
DataSource.id == Annotation.source_id, | ||
DataSource.user_id == cli_input["user-id"], | ||
) | ||
.one_or_none() | ||
) | ||
|
||
|
||
def test_add_holidays(app, db, setup_roles_users): | ||
from flexmeasures.cli.data_add import add_holidays | ||
|
||
cli_input = { | ||
"year": 2020, | ||
"country": "NL", | ||
"account-id": 1, | ||
} | ||
runner = app.test_cli_runner() | ||
result = runner.invoke(add_holidays, to_flags(cli_input)) | ||
|
||
# Check result for 11 public holidays | ||
assert "'NL': 11" in result.output | ||
|
||
# Check database for 11 annotation entries | ||
assert ( | ||
Annotation.query.join(AccountAnnotationRelationship) | ||
.filter( | ||
AccountAnnotationRelationship.account_id == cli_input["account-id"], | ||
AccountAnnotationRelationship.annotation_id == Annotation.id, | ||
) | ||
.join(DataSource) | ||
.filter( | ||
DataSource.id == Annotation.source_id, | ||
DataSource.name == "workalendar", | ||
DataSource.model == cli_input["country"], | ||
) | ||
.count() | ||
== 11 | ||
) |
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,20 @@ | ||
def to_flags(cli_input: dict) -> list: | ||
"""Turn dictionary of CLI input into a list of CLI flags ready for use in FlaskCliRunner.invoke(). | ||
Example: | ||
cli_input = { | ||
"year": 2020, | ||
"country": "NL", | ||
} | ||
cli_flags = to_flags(cli_input) # ["--year", 2020, "--country", "NL"] | ||
runner = app.test_cli_runner() | ||
result = runner.invoke(some_cli_function, to_flags(cli_input)) | ||
""" | ||
return [ | ||
item | ||
for sublist in zip( | ||
[f"--{key.replace('_', '-')}" for key in cli_input.keys()], | ||
cli_input.values(), | ||
) | ||
for item in sublist | ||
] |
Oops, something went wrong.