def test_get_mapped_value(key, multilingual, result):
    file_ = File('Test', '/api/attachments/1', 'main')
    document = Document(id='test', title='Test', category='main', doctype='decree', files=[file_],
                        enactment_date=datetime.date.today(), subtype='Liestal', authority='Office')
    source = OEREBlexSource(host='http://oereblex.example.com', language='de', canton='BL',
                            mapping={'municipality': 'subtype'})
    assert source._get_mapped_value(document, key, multilingual) == result
Esempio n. 2
0
class DatabaseOEREBlexSource(DatabaseSource):
    """
    A source to get oereblex documents attached to public law restrictions in replacement
    of standards documents. Be sure to use a model with an OEREBlex "lexlink" integer
    column for plrs that use this source.
    """
    def __init__(self, **kwargs):
        """
        Keyword Arguments:
            name (str): The name. You are free to choose one.
            code (str): The official code. Regarding to the federal specifications.
            geometry_type (str): The geometry type. Possible are: POINT, POLYGON, LINESTRING,
                GEOMETRYCOLLECTION
            thresholds (dict): The configuration of limits and units used for processing.
            text (dict of str): The speaking title. It must be a dictionary containing language (as
                configured) as key and text as value.
            language (str): The language this public law restriction is originally shipped with.
            federal (bool): Switch if it is a federal topic. This will be taken into account in processing
                steps.
            source (dict): The configuration dictionary of the public law restriction
            hooks (dict of str): The hook methods: get_symbol, get_symbol_ref. They have to be provided as
                dotted string for further use with dotted name resolver of pyramid package.
            law_status (dict of str): The multiple match configuration to provide more flexible use of the
                federal specified classifiers 'inForce' and 'runningModifications'.
        """
        super(DatabaseOEREBlexSource, self).__init__(**kwargs)
        self._oereblex_source = OEREBlexSource(**Config.get_oereblex_config())
        self._queried_lexlinks = {}

    def get_document_records(self, params, public_law_restriction_from_db):
        """
        Override the parent's get_document_records method to obtain the oereblex document instead.
        """
        return self.document_records_from_oereblex(params, public_law_restriction_from_db.geolink)

    def document_records_from_oereblex(self, params, lexlink):
        """
        Create document records parsed from the OEREBlex response with the specified geoLink ID and appends
        them to the current public law restriction.

        Args:
            params (pyramid_oereb.views.webservice.Parameter): The parameters of the extract request.
            lexlink (int): The ID of the geoLink to request the documents for.

        Returns:
            list of pyramid_oereb.lib.records.documents.DocumentRecord:
                The documents created from the parsed OEREBlex response.
        """
        log.debug("document_records_from_oereblex() start")
        if lexlink in self._queried_lexlinks:
            log.debug('skip querying this lexlink "{}" because it was fetched already.'.format(lexlink))
            log.debug('use already queried instead')
            return self._queried_lexlinks[lexlink]
        else:
            self._oereblex_source.read(params, lexlink)
            log.debug("document_records_from_oereblex() returning {} records"
                      .format(len(self._oereblex_source.records)))
            self._queried_lexlinks[lexlink] = self._oereblex_source.records
            return self._queried_lexlinks[lexlink]
def test_read_with_specified_version():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.0.0.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/1.0.0/geolinks/100.xml', content=f.read())
        source = OEREBlexSource(host='http://oereblex.example.com', language='de', canton='BL',
                                pass_version=True, version='1.0.0')
        source.read(100)
        assert len(source.records) == 2
def test_read_with_version_in_url():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.1.1.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/1.1.1/geolinks/100.xml', content=f.read())
        source = OEREBlexSource(host='http://oereblex.example.com', language='de', canton='BL',
                                pass_version=True)
        source.read(MockParameter(), 100)
        assert len(source.records) == 2
def test_read_with_specified_language():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.1.1.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/geolinks/100.xml?locale=fr', content=f.read())
        source = OEREBlexSource(host='http://oereblex.example.com', language='de', canton='BL')
        params = MockParameter()
        params.set_language('fr')
        source.read(params, 100)
        assert len(source.records) == 2
        document = source.records[0]
        assert document.responsible_office.name == {'fr': 'Landeskanzlei'}
        assert document.text_at_web == {
            'fr': 'http://oereblex.example.com/api/attachments/313'
        }
def test_authentication():
    auth = {
        'username': '******',
        'password': '******'
    }
    source = OEREBlexSource(host='http://oereblex.example.com', language='de', canton='BL', auth=auth)
    assert isinstance(source._auth, HTTPBasicAuth)
