Package sabx10 :: Package oxm :: Module utils_test
[hide private]
[frames] | no frames]

Source Code for Module sabx10.oxm.utils_test

  1  #! /usr/bin/python 
  2  ############################################################################### 
  3  # 
  4  # sabx10.oxm - an SABX file manipulation library 
  5  # Copyright (C) 2009, 2010 Jay Farrimond (jay@sabikerides.com) 
  6  # 
  7  # This file is part of sabx10.oxm. 
  8  # 
  9  # sabx10.oxm is free software: you can redistribute it and/or modify it under 
 10  # the terms of the GNU Lesser General Public License as published by the Free 
 11  # Software Foundation, either version 3 of the License, or (at your option) any 
 12  # later version. 
 13  # 
 14  # sabx10.oxm is distributed in the hope that it will be useful, but WITHOUT ANY 
 15  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 16  # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more 
 17  # details. 
 18  # 
 19  # You should have received a copy of the GNU Lesser General Public License 
 20  # along with sabx10.oxm.  If not, see <http://www.gnu.org/licenses/>. 
 21  # 
 22  ############################################################################### 
 23   
 24  import os 
 25  import tempfile 
 26  import unittest 
 27   
 28  import xml.etree.ElementTree as et 
 29   
 30  from utils import * 
 31   
