kopia lustrzana https://github.com/glidernet/ogn-python
Fixed logbook tests
rodzic
8b38268675
commit
46083d0b63
|
@ -17,15 +17,21 @@ def update_entries(session, date, logger=None):
|
||||||
|
|
||||||
# limit time range to given date and set window partition and window order
|
# limit time range to given date and set window partition and window order
|
||||||
(start, end) = date_to_timestamps(date)
|
(start, end) = date_to_timestamps(date)
|
||||||
pa = TakeoffLanding.device_id
|
pa = TakeoffLanding.address
|
||||||
wo = and_(TakeoffLanding.device_id, TakeoffLanding.airport_id, TakeoffLanding.timestamp)
|
wo = and_(TakeoffLanding.address, TakeoffLanding.airport_id, TakeoffLanding.timestamp)
|
||||||
|
|
||||||
|
# delete existing elements
|
||||||
|
session.query(Logbook)\
|
||||||
|
.filter(between(Logbook.reftime, start, end))\
|
||||||
|
.delete(synchronize_session='fetch')
|
||||||
|
session.commit()
|
||||||
|
|
||||||
# make a query with current, previous and next "takeoff_landing" event, so we can find complete flights
|
# make a query with current, previous and next "takeoff_landing" event, so we can find complete flights
|
||||||
sq = (
|
sq = (
|
||||||
session.query(
|
session.query(
|
||||||
TakeoffLanding.device_id,
|
TakeoffLanding.address,
|
||||||
func.lag(TakeoffLanding.device_id).over(partition_by=pa, order_by=wo).label("device_id_prev"),
|
func.lag(TakeoffLanding.address).over(partition_by=pa, order_by=wo).label("address_prev"),
|
||||||
func.lead(TakeoffLanding.device_id).over(partition_by=pa, order_by=wo).label("device_id_next"),
|
func.lead(TakeoffLanding.address).over(partition_by=pa, order_by=wo).label("address_next"),
|
||||||
TakeoffLanding.timestamp,
|
TakeoffLanding.timestamp,
|
||||||
func.lag(TakeoffLanding.timestamp).over(partition_by=pa, order_by=wo).label("timestamp_prev"),
|
func.lag(TakeoffLanding.timestamp).over(partition_by=pa, order_by=wo).label("timestamp_prev"),
|
||||||
func.lead(TakeoffLanding.timestamp).over(partition_by=pa, order_by=wo).label("timestamp_next"),
|
func.lead(TakeoffLanding.timestamp).over(partition_by=pa, order_by=wo).label("timestamp_next"),
|
||||||
|
@ -46,7 +52,7 @@ def update_entries(session, date, logger=None):
|
||||||
# find complete flights
|
# find complete flights
|
||||||
complete_flight_query = session.query(
|
complete_flight_query = session.query(
|
||||||
sq.c.timestamp.label("reftime"),
|
sq.c.timestamp.label("reftime"),
|
||||||
sq.c.device_id.label("device_id"),
|
sq.c.address.label("address"),
|
||||||
sq.c.timestamp.label("takeoff_timestamp"),
|
sq.c.timestamp.label("takeoff_timestamp"),
|
||||||
sq.c.track.label("takeoff_track"),
|
sq.c.track.label("takeoff_track"),
|
||||||
sq.c.airport_id.label("takeoff_airport_id"),
|
sq.c.airport_id.label("takeoff_airport_id"),
|
||||||
|
@ -59,7 +65,7 @@ def update_entries(session, date, logger=None):
|
||||||
only_landings_query = (
|
only_landings_query = (
|
||||||
session.query(
|
session.query(
|
||||||
sq.c.timestamp.label("reftime"),
|
sq.c.timestamp.label("reftime"),
|
||||||
sq.c.device_id.label("device_id"),
|
sq.c.address.label("address"),
|
||||||
null().label("takeoff_timestamp"),
|
null().label("takeoff_timestamp"),
|
||||||
null().label("takeoff_track"),
|
null().label("takeoff_track"),
|
||||||
null().label("takeoff_airport_id"),
|
null().label("takeoff_airport_id"),
|
||||||
|
@ -75,7 +81,7 @@ def update_entries(session, date, logger=None):
|
||||||
only_starts_query = (
|
only_starts_query = (
|
||||||
session.query(
|
session.query(
|
||||||
sq.c.timestamp.label("reftime"),
|
sq.c.timestamp.label("reftime"),
|
||||||
sq.c.device_id.label("device_id"),
|
sq.c.address.label("address"),
|
||||||
sq.c.timestamp.label("takeoff_timestamp"),
|
sq.c.timestamp.label("takeoff_timestamp"),
|
||||||
sq.c.track.label("takeoff_track"),
|
sq.c.track.label("takeoff_track"),
|
||||||
sq.c.airport_id.label("takeoff_airport_id"),
|
sq.c.airport_id.label("takeoff_airport_id"),
|
||||||
|
@ -88,61 +94,13 @@ def update_entries(session, date, logger=None):
|
||||||
)
|
)
|
||||||
|
|
||||||
# unite all computated flights
|
# unite all computated flights
|
||||||
union_query = complete_flight_query.union(only_landings_query, only_starts_query).subquery()
|
logbook_entries = complete_flight_query.union(only_landings_query, only_starts_query).subquery()
|
||||||
|
|
||||||
# if a logbook entry exist --> update it
|
|
||||||
upd = (
|
|
||||||
update(Logbook)
|
|
||||||
.where(
|
|
||||||
and_(
|
|
||||||
Logbook.device_id == union_query.c.device_id,
|
|
||||||
union_query.c.takeoff_airport_id != null(),
|
|
||||||
union_query.c.landing_airport_id != null(),
|
|
||||||
or_(
|
|
||||||
and_(Logbook.takeoff_airport_id == union_query.c.takeoff_airport_id, Logbook.takeoff_timestamp == union_query.c.takeoff_timestamp, Logbook.landing_airport_id == null()),
|
|
||||||
and_(Logbook.takeoff_airport_id == null(), Logbook.landing_airport_id == union_query.c.landing_airport_id, Logbook.landing_timestamp == union_query.c.landing_timestamp),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.values(
|
|
||||||
{
|
|
||||||
"reftime": union_query.c.reftime,
|
|
||||||
"takeoff_timestamp": union_query.c.takeoff_timestamp,
|
|
||||||
"takeoff_track": union_query.c.takeoff_track,
|
|
||||||
"takeoff_airport_id": union_query.c.takeoff_airport_id,
|
|
||||||
"landing_timestamp": union_query.c.landing_timestamp,
|
|
||||||
"landing_track": union_query.c.landing_track,
|
|
||||||
"landing_airport_id": union_query.c.landing_airport_id,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
result = session.execute(upd)
|
|
||||||
update_counter = result.rowcount
|
|
||||||
session.commit()
|
|
||||||
logger.debug("Updated logbook entries: {}".format(update_counter))
|
|
||||||
|
|
||||||
# if a logbook entry doesnt exist --> insert it
|
|
||||||
new_logbook_entries = session.query(union_query).filter(
|
|
||||||
~exists().where(
|
|
||||||
and_(
|
|
||||||
Logbook.device_id == union_query.c.device_id,
|
|
||||||
or_(
|
|
||||||
and_(Logbook.takeoff_airport_id == union_query.c.takeoff_airport_id, Logbook.takeoff_timestamp == union_query.c.takeoff_timestamp),
|
|
||||||
and_(Logbook.takeoff_airport_id == null(), union_query.c.takeoff_airport_id == null()),
|
|
||||||
),
|
|
||||||
or_(
|
|
||||||
and_(Logbook.landing_airport_id == union_query.c.landing_airport_id, Logbook.landing_timestamp == union_query.c.landing_timestamp),
|
|
||||||
and_(Logbook.landing_airport_id == null(), union_query.c.landing_airport_id == null()),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# ... insert them into logbook
|
||||||
ins = insert(Logbook).from_select(
|
ins = insert(Logbook).from_select(
|
||||||
(
|
(
|
||||||
Logbook.reftime,
|
Logbook.reftime,
|
||||||
Logbook.device_id,
|
Logbook.address,
|
||||||
Logbook.takeoff_timestamp,
|
Logbook.takeoff_timestamp,
|
||||||
Logbook.takeoff_track,
|
Logbook.takeoff_track,
|
||||||
Logbook.takeoff_airport_id,
|
Logbook.takeoff_airport_id,
|
||||||
|
@ -150,14 +108,14 @@ def update_entries(session, date, logger=None):
|
||||||
Logbook.landing_track,
|
Logbook.landing_track,
|
||||||
Logbook.landing_airport_id,
|
Logbook.landing_airport_id,
|
||||||
),
|
),
|
||||||
new_logbook_entries,
|
logbook_entries,
|
||||||
)
|
)
|
||||||
|
|
||||||
result = session.execute(ins)
|
result = session.execute(ins)
|
||||||
insert_counter = result.rowcount
|
insert_counter = result.rowcount
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
finish_message = "Logbook: {} inserted, {} updated".format(insert_counter, update_counter)
|
finish_message = "Logbook: {} inserted".format(insert_counter)
|
||||||
logger.debug(finish_message)
|
logger.debug(finish_message)
|
||||||
return finish_message
|
return finish_message
|
||||||
|
|
||||||
|
@ -185,7 +143,7 @@ def update_max_altitudes(session, date, logger=None):
|
||||||
max_altitudes = (
|
max_altitudes = (
|
||||||
session.query(Logbook.id, func.max(AircraftBeacon.altitude).label("max_altitude"))
|
session.query(Logbook.id, func.max(AircraftBeacon.altitude).label("max_altitude"))
|
||||||
.filter(Logbook.id == logbook_entries.c.id)
|
.filter(Logbook.id == logbook_entries.c.id)
|
||||||
.filter(and_(AircraftBeacon.device_id == Logbook.device_id, AircraftBeacon.timestamp >= Logbook.takeoff_timestamp, AircraftBeacon.timestamp <= Logbook.landing_timestamp))
|
.filter(and_(AircraftBeacon.address == Logbook.address, AircraftBeacon.timestamp >= Logbook.takeoff_timestamp, AircraftBeacon.timestamp <= Logbook.landing_timestamp))
|
||||||
.group_by(Logbook.id)
|
.group_by(Logbook.id)
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,6 +8,12 @@ class TestBaseDB(unittest.TestCase):
|
||||||
self.app_context = self.app.app_context()
|
self.app_context = self.app.app_context()
|
||||||
self.app_context.push()
|
self.app_context.push()
|
||||||
|
|
||||||
|
# Remove tables...
|
||||||
|
db.drop_all()
|
||||||
|
db.session.execute("DROP TABLE IF EXISTS elevation;")
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
# ... and create them again
|
||||||
db.session.execute("CREATE EXTENSION IF NOT EXISTS postgis;")
|
db.session.execute("CREATE EXTENSION IF NOT EXISTS postgis;")
|
||||||
db.create_all()
|
db.create_all()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -17,10 +23,6 @@ class TestBaseDB(unittest.TestCase):
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
db.session.remove()
|
db.session.remove()
|
||||||
db.drop_all()
|
|
||||||
|
|
||||||
db.session.execute("DROP TABLE IF EXISTS elevaion;")
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
self.app_context.pop()
|
self.app_context.pop()
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ from app.collect.database import upsert
|
||||||
|
|
||||||
|
|
||||||
class TestDatabase(TestBaseDB):
|
class TestDatabase(TestBaseDB):
|
||||||
@unittest.skip("wip")
|
|
||||||
def test_insert_duplicate_beacons(self):
|
def test_insert_duplicate_beacons(self):
|
||||||
row1 = {"name": "FLRDD0815", "receiver_name": "Koenigsdf", "timestamp": "2019-01-26 11:51:00", "ground_speed": None}
|
row1 = {"name": "FLRDD0815", "receiver_name": "Koenigsdf", "timestamp": "2019-01-26 11:51:00", "ground_speed": None}
|
||||||
row2 = {"name": "FLRDD0815", "receiver_name": "Koenigsdf", "timestamp": "2019-01-26 11:52:00", "ground_speed": 0}
|
row2 = {"name": "FLRDD0815", "receiver_name": "Koenigsdf", "timestamp": "2019-01-26 11:52:00", "ground_speed": 0}
|
||||||
|
|
|
@ -12,8 +12,8 @@ class TestLogbook(TestBaseDB):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
|
|
||||||
# Create basic data and insert
|
# Create basic data and insert
|
||||||
self.dd0815 = Device(address="DD0815")
|
self.dd0815 = Device(name="FLRDD0815", address="DD0815")
|
||||||
self.dd4711 = Device(address="DD4711")
|
self.dd4711 = Device(name="FLRDD4711", address="DD4711")
|
||||||
|
|
||||||
self.koenigsdorf = Airport(name="Koenigsdorf")
|
self.koenigsdorf = Airport(name="Koenigsdorf")
|
||||||
self.ohlstadt = Airport(name="Ohlstadt")
|
self.ohlstadt = Airport(name="Ohlstadt")
|
||||||
|
@ -26,10 +26,10 @@ class TestLogbook(TestBaseDB):
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
# Prepare takeoff and landings
|
# Prepare takeoff and landings
|
||||||
self.takeoff_koenigsdorf_dd0815 = TakeoffLanding(is_takeoff=True, timestamp="2016-06-01 10:00:00", airport_id=self.koenigsdorf.id, device_id=self.dd0815.id)
|
self.takeoff_koenigsdorf_dd0815 = TakeoffLanding(is_takeoff=True, timestamp="2016-06-01 10:00:00", airport_id=self.koenigsdorf.id, address=self.dd0815.address)
|
||||||
self.landing_koenigsdorf_dd0815 = TakeoffLanding(is_takeoff=False, timestamp="2016-06-01 10:05:00", airport_id=self.koenigsdorf.id, device_id=self.dd0815.id)
|
self.landing_koenigsdorf_dd0815 = TakeoffLanding(is_takeoff=False, timestamp="2016-06-01 10:05:00", airport_id=self.koenigsdorf.id, address=self.dd0815.address)
|
||||||
self.landing_koenigsdorf_dd0815_later = TakeoffLanding(is_takeoff=False, timestamp="2016-06-02 10:05:00", airport_id=self.koenigsdorf.id, device_id=self.dd0815.id)
|
self.landing_koenigsdorf_dd0815_later = TakeoffLanding(is_takeoff=False, timestamp="2016-06-02 10:05:00", airport_id=self.koenigsdorf.id, address=self.dd0815.address)
|
||||||
self.takeoff_ohlstadt_dd4711 = TakeoffLanding(is_takeoff=True, timestamp="2016-06-01 10:00:00", airport_id=self.ohlstadt.id, device_id=self.dd4711.id)
|
self.takeoff_ohlstadt_dd4711 = TakeoffLanding(is_takeoff=True, timestamp="2016-06-01 10:00:00", airport_id=self.ohlstadt.id, address=self.dd4711.address)
|
||||||
|
|
||||||
def get_logbook_entries(self):
|
def get_logbook_entries(self):
|
||||||
return db.session.query(Logbook).order_by(Logbook.takeoff_airport_id, Logbook.reftime).all()
|
return db.session.query(Logbook).order_by(Logbook.takeoff_airport_id, Logbook.reftime).all()
|
||||||
|
@ -44,10 +44,6 @@ class TestLogbook(TestBaseDB):
|
||||||
self.assertEqual(entries[0].takeoff_airport_id, self.koenigsdorf.id)
|
self.assertEqual(entries[0].takeoff_airport_id, self.koenigsdorf.id)
|
||||||
self.assertEqual(entries[0].landing_airport_id, None)
|
self.assertEqual(entries[0].landing_airport_id, None)
|
||||||
|
|
||||||
update_entries(session=db.session, date=datetime.date(2016, 6, 1))
|
|
||||||
entries2 = self.get_logbook_entries()
|
|
||||||
self.assertEqual(entries, entries2)
|
|
||||||
|
|
||||||
def test_single_landing(self):
|
def test_single_landing(self):
|
||||||
db.session.add(self.landing_koenigsdorf_dd0815)
|
db.session.add(self.landing_koenigsdorf_dd0815)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -58,10 +54,6 @@ class TestLogbook(TestBaseDB):
|
||||||
self.assertEqual(entries[0].takeoff_airport_id, None)
|
self.assertEqual(entries[0].takeoff_airport_id, None)
|
||||||
self.assertEqual(entries[0].landing_airport_id, self.koenigsdorf.id)
|
self.assertEqual(entries[0].landing_airport_id, self.koenigsdorf.id)
|
||||||
|
|
||||||
update_entries(session=db.session, date=datetime.date(2016, 6, 1))
|
|
||||||
entries2 = self.get_logbook_entries()
|
|
||||||
self.assertEqual(entries, entries2)
|
|
||||||
|
|
||||||
def test_different_takeoffs(self):
|
def test_different_takeoffs(self):
|
||||||
db.session.add(self.takeoff_koenigsdorf_dd0815)
|
db.session.add(self.takeoff_koenigsdorf_dd0815)
|
||||||
db.session.add(self.takeoff_ohlstadt_dd4711)
|
db.session.add(self.takeoff_ohlstadt_dd4711)
|
||||||
|
@ -73,10 +65,6 @@ class TestLogbook(TestBaseDB):
|
||||||
self.assertEqual(entries[0].takeoff_airport_id, self.koenigsdorf.id)
|
self.assertEqual(entries[0].takeoff_airport_id, self.koenigsdorf.id)
|
||||||
self.assertEqual(entries[1].takeoff_airport_id, self.ohlstadt.id)
|
self.assertEqual(entries[1].takeoff_airport_id, self.ohlstadt.id)
|
||||||
|
|
||||||
update_entries(session=db.session, date=datetime.date(2016, 6, 1))
|
|
||||||
entries2 = self.get_logbook_entries()
|
|
||||||
self.assertEqual(entries, entries2)
|
|
||||||
|
|
||||||
def test_takeoff_and_landing(self):
|
def test_takeoff_and_landing(self):
|
||||||
db.session.add(self.takeoff_koenigsdorf_dd0815)
|
db.session.add(self.takeoff_koenigsdorf_dd0815)
|
||||||
db.session.add(self.landing_koenigsdorf_dd0815)
|
db.session.add(self.landing_koenigsdorf_dd0815)
|
||||||
|
@ -88,10 +76,6 @@ class TestLogbook(TestBaseDB):
|
||||||
self.assertEqual(entries[0].takeoff_airport_id, self.koenigsdorf.id)
|
self.assertEqual(entries[0].takeoff_airport_id, self.koenigsdorf.id)
|
||||||
self.assertEqual(entries[0].landing_airport_id, self.koenigsdorf.id)
|
self.assertEqual(entries[0].landing_airport_id, self.koenigsdorf.id)
|
||||||
|
|
||||||
update_entries(session=db.session, date=datetime.date(2016, 6, 1))
|
|
||||||
entries2 = self.get_logbook_entries()
|
|
||||||
self.assertEqual(entries, entries2)
|
|
||||||
|
|
||||||
def test_takeoff_and_landing_on_different_days(self):
|
def test_takeoff_and_landing_on_different_days(self):
|
||||||
db.session.add(self.takeoff_koenigsdorf_dd0815)
|
db.session.add(self.takeoff_koenigsdorf_dd0815)
|
||||||
db.session.add(self.landing_koenigsdorf_dd0815_later)
|
db.session.add(self.landing_koenigsdorf_dd0815_later)
|
||||||
|
@ -106,10 +90,6 @@ class TestLogbook(TestBaseDB):
|
||||||
self.assertEqual(entries[1].landing_airport_id, self.koenigsdorf.id)
|
self.assertEqual(entries[1].landing_airport_id, self.koenigsdorf.id)
|
||||||
self.assertEqual(entries[1].reftime, self.landing_koenigsdorf_dd0815_later.timestamp)
|
self.assertEqual(entries[1].reftime, self.landing_koenigsdorf_dd0815_later.timestamp)
|
||||||
|
|
||||||
update_entries(session=db.session, date=datetime.date(2016, 6, 1))
|
|
||||||
entries2 = self.get_logbook_entries()
|
|
||||||
self.assertEqual(entries, entries2)
|
|
||||||
|
|
||||||
def test_update(self):
|
def test_update(self):
|
||||||
db.session.add(self.takeoff_koenigsdorf_dd0815)
|
db.session.add(self.takeoff_koenigsdorf_dd0815)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -158,10 +138,6 @@ class TestLogbook(TestBaseDB):
|
||||||
self.assertEqual(entries[0].landing_airport_id, self.koenigsdorf.id)
|
self.assertEqual(entries[0].landing_airport_id, self.koenigsdorf.id)
|
||||||
self.assertEqual(entries[0].reftime, self.takeoff_koenigsdorf_dd0815.timestamp)
|
self.assertEqual(entries[0].reftime, self.takeoff_koenigsdorf_dd0815.timestamp)
|
||||||
|
|
||||||
update_entries(session=db.session, date=datetime.date(2016, 6, 1))
|
|
||||||
entries2 = self.get_logbook_entries()
|
|
||||||
self.assertEqual(entries, entries2)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Ładowanie…
Reference in New Issue