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

Source Code for Module madrona.common.tests

  1  from django.test import TestCase 
  2  from django.conf import settings 
  3  from django.contrib.gis import geos 
  4  from madrona.common.utils import * 
  5  import os 
  6   
7 -class AssetsTest(TestCase):
8 - def test_get_js_files(self):
9 """ 10 Should return a list of javascript files from 11 media/common/js_includes.xml 12 """ 13 from madrona.common import assets 14 files = assets.get_js_files() 15 self.assertEquals(files.__class__.__name__, 'list') 16 self.assertTrue(len(files) > 0)
17
18 - def test_get_css_files(self):
19 """ 20 Should return a list of stylesheets from media/common/css_includes.xml 21 """ 22 from madrona.common import assets 23 files = assets.get_css_files() 24 self.assertEquals(files.__class__.__name__, 'list') 25 self.assertTrue(len(files) > 0)
26
27 -class TestRequest:
28 session = None
29
30 -class UtilsTest(TestCase):
31 - def test_angle_method(self):
32 # try without srids 33 pnt1 = Point(1,0) 34 pnt2 = Point(0,0) 35 pnt3 = Point(0,1) 36 result = angle(pnt1,pnt2,pnt3) 37 self.assertEquals(result,1.5707963267949001) 38 39 # with srids 40 pnt1.srid = 3310 41 pnt2.srid = 3310 42 pnt3.srid = 3310 43 result2 = angle(pnt1,pnt2,pnt3) 44 self.assertEquals(result2,1.5707963267949001)
45
46 - def test_spike_ring_indecies(self):
47 poly = geos.fromstr('POLYGON((3480407.01 5483810.171,3480407.01 5483810.17,3480409.11 5483777.431,3480407.348 5483777.421,3480405.15 5483777.409,3480404.816 5483777.407,3480394.58 5483777.35,3480395.36 5483811.12,3480404.55 5483810.46,3480405.951 5483810.295,3480406.312 5483795.106,3480405.951 5483810.296,3480406.903 5483810.184,3480407.01 5483810.171))') 48 indecies = spike_ring_indecies(poly.exterior_ring, 0.01) 49 self.assertEquals(indecies[0],10)
50
51 - def test_remove_spikes(self):
52 poly = geos.fromstr('POLYGON ((3415632.4900000002235174 5291021.4900000002235174, 3415632.4879999998956919 5291021.4939999999478459, 3415632.4900000002235174 5291021.4939999999478459, 3415628.9300000001676381 5291028.2800000002607703, 3415642.9500000001862645 5291001.5599999995902181, 3415651.1800000001676381 5290985.8600000003352761, 3415659.2700000000186265 5290984.6100000003352761, 3415644.7099999999627471 5290947.8099999995902181, 3415629.1699999999254942 5290921.8300000000745058, 3415621.2799999997951090 5290929.7199999997392297, 3415640.2099999999627471 5290959.4299999997019768, 3415625.3799999998882413 5290971.4100000001490116, 3415627.7900000000372529 5290983.9400000004097819, 3415629.4900000002235174 5290992.1900000004097819, 3415630.1400000001303852 5290995.3600000003352761, 3415625.6499999999068677 5291022.5000000000000000, 3415632.4900000002235174 5291021.4900000002235174))') 53 result = remove_spikes(poly, 0.01) 54 expected_wkt = 'POLYGON ((3415632.4900000002235174 5291021.4900000002235174, 3415632.4879999998956919 5291021.4939999999478459, 3415632.4900000002235174 5291021.4939999999478459, 3415642.9500000001862645 5291001.5599999995902181, 3415651.1800000001676381 5290985.8600000003352761, 3415659.2700000000186265 5290984.6100000003352761, 3415644.7099999999627471 5290947.8099999995902181, 3415629.1699999999254942 5290921.8300000000745058, 3415621.2799999997951090 5290929.7199999997392297, 3415640.2099999999627471 5290959.4299999997019768, 3415625.3799999998882413 5290971.4100000001490116, 3415627.7900000000372529 5290983.9400000004097819, 3415629.4900000002235174 5290992.1900000004097819, 3415630.1400000001303852 5290995.3600000003352761, 3415625.6499999999068677 5291022.5000000000000000, 3415632.4900000002235174 5291021.4900000002235174))' 55 self.assertEquals(result.wkt,expected_wkt) 56 result = remove_spikes(result, 0.01) 57 self.assertEquals(result,expected_wkt)
58 59 from django.utils.hashcompat import md5_constructor as md5 60
61 -class SessionsTest(TestCase):
62 - def test_load_session(self):
63 from madrona.common.utils import load_session 64 request = TestRequest() 65 load_session(request, '0') 66 self.assertEquals(request.session, None) 67 load_session(request, md5('blah').hexdigest()) 68 self.assertEquals(request.session.__class__.__name__, 'SessionStore')
69 70
71 -class BrowserUserAgentTest(TestCase):
72 - def setUp(self):
73 exdir = os.path.dirname(os.path.abspath(__file__)) 74 self.supported_uastring_examples = [x.strip() for x in open(exdir + '/supported_user_agent_examples.txt').readlines()] 75 self.unsupported_uastring_examples = [x.strip() for x in open(exdir + '/unsupported_user_agent_examples.txt').readlines()]
76
77 - def test_supported_browsers(self):
78 from madrona.common.utils import valid_browser 79 for ua in self.supported_uastring_examples: 80 if not valid_browser(ua): 81 print "UAPARSER SAYS NOT SUPPORTED .....", ua 82 self.assertEquals(valid_browser(ua),True, "*** uaparser says that '%s' is not supported. " % ua)
83
85 from madrona.common.utils import valid_browser 86 for ua in self.unsupported_uastring_examples: 87 if valid_browser(ua): 88 print "UAPARSER SAYS SUPPORTED .....", ua 89 self.assertEquals(valid_browser(ua),False)
90
91 - def test_rootmap_goodua(self):
92 url = '/' 93 agent = {'HTTP_USER_AGENT': self.supported_uastring_examples[0]} 94 response = self.client.get(url, **agent) 95 self.assertEquals(response.status_code, 200, url) 96 # request again and see if cookies are recognized 97 response = self.client.get(url, **agent) 98 self.assertEquals(response.status_code, 200, url) 99 # and request again and see if news cookies are handled 100 response = self.client.get(url, **agent) 101 self.assertEquals(response.status_code, 200, url)
102
103 - def test_rootmap_badua(self):
104 # should bring up "unsupported browser" page 105 url = '/' 106 agent = {'HTTP_USER_AGENT': self.unsupported_uastring_examples[0]} 107 response = self.client.get(url, **agent) 108 self.assertEquals(response.status_code, 200, url) 109 # request again but go to map despite the user agent 110 url = '/?supported=false' 111 response = self.client.get(url, **agent) 112 self.assertEquals(response.status_code, 200, url)
113
114 -class AccessTest(TestCase):
115
116 - def test_upload_dir_access(self):
117 """ Ensure that the upload directory is not web accessible """ 118 119 url = settings.MEDIA_URL + 'upload/' 120 response = self.client.get(url) 121 self.assertEquals(response.status_code, 403, url) 122 123 url = settings.MEDIA_URL + 'upload/test.txt' 124 response = self.client.get(url) 125 self.assertEquals(response.status_code, 403, url) 126 127 url = settings.MEDIA_URL + '/upload/test.txt' 128 response = self.client.get(url) 129 self.assertEquals(response.status_code, 403, url) 130 131 url = settings.MEDIA_URL + '/./upload/test.txt' 132 response = self.client.get(url) 133 self.assertEquals(response.status_code, 403, url)
134
135 -class InstalledAppTest(TestCase):
136
137 - def test_dup_app_labels(self):
138 """ Make sure we dont have any apps with duplicate app_labels 139 for the sake of south, contenttypes and any other django piece 140 which relies on app_label """ 141 142 labels = [x.split('.')[-1] for x in settings.INSTALLED_APPS] 143 counts = {} 144 for label in labels: 145 if label not in counts.keys(): 146 counts[label] = 1 147 else: 148 counts[label] += 1 149 150 for k,v in counts.items(): 151 self.assertEquals(v, 1, 'app_label %s refers to %s apps' % (k,v))
152
153 -class DependenciesTest(TestCase):
154
155 - def test_dependencies(self):
156 imports = [ 157 'networkx', 158 'south', 159 'django.contrib.gis', 160 'elementtree', 161 'sphinx', 162 'mapnik', 163 'BeautifulSoup', 164 'xlwt', 165 'registration', 166 'compress', 167 'maintenancemode', 168 ] 169 from django.utils import importlib 170 for dep in imports: 171 try: 172 m = importlib.import_module(dep) 173 except: 174 m = None 175 self.assertNotEquals(m,None,'Cannot import %s' % dep)
176 177 178 from django.contrib.gis.geos import GEOSGeometry 179 from madrona.common.utils import forceLHR, forceRHR 180
181 -class GeometryOpsTest(TestCase):
182
183 - def test_reversals(self):
184 gright = GEOSGeometry('SRID=4326;POLYGON ((-120.42 34.37, -119.64 34.32, -119.63 34.12, -122.44 34.15, -120.42 34.37))') 185 gleft = GEOSGeometry('SRID=4326;POLYGON ((-120.42 34.37, -122.44 34.15, -119.63 34.12, -119.64 34.32, -120.42 34.37))') 186 187 # Nothing should change 188 self.assertEqual(gright.coords, forceRHR(gright).coords) 189 self.assertEqual(gleft.coords, forceLHR(gleft).coords) 190 191 # Order should be reversed 192 self.assertEqual(gleft.coords, forceLHR(gright).coords) 193 self.assertEqual(gright.coords, forceRHR(gleft).coords)
194
195 - def test_askml(self):
196 g = GEOSGeometry('SRID=4326;POLYGON ((-120.42 34.37, -119.64 34.32, -119.63 34.12, -122.44 34.15, -120.42 34.37))') 197 kml = asKml(g)
198
199 - def test_simplify(self):
200 # Note second coordinate in relation to the first, simplify will remove it if the tolerance is large enough 201 gcomplex = GEOSGeometry('SRID=4326;POLYGON ((-120.42 34.37, -120.422 34.372, -119.64 34.32, -119.63 34.12, -122.44 34.15, -120.42 34.37))') 202 gsimple = GEOSGeometry('SRID=4326;POLYGON ((-120.42 34.37, -119.64 34.32, -119.63 34.12, -122.44 34.15, -120.42 34.37))') 203 204 gsimplified_large = gcomplex.simplify(0.01) 205 self.assertEqual(gsimple, gsimplified_large) 206 207 gsimplified_small = gcomplex.simplify(0.000001) 208 self.assertEqual(gcomplex, gsimplified_small)
209