Skip to content

Commit b800f65

Browse files
adapt test for coordinates
1 parent 710f642 commit b800f65

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

tests/db/weather/test_weather_models.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,56 @@
11
from datetime import datetime
22

33
import pytest
4-
from sqlmodel import select
4+
from sqlalchemy import text, select
55

6-
from pypsdm.db.weather.models import Coordinate, WeatherValue
6+
from pypsdm.db.weather.models import WeatherValue, Coordinate
77

88

99
@pytest.mark.docker_required
1010
def test_create_coordinate(db_session):
1111
"""Test creating a coordinate."""
1212
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})
1724
db_session.commit()
1825

19-
first_coordinate = db_session.get(Coordinate, 1)
26+
first_coordinate: Coordinate = db_session.get(Coordinate, 1)
2027
second_coordinate = db_session.get(Coordinate, 2)
2128
missing_coordinate = db_session.get(Coordinate, 3)
29+
2230
assert first_coordinate is not None
2331
assert first_coordinate.longitude == 8.645
2432
assert first_coordinate.latitude == 50.123
33+
2534
assert second_coordinate is not None
2635
assert second_coordinate.x == 7.46
2736
assert second_coordinate.y == 51.5
37+
2838
assert missing_coordinate is None
2939

3040

3141
@pytest.mark.docker_required
3242
def test_create_weather_value(db_session):
3343
"""Test creating a weather value."""
34-
# First create a coordinate
3544
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})
3752
db_session.commit()
3853

39-
# Create a weather value
4054
now = datetime.now()
4155
weather = WeatherValue(
4256
time=now,
@@ -50,7 +64,6 @@ def test_create_weather_value(db_session):
5064
db_session.add(weather)
5165
db_session.commit()
5266

53-
# Retrieve it
5467
retrieved = db_session.exec(
5568
select(WeatherValue).where(
5669
WeatherValue.time == now, WeatherValue.coordinate_id == berlin.id

0 commit comments

Comments
 (0)