def test__FacilityQuery__has_any_gsrn__no_ids_exists__returns_nothing(
        seeded_session, gsrn):

    query = FacilityQuery(seeded_session) \
        .has_any_gsrn(gsrn)

    assert query.count() == 0
def test__FacilityQuery__has_any_public_id__no_ids_exists__returns_nothing(
        seeded_session, public_ids):

    query = FacilityQuery(seeded_session) \
        .has_any_public_id(public_ids)

    assert query.count() == 0
def test__FacilityQuery__is_consumer__returns_correct_facilities(seeded_session):

    query = FacilityQuery(seeded_session) \
        .is_consumer()

    assert query.count() > 0
    assert all(f.facility_type == FacilityType.CONSUMPTION for f in query.all())
def test__FacilityQuery__is_retire_receiver__returns_correct_facilities(seeded_session):

    query = FacilityQuery(seeded_session) \
        .is_retire_receiver()

    assert query.count() > 0
    assert all(f.retiring_priority is not None for f in query.all())
def test__FacilityQuery__has_any_gsrn__some_ids_exists__returns_correct_facility(
        seeded_session, gsrn):

    query = FacilityQuery(seeded_session) \
        .has_any_gsrn(gsrn)

    assert query.count() > 0
    assert all(f.gsrn in gsrn for f in query.all())
def test__FacilityQuery__is_type__returns_correct_facilities(
        seeded_session, facility_type):

    query = FacilityQuery(seeded_session) \
        .is_type(facility_type)

    assert query.count() > 0
    assert all(f.facility_type == facility_type for f in query.all())
def test__FacilityQuery__has_gsrn__Facility_exists__returns_correct_facility(
        seeded_session, gsrn):

    query = FacilityQuery(seeded_session) \
        .has_gsrn(gsrn)

    assert query.count() == 1
    assert query.one().gsrn == gsrn
def test__FacilityQuery__has_any_public_id__some_ids_exists__returns_correct_facility(
        seeded_session, public_ids):

    query = FacilityQuery(seeded_session) \
        .has_any_public_id(public_ids)

    assert query.count() > 0
    assert all(f.public_id in public_ids for f in query.all())
def test__FacilityQuery__has_public_id__Facility_does_not_exist__returs_nothing(
        seeded_session, public_id):

    query = FacilityQuery(seeded_session) \
        .has_public_id(public_id)

    assert query.count() == 0
    assert query.one_or_none() is None
def test__FacilityQuery__has_public_id__Facility_exists__returns_correct_facility(
        seeded_session, public_id):

    query = FacilityQuery(seeded_session) \
        .has_public_id(public_id)

    assert query.count() == 1
    assert query.one().public_id == public_id
def test__FacilityQuery__belongs_to__Facility_exists__returns_correct_facility(
        seeded_session, user):

    query = FacilityQuery(seeded_session) \
        .belongs_to(user)

    assert query.count() > 0
    assert all(f.user_id == user.id for f in query.all())
def test__FacilityQuery__has_gsrn__Facility_does_not_exist__returs_nothing(
        seeded_session, gsrn):

    query = FacilityQuery(seeded_session) \
        .has_gsrn(gsrn)

    assert query.count() == 0
    assert query.one_or_none() is None
def test__FacilityQuery__is_eligible_to_retire__sector_does_not_exists__returns_nothing(
        seeded_session, ggo_sector):

    ggo = Mock(sector='NO-FACILITIES-HAS-THIS-SECTOR')

    query = FacilityQuery(seeded_session) \
        .is_eligible_to_retire(ggo)

    assert query.count() == 0
def test__FacilityQuery__is_eligible_to_retire__sector_exists__returns_correct_facilities(
        seeded_session, ggo_sector):

    ggo = Mock(sector=ggo_sector)

    query = FacilityQuery(seeded_session) \
        .is_eligible_to_retire(ggo)

    assert query.count() > 0
    assert all(f.sector == ggo_sector for f in query.all())
    assert all(f.facility_type == FacilityType.CONSUMPTION for f in query.all())
Beispiel #15
0
    def get_facilities(self, user, filters, session):
        """
        :param User user:
        :param FacilityFilters filters:
        :param Session session:
        :rtype: list[Facility]
        """
        query = FacilityQuery(session) \
            .belongs_to(user)

        if filters is not None:
            query = query.apply_filters(filters)

        return query.all()
Beispiel #16
0
    def get_gsrn_numbers(self, user, filters, session):
        """
        :param User user:
        :param FacilityFilters filters:
        :param Session session:
        :rtype: list[str]
        """
        query = FacilityQuery(session) \
            .belongs_to(user)

        if filters is not None:
            query = query.apply_filters(filters)

        return query.get_distinct_gsrn()
