File: test_vfs.py

package info (click to toggle)
fiona 1.10.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,660 kB
  • sloc: python: 12,616; makefile: 214; sh: 45
file content (198 lines) | stat: -rw-r--r-- 6,137 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import logging
import sys
import os

import pytest
import boto3

import fiona
from fiona.errors import FionaDeprecationWarning
from fiona.vfs import vsi_path, parse_paths

from .test_collection import TestReading
from .test_collection_legacy import ReadingTest


# Custom markers (from rasterio)
mingdalversion = pytest.mark.skipif(
    fiona.gdal_version < (2, 1, 0), reason="S3 raster access requires GDAL 2.1"
)

credentials = pytest.mark.skipif(
    not (boto3.Session()._session.get_credentials()),
    reason="S3 raster access requires credentials",
)


# TODO: remove this once we've successfully moved the tar tests over
# to TestVsiReading.


class VsiReadingTest(ReadingTest):
    # There's a bug in GDAL 1.9.2 http://trac.osgeo.org/gdal/ticket/5093
    # in which the VSI driver reports the wrong number of features.
    # I'm overriding ReadingTest's test_filter_1 with a function that
    # passes and creating a new method in this class that we can exclude
    # from the test runner at run time.

    @pytest.mark.xfail(
        reason="The number of features present in the archive "
        "differs based on the GDAL version."
    )
    def test_filter_vsi(self):
        results = list(self.c.filter(bbox=(-114.0, 35.0, -104, 45.0)))
        assert len(results) == 67
        f = results[0]
        assert f["id"] == "0"
        assert f["properties"]["STATE"] == "UT"


class TestVsiReading(TestReading):
    # There's a bug in GDAL 1.9.2 http://trac.osgeo.org/gdal/ticket/5093
    # in which the VSI driver reports the wrong number of features.
    # I'm overriding TestReading's test_filter_1 with a function that
    # passes and creating a new method in this class that we can exclude
    # from the test runner at run time.

    @pytest.mark.xfail(
        reason="The number of features present in the archive "
        "differs based on the GDAL version."
    )
    def test_filter_vsi(self):
        results = list(self.c.filter(bbox=(-114.0, 35.0, -104, 45.0)))
        assert len(results) == 67
        f = results[0]
        assert f["id"] == "0"
        assert f["properties"]["STATE"] == "UT"


class TestZipReading(TestVsiReading):
    @pytest.fixture(autouse=True)
    def zipfile(self, data_dir, path_coutwildrnp_zip):
        self.c = fiona.open(f"zip://{path_coutwildrnp_zip}", "r")
        self.path = os.path.join(data_dir, "coutwildrnp.zip")
        yield
        self.c.close()

    def test_open_repr(self):
        path = f"/vsizip/{self.path}:coutwildrnp"
        assert repr(self.c) == (
            f"<open Collection '{path}', mode 'r' at {hex(id(self.c))}>"
        )

    def test_closed_repr(self):
        self.c.close()
        path = f"/vsizip/{self.path}:coutwildrnp"
        assert repr(self.c) == (
            f"<closed Collection '{path}', mode 'r' at {hex(id(self.c))}>"
        )

    def test_path(self):
        assert self.c.path == f"/vsizip/{self.path}"


class TestZipArchiveReading(TestVsiReading):
    @pytest.fixture(autouse=True)
    def zipfile(self, data_dir, path_coutwildrnp_zip):
        vfs = f"zip://{path_coutwildrnp_zip}"
        self.c = fiona.open(vfs + "!coutwildrnp.shp", "r")
        self.path = os.path.join(data_dir, "coutwildrnp.zip")
        yield
        self.c.close()

    def test_open_repr(self):
        path = f"/vsizip/{self.path}/coutwildrnp.shp:coutwildrnp"
        assert repr(self.c) == (
            f"<open Collection '{path}', mode 'r' at {hex(id(self.c))}>"
        )

    def test_closed_repr(self):
        self.c.close()
        path = f"/vsizip/{self.path}/coutwildrnp.shp:coutwildrnp"
        assert repr(self.c) == (
            f"<closed Collection '{path}', mode 'r' at {hex(id(self.c))}>"
        )

    def test_path(self):
        assert self.c.path == f"/vsizip/{self.path}/coutwildrnp.shp"


class TestZipArchiveReadingAbsPath(TestZipArchiveReading):
    @pytest.fixture(autouse=True)
    def zipfile(self, path_coutwildrnp_zip):
        vfs = f"zip://{os.path.abspath(path_coutwildrnp_zip)}"
        self.c = fiona.open(vfs + "!coutwildrnp.shp", "r")
        yield
        self.c.close()

    def test_open_repr(self):
        assert repr(self.c).startswith("<open Collection '/vsizip/")

    def test_closed_repr(self):
        self.c.close()
        assert repr(self.c).startswith("<closed Collection '/vsizip/")

    def test_path(self):
        assert self.c.path.startswith("/vsizip/")


@pytest.mark.usefixtures("uttc_path_coutwildrnp_tar", "uttc_data_dir")
class TarArchiveReadingTest(VsiReadingTest):
    def setUp(self):
        vfs = f"tar://{self.path_coutwildrnp_tar}"
        self.c = fiona.open(vfs + "!testing/coutwildrnp.shp", "r")
        self.path = os.path.join(self.data_dir, "coutwildrnp.tar")

    def tearDown(self):
        self.c.close()

    def test_open_repr(self):
        path = f"/vsitar/{self.path}/testing/coutwildrnp.shp:coutwildrnp"
        assert repr(self.c) == (
            f"<open Collection '{path}', mode 'r' at {hex(id(self.c))}>"
        )

    def test_closed_repr(self):
        self.c.close()
        path = f"/vsitar/{self.path}/testing/coutwildrnp.shp:coutwildrnp"
        assert repr(self.c) == (
            f"<closed Collection '{path}', mode 'r' at {hex(id(self.c))}>"
        )

    def test_path(self):
        assert self.c.path == f"/vsitar/{self.path}/testing/coutwildrnp.shp"


@pytest.mark.network
def test_open_http():
    ds = fiona.open(
        "https://raw.githubusercontent.com/OSGeo/gdal/master/autotest/ogr/data/poly.shp"
    )
    assert len(ds) == 10


@credentials
@mingdalversion
@pytest.mark.network
def test_open_s3():
    ds = fiona.open("zip+s3://fiona-testing/coutwildrnp.zip")
    assert len(ds) == 67


@credentials
@pytest.mark.network
def test_open_zip_https():
    ds = fiona.open("zip+https://s3.amazonaws.com/fiona-testing/coutwildrnp.zip")
    assert len(ds) == 67


def test_parse_path():
    assert parse_paths("zip://foo.zip") == ("foo.zip", "zip", None)


def test_parse_path2():
    assert parse_paths("foo") == ("foo", None, None)


def test_parse_vfs():
    assert parse_paths("/", "zip://foo.zip") == ("/", "zip", "foo.zip")