Beispiel #1
0
def test_get_schema_file_path_raises_multiple_files_found_exception(
        mocker) -> None:
    mocker.patch('tests.schema_utils.get_schemas_base_path',
                 new=lambda: '/path/to/schemas/')
    mocker.patch.object(
        Path,
        'glob',
        new=lambda self, arg:
        ['/path/to/schemas/test-file', '/path/to/schemas/folder/test-file'])
    with pytest.raises(MultipleFilesFound):
        get_schema_file_path('test-file')
Beispiel #2
0
def test_get_schema_file_path_returns_file_path() -> None:
    """
    Checks that the schemas are installed, a file path is returned and can
    be parsed by lxml.
    """
    path = get_schema_file_path('item-list.rng')
    etree.parse(path)
Beispiel #3
0
def test_empty_response(client) -> None:
    path = get_schema_file_path('item-list.rng')
    xmlschema_doc = etree.parse(path)
    validator = isoschematron.Schematron(xmlschema_doc)

    response = client.get('/search')
    assert response.status_code == 200
    assert response.content_type == 'application/xml; charset=utf-8'

    xml = etree.parse(BytesIO(response.data))
    root = xml.getroot()

    assert validator.validate(xml) is True, validator.error_log
    assert root.getchildren() == []
Beispiel #4
0
def test_search(client, mocker):
    es_search = mocker.patch('search.search.Elasticsearch.search')
    es_search.return_value = {
        'hits': {
            'hits': [
                {
                    '_source': {
                        'id': 'article1',
                        'service': 'scholarly-articles'
                    }
                },
                {
                    '_source': {
                        'id': 'article2',
                        'service': 'scholarly-articles'
                    }
                },
                {
                    '_source': {
                        'id': 'post1',
                        'service': 'blog-articles'
                    }
                },
                {
                    '_source': {
                        'id': 'post2',
                        'service': 'blog-articles'
                    }
                },
            ]
        }
    }

    path = get_schema_file_path('item-list.rng')
    xmlschema_doc = etree.parse(path)
    validator = isoschematron.Schematron(xmlschema_doc)

    response = client.get('/')
    assert response.status_code == 200
    assert response.content_type == 'application/xml; charset=utf-8'

    xml = etree.parse(BytesIO(response.data))
    assert validator.validate(xml) is True, validator.error_log

    root = xml.getroot()
    assert len(root.getchildren()) == 4
Beispiel #5
0
def test_custom_error_handler(client, status_code, name) -> None:
    url = '/error?code=%s' % status_code
    response = client.get(url)
    assert response.status_code == status_code
    assert response.content_type == 'application/problem+xml; charset=utf-8', status_code

    xml = etree.parse(BytesIO(response.data))
    root = xml.getroot()

    schema_path = get_schema_file_path('error.rng')
    xmlschema_doc = etree.parse(schema_path)
    validator = etree.RelaxNG(xmlschema_doc)

    assert validator.validate(xml) is True, validator.error_log
    assert int(root.find('status',
                         namespaces=NAMESPACE_MAP).text) == status_code
    assert root.find('title', namespaces=NAMESPACE_MAP).text == name
Beispiel #6
0
def test_get_schema_file_path_raises_file_not_found_exception(mocker) -> None:
    mocker.patch('tests.schema_utils.get_schemas_base_path',
                 new=lambda: '/path/to/schemas/')
    with pytest.raises(FileNotFoundError):
        get_schema_file_path('test-file')