Commit 862ff084 authored by Eric Duminil's avatar Eric Duminil
Browse files

Adding tests template

parent a1776570
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*.step *.step
*.txt *.txt
*.pdf *.pdf
*.pyc
cache cache
input input
plz/plz-5stellig.geojson plz/plz-5stellig.geojson
...@@ -30,34 +30,34 @@ from shapely.ops import unary_union ...@@ -30,34 +30,34 @@ from shapely.ops import unary_union
INPUT_FOLDER = Path('plz') INPUT_FOLDER = Path('plz')
PLZ_FILENAME = 'plz-5stellig.geojson' PLZ_FILENAME = 'plz-5stellig.geojson'
PLZ_SHAPES = INPUT_FOLDER / PLZ_FILENAME PLZ_SHAPE_FILE = INPUT_FOLDER / PLZ_FILENAME
def download_if_needed(): def _download_plz_shapes_if_needed():
if not PLZ_SHAPES.exists(): if not PLZ_SHAPE_FILE.exists():
from tqdm import tqdm from tqdm import tqdm
import requests import requests
URL = "https://downloads.suche-postleitzahl.org/v2/public/" + PLZ_FILENAME URL = "https://downloads.suche-postleitzahl.org/v2/public/" + PLZ_FILENAME
response = requests.get(URL, stream=True) response = requests.get(URL, stream=True)
INPUT_FOLDER.mkdir(exist_ok=True) INPUT_FOLDER.mkdir(exist_ok=True)
with open(PLZ_SHAPES, "wb") as handle: with open(PLZ_SHAPE_FILE, "wb") as handle:
for data in tqdm(response.iter_content(chunk_size=1024), unit='kB'): for data in tqdm(response.iter_content(chunk_size=1024), unit='kB'):
handle.write(data) handle.write(data)
def parse_data(): def _get_plz_shapes():
print("Parsing %s..." % PLZ_FILENAME) print("Parsing %s..." % PLZ_FILENAME)
download_if_needed() _download_plz_shapes_if_needed()
try: try:
with open(PLZ_SHAPES) as f: with open(PLZ_SHAPE_FILE) as f:
print(' Done') print(' Done')
return json.load(f) return json.load(f)
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
PLZ_SHAPES.unlink() PLZ_SHAPE_FILE.unlink()
raise AttributeError(f"{PLZ_FILENAME} seems to be damaged. Removing it. Please try again!") raise AttributeError(f"{PLZ_FILENAME} seems to be damaged. Removing it. Please try again!")
def get_plz(data, plz_patterns): def _get_coordinates(data, plz_patterns):
geometries = [] geometries = []
for plz_pattern in plz_patterns: for plz_pattern in plz_patterns:
found = False found = False
...@@ -95,9 +95,9 @@ def get_plz(data, plz_patterns): ...@@ -95,9 +95,9 @@ def get_plz(data, plz_patterns):
return wkt_polygon return wkt_polygon
def main(plzs): def get_coordinates_by_zipcode(plzs):
data = parse_data() plz_shapes = _get_plz_shapes()
get_plz(data, plzs) return _get_coordinates(plz_shapes, plzs)
if __name__ == '__main__': if __name__ == '__main__':
...@@ -105,4 +105,4 @@ if __name__ == '__main__': ...@@ -105,4 +105,4 @@ if __name__ == '__main__':
parser.add_argument('plzs', metavar='PLZ', type=str, nargs='+', parser.add_argument('plzs', metavar='PLZ', type=str, nargs='+',
help='desired PLZs') help='desired PLZs')
args = parser.parse_args() args = parser.parse_args()
main(args.plzs) get_coordinates_by_zipcode(args.plzs)
import unittest
import get_coordinates_by_zipcode
class TestClass(unittest.TestCase):
def test_method(self):
self.assertEqual('foo'.upper(), 'FOO')
get_coordinates_by_zipcode.get_coordinates_by_zipcode(['70567'])
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment