From 862ff0845c49cebe977c9d2a0c697a26a7bc7db0 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Wed, 3 Jul 2024 12:43:56 +0200
Subject: [PATCH] Adding tests template

---
 .gitignore                    |  1 +
 get_coordinates_by_zipcode.py | 26 +++++++++++++-------------
 test_zipcode.py               | 13 +++++++++++++
 3 files changed, 27 insertions(+), 13 deletions(-)
 create mode 100644 test_zipcode.py

diff --git a/.gitignore b/.gitignore
index ba6095a..5c678ce 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,7 @@
 *.step
 *.txt
 *.pdf
+*.pyc
 cache
 input
 plz/plz-5stellig.geojson
diff --git a/get_coordinates_by_zipcode.py b/get_coordinates_by_zipcode.py
index 6008144..824164c 100644
--- a/get_coordinates_by_zipcode.py
+++ b/get_coordinates_by_zipcode.py
@@ -30,34 +30,34 @@ from shapely.ops import unary_union
 
 INPUT_FOLDER = Path('plz')
 PLZ_FILENAME = 'plz-5stellig.geojson'
-PLZ_SHAPES = INPUT_FOLDER / PLZ_FILENAME
+PLZ_SHAPE_FILE = INPUT_FOLDER / PLZ_FILENAME
 
 
-def download_if_needed():
-    if not PLZ_SHAPES.exists():
+def _download_plz_shapes_if_needed():
+    if not PLZ_SHAPE_FILE.exists():
         from tqdm import tqdm
         import requests
         URL = "https://downloads.suche-postleitzahl.org/v2/public/" + PLZ_FILENAME
         response = requests.get(URL, stream=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'):
                 handle.write(data)
 
 
-def parse_data():
+def _get_plz_shapes():
     print("Parsing %s..." % PLZ_FILENAME)
-    download_if_needed()
+    _download_plz_shapes_if_needed()
     try:
-        with open(PLZ_SHAPES) as f:
+        with open(PLZ_SHAPE_FILE) as f:
             print('  Done')
             return json.load(f)
     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!")
 
 
-def get_plz(data, plz_patterns):
+def _get_coordinates(data, plz_patterns):
     geometries = []
     for plz_pattern in plz_patterns:
         found = False
@@ -95,9 +95,9 @@ def get_plz(data, plz_patterns):
     return wkt_polygon
 
 
-def main(plzs):
-    data = parse_data()
-    get_plz(data, plzs)
+def get_coordinates_by_zipcode(plzs):
+    plz_shapes = _get_plz_shapes()
+    return _get_coordinates(plz_shapes, plzs)
 
 
 if __name__ == '__main__':
@@ -105,4 +105,4 @@ if __name__ == '__main__':
     parser.add_argument('plzs', metavar='PLZ', type=str, nargs='+',
                         help='desired PLZs')
     args = parser.parse_args()
-    main(args.plzs)
+    get_coordinates_by_zipcode(args.plzs)
diff --git a/test_zipcode.py b/test_zipcode.py
new file mode 100644
index 0000000..e3342a7
--- /dev/null
+++ b/test_zipcode.py
@@ -0,0 +1,13 @@
+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()
-- 
GitLab