From 277679b65de53711fc4ee711ff4477562f23bd84 Mon Sep 17 00:00:00 2001 From: Benjamin Ramser Date: Tue, 29 Nov 2022 22:05:25 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20test=20helpers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/helper/__init__.py | 0 tests/helper/random_data.py | 83 +++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/helper/__init__.py create mode 100644 tests/helper/random_data.py diff --git a/tests/helper/__init__.py b/tests/helper/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/helper/random_data.py b/tests/helper/random_data.py new file mode 100644 index 0000000..b28614a --- /dev/null +++ b/tests/helper/random_data.py @@ -0,0 +1,83 @@ +import random +import uuid +from typing import Dict, List, Literal, Optional, Union + +Position = List[float] +Polygon = List[List[Position]] +BoundingBox = List[float] + + +def random_string() -> str: + return str(uuid.uuid4()) + + +def random_integer(minimum: int = 0, maximum: int = 1000) -> int: + return random.randint(minimum, maximum) + + +def random_float(minimum: float = 0, maximum: float = 1000) -> float: + return random.uniform(minimum, maximum) + + +def random_longitude(minimum=-179.0, maximum=179.0) -> float: + return random.uniform(minimum, maximum) + + +def random_latitude(minimum=-89.0, maximum=89.0) -> float: + return random.uniform(minimum, maximum) + + +def random_position() -> Position: + return [random_longitude(), random_latitude()] + + +def random_point( + position: Optional[Position] = None, +) -> Dict[str, Union[str, List[float]]]: + return {"type": "Point", "coordinates": position if position else random_position()} + + +def random_polygon( + bbox: BoundingBox = [13.1632, 52.4099, 13.6402, 52.6353], +) -> Dict[str, Union[str, Polygon]]: + [min_lng, min_lat, max_lng, max_lat] = bbox + + positions = [ + [random_longitude(min_lng, max_lng), random_latitude(min_lat, max_lat)] + for _ in range(5) + ] + + sw: List[float] = [] + ne: List[float] = [] + + for [lng, lat] in positions: + if len(sw) == 0: + sw = [lng, lat] + + if len(ne) == 0: + ne = [lng, lat] + + if sw[0] > lng: + sw[0] = lng + + if sw[1] > lat: + sw[1] = lat + + if ne[0] < lng: + ne[0] = lng + + if ne[1] < lat: + ne[1] = lat + + return { + "type": "Polygon", + "coordinates": [[sw, [ne[0], sw[1]], ne, [sw[0], ne[1]], sw]], + } + + +def random_feature(feature_type: Literal["Point", "Polygon"]) -> dict: + if feature_type == "Point": + return {"type": "Feature", "geometry": random_point()} + + if feature_type == "Polygon": + return random_polygon()