From 0f053f5c8fe66d94e141adb6068fe24b13127476 Mon Sep 17 00:00:00 2001 From: SimonSanich Date: Mon, 30 Mar 2026 18:55:05 +0300 Subject: [PATCH 1/4] Add unit tests for MapView functions --- MapView/tests/test_mp.py | 92 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 MapView/tests/test_mp.py diff --git a/MapView/tests/test_mp.py b/MapView/tests/test_mp.py new file mode 100644 index 0000000..fab62f5 --- /dev/null +++ b/MapView/tests/test_mp.py @@ -0,0 +1,92 @@ +import unittest +import sys +from unittest.mock import Mock + +sys.modules['lineMapLayer'] = Mock() +sys.modules['datasource'] = Mock() +sys.modules['config'] = Mock() + +from MapView.main import get_lat_lon, MapViewApp +from MapView.datasource import Datasource as DataSourceClass + + +# ----------------------- +# get_lat_lon +# ----------------------- +class TestGetLatLon(unittest.TestCase): + + def test_dict(self): + point = {"lat": 50.45, "lon": 30.52} + self.assertEqual(get_lat_lon(point), (50.45, 30.52)) + + def test_tuple(self): + point = (50.45, 30.52) + self.assertEqual(get_lat_lon(point), (50.45, 30.52)) + + def test_list(self): + point = [50.45, 30.52] + self.assertEqual(get_lat_lon(point), (50.45, 30.52)) + + def test_invalid(self): + point = {"lat": None, "lon": 30.52} + self.assertIsNone(get_lat_lon(point)) + + +# ----------------------- +# map_lat_lon_to_marker +# ----------------------- +class TestMapLatLonToMarker(unittest.TestCase): + + def setUp(self): + self.instance = MapViewApp() + + def test_marker_found(self): + marker = Mock() + marker.lat = 30.52 + marker.lon = 50.45 + + self.instance.pothole_markers = [marker] + self.instance.bump_markers = [] + + result = self.instance.map_lat_lon_to_marker(50.45, 30.52) + + self.assertEqual(result, marker) + + def test_marker_not_found(self): + self.instance.pothole_markers = [] + self.instance.bump_markers = [] + + result = self.instance.map_lat_lon_to_marker(50.45, 30.52) + + self.assertIsNone(result) + + +# ----------------------- +# map_lat_lon_to_processed_agent_data +# ----------------------- +class TestMapLatLonToProcessedAgentData(unittest.TestCase): + + def setUp(self): + self.instance = DataSourceClass() + + def test_returns_marker(self): + marker = Mock() + marker.latitude = 30.52 + marker.longitude = 50.45 + + self.instance._active_markers = [marker] + + result = self.instance.map_lat_lon_to_processed_agent_data(50.45, 30.52) + + self.assertIsNotNone(result) + + def test_empty_markers(self): + self.instance._active_markers = [] + + result = self.instance.map_lat_lon_to_processed_agent_data(50.45, 30.52) + + self.assertIsNone(result) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file -- 2.49.1 From 058aac36c7c2a94a4ce79a19a9a4e72f052ea346 Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Tue, 31 Mar 2026 12:39:26 +0300 Subject: [PATCH 2/4] [P] Fix datasource unit tests --- MapView/datasource.py | 6 +++++- MapView/tests/test_mp.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/MapView/datasource.py b/MapView/datasource.py index 29cf19e..8262eb0 100644 --- a/MapView/datasource.py +++ b/MapView/datasource.py @@ -39,7 +39,11 @@ class Datasource: self.connection_status = None self._new_points = [] self._active_markers = [] - asyncio.ensure_future(self.connect_to_server()) + + try: + asyncio.ensure_future(self.connect_to_server()) + except RuntimeError: + Logger.info("No event loop detected, running in offline mode") def get_new_points(self): Logger.debug(self._new_points) diff --git a/MapView/tests/test_mp.py b/MapView/tests/test_mp.py index fab62f5..af93bf9 100644 --- a/MapView/tests/test_mp.py +++ b/MapView/tests/test_mp.py @@ -3,7 +3,6 @@ import sys from unittest.mock import Mock sys.modules['lineMapLayer'] = Mock() -sys.modules['datasource'] = Mock() sys.modules['config'] = Mock() from MapView.main import get_lat_lon, MapViewApp @@ -79,6 +78,7 @@ class TestMapLatLonToProcessedAgentData(unittest.TestCase): result = self.instance.map_lat_lon_to_processed_agent_data(50.45, 30.52) self.assertIsNotNone(result) + self.assertEqual(result, marker) def test_empty_markers(self): self.instance._active_markers = [] -- 2.49.1 From 92c0acda2c5b4238be1bc333e348219a05d5e26c Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Tue, 31 Mar 2026 12:49:26 +0300 Subject: [PATCH 3/4] [P] Add more datasource tests --- MapView/tests/test_mp.py | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/MapView/tests/test_mp.py b/MapView/tests/test_mp.py index af93bf9..f59b90d 100644 --- a/MapView/tests/test_mp.py +++ b/MapView/tests/test_mp.py @@ -80,6 +80,51 @@ class TestMapLatLonToProcessedAgentData(unittest.TestCase): self.assertIsNotNone(result) self.assertEqual(result, marker) + def test_marker_is_close_enough(self): + marker = Mock() + marker.latitude = 30.521 + marker.longitude = 50.452 + + self.instance._active_markers = [marker] + + result = self.instance.map_lat_lon_to_processed_agent_data(50.45, 30.52) + + self.assertIsNotNone(result) + self.assertEqual(result, marker) + + def test_marker_is_too_far(self): + marker = Mock() + marker.latitude = 30.524 + marker.longitude = 50.454 + + self.instance._active_markers = [marker] + + result = self.instance.map_lat_lon_to_processed_agent_data(50.45, 30.52) + + self.assertIsNone(result) + + def test_return_closer_marker(self): + marker1 = Mock() + marker1.latitude = 30.521 + marker1.longitude = 50.451 + + marker2 = Mock() + marker2.latitude = 30.524 + marker2.longitude = 50.454 + + self.instance._active_markers = [marker1, marker2] + + result = self.instance.map_lat_lon_to_processed_agent_data(50.452, 30.522) + self.assertIsNotNone(result) + self.assertEqual(result, marker1) + + result = self.instance.map_lat_lon_to_processed_agent_data(50.453, 30.523) + self.assertIsNotNone(result) + self.assertEqual(result, marker2) + + result = self.instance.map_lat_lon_to_processed_agent_data(50.459, 30.529) + self.assertIsNone(result) + def test_empty_markers(self): self.instance._active_markers = [] -- 2.49.1 From b171f42fa63b0607407bccba65835469ce3afd53 Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Tue, 31 Mar 2026 11:36:28 +0300 Subject: [PATCH 4/4] [P] Integrate MapView testing into CI pipeline --- .gitea/workflows/tests.yaml | 16 ++++++++++++++++ MapView/Dockerfile-test | 14 ++++++++++++++ MapView/test-entry.sh | 3 +++ MapView/tests/test_mp.py | 6 +++--- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 MapView/Dockerfile-test create mode 100755 MapView/test-entry.sh diff --git a/.gitea/workflows/tests.yaml b/.gitea/workflows/tests.yaml index 3f07f9e..f6f70d3 100644 --- a/.gitea/workflows/tests.yaml +++ b/.gitea/workflows/tests.yaml @@ -49,12 +49,28 @@ jobs: working-directory: IoT-Systems run: docker-compose down -v --remove-orphans + mapview-test: + name: MapView testing + runs-on: host-arch-x86_64 + steps: + - name: Clone repository + run: git clone --revision ${{ gitea.sha }} --depth 1 ${{ gitea.server_url }}/${{ gitea.repository }} + + - name: Build MapView testing container + working-directory: IoT-Systems + run: docker build -t local/mapview -f MapView/Dockerfile-test . + + - name: Run MapView tests + working-directory: IoT-Systems + run: docker run --rm -it -v $PWD/MapView:/app:ro local/mapview + integration-smoke-test: name: Integration smoke testing runs-on: host-arch-x86_64 needs: - hub-test - store-test + - mapview-test steps: - name: Clone repository run: git clone --revision ${{ gitea.sha }} --depth 1 ${{ gitea.server_url }}/${{ gitea.repository }} diff --git a/MapView/Dockerfile-test b/MapView/Dockerfile-test new file mode 100644 index 0000000..883af29 --- /dev/null +++ b/MapView/Dockerfile-test @@ -0,0 +1,14 @@ +# Use the official Python image as the base image +FROM python:latest +# Set the working directory inside the container +WORKDIR /app +# Copy the requirements.txt file and install dependencies +COPY MapView/requirements.txt . +RUN sed -i 's/==.*//' requirements.txt + +RUN apt update && apt install -y libgl-dev libsdl1.2-dev + +RUN pip install --no-cache-dir -r requirements.txt +RUN pip install --root-user-action ignore --no-cache-dir python-unittest kivy[base] pygame + +CMD ["./test-entry.sh"] diff --git a/MapView/test-entry.sh b/MapView/test-entry.sh new file mode 100755 index 0000000..1f26b56 --- /dev/null +++ b/MapView/test-entry.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +PYTHONPATH=$PWD python3 -m unittest tests.test_mp diff --git a/MapView/tests/test_mp.py b/MapView/tests/test_mp.py index f59b90d..80f3fc9 100644 --- a/MapView/tests/test_mp.py +++ b/MapView/tests/test_mp.py @@ -5,8 +5,8 @@ from unittest.mock import Mock sys.modules['lineMapLayer'] = Mock() sys.modules['config'] = Mock() -from MapView.main import get_lat_lon, MapViewApp -from MapView.datasource import Datasource as DataSourceClass +from main import get_lat_lon, MapViewApp +from datasource import Datasource as DataSourceClass # ----------------------- @@ -134,4 +134,4 @@ class TestMapLatLonToProcessedAgentData(unittest.TestCase): if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() -- 2.49.1