1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515# ===============================================================================
16+ from datetime import date
1617from pathlib import Path
1718
1819import pytest
19- from geoalchemy2 import functions as geofunc
20-
2120from core .constants import SRID_WGS84
2221from core .dependencies import (
2322 admin_function ,
2928)
3029from db import Thing , Location , LocationThingAssociation , Group , MeasuringPointHistory
3130from db .engine import session_ctx
31+ from geoalchemy2 import functions as geofunc
3232from main import app
3333from tests import client , override_authentication
3434
@@ -68,46 +68,16 @@ def override_authentication_dependency_fixture():
6868@pytest .fixture (autouse = True , scope = "module" )
6969def populate ():
7070 with session_ctx () as session :
71- # Create some sample data
72- thing1 = Thing (name = "Thing 1" , thing_type = "water well" )
73- thing2 = Thing (name = "Thing 2" , thing_type = "water well" )
74- session .add (thing1 )
75- session .add (thing2 )
76-
77- session .commit ()
78-
79- mp_history_1 = MeasuringPointHistory (
80- thing_id = thing1 .id ,
81- measuring_point_height = 5.0 ,
82- measuring_point_description = "MP for Thing 1" ,
83- start_date = "2023-01-01" ,
84- reason = "Initial entry" ,
85- )
86- mp_history_2 = MeasuringPointHistory (
87- thing_id = thing2 .id ,
88- measuring_point_height = 10.0 ,
89- measuring_point_description = "MP for Thing 2" ,
90- start_date = "2023-01-01" ,
91- reason = "Initial entry" ,
92- )
93- session .add (mp_history_1 )
94- session .add (mp_history_2 )
95-
96- loc1 = Location (
97- # name="Test Location 1",
98- point = geofunc .ST_GeomFromText ("POINT(10.1 10.1)" , srid = SRID_WGS84 ),
99- elevation = 0 ,
71+ thing1 = _create_well_with_location (
72+ session ,
73+ name = "Thing 1" ,
74+ point_wkt = "POINT(10.1 10.1)" ,
10075 )
101- loc2 = Location (
102- # name="Test Location 2" ,
103- point = geofunc . ST_GeomFromText ( "POINT(20 20)" , srid = SRID_WGS84 ) ,
104- elevation = 0 ,
76+ thing2 = _create_well_with_location (
77+ session ,
78+ name = "Thing 2" ,
79+ point_wkt = "POINT(20 20)" ,
10580 )
106- session .add (loc1 )
107- session .add (loc2 )
108-
109- session .add (LocationThingAssociation (location = loc1 , thing = thing1 ))
110- session .add (LocationThingAssociation (location = loc2 , thing = thing2 ))
11181
11282 group = Group (
11383 name = "Test Group Foo" ,
@@ -117,15 +87,48 @@ def populate():
11787
11888 session .add (group )
11989 session .commit ()
120- yield
121-
122- # Cleanup
123- session .delete (loc1 )
124- session .delete (loc2 )
125- session .delete (group )
126- session .delete (thing1 )
127- session .delete (thing2 )
128- session .commit ()
90+ try :
91+ yield
92+ finally :
93+ session .delete (group )
94+ session .delete (thing1 )
95+ session .delete (thing2 )
96+ session .commit ()
97+
98+
99+ def _create_well_with_location (session , name : str , point_wkt : str ) -> Thing :
100+ thing = Thing (name = name , thing_type = "water well" , release_status = "draft" )
101+ session .add (thing )
102+ session .commit ()
103+ session .refresh (thing )
104+
105+ location = Location (
106+ point = geofunc .ST_GeomFromText (point_wkt , srid = SRID_WGS84 ),
107+ elevation = 0 ,
108+ release_status = "draft" ,
109+ )
110+ session .add (location )
111+ session .flush ()
112+ session .add (
113+ LocationThingAssociation (
114+ location_id = location .id ,
115+ thing_id = thing .id ,
116+ effective_start = date .today ().isoformat (),
117+ )
118+ )
119+ session .add (
120+ MeasuringPointHistory (
121+ thing_id = thing .id ,
122+ measuring_point_height = 5.0 ,
123+ measuring_point_description = f"MP for { name } " ,
124+ start_date = date .today (),
125+ release_status = "draft" ,
126+ )
127+ )
128+ session .commit ()
129+ return thing
130+
131+ # Cleanup
129132
130133
131134def test_get_project_area ():
0 commit comments