kopia lustrzana https://github.com/glidernet/ogn-python
Fixed backend tests
rodzic
88a807406b
commit
9692d5031e
|
@ -4,6 +4,8 @@ from sqlalchemy import func, and_, between, case
|
||||||
|
|
||||||
from ogn_python.model import AircraftBeacon, Device, Receiver
|
from ogn_python.model import AircraftBeacon, Device, Receiver
|
||||||
|
|
||||||
|
from ogn_python import db
|
||||||
|
|
||||||
|
|
||||||
def utc_to_local(utc_dt):
|
def utc_to_local(utc_dt):
|
||||||
return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
|
return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
|
||||||
|
@ -17,9 +19,9 @@ def decode(code):
|
||||||
return code[2:9]
|
return code[2:9]
|
||||||
|
|
||||||
|
|
||||||
def rec(session):
|
def rec():
|
||||||
last_10_minutes = datetime.utcnow() - timedelta(minutes=10)
|
last_10_minutes = datetime.utcnow() - timedelta(minutes=10)
|
||||||
receiver_query = session.query(Receiver,
|
receiver_query = db.session.query(Receiver,
|
||||||
case([(Receiver.lastseen > last_10_minutes, True)],
|
case([(Receiver.lastseen > last_10_minutes, True)],
|
||||||
else_=False).label('is_online')) \
|
else_=False).label('is_online')) \
|
||||||
.order_by(Receiver.name)
|
.order_by(Receiver.name)
|
||||||
|
@ -38,14 +40,14 @@ def rec(session):
|
||||||
return xml
|
return xml
|
||||||
|
|
||||||
|
|
||||||
def lxml(session, show_offline=False, lat_max=90, lat_min=-90, lon_max=180, lon_min=-180):
|
def lxml(show_offline=False, lat_max=90, lat_min=-90, lon_max=180, lon_min=-180):
|
||||||
|
|
||||||
if show_offline:
|
if show_offline:
|
||||||
observation_start = date.today()
|
observation_start = date.today()
|
||||||
else:
|
else:
|
||||||
observation_start = datetime.utcnow() - timedelta(minutes=5)
|
observation_start = datetime.utcnow() - timedelta(minutes=5)
|
||||||
|
|
||||||
position_query = session.query(AircraftBeacon, Device) \
|
position_query = db.session.query(AircraftBeacon, Device) \
|
||||||
.filter(and_(between(func.ST_Y(AircraftBeacon.location_wkt), lat_min, lat_max),
|
.filter(and_(between(func.ST_Y(AircraftBeacon.location_wkt), lat_min, lat_max),
|
||||||
between(func.ST_X(AircraftBeacon.location_wkt), lon_min, lon_max))) \
|
between(func.ST_X(AircraftBeacon.location_wkt), lon_min, lon_max))) \
|
||||||
.filter(Device.lastseen > observation_start) \
|
.filter(Device.lastseen > observation_start) \
|
||||||
|
|
|
@ -5,6 +5,8 @@ from sqlalchemy import func, case
|
||||||
from sqlalchemy.sql.expression import label
|
from sqlalchemy.sql.expression import label
|
||||||
from ogn_python.model import Receiver
|
from ogn_python.model import Receiver
|
||||||
|
|
||||||
|
from ogn_python import db
|
||||||
|
|
||||||
|
|
||||||
def alchemyencoder(obj):
|
def alchemyencoder(obj):
|
||||||
"""JSON encoder function for SQLAlchemy special classes."""
|
"""JSON encoder function for SQLAlchemy special classes."""
|
||||||
|
@ -17,10 +19,10 @@ def alchemyencoder(obj):
|
||||||
return float(obj)
|
return float(obj)
|
||||||
|
|
||||||
|
|
||||||
def stations2_filtered_pl(session):
|
def stations2_filtered_pl():
|
||||||
last_10_minutes = datetime.utcnow() - timedelta(minutes=10)
|
last_10_minutes = datetime.utcnow() - timedelta(minutes=10)
|
||||||
|
|
||||||
query = session.query(
|
query = db.session.query(
|
||||||
Receiver.name.label('s'),
|
Receiver.name.label('s'),
|
||||||
label('lt', func.round(func.ST_Y(Receiver.location_wkt) * 10000) / 10000),
|
label('lt', func.round(func.ST_Y(Receiver.location_wkt) * 10000) / 10000),
|
||||||
label('lg', func.round(func.ST_X(Receiver.location_wkt) * 10000) / 10000),
|
label('lg', func.round(func.ST_X(Receiver.location_wkt) * 10000) / 10000),
|
||||||
|
@ -30,7 +32,7 @@ def stations2_filtered_pl(session):
|
||||||
label('v', Receiver.version + '.' + Receiver.platform)) \
|
label('v', Receiver.version + '.' + Receiver.platform)) \
|
||||||
.order_by(Receiver.lastseen)
|
.order_by(Receiver.lastseen)
|
||||||
|
|
||||||
res = session.execute(query)
|
res = db.session.execute(query)
|
||||||
stations = json.dumps({'stations': [dict(r) for r in res]}, default=alchemyencoder)
|
stations = json.dumps({'stations': [dict(r) for r in res]}, default=alchemyencoder)
|
||||||
|
|
||||||
return stations
|
return stations
|
||||||
|
|
|
@ -51,7 +51,7 @@ class TestDB(TestBaseDB, XmlTestMixin):
|
||||||
def test_rec(self, datetime_mock):
|
def test_rec(self, datetime_mock):
|
||||||
datetime_mock.utcnow.return_value = datetime(2017, 12, 20, 10, 0)
|
datetime_mock.utcnow.return_value = datetime(2017, 12, 20, 10, 0)
|
||||||
|
|
||||||
data = rec(db.session).encode(encoding='utf-8')
|
data = rec().encode(encoding='utf-8')
|
||||||
|
|
||||||
# Check the document
|
# Check the document
|
||||||
root = self.assertXmlDocument(data)
|
root = self.assertXmlDocument(data)
|
||||||
|
@ -75,7 +75,7 @@ class TestDB(TestBaseDB, XmlTestMixin):
|
||||||
def test_lxml(self, datetime_mock, utc_to_local_mock):
|
def test_lxml(self, datetime_mock, utc_to_local_mock):
|
||||||
datetime_mock.utcnow.return_value = datetime(2017, 12, 20, 10, 0, 5)
|
datetime_mock.utcnow.return_value = datetime(2017, 12, 20, 10, 0, 5)
|
||||||
|
|
||||||
data = lxml(db.session).encode(encoding='utf-8')
|
data = lxml().encode(encoding='utf-8')
|
||||||
|
|
||||||
# Check the complete document
|
# Check the complete document
|
||||||
expected = """<?xml version="1.0" encoding="UTF-8"?>
|
expected = """<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
@ -91,7 +91,7 @@ class TestDB(TestBaseDB, XmlTestMixin):
|
||||||
def test_stations2_filtered_pl(self, datetime_mock):
|
def test_stations2_filtered_pl(self, datetime_mock):
|
||||||
datetime_mock.utcnow.return_value = datetime(2017, 12, 20, 10, 0)
|
datetime_mock.utcnow.return_value = datetime(2017, 12, 20, 10, 0)
|
||||||
|
|
||||||
result = stations2_filtered_pl(db.session)
|
result = stations2_filtered_pl()
|
||||||
|
|
||||||
data = json.loads(result)
|
data = json.loads(result)
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,11 @@ class TestBaseDB(unittest.TestCase):
|
||||||
DELETE FROM receiver_beacons;
|
DELETE FROM receiver_beacons;
|
||||||
DELETE FROM takeoff_landings;
|
DELETE FROM takeoff_landings;
|
||||||
DELETE FROM logbook;
|
DELETE FROM logbook;
|
||||||
|
DELETE FROM receiver_coverages;
|
||||||
|
DELETE FROM device_stats;
|
||||||
|
DELETE FROM receiver_stats;
|
||||||
|
DELETE FROM receivers;
|
||||||
|
DELETE FROM devices;
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue