def add_battery_assets(db: SQLAlchemy, setup_roles_users, setup_markets): """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, ) ) from flexmeasures.data.models.user import User, Role user_datastore = SQLAlchemySessionUserDatastore(db.session, User, Role) test_prosumer = user_datastore.find_user(email="*****@*****.**") epex_da = Market.query.filter(Market.name == "epex_da").one_or_none() battery = Asset( name="Test battery", 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=as_server_time(datetime(2015, 1, 1)), soc_udi_event_id=203, latitude=10, longitude=100, market_id=epex_da.id, unit="MW", ) battery.owner = test_prosumer db.session.add(battery) battery = Asset( name="Test battery with no known prices", 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=as_server_time(datetime(2040, 1, 1)), soc_udi_event_id=203, latitude=10, longitude=100, market_id=epex_da.id, unit="MW", ) battery.owner = test_prosumer db.session.add(battery)
def setup_assets(db, setup_roles_users, setup_markets): """Make some asset types and add assets to known test users.""" data_source = DataSource(name="Seita", type="demo script") db.session.add(data_source) db.session.add( AssetType( name="solar", is_producer=True, can_curtail=True, daily_seasonality=True, yearly_seasonality=True, ) ) db.session.add( AssetType( name="wind", is_producer=True, can_curtail=True, daily_seasonality=True, yearly_seasonality=True, ) ) test_prosumer = find_user_by_email("*****@*****.**") test_market = Market.query.filter_by(name="epex_da").one_or_none() for asset_name in ["wind-asset-1", "wind-asset-2", "solar-asset-1"]: asset = Asset( name=asset_name, 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=test_market.id, ) asset.owner = test_prosumer db.session.add(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 / 15)) for x in range(len(time_slots))] for dt, val in zip(time_slots, values): p = Power( datetime=as_server_time(dt), horizon=parse_duration("PT0M"), value=val, data_source_id=data_source.id, ) p.asset = asset db.session.add(p)
def setup_ui_test_data( db, setup_accounts, setup_roles_users, setup_markets, setup_sources, setup_asset_types, ): """ Create another prosumer, without data, and an admin Also, a weather sensor (and sensor type). TODO: review if any of these are really needed (might be covered now by main conftest) """ print("Setting up data for UI tests on %s" % db.engine) create_user( username="******", email="*****@*****.**", password="******", account_name=setup_accounts["Prosumer"].name, user_roles=dict(name="admin", description="A site admin."), ) test_user_ui = create_user( username="******", email="*****@*****.**", password="******", account_name=setup_accounts["Prosumer"].name, ) asset = Asset( name="solar pane 1", display_name="Solar Pane 1", asset_type_name="solar", unit="MW", capacity_in_mw=10, latitude=10, longitude=100, min_soc_in_mwh=0, max_soc_in_mwh=0, soc_in_mwh=0, ) db.session.add(asset) asset.owner = test_user_ui # Create 1 weather sensor test_sensor_type = WeatherSensorType(name="irradiance") db.session.add(test_sensor_type) sensor = WeatherSensor( name="irradiance_sensor", weather_sensor_type_name="irradiance", latitude=33.4843866, longitude=126, unit="kW/m²", ) db.session.add(sensor) print("Done setting up data for UI tests")
def setup_ui_test_data(db): """ Create another prosumer, without data, and an admin Also, a weather sensor (and sensor type). """ print("Setting up data for UI tests on %s" % db.engine) create_user( username="******", email="*****@*****.**", password=hash_password("testtest"), user_roles=dict(name="admin", description="A site admin."), ) test_prosumer2 = create_user( username="******", email="*****@*****.**", password=hash_password("testtest"), user_roles=dict(name="Prosumer", description="A Prosumer with one asset but no data."), ) asset = Asset( name="solar pane 1", display_name="Solar Pane 1", asset_type_name="solar", unit="MW", capacity_in_mw=10, latitude=10, longitude=100, min_soc_in_mwh=0, max_soc_in_mwh=0, soc_in_mwh=0, ) db.session.add(asset) asset.owner = test_prosumer2 # Create 1 weather sensor test_sensor_type = WeatherSensorType(name="radiation") db.session.add(test_sensor_type) sensor = WeatherSensor( name="radiation_sensor", weather_sensor_type_name="radiation", latitude=33.4843866, longitude=126, unit="kW/m²", ) db.session.add(sensor) print("Done setting up data for UI tests")
def setup_fresh_test_data( fresh_db, setup_markets_fresh_db, setup_roles_users_fresh_db, setup_generic_asset_types_fresh_db, app, fresh_remove_seasonality_for_power_forecasts, ): db = fresh_db setup_roles_users = setup_roles_users_fresh_db setup_markets = setup_markets_fresh_db data_source = DataSource(name="Seita", type="demo script") db.session.add(data_source) db.session.flush() for asset_name in ["wind-asset-2", "solar-asset-1"]: asset = Asset( name=asset_name, 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, ) asset.owner = setup_roles_users["Test Prosumer User"] db.session.add(asset) time_slots = pd.date_range( datetime(2015, 1, 1), datetime(2015, 1, 1, 23, 45), freq="15T" ) values = [random() * (1 + np.sin(x / 15)) 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=data_source, ) for dt, val in zip(time_slots, values) ] db.session.add_all(beliefs) add_test_weather_sensor_and_forecasts(fresh_db, setup_generic_asset_types_fresh_db)
def setup_api_test_data(db): """ Set up data for API v1 tests. """ print("Setting up data for API v1 tests on %s" % db.engine) from flexmeasures.data.models.user import User, Role from flexmeasures.data.models.assets import Asset, AssetType, Power from flexmeasures.data.models.data_sources import DataSource user_datastore = SQLAlchemySessionUserDatastore(db.session, User, Role) # Create an anonymous user create_user( username="******", email="*****@*****.**", password=hash_password("testtest"), user_roles=[ "Prosumer", dict(name="anonymous", description="Anonymous test user"), ], ) # Create 1 test asset for the anonymous user test_prosumer = user_datastore.find_user(email="*****@*****.**") test_asset_type = AssetType(name="test-type") db.session.add(test_asset_type) asset_names = ["CS 0"] assets: List[Asset] = [] for asset_name in asset_names: asset = Asset( name=asset_name, asset_type_name="test-type", event_resolution=timedelta(minutes=15), capacity_in_mw=1, latitude=100, longitude=100, unit="MW", ) asset.owner = test_prosumer assets.append(asset) db.session.add(asset) # Create a test user without a USEF role create_user( username="******", email="*****@*****.**", password=hash_password("testtest"), ) # Create 5 test assets for the test_prosumer user test_prosumer = user_datastore.find_user(email="*****@*****.**") asset_names = ["CS 1", "CS 2", "CS 3", "CS 4", "CS 5"] assets: List[Asset] = [] for asset_name in asset_names: asset = Asset( name=asset_name, asset_type_name="test-type", event_resolution=timedelta(minutes=15), capacity_in_mw=1, latitude=100, longitude=100, unit="MW", ) asset.owner = test_prosumer 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() test_supplier = user_datastore.find_user(email="*****@*****.**") prosumer_data_source = DataSource.query.filter( DataSource.user == test_prosumer).one_or_none() supplier_data_source = DataSource.query.filter( DataSource.user == test_supplier).one_or_none() meter_data = [] for i in range(6): p_1 = Power( datetime=isodate.parse_datetime("2015-01-01T00:00:00Z") + timedelta(minutes=15 * i), horizon=timedelta(0), value=(100.0 + i) * -1, asset_id=cs_5.id, data_source_id=prosumer_data_source.id, ) p_2 = Power( datetime=isodate.parse_datetime("2015-01-01T00:00:00Z") + timedelta(minutes=15 * i), horizon=timedelta(hours=0), value=(1000.0 - 10 * i) * -1, asset_id=cs_5.id, data_source_id=supplier_data_source.id, ) meter_data.append(p_1) meter_data.append(p_2) db.session.bulk_save_objects(meter_data) print("Done setting up data for API v1 tests")
def add_charging_station_assets(db: SQLAlchemy, setup_roles_users, setup_markets): """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, ) ) from flexmeasures.data.models.user import User, Role user_datastore = SQLAlchemySessionUserDatastore(db.session, User, Role) test_prosumer = user_datastore.find_user(email="*****@*****.**") epex_da = Market.query.filter(Market.name == "epex_da").one_or_none() charging_station = Asset( name="Test charging station", 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=as_server_time(datetime(2015, 1, 1)), soc_udi_event_id=203, latitude=10, longitude=100, market_id=epex_da.id, unit="MW", ) charging_station.owner = test_prosumer db.session.add(charging_station) bidirectional_charging_station = Asset( name="Test charging station (bidirectional)", 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=as_server_time(datetime(2015, 1, 1)), soc_udi_event_id=203, latitude=10, longitude=100, market_id=epex_da.id, unit="MW", ) bidirectional_charging_station.owner = test_prosumer db.session.add(bidirectional_charging_station)
def setup_api_test_data(db): """ Set up data for API v1.1 tests. """ print("Setting up data for API v1.1 tests on %s" % db.engine) from flexmeasures.data.models.user import User, Role from flexmeasures.data.models.assets import Asset, AssetType from flexmeasures.data.models.weather import WeatherSensor, WeatherSensorType user_datastore = SQLAlchemySessionUserDatastore(db.session, User, Role) # Create a user without proper registration as a data source user = user_datastore.create_user( username="******", email="*****@*****.**", password=hash_password("testtest"), ) role = user_datastore.find_role("Prosumer") user_datastore.add_role_to_user(user, role) # Create a test user without a USEF role create_user( username="******", email="*****@*****.**", password=hash_password("testtest"), ) # Create 3 test assets for the test_prosumer user test_prosumer = user_datastore.find_user(email="*****@*****.**") test_asset_type = AssetType(name="test-type") db.session.add(test_asset_type) asset_names = ["CS 1", "CS 2", "CS 3"] assets: List[Asset] = [] for asset_name in asset_names: asset = Asset( name=asset_name, asset_type_name="test-type", event_resolution=timedelta(minutes=15), capacity_in_mw=1, latitude=100, longitude=100, unit="MW", ) asset.owner = test_prosumer assets.append(asset) 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() data_source = DataSource.query.filter( DataSource.user == test_prosumer).one_or_none() power_forecasts = [] for i in range(6): p_1 = Power( datetime=isodate.parse_datetime("2015-01-01T00:00:00Z") + timedelta(minutes=15 * i), horizon=timedelta(hours=6), value=(300 + i) * -1, asset_id=cs_1.id, data_source_id=data_source.id, ) p_2 = Power( datetime=isodate.parse_datetime("2015-01-01T00:00:00Z") + timedelta(minutes=15 * i), horizon=timedelta(hours=6), value=(300 - i) * -1, asset_id=cs_2.id, data_source_id=data_source.id, ) p_3 = Power( datetime=isodate.parse_datetime("2015-01-01T00:00:00Z") + timedelta(minutes=15 * i), horizon=timedelta(hours=6), value=(0 + i) * -1, asset_id=cs_3.id, data_source_id=data_source.id, ) power_forecasts.append(p_1) power_forecasts.append(p_2) power_forecasts.append(p_3) db.session.bulk_save_objects(power_forecasts) # Create 2 weather sensors test_sensor_type = WeatherSensorType(name="wind_speed") db.session.add(test_sensor_type) sensor = WeatherSensor( name="wind_speed_sensor", weather_sensor_type_name="wind_speed", event_resolution=timedelta(minutes=5), latitude=33.4843866, longitude=126, unit="m/s", ) db.session.add(sensor) test_sensor_type = WeatherSensorType(name="temperature") db.session.add(test_sensor_type) sensor = WeatherSensor( name="temperature_sensor", weather_sensor_type_name="temperature", event_resolution=timedelta(minutes=5), latitude=33.4843866, longitude=126, unit="°C", ) db.session.add(sensor) print("Done setting up data for API v1.1 tests")