Beispiel #1
0
class TestHookInject(object):
    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    def test_hooks_inject(self, force_rebuild_wfs, test_hook_inject):
        """Test the of the hook inject events.

        Test whether the requests are intercepted correctly.

        Parameters
        ----------
        force_rebuild_wfs : pytest.fixture
            Fixture removing cached WFS capabilities document.
        test_hook_types : pytest.fixture
            Fixture removing default hooks and installing HookTester.

        """
        query = PropertyIsEqualTo(propertyname='boornummer',
                                  literal='GEO-04/169-BNo-B1')

        boringsearch = BoringSearch()

        boringsearch.search(query=query)
        df = boringsearch.search(query=query)

        assert df.iloc[0].gemeente == 'Bevergem'
        assert df.iloc[0].boormethode == 'De Pypere 106 T'
Beispiel #2
0
class TestHookTypes(object):
    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    def test_default_hooks(self, nocache):
        """Test the default hooks by performing a simple search.

        Test whether no exceptions are raised.

        Parameters
        ----------
        nocache : pytest.fixture
            Fixture temporarily disabling caching.

        """
        pydov.hooks = Hooks((SimpleStatusHook(), ))

        query = PropertyIsEqualTo(propertyname='boornummer',
                                  literal='GEO-04/169-BNo-B1')

        boringsearch = BoringSearch()
        boringsearch.search(query=query)

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    def test_hooks(self, force_rebuild_wfs, test_hook_types):
        """Test the argument types of the hook events.

        Parameters
        ----------
        force_rebuild_wfs : pytest.fixture
            Fixture removing cached WFS capabilities document.
        test_hook_types : pytest.fixture
            Fixture removing default hooks and installing HookTester.

        """
        query = PropertyIsEqualTo(propertyname='boornummer',
                                  literal='GEO-04/169-BNo-B1')

        boringsearch = BoringSearch()
        boringsearch.search(query=query)