def test_read():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.1.1.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/geolinks/100.xml', content=f.read())
        source = OEREBlexSource(host='http://oereblex.example.com', language='de', canton='BL')
        source.read(100)
        assert len(source.records) == 2
        document = source.records[0]
        assert isinstance(document, DocumentRecord)
        assert isinstance(document.responsible_office, OfficeRecord)
        assert document.responsible_office.name == {'de': 'Landeskanzlei'}
        assert document.canton == 'BL'
        assert document.text_at_web == {
            'de': 'http://oereblex.example.com/api/attachments/313'
        }
        assert len(document.references) == 5
def test_read_related_notice_as_main():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.2.0.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/geolinks/100.xml',
                  content=f.read())
        source = OEREBlexSource(host='http://oereblex.example.com',
                                language='de',
                                canton='BL',
                                related_notice_as_main=True)
        source.read(MockParameter(), 100)
        assert len(source.records) == 6
        document = source.records[5]
        assert isinstance(document, HintRecord)
        assert isinstance(document.responsible_office, OfficeRecord)
        assert document.responsible_office.name == {'de': '-'}
        assert document.responsible_office.office_at_web is None
        assert document.published_from == datetime.date(1970, 1, 1)
        assert len(document.references) == 3
Esempio n. 9
0
 def __init__(self, **kwargs):
     """
     Keyword Arguments:
         name (str): The name. You are free to choose one.
         code (str): The official code. Regarding to the federal specifications.
         geometry_type (str): The geometry type. Possible are: POINT, POLYGON, LINESTRING,
             GEOMETRYCOLLECTION
         thresholds (dict): The configuration of limits and units used for processing.
         text (dict of str): The speaking title. It must be a dictionary containing language (as
             configured) as key and text as value.
         language (str): The language this public law restriction is originally shipped whith.
         federal (bool): Switch if it is a federal topic. This will be taken into account in processing
             steps.
         source (dict): The configuration dictionary of the public law restriction
         hooks (dict of str): The hook methods: get_symbol, get_symbol_ref. They have to be provided as
             dotted string for further use with dotted name resolver of pyramid package.
         law_status (dict of str): The multiple match configuration to provide more flexible use of the
             federal specified classifiers 'inForce' and 'runningModifications'.
     """
     super(DatabaseOEREBlexSource, self).__init__(**kwargs)
     self._oereblex_source = OEREBlexSource(**Config.get_oereblex_config())
def test_read_related_decree_as_main():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.2.0.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/geolinks/100.xml',
                  content=f.read())
        source = OEREBlexSource(host='http://oereblex.example.com',
                                language='de',
                                canton='BL',
                                related_decree_as_main=True)
        source.read(MockParameter(), 100)
        assert len(source.records) == 5
        document = source.records[0]
        assert isinstance(document, DocumentRecord)
        assert isinstance(document.responsible_office, OfficeRecord)
        assert document.responsible_office.name == {
            'de': 'Bauverwaltung Gemeinde'
        }
        assert document.canton == 'BL'
        assert document.text_at_web == {
            'de': 'http://oereblex.example.com/api/attachments/4735'
        }
        assert len(document.references) == 4
def test_get_document_records(i, document):
    language = 'de'
    source = OEREBlexSource(host='http://oereblex.example.com',
                            language='de',
                            canton='BL')
    references = [
        Document(id='ref',
                 title='Reference',
                 category='related',
                 doctype='edict',
                 authority='Office',
                 files=[File('Reference file', '/api/attachments/4', 'main')],
                 enactment_date=datetime.date.today())
    ]

    if i == 3:
        with pytest.raises(TypeError):
            source._get_document_records(document, language, references)
    elif i == 4:
        assert source._get_document_records(document, language,
                                            references) == []
    else:
        records = source._get_document_records(document, language, references)
        assert len(records) == i
        for idx, record in enumerate(records):
            if i == 1:
                assert isinstance(record, DocumentRecord)
            elif i == 2:
                assert isinstance(record, LegalProvisionRecord)
            assert record.title == {'de': 'Document {0}'.format(i)}
            assert record.published_from == datetime.date.today()
            assert record.canton == 'BL'
            assert record.text_at_web == {
                'de': '/api/attachments/{fid}'.format(fid=i + idx)
            }
            assert len(record.references) == 1
            reference = record.references[0]
            assert isinstance(reference, DocumentRecord)
            assert reference.title == {'de': 'Reference'}
            assert reference.canton == 'BL'
            assert reference.text_at_web == {'de': '/api/attachments/4'}
def test_init(valid, cfg):
    if valid:
        assert isinstance(OEREBlexSource(**cfg), OEREBlexSource)
    else:
        with pytest.raises(AssertionError):
            OEREBlexSource(**cfg)
def test_get_document_title():
    document = Document([], id='1', title='Test')
    result = {'de': 'Test'}
    assert OEREBlexSource._get_document_title(document, File(), 'de') == result