1
1
from datetime import datetime
2
2
3
3
import pytest
4
- from sqlmodel import select
4
+ from sqlalchemy import text , select
5
5
6
- from pypsdm .db .weather .models import Coordinate , WeatherValue
6
+ from pypsdm .db .weather .models import WeatherValue , Coordinate
7
7
8
8
9
9
@pytest .mark .docker_required
10
10
def test_create_coordinate (db_session ):
11
11
"""Test creating a coordinate."""
12
12
coordinates = []
13
- coord_from_str = Coordinate (id = 1 , coordinate = "POINT(8.645 50.123)" )
14
- coord_from_xy = Coordinate .from_xy (2 , 7.46 , 51.5 )
15
- coordinates : list [Coordinate ] = coordinates + [coord_from_str , coord_from_xy ]
16
- db_session .add_all (coordinates )
13
+ coord_1 = Coordinate .from_xy (1 , 8.645 , 50.123 )
14
+ coord_2 = Coordinate .from_xy (2 , 7.46 , 51.5 )
15
+ coordinates : list [Coordinate ] = coordinates + [coord_1 , coord_2 ]
16
+
17
+ query = text ("""
18
+ INSERT INTO coordinate (id, coordinate)
19
+ VALUES (:id, ST_SetSRID(ST_GeomFromWKB(:geom), 4326));
20
+ """ )
21
+
22
+ for coord in coordinates :
23
+ db_session .execute (query , {'id' : coord .id , 'geom' : coord .coordinate })
17
24
db_session .commit ()
18
25
19
- first_coordinate = db_session .get (Coordinate , 1 )
26
+ first_coordinate : Coordinate = db_session .get (Coordinate , 1 )
20
27
second_coordinate = db_session .get (Coordinate , 2 )
21
28
missing_coordinate = db_session .get (Coordinate , 3 )
29
+
22
30
assert first_coordinate is not None
23
31
assert first_coordinate .longitude == 8.645
24
32
assert first_coordinate .latitude == 50.123
33
+
25
34
assert second_coordinate is not None
26
35
assert second_coordinate .x == 7.46
27
36
assert second_coordinate .y == 51.5
37
+
28
38
assert missing_coordinate is None
29
39
30
40
31
41
@pytest .mark .docker_required
32
42
def test_create_weather_value (db_session ):
33
43
"""Test creating a weather value."""
34
- # First create a coordinate
35
44
berlin = Coordinate .from_xy (id = 1 , x = 13.405 , y = 52.52 )
36
- db_session .add (berlin )
45
+
46
+ query = text ("""
47
+ INSERT INTO coordinate (id, coordinate)
48
+ VALUES (:id, ST_SetSRID(ST_GeomFromWKB(:geom), 4326));
49
+ """ )
50
+
51
+ db_session .execute (query , {'id' : berlin .id , 'geom' : berlin .coordinate })
37
52
db_session .commit ()
38
53
39
- # Create a weather value
40
54
now = datetime .now ()
41
55
weather = WeatherValue (
42
56
time = now ,
@@ -50,7 +64,6 @@ def test_create_weather_value(db_session):
50
64
db_session .add (weather )
51
65
db_session .commit ()
52
66
53
- # Retrieve it
54
67
retrieved = db_session .exec (
55
68
select (WeatherValue ).where (
56
69
WeatherValue .time == now , WeatherValue .coordinate_id == berlin .id
0 commit comments