|
27 | 27 | # |
28 | 28 | # ================================================================= |
29 | 29 |
|
30 | | -import csv |
31 | | -import io |
| 30 | +from csv import DictReader |
| 31 | +from io import StringIO |
| 32 | +import json |
| 33 | + |
32 | 34 | import pytest |
33 | 35 |
|
| 36 | +from pygeoapi.formatter.base import FormatterSerializationError |
34 | 37 | from pygeoapi.formatter.csv_ import CSVFormatter |
35 | 38 |
|
| 39 | +from ..util import get_test_file_path |
36 | 40 |
|
37 | | -@pytest.fixture() |
38 | | -def fixture(): |
39 | | - data = { |
40 | | - 'features': [{ |
41 | | - 'geometry': { |
42 | | - 'type': 'Point', |
43 | | - 'coordinates': [ |
44 | | - -130.44472222222223, |
45 | | - 54.28611111111111 |
46 | | - ] |
47 | | - }, |
48 | | - 'type': 'Feature', |
49 | | - 'properties': { |
50 | | - 'id': 1972, |
51 | | - 'foo': 'bar', |
52 | | - 'title': None, |
53 | | - }, |
54 | | - 'id': 48693 |
55 | | - }] |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def data(): |
| 44 | + data_path = get_test_file_path('data/items.geojson') |
| 45 | + with open(data_path, 'r', encoding='utf-8') as fh: |
| 46 | + return json.load(fh) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture(scope='function') |
| 50 | +def csv_reader_geom_enabled(data): |
| 51 | + """csv_reader with geometry enabled""" |
| 52 | + formatter = CSVFormatter({'geom': True}) |
| 53 | + output = formatter.write(data=data) |
| 54 | + return DictReader(StringIO(output.decode('utf-8'))) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +def invalid_geometry_data(): |
| 59 | + return { |
| 60 | + 'features': [ |
| 61 | + { |
| 62 | + 'id': 1, |
| 63 | + 'type': 'Feature', |
| 64 | + 'properties': { |
| 65 | + 'id': 1, |
| 66 | + 'title': 'Invalid Point Feature' |
| 67 | + }, |
| 68 | + 'geometry': { |
| 69 | + 'type': 'Point', |
| 70 | + 'coordinates': [-130.44472222222223] |
| 71 | + } |
| 72 | + } |
| 73 | + ] |
56 | 74 | } |
57 | 75 |
|
58 | | - return data |
59 | 76 |
|
| 77 | +def test_write_with_geometry_enabled(csv_reader_geom_enabled): |
| 78 | + """Test CSV output with geometry enabled""" |
| 79 | + rows = list(csv_reader_geom_enabled) |
| 80 | + |
| 81 | + # Verify the header |
| 82 | + header = list(csv_reader_geom_enabled.fieldnames) |
| 83 | + assert len(header) == 4 |
60 | 84 |
|
61 | | -def test_csv__formatter(fixture): |
62 | | - f = CSVFormatter({'geom': True}) |
63 | | - f_csv = f.write(data=fixture) |
| 85 | + # Verify number of rows |
| 86 | + assert len(rows) == 9 |
64 | 87 |
|
65 | | - buffer = io.StringIO(f_csv.decode('utf-8')) |
66 | | - reader = csv.DictReader(buffer) |
67 | 88 |
|
68 | | - header = list(reader.fieldnames) |
| 89 | +def test_write_without_geometry(data): |
| 90 | + formatter = CSVFormatter({'geom': False}) |
| 91 | + output = formatter.write(data=data) |
| 92 | + csv_reader = DictReader(StringIO(output.decode('utf-8'))) |
| 93 | + |
| 94 | + """Test CSV output with geometry disabled""" |
| 95 | + rows = list(csv_reader) |
| 96 | + |
| 97 | + # Verify headers don't include geometry |
| 98 | + headers = csv_reader.fieldnames |
| 99 | + assert 'geometry' not in headers |
| 100 | + |
| 101 | + # Verify data |
| 102 | + first_row = rows[0] |
| 103 | + assert first_row['uri'] == \ |
| 104 | + 'http://localhost:5000/collections/objects/items/1' |
| 105 | + assert first_row['name'] == 'LineString' |
| 106 | + |
| 107 | + |
| 108 | +def test_write_empty_features(): |
| 109 | + """Test handling of empty feature collection""" |
| 110 | + formatter = CSVFormatter({'geom': True}) |
| 111 | + data = { |
| 112 | + 'features': [] |
| 113 | + } |
| 114 | + output = formatter.write(data=data) |
| 115 | + assert output == '' |
| 116 | + |
69 | 117 |
|
70 | | - assert f.mimetype == 'text/csv; charset=utf-8' |
| 118 | +@pytest.mark.parametrize( |
| 119 | + 'row_index,expected_wkt', |
| 120 | + [ |
| 121 | + (2, 'POINT (-85 33)'), |
| 122 | + (3, 'MULTILINESTRING ((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))'), # noqa |
| 123 | + (4, 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))'), |
| 124 | + (5, 'POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 35 35, 30 20, 20 30))'), # noqa |
| 125 | + (6, 'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))') # noqa |
| 126 | + ] |
| 127 | +) |
| 128 | +def test_wkt(csv_reader_geom_enabled, row_index, expected_wkt): |
| 129 | + """Test CSV output of multi-point geometry""" |
| 130 | + rows = list(csv_reader_geom_enabled) |
71 | 131 |
|
72 | | - assert len(header) == 5 |
| 132 | + # Verify data |
| 133 | + geometry_row = rows[row_index] |
| 134 | + assert geometry_row['wkt'] == expected_wkt |
73 | 135 |
|
74 | | - assert 'x' in header |
75 | | - assert 'y' in header |
76 | 136 |
|
77 | | - data = next(reader) |
78 | | - assert data['x'] == '-130.44472222222223' |
79 | | - assert data['y'] == '54.28611111111111' |
80 | | - assert data['id'] == '1972' |
81 | | - assert data['foo'] == 'bar' |
82 | | - assert data['title'] == '' |
| 137 | +def test_invalid_geometry_data(invalid_geometry_data): |
| 138 | + formatter = CSVFormatter({'geom': True}) |
| 139 | + with pytest.raises(FormatterSerializationError): |
| 140 | + formatter.write(data=invalid_geometry_data) |
0 commit comments