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
Exemplo n.º 2
0
def document_without_file():
    return [
        Document(
            id='1',
            title='Document without file',
            category='main',
            doctype='decree',
            files=[],
            enactment_date=datetime.date(2017, 1, 15)
        )
    ]
Exemplo n.º 3
0
def documents():
    return [
        Document(
            id='1',
            title='Document with file',
            category='main',
            doctype='decree',
            files=[File(description='Test file', title='test.pdf',
                        href='http://www.example.com/test.pdf', category='main')],
            enactment_date=datetime.date(2017, 1, 15)
        )
    ]
Exemplo n.º 4
0
def document_archived():
    return [
        Document(
            id='1',
            title='Archived document',
            category='main',
            doctype='decree',
            files=[File(description='Test file', title='test.pdf',
                        href='http://www.example.com/test.pdf', category='main')],
            enactment_date=datetime.date(2017, 1, 15),
            abrogation_date=datetime.date(2019, 1, 1)
        )
    ]
Exemplo n.º 5
0
def test_document_empty():
    d = Document([])
    assert isinstance(d, Document)
    assert d.id is None
    assert d.title is None
    assert d.category is None
    assert d.doctype is None
    assert len(d.files) == 0 and isinstance(d.files, list)
    assert d.enactment_date is None
    assert d.federal_level is None
    assert d.authority is None
    assert d.authority_url is None
    assert d.type is None
    assert d.subtype is None
    assert d.decree_date is None
    assert d.instance is None
    assert d.number is None
    assert d.abbreviation is None
    assert d.abrogation_date is None
    assert d.cycle is None
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'}
Exemplo n.º 7
0
def test_document():
    date = datetime.date.today()
    d = Document(id='1',
                 title='Test',
                 category='test',
                 doctype='testdoc',
                 files=[
                     File('TestCategory', 'http://my.link.to/file',
                          'Test Title', 'test.pdf')
                 ],
                 enactment_date=date,
                 federal_level='testlevel',
                 authority='Authority',
                 authority_url='http://my.link.to/authority',
                 type='testtype',
                 subtype='testsubtype',
                 decree_date=date,
                 instance='INST',
                 number='123',
                 abbreviation='abbr',
                 abrogation_date=date,
                 cycle='cycle')
    assert isinstance(d, Document)
    assert d.id == '1'
    assert d.title == 'Test'
    assert d.category == 'test'
    assert d.doctype == 'testdoc'
    assert len(d.files) == 1 and isinstance(d.files[0], File)
    assert d.enactment_date == date
    assert d.federal_level == 'testlevel'
    assert d.authority == 'Authority'
    assert d.authority_url == 'http://my.link.to/authority'
    assert d.type == 'testtype'
    assert d.subtype == 'testsubtype'
    assert d.decree_date == date
    assert d.instance == 'INST'
    assert d.number == '123'
    assert d.abbreviation == 'abbr'
    assert d.abrogation_date == date
    assert d.cycle == 'cycle'
Exemplo n.º 8
0
def test_get_multilingual():
    document = Document([], id='1', title='Test')
    result = {'de': 'Test'}
    assert OEREBlexSource._get_multilingual(document.title, 'de') == result
    assert OEREBlexSource._get_multilingual(None, 'de') is None
Exemplo n.º 9
0
                        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, language=language) == result


