def get_measurements(self): """ :rtype: MeasurementQuery """ return MeasurementQuery(Session.object_session(self)) \ .is_active() \ .has_any_gsrn(self.get_gsrn()) \ .begins_within(self.date_range.to_datetime_range()) \ .is_published()
def test__MeasurementQuery__get_distinct_begins__Measurement_exists__returns_list_of_begins(seeded_session): begins = MeasurementQuery(seeded_session) \ .get_distinct_begins() assert sorted(begins) == sorted(( datetime(2020, 1, 1, 0, 0, 0, tzinfo=timezone.utc), datetime(2020, 1, 1, 1, 0, 0, tzinfo=timezone.utc), datetime(2020, 1, 2, 1, 0, 0, tzinfo=timezone.utc), datetime(2020, 2, 1, 1, 0, 0, tzinfo=timezone.utc), datetime(2021, 2, 1, 1, 0, 0, tzinfo=timezone.utc), ))
def test__MeasurementImportController__integration(energtype_service, eloverblik_service, seeded_session): def __get_time_series(gsrn, date_from, date_to): eloverblik_response_schema = md.class_schema(GetTimeSeriesResponse) eloverblik_response = eloverblik_response_schema() if gsrn == gsrn1: return eloverblik_response.loads( IMPORTED_TIMESERIES_DATA_GSRN1).result elif gsrn == gsrn2: return eloverblik_response.loads( IMPORTED_TIMESERIES_DATA_GSRN2).result else: raise RuntimeError # Arrange eloverblik_service.get_time_series.side_effect = __get_time_series energtype_service.get_emissions.return_value = {} uut = MeasurementImportController() # Act uut.import_measurements_for(meteringpoint1, seeded_session) uut.import_measurements_for(meteringpoint2, seeded_session) seeded_session.commit() # Assert assert MeasurementQuery(seeded_session).has_gsrn(gsrn1).count() == 624 assert MeasurementQuery(seeded_session).has_gsrn( gsrn1).get_first_measured_begin().astimezone(timezone.utc) == datetime( 2019, 1, 5, 23, 0, tzinfo=timezone.utc) assert MeasurementQuery(seeded_session).has_gsrn( gsrn1).get_last_measured_begin().astimezone(timezone.utc) == datetime( 2019, 1, 31, 22, 0, tzinfo=timezone.utc) assert GgoQuery(seeded_session).has_gsrn(gsrn1).count() == 624 assert all(m.ggo is not None for m in MeasurementQuery(seeded_session).has_gsrn(gsrn1)) assert MeasurementQuery(seeded_session).has_gsrn(gsrn2).count() == 743 assert MeasurementQuery(seeded_session).has_gsrn( gsrn2).get_first_measured_begin().astimezone(timezone.utc) == datetime( 2019, 1, 1, 0, 0, tzinfo=timezone.utc) assert MeasurementQuery(seeded_session).has_gsrn( gsrn2).get_last_measured_begin().astimezone(timezone.utc) == datetime( 2019, 1, 31, 22, 0, tzinfo=timezone.utc) assert GgoQuery(seeded_session).has_gsrn(gsrn2).count() == 0 assert all(m.ggo is None for m in MeasurementQuery(seeded_session).has_gsrn(gsrn2))
def resubmit_measurements(session): """ Resubmits unpublished measurements to the ledger. These are the measurements which has not been successfully submitted to the ledger for some reason, for instance if the ledger has been down for a period of time etc. :param sqlalchemy.orm.Session session: """ measurements = MeasurementQuery(session) \ .needs_resubmit_to_ledger() \ .all() for measurement in measurements: start_submit_measurement_pipeline(measurement, measurement.meteringpoint, session)
def test__MeasurementQuery__is_published__Measurement_exists__returns_correct_Measurements(seeded_session, is_published): query = MeasurementQuery(seeded_session) \ .is_published(is_published) assert query.count() > 0 assert all(m.published == is_published for m in query.all())
def test__MeasurementQuery__begins_within__Measurement_does_not_exist__returs_nothing(seeded_session, begin, end): query = MeasurementQuery(seeded_session) \ .begins_within(DateTimeRange(begin=begin, end=end)) assert query.count() == 0
def test__MeasurementQuery__is_type__Measurement_exists__returns_correct_Measurements(seeded_session, type): query = MeasurementQuery(seeded_session) \ .is_type(type) assert query.count() > 0 assert all(m.type == type for m in query.all())
def test__MeasurementQuery__is_consumption__Measurement_exists__returns_correct_Measurements(seeded_session): query = MeasurementQuery(seeded_session) \ .is_consumption() assert query.count() > 0 assert all(m.type == MeasurementType.CONSUMPTION for m in query.all())
def test__MeasurementQuery__has_any_gsrn__Measurement_exists__returns_correct_Measurements(seeded_session, gsrn): query = MeasurementQuery(seeded_session) \ .has_any_gsrn(gsrn) assert query.count() > 0 assert all(m.gsrn in gsrn for m in query.all())
def test__MeasurementQuery__has_any_gsrn__Measurement_does_not_exist__returs_nothing(seeded_session): query = MeasurementQuery(seeded_session) \ .has_any_gsrn(['a GSRN not present in the database', 'another']) assert query.count() == 0 assert query.one_or_none() is None
def test__MeasurementQuery__get_last_measured_begin__Measurement_does_not_exist__returns_None(seeded_session): begin = MeasurementQuery(seeded_session) \ .has_id(-1) \ .get_last_measured_begin() assert begin is None
def test__MeasurementQuery__belongs_to__returns_correct_measurements(seeded_session, sub): query = MeasurementQuery(seeded_session) \ .belongs_to(sub) assert query.count() > 0 assert all(m.sub == sub for m in query.all())
def test__MeasurementQuery__get_distinct_begins__Measurement_does_not_exist__returs_empty_list(seeded_session): begins = MeasurementQuery(seeded_session) \ .has_id(-1) \ .get_distinct_begins() assert begins == []
def test__MeasurementQuery__begins_within__returns_correct_measurements(seeded_session, begin, end): query = MeasurementQuery(seeded_session) \ .begins_within(DateTimeRange(begin=begin, end=end)) assert query.count() > 0 assert all(begin <= m.begin <= end for m in query.all())
def test__MeasurementQuery__begins_at__Measurement_does_not_exist__returs_nothing(seeded_session, begin): query = MeasurementQuery(seeded_session) \ .begins_at(begin) assert query.count() == 0
def test__MeasurementQuery__begins_at__returns_correct_measurements(seeded_session, begin): query = MeasurementQuery(seeded_session) \ .begins_at(begin) assert query.count() > 0 assert all(m.begin == begin for m in query.all())
def test__MeasurementQuery__belongs_to__Measurement_does_not_exist__returs_nothing(seeded_session): query = MeasurementQuery(seeded_session) \ .belongs_to('a subject that does not exists') assert query.count() == 0
def test__MeasurementQuery__get_last_measured_begin__Measurement_exists__returns_correct_begin(seeded_session): begin = MeasurementQuery(seeded_session) \ .get_last_measured_begin() assert begin == datetime(2021, 2, 1, 1, 0, 0, tzinfo=timezone.utc)
def test__MeasurementQuery__has_id__Measurement_exists__returns_correct_Measurement(seeded_session, measurement_id): query = MeasurementQuery(seeded_session) \ .has_id(measurement_id) assert query.count() == 1 assert query.one().id == measurement_id
def test__MeasurementQuery__get_summary__returns_new_MeasurementSummary(seeded_session): summary = MeasurementQuery(seeded_session) \ .get_summary(SummaryResolution.day, []) assert isinstance(summary, MeasurementSummary)
def test__MeasurementQuery__has_id__Measurement_does_not_exist__returs_nothing(seeded_session, measurement_id): query = MeasurementQuery(seeded_session) \ .has_id(measurement_id) assert query.count() == 0 assert query.one_or_none() is None