Jelajahi Sumber

before 11.10

Olesya Ivanova 3 tahun lalu
melakukan
0e650d3ed8

+ 1 - 0
plib/__init__.py

@@ -0,0 +1 @@
+from .base import Point, PointError

TEMPAT SAMPAH
plib/__pycache__/__init__.cpython-310.pyc


TEMPAT SAMPAH
plib/__pycache__/__init__.cpython-39.pyc


TEMPAT SAMPAH
plib/__pycache__/base.cpython-310.pyc


TEMPAT SAMPAH
plib/__pycache__/base.cpython-39.pyc


+ 26 - 0
plib/base.py

@@ -0,0 +1,26 @@
+class PointError(Exception):
+    pass
+
+
+class Point:
+    def __init__(self, x: int, y: int) -> None:
+        if not isinstance(x, int) or not isinstance(y, int):
+            raise PointError("x,y should be an integer type")
+        self.x = x
+        self.y = y
+
+    def __add__(self, other: "Point") -> "Point":
+        return Point(self.x + other.x, self.y + other.y)
+
+    def __sub__(self, other: "Point") -> "Point":
+        return Point(self.x - other.x, self.y - other.y)
+
+    def __eq__(self, other: object) -> bool:
+        if not isinstance(other, Point):
+            raise NotImplementedError
+        return self.x == other.x and self.y == other.y
+
+    def distance_to(self, other: "Point") -> float:
+        p = self - other
+        return (p.x ** 2 + p.y ** 2) ** 0.5
+

+ 0 - 0
tests/__init__.py


TEMPAT SAMPAH
tests/__pycache__/__init__.cpython-310.pyc


TEMPAT SAMPAH
tests/__pycache__/__init__.cpython-39.pyc


TEMPAT SAMPAH
tests/__pycache__/test_point.cpython-310-pytest-6.2.5.pyc


TEMPAT SAMPAH
tests/__pycache__/test_point.cpython-39-pytest-6.2.5.pyc


+ 48 - 0
tests/test_point.py

@@ -0,0 +1,48 @@
+import pytest
+from plib import Point, PointError
+
+
+@pytest.fixture
+def points():
+    return Point(1, 2), Point(2, 2)
+
+
+class TestPoint:
+
+    def test_creation(self):
+        p = Point(1, 2)
+        assert p.x == 1 and p.y == 2
+
+        with pytest.raises(PointError):
+            Point(1, 1.5)
+        with pytest.raises(PointError):
+            Point("a", 10)
+
+    def test_addition(self, points):
+        p1, p2 = points
+        assert p1 + p2 == Point(3, 4)
+
+    def test_subtract(self, points):
+        p1, p2 = points
+        assert p1 - p2 == Point(-1, 0)
+
+    @pytest.mark.parametrize(
+        "p1, p2, distance",
+        [(Point(0, 0), Point(0, 10), 10),
+         (Point(0, 0), Point(10, 0), 10),
+         (Point(0, 0), Point(10, 10), 14)]
+    )
+    def test_distance_all_axis(self, p1, p2, distance):
+        assert p1.distance_to(p2) == pytest.approx(distance, 1)
+
+    def test_eq_with_other_type(self):
+        p = Point(0,0)
+        try:
+            p == {0,0}
+
+        except NotImplementedError:
+            assert False
+        assert True
+
+
+

+ 35 - 0
tests/test_station.py

@@ -0,0 +1,35 @@
+import pytest
+from copy import deepcopy
+
+
+@pytest.fixture
+def station_data():
+    js = {
+        "code": "abdl",
+        "height": 162.27803882118315,
+        "status": "new",
+        "location": {
+            "lat": 53.68908284179442,
+            "lon": 53.64383132493483
+        },
+        "xyz": [
+            2243908.9008,
+            3048443.7188,
+            5116457.9962
+        ]
+    }
+    return js
+
+
+class TestStation:
+    def test_creation(self):
+        try:
+            Station(**js)
+        except Exception as e:
+            pytest.fail(f"Failed with exception  '{type(e)} - {e}'")
+
+        for field in ["code", "height", "status", "location"]:
+            js_copy = deepcopy(js)
+            del js["location"]
+            with pytest.raises(StationError):
+                Station(**js)