@pytest.mark.parametrize(
    'i,document',
    [(1,
      Document(id='doc1',
               title='Document 1',
               category='main',
               doctype='edict',
               authority='Office',
               files=[File(href='/api/attachments/1', category='main')],
               enactment_date=datetime.date.today(),
               index=1)),
     (2,
      Document(id='doc2',
               title='Document 2',
               category='main',
               doctype='decree',
               authority='Office',
               files=[
                   File(href='/api/attachments/2', category='main'),
                   File(href='/api/attachments/3', category='additional')
               ],
               enactment_date=datetime.date.today(),
               index=2)),
Exemplo n.º 10
0
    def from_string(self, xml):
        """Parses XML into internal structure.

        The specified XML string is gets validated against the geoLink XSD on parsing.

        Args:
            xml (str or bytes): The XML to be parsed.

        Returns:
            list[geolink_formatter.entity.Document]: A list containing the parsed document elements.

        Raises:
            lxml.etree.XMLSyntaxError: Raised on failed validation.
        """
        root = self._parse_xml(xml)
        documents = list()

        for document_el in root.iter('document'):
            doc_id = document_el.attrib.get('id')
            doctype = document_el.attrib.get('doctype')

            # Mangle doc_id for notices. While IDs are unique between decrees
            # and edicts, this is not the case when adding notices to the mix.
            if doctype == 'notice':
                doc_id += doctype

            if doc_id and doc_id not in [doc.id for doc in documents]:
                files = list()
                for file_el in document_el.iter('file'):
                    href = file_el.attrib.get('href')
                    if self.host_url and not href.startswith(u'http://') and not href.startswith(u'https://'):
                        href = u'{host}{href}'.format(host=self.host_url, href=href)
                    files.append(File(
                        title=file_el.attrib.get('title'),
                        description=file_el.attrib.get('description'),
                        href=href,
                        category=file_el.attrib.get('category')
                    ))
                enactment_date = document_el.attrib.get('enactment_date')
                if enactment_date:
                    enactment_date = datetime.datetime.strptime(enactment_date, self._date_format).date()
                decree_date = document_el.attrib.get('decree_date')
                if decree_date:
                    decree_date = datetime.datetime.strptime(decree_date, self._date_format).date()
                abrogation_date = document_el.attrib.get('abrogation_date')
                if abrogation_date:
                    abrogation_date = datetime.datetime.strptime(abrogation_date, self._date_format).date()
                documents.append(Document(
                    files=files,
                    id=doc_id,
                    category=document_el.attrib.get('category'),
                    doctype=document_el.attrib.get('doctype'),
                    federal_level=document_el.attrib.get('federal_level'),
                    authority=document_el.attrib.get('authority'),
                    authority_url=document_el.attrib.get('authority_url'),
                    title=document_el.attrib.get('title'),
                    number=document_el.attrib.get('number'),
                    abbreviation=document_el.attrib.get('abbreviation'),
                    instance=document_el.attrib.get('instance'),
                    type=document_el.attrib.get('type'),
                    subtype=document_el.attrib.get('subtype'),
                    decree_date=decree_date,
                    enactment_date=enactment_date,
                    abrogation_date=abrogation_date,
                    cycle=document_el.attrib.get('cycle')
                ))

        return documents
])
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


@pytest.mark.parametrize('i,document', [
    (1, Document(
        id='doc1',
        title='Document 1',
        category='main',
        doctype='edict',
        authority='Office',
        files=[File('File 1', '/api/attachments/1', 'main')],
        enactment_date=datetime.date.today()
    )),
    (2, Document(
        id='doc2',
        title='Document 2',
        category='main',
        doctype='decree',
        authority='Office',
        files=[
            File('File 2', '/api/attachments/2', 'main'),
            File('File 3', '/api/attachments/3', 'additional')
        ],
        enactment_date=datetime.date.today()
def test_get_document_title():
    document = Document([], id='1', title='Test')
    result = {'de': 'Test'}
    assert OEREBlexSource._get_document_title(document, File(), 'de') == result
Exemplo n.º 13
0
def test_document_invalid_enactment_date():
    with pytest.raises(TypeError):
        Document([], enactment_date='invalid')
Exemplo n.º 14
0
def test_document_invalid_decree_date():
    with pytest.raises(TypeError):
        Document([], decree_date='invalid')
Exemplo n.º 15
0
def test_document_invalid_files():
    with pytest.raises(TypeError):
        Document('invalid')
Exemplo n.º 16
0
def test_document_invalid_abrogation_date():
    with pytest.raises(TypeError):
        Document([], abrogation_date='invalid')