Beispiel #3
0
class TestHookCount(object):
    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    def test_wfs_only(self, force_rebuild_wfs, test_hook_count):
        """Test the search method providing both a location and a query.

        Test whether a dataframe is returned.

        Parameters
        ----------
        force_rebuild_wfs : pytest.fixture
            Fixture removing cached WFS capabilities document.
        test_hook_count : pytest.fixture
            Fixture removing default hooks and installing HookCounter.

        """
        query = PropertyIsEqualTo(propertyname='boornummer',
                                  literal='GEO-04/169-BNo-B1')

        boringsearch = BoringSearch()
        boringsearch.search(query=query,
                            return_fields=('pkey_boring', 'x', 'y'))

        assert pydov.hooks[0].count_wfs_search_init == 1
        assert pydov.hooks[0].count_wfs_search_result == 1
        assert pydov.hooks[0].count_wfs_search_result_received == 1
        assert pydov.hooks[0].count_inject_wfs_getfeature_response == 1

        assert pydov.hooks[0].count_xml_received == 0
        assert pydov.hooks[0].count_inject_xml_response == 0
        assert pydov.hooks[0].count_xml_cache_hit == 0
        assert pydov.hooks[0].count_xml_downloaded == 0

        assert pydov.hooks[0].count_meta_received > 0
        assert pydov.hooks[0].count_inject_meta_response > 0

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    def test_wfs_and_xml_nocache(self, force_rebuild_wfs, test_hook_count,
                                 nocache):
        """Test the search method providing both a location and a query.

        Test whether a dataframe is returned.

        Parameters
        ----------
        force_rebuild_wfs : pytest.fixture
            Fixture removing cached WFS capabilities document.
        test_hook_count : pytest.fixture
            Fixture removing default hooks and installing HookCounter.
        nocache : pytest.fixture
            Fixture temporarily disabling caching.

        """
        query = PropertyIsEqualTo(propertyname='boornummer',
                                  literal='GEO-04/169-BNo-B1')

        boringsearch = BoringSearch()
        boringsearch.search(query=query,
                            return_fields=('pkey_boring', 'mv_mtaw'))

        assert pydov.hooks[0].count_wfs_search_init == 1
        assert pydov.hooks[0].count_wfs_search_result == 1
        assert pydov.hooks[0].count_wfs_search_result_received == 1
        assert pydov.hooks[0].count_inject_wfs_getfeature_response == 1

        assert pydov.hooks[0].count_xml_received == 1
        assert pydov.hooks[0].count_inject_xml_response == 1
        assert pydov.hooks[0].count_xml_cache_hit == 0
        assert pydov.hooks[0].count_xml_downloaded == 1

        assert pydov.hooks[0].count_meta_received > 0
        assert pydov.hooks[0].count_inject_meta_response > 0

        boringsearch.search(query=query,
                            return_fields=('pkey_boring', 'mv_mtaw'))

        assert pydov.hooks[0].count_wfs_search_init == 2
        assert pydov.hooks[0].count_wfs_search_result == 2
        assert pydov.hooks[0].count_wfs_search_result_received == 2
        assert pydov.hooks[0].count_inject_wfs_getfeature_response == 2

        assert pydov.hooks[0].count_xml_received == 2
        assert pydov.hooks[0].count_inject_xml_response == 2
        assert pydov.hooks[0].count_xml_cache_hit == 0
        assert pydov.hooks[0].count_xml_downloaded == 2

        assert pydov.hooks[0].count_meta_received > 0
        assert pydov.hooks[0].count_inject_meta_response > 0

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('plaintext_cache',
                             [[datetime.timedelta(minutes=15)]],
                             indirect=['plaintext_cache'])
    def test_wfs_and_xml_cache(self, force_rebuild_wfs, test_hook_count,
                               plaintext_cache):
        """Test the search method providing both a location and a query.

        Test whether a dataframe is returned.

        Parameters
        ----------
        force_rebuild_wfs : pytest.fixture
            Fixture removing cached WFS capabilities document.
        test_hook_count : pytest.fixture
            Fixture removing default hooks and installing HookCounter.
        plaintext_cache : pytest.fixture
            Fixture temporarily setting up a testcache with max_age of 1
            second.

        """
        query = PropertyIsEqualTo(propertyname='boornummer',
                                  literal='GEO-04/169-BNo-B1')

        boringsearch = BoringSearch()
        boringsearch.search(query=query,
                            return_fields=('pkey_boring', 'mv_mtaw'))

        assert pydov.hooks[0].count_wfs_search_init == 1
        assert pydov.hooks[0].count_wfs_search_result == 1
        assert pydov.hooks[0].count_wfs_search_result_received == 1
        assert pydov.hooks[0].count_inject_wfs_getfeature_response == 1

        assert pydov.hooks[0].count_xml_received == 1
        assert pydov.hooks[0].count_inject_xml_response == 2
        assert pydov.hooks[0].count_xml_cache_hit == 0
        assert pydov.hooks[0].count_xml_downloaded == 1

        assert pydov.hooks[0].count_meta_received > 0
        assert pydov.hooks[0].count_inject_meta_response > 0

        boringsearch.search(query=query,
                            return_fields=('pkey_boring', 'mv_mtaw'))

        assert pydov.hooks[0].count_wfs_search_init == 2
        assert pydov.hooks[0].count_wfs_search_result == 2
        assert pydov.hooks[0].count_wfs_search_result_received == 2
        assert pydov.hooks[0].count_inject_wfs_getfeature_response == 2

        assert pydov.hooks[0].count_xml_received == 2
        assert pydov.hooks[0].count_inject_xml_response == 3
        assert pydov.hooks[0].count_xml_cache_hit == 1
        assert pydov.hooks[0].count_xml_downloaded == 1

        assert pydov.hooks[0].count_meta_received > 0
        assert pydov.hooks[0].count_inject_meta_response > 0
