Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve coordinate checks #96

Merged
merged 7 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .coverage
Binary file not shown.
9 changes: 7 additions & 2 deletions stats/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,18 @@ def valid_value(value_element):


def valid_coords(x):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make life easier for ourselves in future by documenting this function to our better practices...

Suggest adding a docstring here

coords = x.split(' ')
try:
coords = x.split(' ')
except AttributeError:
return False
if len(coords) != 2:
return False
try:
x = decimal.Decimal(coords[0])
y = decimal.Decimal(coords[1])
if x == 0 and y ==0:
if x == 0 and y == 0:
return False
elif x < -90 or x > 90 or y < -180 or y > 180:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, on the documentation front: might be worth adding a one-line comment to say what this is doing...

return False
else:
return True
Expand Down
39 changes: 38 additions & 1 deletion stats/tests/test_valid_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
from stats.dashboard import valid_date, valid_url, valid_value
from stats.dashboard import valid_coords, valid_date, valid_url, valid_value
from lxml import etree

def test_valid_coords():
# empty values
assert not valid_coords(etree.fromstring('<pos/>').text)
assert not valid_coords(etree.fromstring('<pos />').text)
assert not valid_coords(etree.fromstring('<pos></pos>').text)
assert not valid_coords(etree.fromstring('<pos> </pos>').text)
# incorrect spacing
assert not valid_coords(etree.fromstring('<pos> 1 2 </pos>').text)
assert not valid_coords(etree.fromstring('<pos> 1 2</pos>').text)
assert not valid_coords(etree.fromstring('<pos>1 2 </pos>').text)
# invalid characters
assert not valid_coords(etree.fromstring('<pos>a b</pos>').text)
assert not valid_coords(etree.fromstring('<pos>one two</pos>').text)
# incorrect number of values
assert not valid_coords(etree.fromstring('<pos>1</pos>').text)
assert not valid_coords(etree.fromstring('<pos>1 2 3</pos>').text)
assert not valid_coords(etree.fromstring('<pos>1 2.3 4</pos>').text)
# beyond boundary values
assert not valid_coords(etree.fromstring('<pos>-90.00001 -180.00001</pos>').text)
assert not valid_coords(etree.fromstring('<pos>-90.00001 180.00001</pos>').text)
assert not valid_coords(etree.fromstring('<pos>90.00001 -180.00001</pos>').text)
assert not valid_coords(etree.fromstring('<pos>90.00001 180.00001</pos>').text)
# value deemed invalid for Stats purposes (though valid against the Standard)
assert not valid_coords(etree.fromstring('<pos>0 0</pos>').text)

# general permitted values
assert valid_coords(etree.fromstring('<pos>1 2</pos>').text)
assert valid_coords(etree.fromstring('<pos>1.2 2.3</pos>').text)
assert valid_coords(etree.fromstring('<pos>1.23456789 2.34567890</pos>').text)
assert valid_coords(etree.fromstring('<pos>-1.2 -2.3</pos>').text)
assert valid_coords(etree.fromstring('<pos>-1.23456789 -2.34567890</pos>').text)
# boundary values
assert valid_coords(etree.fromstring('<pos>-90 -180</pos>').text)
assert valid_coords(etree.fromstring('<pos>-90 180</pos>').text)
assert valid_coords(etree.fromstring('<pos>90 -180</pos>').text)
assert valid_coords(etree.fromstring('<pos>90 180</pos>').text)

def test_valid_date():
assert not valid_date(etree.XML('<activity-date iso-date=""/>'))
assert valid_date(etree.XML('<activity-date iso-date="2014-01-01"/>'))
Expand Down