diff --git a/alembic/versions/7ea883c8a245_add_road_junction_km_table.py b/alembic/versions/7ea883c8a245_add_road_junction_km_table.py index a27c50bf..5a4b49c7 100644 --- a/alembic/versions/7ea883c8a245_add_road_junction_km_table.py +++ b/alembic/versions/7ea883c8a245_add_road_junction_km_table.py @@ -1,14 +1,14 @@ """add suburban junction table Revision ID: 7ea883c8a245 -Revises: 664f8a93794e +Revises: 881e7b1dba8a Create Date: 2023-06-04 17:43:13.170728 """ # revision identifiers, used by Alembic. revision = '7ea883c8a245' -down_revision = '664f8a93794e' +down_revision = '881e7b1dba8a' branch_labels = None depends_on = None diff --git a/anyway/widgets/segment_junctions.py b/anyway/widgets/segment_junctions.py index 2374a14b..a7edb80b 100644 --- a/anyway/widgets/segment_junctions.py +++ b/anyway/widgets/segment_junctions.py @@ -43,7 +43,7 @@ def __calc_fill_segment_junctions(): continue # logger.debug(f"seg:{k},road:{v.road}:juncs:{rkj[v.road]}.") junctions = [ - rkj[v.road][km] for km in rkj[v.road].keys() if v.from_km - 1 <= km <= v.to_km + 1 + rkj[v.road][km] for km in rkj[v.road].keys() if v.from_km <= km <= v.to_km ] # logger.debug(f"segment junctions:{junctions}") res[k] = junctions diff --git a/tests/test_infographics_utils.py b/tests/test_infographics_utils.py index 81211a2b..3f3cba6e 100644 --- a/tests/test_infographics_utils.py +++ b/tests/test_infographics_utils.py @@ -3,7 +3,10 @@ from anyway.widgets.widget_utils import (format_2_level_items, get_query, get_filter_expression, get_expression_for_segment_junctions) from anyway.backend_constants import AccidentSeverity -from anyway.models import AccidentMarkerView +from anyway.models import AccidentMarkerView, RoadJunctionKM, RoadSegments +from anyway.widgets.segment_junctions import SegmentJunctions +from widgets import segment_junctions + class TestInfographicsUtilsCase(unittest.TestCase): item1 = { @@ -37,6 +40,28 @@ class TestInfographicsUtilsCase(unittest.TestCase): } ] + t = RoadJunctionKM() + rjks = [ + RoadJunctionKM(road=1, non_urban_intersection=1, km=1.0), + RoadJunctionKM(road=1, non_urban_intersection=2, km=2.0), + RoadJunctionKM(road=1, non_urban_intersection=3, km=3.0), + RoadJunctionKM(road=10, non_urban_intersection=1, km=10.0), + RoadJunctionKM(road=20, non_urban_intersection=2, km=10.0), + RoadJunctionKM(road=30, non_urban_intersection=3, km=10.0), + ] + segments = [ + RoadSegments(segment_id=1, road=1, from_km=0.0, to_km=1.0), + # RoadSegments(segment_id=2, road=1, from_km=1.0, to_km=2.0), + RoadSegments(segment_id=3, road=1, from_km=1.0, to_km=3.0), + RoadSegments(segment_id=4, road=1, from_km=3.0, to_km=4.0), + RoadSegments(segment_id=11, road=10, from_km=0.0, to_km=10.0), + RoadSegments(segment_id=12, road=10, from_km=10.0, to_km=20.0), + RoadSegments(segment_id=21, road=20, from_km=0.0, to_km=10.0), + RoadSegments(segment_id=22, road=20, from_km=10.0, to_km=20.0), + RoadSegments(segment_id=31, road=30, from_km=0.0, to_km=10.0), + RoadSegments(segment_id=32, road=30, from_km=10.0, to_km=20.0), + ] + def test_format_two_level_items(self): actual = format_2_level_items( self.item1, @@ -45,8 +70,15 @@ def test_format_two_level_items(self): ) self.assertEqual(self.items1_res, actual, "leve 2 formatting") + @patch("anyway.widgets.segment_junctions.db") + def test_get_segment_junctions(self, db): + db.session.query.all.side_effect = [self.rjks, self.segments] + db.session.query.return_value = db.session.query + sg = SegmentJunctions() + actual = sg.get_segment_junctions(1) + self.assertEqual([1], actual, "1") + def test_segment_junctions(self): - from anyway.widgets.segment_junctions import SegmentJunctions i = SegmentJunctions.get_instance() segment_junctions = i.get_segment_junctions(1) self.assertTrue(isinstance(segment_junctions, list))