Beispiel #4
0
class TestEncoding(object):
    """Class grouping tests related to encoding issues."""
    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    def test_search(self, nocache):
        """Test the search method with strange character in the output.

        Test whether the output has the correct encoding.

        Parameters
        ----------
        nocache : pytest.fixture
            Fixture to disable caching.

        """
        boringsearch = BoringSearch()
        query = PropertyIsEqualTo(
            propertyname='pkey_boring',
            literal=build_dov_url('data/boring/1928-031159'))

        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'uitvoerder'))

        assert df.uitvoerder[0] == u'Societé Belge des Bétons'

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('plaintext_cache',
                             [[datetime.timedelta(minutes=15)]],
                             indirect=['plaintext_cache'])
    def test_search_plaintext_cache(self, plaintext_cache):
        """Test the search method with strange character in the output.

        Test whether the output has the correct encoding, both with and
        without using the cache.

        Parameters
        ----------
        plaintext_cache : pytest.fixture providing
                pydov.util.caching.PlainTextFileCache
            PlainTextFileCache using a temporary directory and a maximum age
            of 1 second.

        """
        boringsearch = BoringSearch()
        query = PropertyIsEqualTo(
            propertyname='pkey_boring',
            literal=build_dov_url('data/boring/1928-031159'))

        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'uitvoerder',
                                                'mv_mtaw'))

        assert df.uitvoerder[0] == u'Societé Belge des Bétons'

        assert os.path.exists(
            os.path.join(plaintext_cache.cachedir, 'boring',
                         '1928-031159.xml'))

        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'uitvoerder',
                                                'mv_mtaw'))

        assert df.uitvoerder[0] == u'Societé Belge des Bétons'

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('gziptext_cache',
                             [[datetime.timedelta(minutes=15)]],
                             indirect=['gziptext_cache'])
    def test_search_gziptext_cache(self, gziptext_cache):
        """Test the search method with strange character in the output.

        Test whether the output has the correct encoding, both with and
        without using the cache.

        Parameters
        ----------
        gziptext_cache : pytest.fixture providing
                pydov.util.caching.GzipTextFileCache
            GzipTextFileCache using a temporary directory and a maximum age
            of 1 second.

        """
        boringsearch = BoringSearch()
        query = PropertyIsEqualTo(
            propertyname='pkey_boring',
            literal=build_dov_url('data/boring/1928-031159'))

        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'uitvoerder',
                                                'mv_mtaw'))

        assert df.uitvoerder[0] == u'Societé Belge des Bétons'

        assert os.path.exists(
            os.path.join(gziptext_cache.cachedir, 'boring',
                         '1928-031159.xml.gz'))

        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'uitvoerder',
                                                'mv_mtaw'))

        assert df.uitvoerder[0] == u'Societé Belge des Bétons'

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('plaintext_cache',
                             [[datetime.timedelta(minutes=15)]],
                             indirect=['plaintext_cache'])
    def test_caching_plaintext(self, plaintext_cache):
        """Test the caching of an XML containing strange characters.

        Test whether the data is saved in the cache.

        Parameters
        ----------
        plaintext_cache : pytest.fixture providing
                pydov.util.caching.PlainTextFileCache
            PlainTextFileCache using a temporary directory and a maximum age
            of 1 second.

        """
        cached_file = os.path.join(plaintext_cache.cachedir, 'boring',
                                   '1995-056089.xml')

        plaintext_cache.clean()
        assert not os.path.exists(cached_file)

        plaintext_cache.get(build_dov_url('data/boring/1995-056089.xml')),
        assert os.path.exists(cached_file)

        with open(cached_file, 'r', encoding='utf-8') as cf:
            cached_data = cf.read()
            assert cached_data != ""

        first_download_time = os.path.getmtime(cached_file)

        time.sleep(0.5)
        plaintext_cache.get(build_dov_url('data/boring/1995-056089.xml'))
        # assure we didn't redownload the file:
        assert os.path.getmtime(cached_file) == first_download_time

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('gziptext_cache',
                             [[datetime.timedelta(minutes=15)]],
                             indirect=['gziptext_cache'])
    def test_caching_gziptext(self, gziptext_cache):
        """Test the caching of an XML containing strange characters.

        Test whether the data is saved in the cache.

        Parameters
        ----------
        gziptext_cache : pytest.fixture providing
                pydov.util.caching.GzipTextFileCache
            GzipTextFileCache using a temporary directory and a maximum age
            of 1 second.

        """
        cached_file = os.path.join(gziptext_cache.cachedir, 'boring',
                                   '1995-056089.xml.gz')

        gziptext_cache.clean()
        assert not os.path.exists(cached_file)

        gziptext_cache.get(build_dov_url('data/boring/1995-056089.xml')),
        assert os.path.exists(cached_file)

        with gzip.open(cached_file, 'rb') as cf:
            cached_data = cf.read().decode('utf-8')
            assert cached_data != ""

        first_download_time = os.path.getmtime(cached_file)

        time.sleep(0.5)
        gziptext_cache.get(build_dov_url('data/boring/1995-056089.xml'))
        # assure we didn't redownload the file:
        assert os.path.getmtime(cached_file) == first_download_time

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('plaintext_cache',
                             [[datetime.timedelta(minutes=15)]],
                             indirect=['plaintext_cache'])
    def test_save_content_plaintext(self, plaintext_cache):
        """Test the caching of an XML containing strange characters.

        Test if the contents of the saved document are the same as the
        original data.

        Parameters
        ----------
        plaintext_cache : pytest.fixture providing
                pydov.util.caching.PlainTextFileCache
            PlainTextFileCache using a temporary directory and a maximum age
            of 1 second.

        """
        cached_file = os.path.join(plaintext_cache.cachedir, 'boring',
                                   '1995-056089.xml')

        plaintext_cache.remove()
        assert not os.path.exists(cached_file)

        ref_data = plaintext_cache.get(
            build_dov_url('data/boring/1995-056089.xml'))
        assert os.path.exists(cached_file)

        with open(cached_file, 'r', encoding='utf-8') as cached:
            cached_data = cached.read().encode('utf-8')

        assert cached_data == ref_data

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('gziptext_cache',
                             [[datetime.timedelta(minutes=15)]],
                             indirect=['gziptext_cache'])
    def test_save_content_gziptext(self, gziptext_cache):
        """Test the caching of an XML containing strange characters.

        Test if the contents of the saved document are the same as the
        original data.

        Parameters
        ----------
        gziptext_cache : pytest.fixture providing
                pydov.util.caching.GzipTextFileCache
            GzipTextFileCache using a temporary directory and a maximum age
            of 1 second.

        """
        cached_file = os.path.join(gziptext_cache.cachedir, 'boring',
                                   '1995-056089.xml.gz')

        gziptext_cache.remove()
        assert not os.path.exists(cached_file)

        ref_data = gziptext_cache.get(
            build_dov_url('data/boring/1995-056089.xml'))
        assert os.path.exists(cached_file)

        with gzip.open(cached_file, 'rb') as cached:
            cached_data = cached.read()

        assert cached_data == ref_data

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('plaintext_cache',
                             [[datetime.timedelta(minutes=15)]],
                             indirect=['plaintext_cache'])
    def test_reuse_content_plaintext(self, plaintext_cache):
        """Test the caching of an XML containing strange characters.

        Test if the contents returned by the cache are the same as the
        original data.

        Parameters
        ----------
        plaintext_cache : pytest.fixture providing
                pydov.util.caching.PlainTextFileCache
            PlainTextFileCache using a temporary directory and a maximum age
            of 1 second.

        """
        cached_file = os.path.join(plaintext_cache.cachedir, 'boring',
                                   '1995-056089.xml')

        plaintext_cache.remove()
        assert not os.path.exists(cached_file)

        ref_data = plaintext_cache.get(
            build_dov_url('data/boring/1995-056089.xml'))
        assert os.path.exists(cached_file)

        cached_data = plaintext_cache.get(
            build_dov_url('data/boring/1995-056089.xml'))

        assert cached_data == ref_data

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('gziptext_cache',
                             [[datetime.timedelta(minutes=15)]],
                             indirect=['gziptext_cache'])
    def test_reuse_content_gziptext(self, gziptext_cache):
        """Test the caching of an XML containing strange characters.

        Test if the contents returned by the cache are the same as the
        original data.

        Parameters
        ----------
        gziptext_cache : pytest.fixture providing
                pydov.util.caching.GzipTextFileCache
            GzipTextFileCache using a temporary directory and a maximum age
            of 1 second.

        """
        cached_file = os.path.join(gziptext_cache.cachedir, 'boring',
                                   '1995-056089.xml.gz')

        gziptext_cache.remove()
        assert not os.path.exists(cached_file)

        ref_data = gziptext_cache.get(
            build_dov_url('data/boring/1995-056089.xml'))
        assert os.path.exists(cached_file)

        cached_data = gziptext_cache.get(
            build_dov_url('data/boring/1995-056089.xml'))

        assert cached_data == ref_data

    def test_search_invalidxml_single(self, mp_wfs,
                                      mp_remote_describefeaturetype,
                                      mp_get_schema, mp_remote_md,
                                      mp_remote_fc, mp_remote_wfs_feature,
                                      mp_dov_xml, nocache):
        """Test the search method when the XML is invalid.

        If lxml is installed, the XML should parse regardless of invalid
        characters. If lxml is not installed, the dataframe should be
        returned with a warning that it will be incomplete.

        Parameters
        ----------
        mp_wfs : pytest.fixture
            Monkeypatch the call to the remote GetCapabilities request.
        mp_remote_describefeaturetype : pytest.fixture
            Monkeypatch the call to a remote DescribeFeatureType.
        mp_get_schema : pytest.fixture
            Monkeypatch the call to a remote OWSLib schema.
        mp_remote_md : pytest.fixture
            Monkeypatch the call to get the remote metadata.
        mp_remote_fc : pytest.fixture
            Monkeypatch the call to get the remote feature catalogue.
        mp_remote_wfs_feature : pytest.fixture
            Monkeypatch the call to get WFS features.
        mp_dov_xml : pytest.fixture
            Monkeypatch the call to get the remote XML data.
        nocache : pytest.fixture
            Fixture to disable caching.

        """
        lithosearch = LithologischeBeschrijvingenSearch()
        query = PropertyIsEqualTo(
            propertyname='pkey_interpretatie',
            literal=build_dov_url('data/interpretatie/1987-070909'))

        try:
            import lxml
            have_lxml = True
        except ImportError:
            have_lxml = False

        if have_lxml:
            df = lithosearch.search(query=query)
            assert df.beschrijving[1].startswith(u'homogene groene zanden')
        else:
            with pytest.warns(XmlParseWarning):
                df = lithosearch.search(query=query)
                assert len(df) == 1
                assert pd.isnull(df.diepte_laag_van[0])
                assert pd.isnull(df.diepte_laag_tot[0])
                assert pd.isnull(df.beschrijving[0])
