Package madrona :: Package unit_converter :: Module tests
[hide private]

Source Code for Module madrona.unit_converter.tests

  1  """ 
  2  This file demonstrates two different styles of tests (one doctest and one 
  3  unittest). These will both pass when you run "manage.py test". 
  4   
  5  Replace these with more appropriate tests for your application. 
  6  """ 
  7   
  8  from django.test import TestCase 
  9  from django.conf import settings 
 10  from django.contrib.gis import geos 
 11  from madrona.unit_converter.models import * 
 12  from django.contrib.gis.measure import * 
 13   
 14  # Number of digits to round results to for comparison 
 15  NUM_DIGITS = 4 
 16   
17 -def create_test_geometries():
18 geoms = {} 19 poly = geos.fromstr('POLYGON ((-370109.1943174076732248 256715.2728654230013490, -376985.4612110010348260 257023.5105245867744088, -376389.0298118293867446 266349.4951122021302581, -369815.3686999985366128 265947.4629999864846468, -370941.4576000009546988 259130.8280999893322587, -370109.1943174076732248 256715.2728654230013490))') 20 line = geos.fromstr('LINESTRING (-370109.1943174076732248 256715.2728654230013490, -376985.4612110010348260 257023.5105245867744088)') 21 mpoly = geos.MultiPolygon(poly, geos.fromstr('POLYGON ((-340404.9563809176324867 383194.1489779399707913, -341376.1892856784397736 378422.7254901370033622, -346118.1728583426447585 378627.0628479672595859, -345907.4695642645237967 383431.2573733469471335, -340404.9563809176324867 383194.1489779399707913))')) 22 mline = geos.MultiLineString(line, geos.fromstr('LINESTRING (-340404.9563809176324867 383194.1489779399707913, -341376.1892856784397736 378422.7254901370033622, -346118.1728583426447585 378627.0628479672595859)')) 23 gc_polys = geos.GeometryCollection(poly, mpoly) 24 gc_lines = geos.GeometryCollection(line, mline) 25 gc_everything = geos.GeometryCollection(poly,line,mpoly,mline) 26 geoms = { 27 'poly': poly, 28 'line': line, 29 'mpoly': mpoly, 30 'mline': mline, 31 'gc_polys': gc_polys, 32 'gc_lines': gc_lines, 33 'gc_everything': gc_everything, 34 } 35 for geom in geoms.values(): 36 geom.srid = 3310 37 return geoms
38
39 -def meter_measurements(geoms):
40 rdict = {} 41 for k,v in geoms.iteritems(): 42 if k.find('line') > -1: 43 rdict[k] = v.length 44 else: 45 rdict[k] = v.area 46 return rdict
47
48 -def meter_dict_convert(mdict,unit):
49 con_dict = {} 50 for k,v in mdict.iteritems(): 51 if k.find('line') > -1: 52 con_dict[k] = D(m=v).__getattr__(unit) 53 else: 54 con_dict[k] = A(sq_m=v).__getattr__('sq_' + unit) 55 return con_dict
56
57 -class SettingsTest(TestCase):
58 - def test_settings(self):
59 """ 60 Make sure that the necessary settings exist and are appropriate. 61 """ 62 dlu = settings.DISPLAY_LENGTH_UNITS 63 dau = settings.DISPLAY_AREA_UNITS 64 gds = settings.GEOMETRY_DB_SRID 65 self.failIf(None in [dlu,dau,gds]) 66 67 for unit in [dlu,dau]: 68 self.failIf(unit not in D.UNITS.keys() + A.UNITS.keys())
69
70 -class AppendedMethodsTest(TestCase):
71 - def test_appended_methods(self):
72 geoms = create_test_geometries() 73 m_dict = meter_measurements(geoms) 74 mi_dict = meter_dict_convert(m_dict,'mi') 75 ft_dict = meter_dict_convert(m_dict,'ft') 76 km_dict = meter_dict_convert(m_dict,'km') 77 display_dict = meter_dict_convert(m_dict,settings.DISPLAY_LENGTH_UNITS) 78 units_dict = { 79 'm': m_dict, 80 'mi': mi_dict, 81 'ft': ft_dict, 82 'km': km_dict, 83 settings.DISPLAY_LENGTH_UNITS: display_dict, 84 } 85 add_conversion_methods_to_GEOSGeometry() 86 87 for unit, result_dict in units_dict.iteritems(): 88 for key, geom in geoms.iteritems(): 89 fail_str = "unit = %s, key = %s, dict value = %f" % (unit,key,result_dict[key]) 90 if key.find('line') > -1: 91 att_name = 'length_' + unit 92 else: 93 att_name = 'area_sq_' + unit 94 fail_str += ', appended method value = %f' % getattr(geom,att_name) 95 self.assertAlmostEqual(result_dict[key],getattr(geom,att_name),NUM_DIGITS,fail_str)
96
97 -class OtherMethodsTest(TestCase):
98 - def test_other_methods(self):
99 unit = settings.DISPLAY_LENGTH_UNITS 100 geoms = create_test_geometries() 101 m_dict = meter_measurements(geoms) 102 result_dict = meter_dict_convert(m_dict,unit) 103 104 for key, geom in geoms.iteritems(): 105 fail_str = "unit = %s, key = %s, dict value = %f" % (unit,key,result_dict[key]) 106 if key.find('line') > -1: 107 att_name = 'length_' + unit 108 else: 109 att_name = 'area_sq_' + unit 110 fail_str += ', appended method value = %f' % getattr(geom,att_name) 111 self.assertAlmostEqual(result_dict[key],getattr(geom,att_name),NUM_DIGITS,fail_str)
112