Beispiel #17
0
    def handle_request(self, request, user, session):
        """
        :param CreateDisclosureRequest request:
        :param User user:
        :param Session session:
        :rtype: CreateDisclosureResponse
        """
        facilities = FacilityQuery(session) \
            .belongs_to(user) \
            .is_consumer() \
            .has_any_gsrn(request.gsrn) \
            .all()

        if not facilities:
            raise BadRequest('No [consuming] facilities selected')

        request = dh.CreateDisclosureRequest(
            name=request.name,
            description=request.description,
            gsrn=request.gsrn,
            begin=request.date_range.begin,
            end=request.date_range.end,
            max_resolution=request.max_resolution,
            publicize_meteringpoints=request.publicize_meteringpoints,
            publicize_gsrn=request.publicize_gsrn,
            publicize_physical_address=request.publicize_physical_address,
        )

        response = datahub.create_disclosure(user.access_token, request)

        return CreateDisclosureResponse(
            success=True,
            id=response.id,
        )
def test__FacilityQuery__get_distinct_sectors__facilities_does_notexists__returns_empty_list(seeded_session):

    distinct_sectors = FacilityQuery(seeded_session) \
        .has_gsrn('NO-FACILITY-HAS-THIS-GSRN') \
        .get_distinct_sectors()

    assert sorted(distinct_sectors) == []
def save_imported_meteringpoints(user, response, session):
    """
    :param originexample.auth.User user:
    :param originexample.services.datahub.GetMeteringPointsResponse response:
    :param sqlalchemy.orm.Session session:
    :rtype: list[Facility]
    """
    imported_facilities = []

    for meteringpoint in response.meteringpoints:
        count = FacilityQuery(session) \
            .has_gsrn(meteringpoint.gsrn) \
            .count()

        if count > 0:
            logger.info(
                f'Skipping meteringpoint with GSRN: {meteringpoint.gsrn} (already exists in DB)',
                extra={
                    'gsrn': meteringpoint.gsrn,
                    'subject': user.sub,
                    'pipeline': 'import_meteringpoints',
                    'task': 'import_meteringpoints_and_insert_to_db',
                })
            continue

        if meteringpoint.type is MeteringPointType.PRODUCTION:
            facility_type = FacilityType.PRODUCTION
        elif meteringpoint.type is MeteringPointType.CONSUMPTION:
            facility_type = FacilityType.CONSUMPTION
        else:
            raise RuntimeError('Should NOT have happened!')

        imported_facilities.append(
            Facility(
                user=user,
                gsrn=meteringpoint.gsrn,
                sector=meteringpoint.sector,
                facility_type=facility_type,
                technology_code=meteringpoint.technology_code,
                fuel_code=meteringpoint.fuel_code,
                street_code=meteringpoint.street_code,
                street_name=meteringpoint.street_name,
                building_number=meteringpoint.building_number,
                city_name=meteringpoint.city_name,
                postcode=meteringpoint.postcode,
                municipality_code=meteringpoint.municipality_code,
            ))

        logger.info(f'Imported meteringpoint with GSRN: {meteringpoint.gsrn}',
                    extra={
                        'gsrn': meteringpoint.gsrn,
                        'subject': user.sub,
                        'pipeline': 'import_meteringpoints',
                        'task': 'import_meteringpoints_and_insert_to_db',
                    })

    session.add_all(imported_facilities)
    session.flush()

    return imported_facilities
 def get_facilities(self, user, facility_public_ids, session):
     """
     :param User user:
     :param list[str] facility_public_ids:
     :param Session session:
     """
     return FacilityQuery(session) \
         .belongs_to(user) \
         .has_any_public_id(facility_public_ids) \
         .all()
 def get_facilities(self, ggo):
     """
     :param Ggo ggo:
     :rtype: list[Facility]
     """
     return FacilityQuery(self.session) \
         .belongs_to(self.agreement.user_to) \
         .is_retire_receiver() \
         .is_eligible_to_retire(ggo) \
         .all()
 def get_facilities(self, user, facility_gsrn, session):
     """
     :param User user:
     :param list[str] facility_gsrn:
     :param Session session:
     :rtype: list[Facility]
     """
     return FacilityQuery(session) \
         .belongs_to(user) \
         .has_any_gsrn(facility_gsrn) \
         .all()
    def facility_exists(self, gsrn, session):
        """
        :param str gsrn:
        :param sqlalchemy.orm.Session session:
        :rtype: bool
        """
        count = FacilityQuery(session) \
            .has_gsrn(gsrn) \
            .count()

        return count > 0
    def get_retire_consumers(self, user, ggo, session):
        """
        TODO test this

        :param User user:
        :param Ggo ggo:
        :param sqlalchemy.orm.Session session:
        :rtype: collections.abc.Iterable[RetiringConsumer]
        """
        facilities = FacilityQuery(session) \
            .belongs_to(user) \
            .is_retire_receiver() \
            .is_eligible_to_retire(ggo) \
            .order_by(Facility.retiring_priority.asc()) \
            .all()

        for facility in facilities:
            yield RetiringConsumer(facility)
def test__FacilityQuery__get_distinct_sectors__facilities_exists__returns_list_of_correct_sectors(seeded_session):

    distinct_sectors = FacilityQuery(seeded_session) \
        .get_distinct_sectors()

    assert sorted(distinct_sectors) == ['DK1', 'DK2']
def test__FacilityQuery__belongs_to__Facility_does_not_exists__returns_nothing(seeded_session):

    query = FacilityQuery(seeded_session) \
        .belongs_to(user4)

    assert query.count() == 0