Skip to content

Commit

Permalink
Merge pull request #614 from Turbo87/tests
Browse files Browse the repository at this point in the history
`FlightPathFix` unit tests
  • Loading branch information
Turbo87 committed Feb 14, 2017
2 parents 46d8a10 + a1716a9 commit ea471f5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
13 changes: 0 additions & 13 deletions skylines/lib/xcsoar_/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ def read_location(node):
return None


def import_location_attribute(node, name):
"""
Reads a Location instance from an attribute of the node
with the given name.
"""

if node is None or name not in node:
return None

location = node[name]
return read_location(location)


def import_datetime_attribute(node, name):
if node is None or name not in node:
return None
Expand Down
19 changes: 12 additions & 7 deletions skylines/lib/xcsoar_/flightpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
from skylines.model import Elevation, IGCFile, Location
from xcsoar import Flight

_FlightPathFix = namedtuple('FlightPathFix', [
'datetime', 'seconds_of_day', 'location', 'gps_altitude', 'pressure_altitude',
'enl', 'track', 'groundspeed', 'tas', 'ias', 'siu', 'elevation'
])

flightpathfix_fields = ['datetime', 'seconds_of_day',
'location', 'gps_altitude', 'pressure_altitude',
'enl', 'track', 'groundspeed', 'tas', 'ias',
'siu', 'elevation']


class FlightPathFix(namedtuple('FlightPathFix', flightpathfix_fields)):
class FlightPathFix(_FlightPathFix):
def __new__(cls, *args, **kwargs):
"""
Custom constructor to support:
- Filling fields with None by default
- Supplying more `args` then field names
- Not specifying all arguments when using `kwargs`
"""
values = [None] * 12

values[:min(12, len(args))] = args[:12]

for i, key in enumerate(flightpathfix_fields):
for i, key in enumerate(cls._fields):
if key in kwargs:
values[i] = kwargs[key]

Expand Down
48 changes: 48 additions & 0 deletions tests/lib/xcsoar/flightpath_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from datetime import datetime

from skylines.lib.xcsoar_ import FlightPathFix

from pytest import approx


def test_list_to_fix():
values = [datetime(2016, 5, 4, 8, 10, 50), 29450,
dict(latitude=50.82191666668235, longitude=6.181650000001908),
230, 48, None, None, 0, None, None, 8, None, 0]

fix = FlightPathFix(*values)
assert fix.datetime.isoformat() == '2016-05-04T08:10:50'
assert fix.seconds_of_day == 29450
assert fix.location['latitude'] == approx(50.82191666668235)
assert fix.location['longitude'] == approx(6.181650000001908)
assert fix.gps_altitude == 230
assert fix.pressure_altitude == 48
assert fix.enl == None
assert fix.track == None
assert fix.groundspeed == 0
assert fix.tas == None
assert fix.ias == None
assert fix.siu == 8
assert fix.elevation == None


def test_kwargs():
fix = FlightPathFix(
datetime=datetime(2016, 5, 4, 8, 10, 50), seconds_of_day=29450,
location=dict(latitude=50.82191666668235, longitude=6.181650000001908),
gps_altitude=230, pressure_altitude=48, groundspeed=0, siu=8
)

assert fix.datetime.isoformat() == '2016-05-04T08:10:50'
assert fix.seconds_of_day == 29450
assert fix.location['latitude'] == approx(50.82191666668235)
assert fix.location['longitude'] == approx(6.181650000001908)
assert fix.gps_altitude == 230
assert fix.pressure_altitude == 48
assert fix.enl == None
assert fix.track == None
assert fix.groundspeed == 0
assert fix.tas == None
assert fix.ias == None
assert fix.siu == 8
assert fix.elevation == None

0 comments on commit ea471f5

Please sign in to comment.