import json
from os import path

import pytest

from tests import ROOT_DIR, DATA_DIR, generate_filenames


BOUNDS = (
    (-28, 56),  # Azores (West), Novaya Zemlya (East)
    (34, 71),  # Gavdos (South), Nordkapp (North)
)


def generate_geojson_entries(filenames):
    for filename in filenames:
        with open(filename) as f:
            data = json.load(f)

        for feature in data['features']:
            yield {
                'filename': filename,
                'name': feature['properties']['name'],
                'geometry_type': feature['geometry']['type'],
                'coords': feature['geometry']['coordinates'],
            }


glob_patterns = [
    path.join(DATA_DIR, '*.geojson'),
    path.join(ROOT_DIR, '*.geojson'),
]

entries = list(generate_geojson_entries(generate_filenames(glob_patterns)))


def test_there_are_geojson_entries_to_be_tested():
    assert len(entries) > 0


@pytest.mark.parametrize('entry', entries)
def test_geojson_coords_are_in_europe(entry):
    """Tests whether entries in GeoJSON are in Europe. If this test failed
    for you, it's very likely because you have

        "coordinates": [50.0703272, 14.4006753]

    (which is Yemen) instead of

        "coordinates": [14.4006753, 50.0703272]

    (which is Prague) in your GeoJSON entry.
    """
    # For Point, convert list of coords to nested list
    if entry['geometry_type'] == 'Point':
        places = [entry['coords']]
    else:
        places = entry['coords']

    for place_coords in places:
        for i, coord in enumerate(place_coords):
            assert BOUNDS[i][0] < place_coords[i] < BOUNDS[i][1]

@@ -14,18 +14,23 @@
 rom 
-os
+pathlib
  import 
 path
@@ -25,17 +25,17 @@
  import 
-p
+P
 ath%0A%0Aimp
@@ -50,66 +50,8 @@
 st%0A%0A
-from tests import ROOT_DIR, DATA_DIR, generate_filenames%0A%0A
 %0ABOU
@@ -170,738 +170,305 @@
 )%0A%0A%0A
-def generate_geojson_entries(filenames):%0A    for filename in filenames:%0A        with open(filename) as f:%0A            data = json.load(f)%0A%0A        for feature in data%5B'features'%5D:%0A            yield %7B%0A                'filename': filename,%0A                'name': feature%5B'properties'%5D%5B'name'%5D,%0A                'geometry_type': feature%5B'geometry'%5D%5B'type'%5D,%0A                'coords': feature%5B'geometry'%5D%5B'coordinates'%5D,%0A            %7D%0A%0A%0Aglob_patterns = %5B%0A    path.join(DATA_DIR, '*.geojson'),%0A    path.join(ROOT_DIR, '*.geojson'),%0A%5D%0A%0Aentries = list(generate_geojson_entries(generate_filenames(glob_patterns)))%0A%0A%0Adef test_there_are_geojson_entries_to_be_tested():%0A    assert len(entries) %3E 0%0A%0A%0A@pytest.mark.parametrize('entry', entries
+path = Path(__file__).parent / '../../../pythoncz/static/data/business.geojson'%0Awith path.open() as f:%0A    features = json.load(f)%5B'features'%5D%0Aassert len(features) %3E 0%0A%0A%0A@pytest.mark.parametrize('feature', %5B%0A    pytest.param(feature, id=feature%5B'properties'%5D%5B'name'%5D)%0A    for feature in features%0A%5D
 )%0Ade
@@ -503,21 +503,23 @@
 _europe(
-entry
+feature
 ):%0A    %22
@@ -883,18 +883,8 @@
     
-if entry%5B'
 geom
@@ -896,100 +896,148 @@
 type
-'%5D == 'Point':%0A        places = %5Ben
+ = feature%5B'geometry'%5D%5B'type'%5D%0A    coords = feature%5B'geome
 try
+'%5D
 %5B'coord
+inate
 s'%5D
-%5D
 %0A    
-else:%0A        places = entry%5B'
+places = %5Bcoords%5D if geometry_type == 'Point' else 
 coords
-'%5D
 %0A%0A  

remove dependency on the tests package