32 -class TestUtilsPoints(unittest.TestCase):
33
34 - def setUp(self):
35 self.lax_lat = 33.0 + (57.0 / 60.0) 36 self.lax_lon = -(118.0 + (24.0 / 60.0)) 37 38 self.jfk_lat = 40.0 + (38.0 / 60.0) 39 self.jfk_lon = -(73.0 + (47.0 / 60.0))
40
41 - def test_calculate_distance(self):
42 self.assertAlmostEqual( 43 calculate_distance(self.lax_lat, self.lax_lon, 44 self.jfk_lat, self.jfk_lon), 45 0.623584645464 * rad_to_nm * nm_to_statute)
46
47 - def test_pt_dist_from_pt(self):
48 new_pt = pt_dist_from_pt(self.lax_lat, self.lax_lon, 49 100.0 * nm_to_statute, 65.8921517923) 50 self.assertAlmostEqual(new_pt[0], 34.0 + (37.0 / 60.0), 3) 51 self.assertAlmostEqual(new_pt[1], -(116.0 + (33.0 / 60.0)), 2)
52
53 - def test_pt_dist_from_pt_up(self):
54 new_pt = pt_dist_from_pt(29.738246, -98.103823, 1.0, 0.0) 55 self.assertAlmostEqual(new_pt[0], 29.752728937365013) 56 self.assertAlmostEqual(new_pt[1], -98.103822999999977)
57
59 new_pt = pt_dist_from_pt(29.738246, -98.103823, 1.0, 90.0) 60 self.assertAlmostEqual(new_pt[0], 29.738244954303006) 61 self.assertAlmostEqual(new_pt[1], -98.087143364652249)
62
64 new_pt = pt_dist_from_pt(29.738246, -98.103823, 1.0, 180.0) 65 self.assertAlmostEqual(new_pt[0], 29.72376306263499) 66 self.assertAlmostEqual(new_pt[1], -98.103822999999977)
67
69 new_pt = pt_dist_from_pt(29.738246, -98.103823, 1.0, 270.0) 70 self.assertAlmostEqual(new_pt[0], 29.738244954303006) 71 self.assertAlmostEqual(new_pt[1], -98.120502635347805)
72
73 -class TestUtilsXML(unittest.TestCase):
74
75 - def setUp(self):
76 sabx_valid = """<?xml version='1.0' encoding='utf-8'?> 77 <rideset xmlns="http://www.sabikerides.com/SABX/1/0/" 78 version="1.0"> 79 80 <uuid>147dbb84-d109-44f7-9ac2-09b2a736993f</uuid> 81 <version>1</version> 82 <zip_prefix>781</zip_prefix> 83 84 <ride_type>road</ride_type> 85 <title>Tour de Gruene 2009 Tour</title> 86 <description><p>This is the tour route for the 2009 Tour de Gruene. 87 The time trial courses will have their own map. 88 Please note that the stops listed on this map are not the official 89 rest areas for the ride. Those will come later. For now I have 90 included the stops you would use if you did the ride on your own.</p> 91 <p>Check out the <a href="http://www.tourdegruene.com/">Tour de Gruene 92 web site</a> for more information.</p> 93 </description> 94 <terrain>Flat</terrain> 95 96 <segment id='2'> 97 <road>Gruene Rd.</road> 98 <fromto>parking to Hunter Rd.</fromto> 99 <comments>Gruene Hall is on your left.</comments> 100 <lanes>2</lanes> 101 <shoulder>0</shoulder> 102 <traffic>Moderate</traffic> 103 <speed>20</speed> 104 <waypoint id='6'> 105 <lat>29.738246</lat> 106 <lon>-98.103823</lon> 107 <ele>213.6</ele> 108 <usgs>206.44480896</usgs> 109 </waypoint> 110 <waypoint id='7'> 111 <lat>29.738285</lat> 112 <lon>-98.103958</lon> 113 <ele>213.1</ele> 114 <usgs>206.279098511</usgs> 115 </waypoint> 116 <waypoint id='8'> 117 <lat>29.738286</lat> 118 <lon>-98.103996</lon> 119 <ele>212.6</ele> 120 <usgs>206.126815796</usgs> 121 </waypoint> 122 <waypoint id='9' stop='0a'> 123 <lat>29.738416</lat> 124 <lon>-98.10415</lon> 125 <ele>212.6</ele> 126 <usgs>205.945068359</usgs> 127 </waypoint> 128 </segment> 129 </rideset>""" 130 131 sabx_invalid_version = """<?xml version='1.0' encoding='utf-8'?> 132 <rideset xmlns="http://www.sabikerides.com/SABX/1/0/" 133 version="2.0"> 134 <uuid>147dbb84-d109-44f7-9ac2-09b2a736993f</uuid> 135 </rideset>""" 136 137 self.file_valid = tempfile.TemporaryFile(mode='w+') 138 self.file_valid.write(sabx_valid) 139 self.file_valid.seek(0, os.SEEK_SET) 140 141 self.file_invalid_version = tempfile.TemporaryFile(mode='w+') 142 self.file_invalid_version.write(sabx_invalid_version) 143 self.file_invalid_version.seek(0, os.SEEK_SET)
144
145 - def test_check_version_valid(self):
146 tree = parse_no_def_namespaces(self.file_valid) 147 self.assert_(check_version(tree))
148
150 tree = parse_no_def_namespaces(self.file_invalid_version) 151 self.assertRaises(VersionException, check_version, tree)
152
153 - def test_get_from_id_found(self):
154 tree = parse_no_def_namespaces(self.file_valid) 155 pts = tree.findall("segment/waypoint") 156 self.assert_(get_from_id(pts, '7'))
157
159 tree = parse_no_def_namespaces(self.file_valid) 160 pts = tree.findall("segment/waypoint") 161 self.assertFalse(get_from_id(pts, '77'))
162
164 full_tree = et.parse(self.file_valid) 165 self.file_valid.seek(0, os.SEEK_SET) 166 no_namespace_tree = parse_no_def_namespaces(self.file_valid) 167 168 self.assert_( 169 full_tree.findtext( 170 '{http://www.sabikerides.com/SABX/1/0/}title')) 171 self.assertFalse( 172 no_namespace_tree.findtext( 173 '{http://www.sabikerides.com/SABX/1/0/}title'))
174
176 full_tree = et.parse(self.file_valid) 177 self.file_valid.seek(0, os.SEEK_SET) 178 no_namespace_tree = parse_no_def_namespaces(self.file_valid) 179 180 self.assertFalse(full_tree.findtext('title')) 181 self.assert_(no_namespace_tree.findtext('title'))
182 183 if __name__ == "__main__": 184 unittest.main() 185