-
Notifications
You must be signed in to change notification settings - Fork 30
/
conftest.py
878 lines (745 loc) · 27.2 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
from __future__ import annotations
from contextlib import contextmanager
import pytest
from random import random
from datetime import datetime, timedelta
import pytz
from isodate import parse_duration
import pandas as pd
import numpy as np
from flask import request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_security import roles_accepted
from werkzeug.exceptions import (
InternalServerError,
BadRequest,
Unauthorized,
Forbidden,
Gone,
)
from flexmeasures.app import create as create_app
from flexmeasures.auth.policy import ADMIN_ROLE
from flexmeasures.utils.time_utils import as_server_time
from flexmeasures.data.services.users import create_user
from flexmeasures.data.models.assets import AssetType, Asset
from flexmeasures.data.models.generic_assets import GenericAssetType, GenericAsset
from flexmeasures.data.models.data_sources import DataSource
from flexmeasures.data.models.planning.utils import initialize_index
from flexmeasures.data.models.markets import Market, MarketType
from flexmeasures.data.models.time_series import Sensor, TimedBelief
from flexmeasures.data.models.user import User, Account, AccountRole
"""
Useful things for all tests.
# App
One application is made per test session.
# Database
Database recreation and cleanup can happen per test (use fresh_db) or per module (use db).
Having tests inside a module share a database makes those tests faster.
Tests that use fresh_db should be put in a separate module to avoid clashing with the module scoped test db.
For example:
- test_api_v1_1.py contains tests that share a module scoped database
- test_api_v1_1_fresh_db.py contains tests that each get a fresh function-scoped database
Further speed-up may be possible by defining a "package" scoped or even "session" scoped database,
but then tests in different modules need to share data and data modifications can lead to tricky debugging.
# Data
Various fixture below set up data that many tests use.
In case a test needs to use such data with a fresh test database,
that test should also use a fixture that requires the fresh_db.
Such fixtures can be recognised by having fresh_db appended to their name.
"""
@pytest.fixture(scope="session")
def app():
print("APP FIXTURE")
test_app = create_app(env="testing")
# Establish an application context before running the tests.
ctx = test_app.app_context()
ctx.push()
yield test_app
ctx.pop()
print("DONE WITH APP FIXTURE")
@pytest.fixture(scope="module")
def db(app):
"""Fresh test db per module."""
with create_test_db(app) as test_db:
yield test_db
@pytest.fixture(scope="function")
def fresh_db(app):
"""Fresh test db per function."""
with create_test_db(app) as test_db:
yield test_db
@contextmanager
def create_test_db(app):
"""
Provide a db object with the structure freshly created. This assumes a clean database.
It does clean up after itself when it's done (drops everything).
"""
print("DB FIXTURE")
# app is an instance of a flask app, _db a SQLAlchemy DB
from flexmeasures.data import db as _db
_db.app = app
with app.app_context():
_db.create_all()
yield _db
print("DB FIXTURE CLEANUP")
# Explicitly close DB connection
_db.session.close()
_db.drop_all()
@pytest.fixture(scope="module")
def setup_accounts(db) -> dict[str, Account]:
return create_test_accounts(db)
@pytest.fixture(scope="function")
def setup_accounts_fresh_db(fresh_db) -> dict[str, Account]:
return create_test_accounts(fresh_db)
def create_test_accounts(db) -> dict[str, Account]:
prosumer_account_role = AccountRole(name="Prosumer", description="A Prosumer")
prosumer_account = Account(
name="Test Prosumer Account", account_roles=[prosumer_account_role]
)
db.session.add(prosumer_account)
supplier_account_role = AccountRole(
name="Supplier", description="A supplier trading on markets"
)
supplier_account = Account(
name="Test Supplier Account", account_roles=[supplier_account_role]
)
db.session.add(supplier_account)
dummy_account_role = AccountRole(
name="Dummy", description="A role we haven't hardcoded anywhere"
)
dummy_account = Account(
name="Test Dummy Account", account_roles=[dummy_account_role]
)
db.session.add(dummy_account)
empty_account = Account(name="Test Empty Account")
db.session.add(empty_account)
multi_role_account = Account(
name="Multi Role Account",
account_roles=[
prosumer_account_role,
supplier_account_role,
dummy_account_role,
],
)
db.session.add(multi_role_account)
return dict(
Prosumer=prosumer_account,
Supplier=supplier_account,
Dummy=dummy_account,
Multi=multi_role_account,
)
@pytest.fixture(scope="module")
def setup_roles_users(db, setup_accounts) -> dict[str, User]:
return create_roles_users(db, setup_accounts)
@pytest.fixture(scope="function")
def setup_roles_users_fresh_db(fresh_db, setup_accounts_fresh_db) -> dict[str, User]:
return create_roles_users(fresh_db, setup_accounts_fresh_db)
def create_roles_users(db, test_accounts) -> dict[str, User]:
"""Create a minimal set of roles and users"""
new_users: list[User] = []
# Two Prosumer users
new_users.append(
create_user(
username="Test Prosumer User",
email="test_prosumer_user@seita.nl",
account_name=test_accounts["Prosumer"].name,
password="testtest",
# TODO: test some normal user roles later in our auth progress
# user_roles=dict(name="", description=""),
)
)
new_users.append(
create_user(
username="Test Prosumer User 2",
email="test_prosumer_user_2@seita.nl",
account_name=test_accounts["Prosumer"].name,
password="testtest",
user_roles=dict(name="account-admin", description="Admin for this account"),
)
)
# A user on an account without any special rights
new_users.append(
create_user(
username="Test Dummy User",
email="test_dummy_user_3@seita.nl",
account_name=test_accounts["Dummy"].name,
password="testtest",
)
)
# A supplier user
new_users.append(
create_user(
username="Test Supplier User",
email="test_supplier_user_4@seita.nl",
account_name=test_accounts["Supplier"].name,
password="testtest",
)
)
# One platform admin
new_users.append(
create_user(
username="Test Admin User",
email="test_admin_user@seita.nl",
account_name=test_accounts[
"Dummy"
].name, # the account does not give rights
password="testtest",
user_roles=dict(
name=ADMIN_ROLE, description="A user who can do everything."
),
)
)
return {user.username: user for user in new_users}
@pytest.fixture(scope="module")
def setup_markets(db) -> dict[str, Market]:
return create_test_markets(db)
@pytest.fixture(scope="function")
def setup_markets_fresh_db(fresh_db) -> dict[str, Market]:
return create_test_markets(fresh_db)
def create_test_markets(db) -> dict[str, Market]:
"""Create the epex_da market."""
day_ahead = MarketType(
name="day_ahead",
daily_seasonality=True,
weekly_seasonality=True,
yearly_seasonality=True,
)
db.session.add(day_ahead)
epex_da = Market(
name="epex_da",
market_type_name="day_ahead",
event_resolution=timedelta(hours=1),
unit="EUR/MWh",
knowledge_horizon_fnc="x_days_ago_at_y_oclock",
knowledge_horizon_par={"x": 1, "y": 12, "z": "Europe/Paris"},
)
db.session.add(epex_da)
epex_da_production = Market(
name="epex_da_production",
market_type_name="day_ahead",
event_resolution=timedelta(hours=1),
unit="EUR/MWh",
knowledge_horizon_fnc="x_days_ago_at_y_oclock",
knowledge_horizon_par={"x": 1, "y": 12, "z": "Europe/Paris"},
)
db.session.add(epex_da_production)
return {"epex_da": epex_da, "epex_da_production": epex_da_production}
@pytest.fixture(scope="module")
def setup_sources(db) -> dict[str, DataSource]:
return create_sources(db)
@pytest.fixture(scope="function")
def setup_sources_fresh_db(fresh_db) -> dict[str, DataSource]:
return create_sources(fresh_db)
def create_sources(db) -> dict[str, DataSource]:
seita_source = DataSource(name="Seita", type="demo script")
db.session.add(seita_source)
entsoe_source = DataSource(name="ENTSO-E", type="demo script")
db.session.add(entsoe_source)
return {"Seita": seita_source, "ENTSO-E": entsoe_source}
@pytest.fixture(scope="module")
def setup_asset_types(db) -> dict[str, AssetType]:
return create_test_asset_types(db)
@pytest.fixture(scope="function")
def setup_asset_types_fresh_db(fresh_db) -> dict[str, AssetType]:
return create_test_asset_types(fresh_db)
@pytest.fixture(scope="module")
def setup_generic_assets(
db, setup_generic_asset_types, setup_accounts
) -> dict[str, AssetType]:
"""Make some generic assets used throughout."""
return create_generic_assets(db, setup_generic_asset_types, setup_accounts)
@pytest.fixture(scope="function")
def setup_generic_assets_fresh_db(
fresh_db, setup_generic_asset_types_fresh_db, setup_accounts_fresh_db
) -> dict[str, AssetType]:
"""Make some generic assets used throughout."""
return create_generic_assets(
fresh_db, setup_generic_asset_types_fresh_db, setup_accounts_fresh_db
)
def create_generic_assets(db, setup_generic_asset_types, setup_accounts):
troposphere = GenericAsset(
name="troposphere", generic_asset_type=setup_generic_asset_types["public_good"]
)
db.session.add(troposphere)
test_battery = GenericAsset(
name="Test grid connected battery storage",
generic_asset_type=setup_generic_asset_types["battery"],
owner=setup_accounts["Prosumer"],
attributes={"some-attribute": "some-value", "sensors_to_show": [1, 2]},
)
db.session.add(test_battery)
test_wind_turbine = GenericAsset(
name="Test wind turbine",
generic_asset_type=setup_generic_asset_types["wind"],
owner=setup_accounts["Supplier"],
)
db.session.add(test_wind_turbine)
return dict(
troposphere=troposphere,
test_battery=test_battery,
test_wind_turbine=test_wind_turbine,
)
@pytest.fixture(scope="module")
def setup_generic_asset_types(db) -> dict[str, AssetType]:
"""Make some generic asset types used throughout."""
return create_generic_asset_types(db)
@pytest.fixture(scope="function")
def setup_generic_asset_types_fresh_db(fresh_db) -> dict[str, AssetType]:
"""Make some generic asset types used throughout."""
return create_generic_asset_types(fresh_db)
def create_generic_asset_types(db):
public_good = GenericAssetType(
name="public good",
)
db.session.add(public_good)
solar = GenericAssetType(name="solar panel")
db.session.add(solar)
wind = GenericAssetType(name="wind turbine")
db.session.add(wind)
battery = GenericAssetType.query.filter_by(name="battery").one_or_none()
if (
not battery
): # legacy if-block, because create_test_battery_assets might have created it already - refactor!
battery = GenericAssetType(name="battery")
db.session.add(battery)
weather_station = GenericAssetType(name="weather station")
db.session.add(weather_station)
return dict(
public_good=public_good,
solar=solar,
wind=wind,
battery=battery,
weather_station=weather_station,
)
def create_test_asset_types(db) -> dict[str, AssetType]:
"""Make some asset types used throughout.
Deprecated. Remove with Asset model."""
solar = AssetType(
name="solar",
is_producer=True,
can_curtail=True,
daily_seasonality=True,
yearly_seasonality=True,
)
db.session.add(solar)
wind = AssetType(
name="wind",
is_producer=True,
can_curtail=True,
daily_seasonality=True,
yearly_seasonality=True,
)
db.session.add(wind)
return dict(solar=solar, wind=wind)
@pytest.fixture(scope="module")
def setup_assets(
db, setup_roles_users, setup_markets, setup_sources, setup_asset_types
) -> dict[str, Asset]:
"""Add assets to known test users.
Deprecated. Remove with Asset model."""
assets = []
for asset_name in ["wind-asset-1", "wind-asset-2", "solar-asset-1"]:
asset = Asset(
name=asset_name,
owner_id=setup_roles_users["Test Prosumer User"].id,
asset_type_name="wind" if "wind" in asset_name else "solar",
event_resolution=timedelta(minutes=15),
capacity_in_mw=1,
latitude=10,
longitude=100,
min_soc_in_mwh=0,
max_soc_in_mwh=0,
soc_in_mwh=0,
unit="MW",
market_id=setup_markets["epex_da"].id,
)
db.session.add(asset)
assets.append(asset)
# one day of test data (one complete sine curve)
time_slots = pd.date_range(
datetime(2015, 1, 1), datetime(2015, 1, 1, 23, 45), freq="15T"
)
values = [
random() * (1 + np.sin(x * 2 * np.pi / (4 * 24)))
for x in range(len(time_slots))
]
beliefs = [
TimedBelief(
event_start=as_server_time(dt),
belief_horizon=parse_duration("PT0M"),
event_value=val,
sensor=asset.corresponding_sensor,
source=setup_sources["Seita"],
)
for dt, val in zip(time_slots, values)
]
db.session.add_all(beliefs)
return {asset.name: asset for asset in assets}
@pytest.fixture(scope="module")
def setup_beliefs(db, setup_markets, setup_sources) -> int:
"""
Make some beliefs.
:returns: the number of beliefs set up
"""
return create_beliefs(db, setup_markets, setup_sources)
@pytest.fixture(scope="function")
def setup_beliefs_fresh_db(
fresh_db, setup_markets_fresh_db, setup_sources_fresh_db
) -> int:
"""
Make some beliefs.
:returns: the number of beliefs set up
"""
return create_beliefs(fresh_db, setup_markets_fresh_db, setup_sources_fresh_db)
def create_beliefs(db: SQLAlchemy, setup_markets, setup_sources) -> int:
"""
:returns: the number of beliefs set up
"""
sensor = Sensor.query.filter(Sensor.name == "epex_da").one_or_none()
beliefs = [
TimedBelief(
sensor=sensor,
source=setup_sources["ENTSO-E"],
event_value=21,
event_start="2021-03-28 16:00+01",
belief_horizon=timedelta(0),
),
TimedBelief(
sensor=sensor,
source=setup_sources["ENTSO-E"],
event_value=21,
event_start="2021-03-28 17:00+01",
belief_horizon=timedelta(0),
),
TimedBelief(
sensor=sensor,
source=setup_sources["ENTSO-E"],
event_value=20,
event_start="2021-03-28 17:00+01",
belief_horizon=timedelta(hours=2),
cp=0.2,
),
TimedBelief(
sensor=sensor,
source=setup_sources["ENTSO-E"],
event_value=21,
event_start="2021-03-28 17:00+01",
belief_horizon=timedelta(hours=2),
cp=0.5,
),
]
db.session.add_all(beliefs)
return len(beliefs)
@pytest.fixture(scope="module")
def add_market_prices(
db: SQLAlchemy, setup_assets, setup_markets, setup_sources
) -> dict[str, Sensor]:
"""Add three days of market prices for the EPEX day-ahead market."""
# one day of test data (one complete sine curve)
time_slots = initialize_index(
start=pd.Timestamp("2015-01-01").tz_localize("Europe/Amsterdam"),
end=pd.Timestamp("2015-01-02").tz_localize("Europe/Amsterdam"),
resolution="1H",
)
values = [
random() * (1 + np.sin(x * 2 * np.pi / 24)) for x in range(len(time_slots))
]
day1_beliefs = [
TimedBelief(
event_start=dt,
belief_horizon=timedelta(hours=0),
event_value=val,
source=setup_sources["Seita"],
sensor=setup_markets["epex_da"].corresponding_sensor,
)
for dt, val in zip(time_slots, values)
]
db.session.add_all(day1_beliefs)
# another day of test data (8 expensive hours, 8 cheap hours, and again 8 expensive hours)
time_slots = initialize_index(
start=pd.Timestamp("2015-01-02").tz_localize("Europe/Amsterdam"),
end=pd.Timestamp("2015-01-03").tz_localize("Europe/Amsterdam"),
resolution="1H",
)
values = [100] * 8 + [90] * 8 + [100] * 8
day2_beliefs = [
TimedBelief(
event_start=dt,
belief_horizon=timedelta(hours=0),
event_value=val,
source=setup_sources["Seita"],
sensor=setup_markets["epex_da"].corresponding_sensor,
)
for dt, val in zip(time_slots, values)
]
db.session.add_all(day2_beliefs)
# the third day of test data (8 hours with negative prices, followed by 16 expensive hours)
time_slots = initialize_index(
start=pd.Timestamp("2015-01-03").tz_localize("Europe/Amsterdam"),
end=pd.Timestamp("2015-01-04").tz_localize("Europe/Amsterdam"),
resolution="1H",
)
# consumption prices
values = [-10] * 8 + [100] * 16
day3_beliefs = [
TimedBelief(
event_start=dt,
belief_horizon=timedelta(hours=0),
event_value=val,
source=setup_sources["Seita"],
sensor=setup_markets["epex_da"].corresponding_sensor,
)
for dt, val in zip(time_slots, values)
]
db.session.add_all(day3_beliefs)
# production prices = consumption prices - 40
values = [-50] * 8 + [60] * 16
day3_beliefs_production = [
TimedBelief(
event_start=dt,
belief_horizon=timedelta(hours=0),
event_value=val,
source=setup_sources["Seita"],
sensor=setup_markets["epex_da_production"].corresponding_sensor,
)
for dt, val in zip(time_slots, values)
]
db.session.add_all(day3_beliefs_production)
yield {
"epex_da": setup_markets["epex_da"].corresponding_sensor,
"epex_da_production": setup_markets["epex_da_production"].corresponding_sensor,
}
@pytest.fixture(scope="module")
def add_battery_assets(
db: SQLAlchemy, setup_roles_users, setup_markets
) -> dict[str, Asset]:
return create_test_battery_assets(db, setup_roles_users, setup_markets)
@pytest.fixture(scope="function")
def add_battery_assets_fresh_db(
fresh_db, setup_roles_users_fresh_db, setup_markets_fresh_db, create
) -> dict[str, Asset]:
return create_test_battery_assets(
fresh_db, setup_roles_users_fresh_db, setup_markets_fresh_db
)
def create_test_battery_assets(
db: SQLAlchemy, setup_roles_users, setup_markets
) -> dict[str, Asset]:
"""
Add two battery assets, set their capacity values and their initial SOC.
"""
db.session.add(
AssetType(
name="battery",
is_consumer=True,
is_producer=True,
can_curtail=True,
can_shift=True,
daily_seasonality=True,
weekly_seasonality=True,
yearly_seasonality=True,
)
)
test_battery = Asset(
name="Test battery",
owner_id=setup_roles_users["Test Prosumer User"].id,
asset_type_name="battery",
event_resolution=timedelta(minutes=15),
capacity_in_mw=2,
max_soc_in_mwh=5,
min_soc_in_mwh=0,
soc_in_mwh=2.5,
soc_datetime=pytz.timezone("Europe/Amsterdam").localize(datetime(2015, 1, 1)),
soc_udi_event_id=203,
latitude=10,
longitude=100,
market_id=setup_markets["epex_da"].id,
unit="MW",
)
db.session.add(test_battery)
test_battery_no_prices = Asset(
name="Test battery with no known prices",
owner_id=setup_roles_users["Test Prosumer User"].id,
asset_type_name="battery",
event_resolution=timedelta(minutes=15),
capacity_in_mw=2,
max_soc_in_mwh=5,
min_soc_in_mwh=0,
soc_in_mwh=2.5,
soc_datetime=pytz.timezone("Europe/Amsterdam").localize(datetime(2040, 1, 1)),
soc_udi_event_id=203,
latitude=10,
longitude=100,
market_id=setup_markets["epex_da"].id,
unit="MW",
)
db.session.add(test_battery_no_prices)
return {
"Test battery": test_battery,
"Test battery with no known prices": test_battery_no_prices,
}
@pytest.fixture(scope="module")
def add_charging_station_assets(
db: SQLAlchemy, setup_roles_users, setup_markets
) -> dict[str, Asset]:
return create_charging_station_assets(db, setup_roles_users, setup_markets)
@pytest.fixture(scope="function")
def add_charging_station_assets_fresh_db(
fresh_db: SQLAlchemy, setup_roles_users_fresh_db, setup_markets_fresh_db
) -> dict[str, Asset]:
return create_charging_station_assets(
fresh_db, setup_roles_users_fresh_db, setup_markets_fresh_db
)
def create_charging_station_assets(
db: SQLAlchemy, setup_roles_users, setup_markets
) -> dict[str, Asset]:
"""Add uni- and bi-directional charging station assets, set their capacity value and their initial SOC."""
db.session.add(
AssetType(
name="one-way_evse",
is_consumer=True,
is_producer=False,
can_curtail=True,
can_shift=True,
daily_seasonality=True,
weekly_seasonality=True,
yearly_seasonality=True,
)
)
db.session.add(
AssetType(
name="two-way_evse",
is_consumer=True,
is_producer=True,
can_curtail=True,
can_shift=True,
daily_seasonality=True,
weekly_seasonality=True,
yearly_seasonality=True,
)
)
charging_station = Asset(
name="Test charging station",
owner_id=setup_roles_users["Test Prosumer User"].id,
asset_type_name="one-way_evse",
event_resolution=timedelta(minutes=15),
capacity_in_mw=2,
max_soc_in_mwh=5,
min_soc_in_mwh=0,
soc_in_mwh=2.5,
soc_datetime=pytz.timezone("Europe/Amsterdam").localize(datetime(2015, 1, 1)),
soc_udi_event_id=203,
latitude=10,
longitude=100,
market_id=setup_markets["epex_da"].id,
unit="MW",
)
db.session.add(charging_station)
bidirectional_charging_station = Asset(
name="Test charging station (bidirectional)",
owner_id=setup_roles_users["Test Prosumer User"].id,
asset_type_name="two-way_evse",
event_resolution=timedelta(minutes=15),
capacity_in_mw=2,
max_soc_in_mwh=5,
min_soc_in_mwh=0,
soc_in_mwh=2.5,
soc_datetime=pytz.timezone("Europe/Amsterdam").localize(datetime(2015, 1, 1)),
soc_udi_event_id=203,
latitude=10,
longitude=100,
market_id=setup_markets["epex_da"].id,
unit="MW",
)
db.session.add(bidirectional_charging_station)
return {
"Test charging station": charging_station,
"Test charging station (bidirectional)": bidirectional_charging_station,
}
@pytest.fixture(scope="module")
def add_weather_sensors(db, setup_generic_asset_types) -> dict[str, Sensor]:
return create_weather_sensors(db, setup_generic_asset_types)
@pytest.fixture(scope="function")
def add_weather_sensors_fresh_db(
fresh_db, setup_generic_asset_types_fresh_db
) -> dict[str, Sensor]:
return create_weather_sensors(fresh_db, setup_generic_asset_types_fresh_db)
def create_weather_sensors(db: SQLAlchemy, generic_asset_types) -> dict[str, Sensor]:
"""Add a weather station asset with two weather sensors."""
weather_station = GenericAsset(
name="Test weather station",
generic_asset_type=generic_asset_types["weather_station"],
latitude=33.4843866,
longitude=126,
)
db.session.add(weather_station)
wind_sensor = Sensor(
name="wind speed",
generic_asset=weather_station,
event_resolution=timedelta(minutes=5),
unit="m/s",
)
db.session.add(wind_sensor)
temp_sensor = Sensor(
name="temperature",
generic_asset=weather_station,
event_resolution=timedelta(minutes=5),
unit="°C",
)
db.session.add(temp_sensor)
return {"wind": wind_sensor, "temperature": temp_sensor}
@pytest.fixture(scope="module")
def add_sensors(db: SQLAlchemy, setup_generic_assets):
"""Add some generic sensors."""
height_sensor = Sensor(
name="height", unit="m", generic_asset=setup_generic_assets["troposphere"]
)
db.session.add(height_sensor)
return height_sensor
@pytest.fixture(scope="module")
def battery_soc_sensor(db: SQLAlchemy, setup_generic_assets):
"""Add a battery SOC sensor."""
soc_sensor = Sensor(
name="state of charge",
unit="%",
generic_asset=setup_generic_assets["test_battery"],
)
db.session.add(soc_sensor)
return soc_sensor
@pytest.fixture
def run_as_cli(app, monkeypatch):
"""
Use this to run your test as if it is run from the CLI.
This is useful where some auth restrictions (e.g. for querying) are in place.
FlexMeasures is more lenient with them if the CLI is running, as it considers
the user a sysadmin.
"""
monkeypatch.setitem(app.config, "PRETEND_RUNNING_AS_CLI", True)
@pytest.fixture(scope="function")
def clean_redis(app):
failed = app.queues["forecasting"].failed_job_registry
app.queues["forecasting"].empty()
for job_id in failed.get_job_ids():
failed.remove(app.queues["forecasting"].fetch_job(job_id))
@pytest.fixture(scope="session", autouse=True)
def error_endpoints(app):
"""Adding endpoints for the test session, which can be used to generate errors.
Adding endpoints only for testing can only be done *before* the first request
so scope=session and autouse=True are required, as well as adding them in the top
conftest module."""
@app.route("/raise-error")
def error_generator():
if "type" in request.args:
if request.args.get("type") == "server_error":
raise InternalServerError("InternalServerError Test Message")
if request.args.get("type") == "bad_request":
raise BadRequest("BadRequest Test Message")
if request.args.get("type") == "gone":
raise Gone("Gone Test Message")
if request.args.get("type") == "unauthorized":
raise Unauthorized("Unauthorized Test Message")
if request.args.get("type") == "forbidden":
raise Forbidden("Forbidden Test Message")
return jsonify({"message": "Nothing bad happened."}), 200
@app.route("/protected-endpoint-only-for-admins")
@roles_accepted(ADMIN_ROLE)
def vips_only():
return jsonify({"message": "Nothing bad happened."}), 200