Beispiel #5
0
    ----------
    mp_wfs : pytest.fixture
        Monkeypatch the call to the remote GetCapabilities request.
    objectsearch : pytest.fixture
        An instance of a subclass of AbstractTestSearch to perform search
        operations on the corresponding DOV type.

    """
    description = objectsearch.get_description()

    assert isinstance(description, str)
    assert len(description) > 0


@pytest.mark.online
@pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
@pytest.mark.parametrize("objectsearch", search_objects)
def test_search_location(objectsearch):
    """Test the get_description method.

    Test whether the method returns a non-empty string.

    Parameters
    ----------
    mp_wfs : pytest.fixture
        Monkeypatch the call to the remote GetCapabilities request.
    objectsearch : pytest.fixture
        An instance of a subclass of AbstractTestSearch to perform search
        operations on the corresponding DOV type.

    """
Beispiel #6
0
class TestEncoding(object):
    """Class grouping tests related to encoding issues."""
    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    def test_search(self, nocache):
        """Test the search method with strange character in the output.

        Test whether the output has the correct encoding.

        """
        boringsearch = BoringSearch()
        query = PropertyIsEqualTo(
            propertyname='pkey_boring',
            literal='https://www.dov.vlaanderen.be/data/boring/1928-031159')

        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'uitvoerder'))

        assert df.uitvoerder[0] == u'Societé Belge des Bétons'

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('cache', [[datetime.timedelta(minutes=15)]],
                             indirect=['cache'])
    def test_search_cache(self, cache):
        """Test the search method with strange character in the output.

        Test whether the output has the correct encoding, both with and
        without using the cache.

        Parameters
        ----------
        cache : pytest.fixture providing  pydov.util.caching.TransparentCache
            TransparentCache using a temporary directory and a maximum age
            of 1 second.

        """
        boringsearch = BoringSearch()
        query = PropertyIsEqualTo(
            propertyname='pkey_boring',
            literal='https://www.dov.vlaanderen.be/data/boring/1928-031159')

        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'uitvoerder',
                                                'mv_mtaw'))

        assert df.uitvoerder[0] == u'Societé Belge des Bétons'

        assert os.path.exists(
            os.path.join(cache.cachedir, 'boring', '1928-031159.xml'))

        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'uitvoerder',
                                                'mv_mtaw'))

        assert df.uitvoerder[0] == u'Societé Belge des Bétons'

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('cache', [[datetime.timedelta(minutes=15)]],
                             indirect=['cache'])
    def test_caching(self, cache):
        """Test the caching of an XML containing strange characters.

        Test whether the data is saved in the cache.

        Parameters
        ----------
        cache : pytest.fixture providing  pydov.util.caching.TransparentCache
            TransparentCache using a temporary directory and a maximum age
            of 1 second.

        """
        cached_file = os.path.join(cache.cachedir, 'boring', '1995-056089.xml')

        cache.clean()
        assert not os.path.exists(cached_file)

        cache.get('https://www.dov.vlaanderen.be/data/boring/1995-056089.xml')
        assert os.path.exists(cached_file)

        with open(cached_file, 'r', encoding='utf-8') as cf:
            cached_data = cf.read()
            assert cached_data != ""

        first_download_time = os.path.getmtime(cached_file)

        time.sleep(0.5)
        cache.get('https://www.dov.vlaanderen.be/data/boring/1995-056089.xml')
        # assure we didn't redownload the file:
        assert os.path.getmtime(cached_file) == first_download_time

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('cache', [[datetime.timedelta(minutes=15)]],
                             indirect=['cache'])
    def test_save_content(self, cache):
        """Test the caching of an XML containing strange characters.

        Test if the contents of the saved document are the same as the
        original data.

        Parameters
        ----------
        cache : pytest.fixture providing  pydov.util.caching.TransparentCache
            TransparentCache using a temporary directory and a maximum age
            of 1 second.

        """
        cached_file = os.path.join(cache.cachedir, 'boring', '1995-056089.xml')

        cache.remove()
        assert not os.path.exists(cached_file)

        ref_data = cache.get(
            'https://www.dov.vlaanderen.be/data/boring/1995-056089.xml')
        assert os.path.exists(cached_file)

        with open(cached_file, 'r', encoding='utf-8') as cached:
            cached_data = cached.read().encode('utf-8')

        assert cached_data == ref_data

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('cache', [[datetime.timedelta(minutes=15)]],
                             indirect=['cache'])
    def test_reuse_content(self, cache):
        """Test the caching of an XML containing strange characters.

        Test if the contents returned by the cache are the same as the
        original data.

        Parameters
        ----------
        cache : pytest.fixture providing  pydov.util.caching.TransparentCache
            TransparentCache using a temporary directory and a maximum age
            of 1 second.

        """
        cached_file = os.path.join(cache.cachedir, 'boring', '1995-056089.xml')

        cache.remove()
        assert not os.path.exists(cached_file)

        ref_data = cache.get(
            'https://www.dov.vlaanderen.be/data/boring/1995-056089.xml')
        assert os.path.exists(cached_file)

        cached_data = cache.get(
            'https://www.dov.vlaanderen.be/data/boring/1995-056089.xml')

        assert cached_data == ref_data
class TestHooks(object):
    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    def test_wfs_only(self, temp_hooks):
        """Test the search method providing both a location and a query.

        Test whether a dataframe is returned.

        Parameters
        ----------
        mp_remote_describefeaturetype : pytest.fixture
            Monkeypatch the call to a remote DescribeFeatureType of the
            dov-pub:Boringen layer.
        mp_remote_wfs_feature : pytest.fixture
            Monkeypatch the call to get WFS features.
        temp_hooks : pytest.fixture
            Fixture removing default hooks and installing HookCounter.

        """
        query = PropertyIsEqualTo(propertyname='boornummer',
                                  literal='GEO-04/169-BNo-B1')

        boringsearch = BoringSearch()
        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'x', 'y'))

        assert pydov.hooks[0].count_wfs_search_init == 1
        assert pydov.hooks[0].count_wfs_search_result == 1
        assert pydov.hooks[0].count_xml_requested == 0
        assert pydov.hooks[0].count_xml_cache_hit == 0
        assert pydov.hooks[0].count_xml_downloaded == 0

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    def test_wfs_and_xml_nocache(self, temp_hooks, nocache):
        """Test the search method providing both a location and a query.

        Test whether a dataframe is returned.

        Parameters
        ----------
        mp_remote_describefeaturetype : pytest.fixture
            Monkeypatch the call to a remote DescribeFeatureType of the
            dov-pub:Boringen layer.
        mp_remote_wfs_feature : pytest.fixture
            Monkeypatch the call to get WFS features.
        temp_hooks : pytest.fixture
            Fixture removing default hooks and installing HookCounter.
        nocache : pytest.fixture
            Fixture temporarily disabling caching.

        """
        query = PropertyIsEqualTo(propertyname='boornummer',
                                  literal='GEO-04/169-BNo-B1')

        boringsearch = BoringSearch()
        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'mv_mtaw'))

        assert pydov.hooks[0].count_wfs_search_init == 1
        assert pydov.hooks[0].count_wfs_search_result == 1
        assert pydov.hooks[0].count_xml_requested == 1
        assert pydov.hooks[0].count_xml_cache_hit == 0
        assert pydov.hooks[0].count_xml_downloaded == 1

        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'mv_mtaw'))

        assert pydov.hooks[0].count_wfs_search_init == 2
        assert pydov.hooks[0].count_wfs_search_result == 2
        assert pydov.hooks[0].count_xml_requested == 2
        assert pydov.hooks[0].count_xml_cache_hit == 0
        assert pydov.hooks[0].count_xml_downloaded == 2

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    @pytest.mark.parametrize('plaintext_cache',
                             [[datetime.timedelta(minutes=15)]],
                             indirect=['plaintext_cache'])
    def test_wfs_and_xml_cache(self, temp_hooks, plaintext_cache):
        """Test the search method providing both a location and a query.

        Test whether a dataframe is returned.

        Parameters
        ----------
        mp_remote_describefeaturetype : pytest.fixture
            Monkeypatch the call to a remote DescribeFeatureType of the
            dov-pub:Boringen layer.
        mp_remote_wfs_feature : pytest.fixture
            Monkeypatch the call to get WFS features.
        temp_hooks : pytest.fixture
            Fixture removing default hooks and installing HookCounter.
        plaintext_cache : pytest.fixture
            Fixture temporarily setting up a testcache with max_age of 1
            second.

        """
        query = PropertyIsEqualTo(propertyname='boornummer',
                                  literal='GEO-04/169-BNo-B1')

        boringsearch = BoringSearch()
        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'mv_mtaw'))

        assert pydov.hooks[0].count_wfs_search_init == 1
        assert pydov.hooks[0].count_wfs_search_result == 1
        assert pydov.hooks[0].count_xml_requested == 1
        assert pydov.hooks[0].count_xml_cache_hit == 0
        assert pydov.hooks[0].count_xml_downloaded == 1

        df = boringsearch.search(query=query,
                                 return_fields=('pkey_boring', 'mv_mtaw'))

        assert pydov.hooks[0].count_wfs_search_init == 2
        assert pydov.hooks[0].count_wfs_search_result == 2
        assert pydov.hooks[0].count_xml_requested == 2
        assert pydov.hooks[0].count_xml_cache_hit == 1
        assert pydov.hooks[0].count_xml_downloaded == 1

    @pytest.mark.online
    @pytest.mark.skipif(not service_ok(), reason="DOV service is unreachable")
    def test_default_hooks(self, nocache):
        """Test the default hooks by performing a simple search.

        Test whether no exceptions are raised.

        Parameters
        ----------
        nocache : pytest.fixture
            Fixture temporarily disabling caching.

        """
        pydov.hooks = [SimpleStatusHook()]

        query = PropertyIsEqualTo(propertyname='boornummer',
                                  literal='GEO-04/169-BNo-B1')

        boringsearch = BoringSearch()
        df = boringsearch.search(query=query)