PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/geopy/tests/test_microformats.py

https://bitbucket.org/inirudebwoy/gdziebylkaziu
Python | 155 lines | 141 code | 10 blank | 4 comment | 7 complexity | 99ba12e111fed6505ef869487f31573c MD5 | raw file
  1. #!/usr/bin/env python
  2. import unittest
  3. from geopy import format, Location, Point
  4. from geopy.parsers.html import GeoMicroformat
  5. try:
  6. from BeautifulSoup import BeautifulSoup
  7. except ImportError:
  8. BeautifulSoup = None
  9. class GeoMicroformatFound(object):
  10. def setUp(self):
  11. self.parser = GeoMicroformat()
  12. def test_one_str(self):
  13. locations = self.parser.find_all(self.MARKUP)
  14. self.failUnless(len(locations) == 1)
  15. self._location_test(locations[0])
  16. def test_multi_str(self):
  17. locations = self.parser.find_all(self.MARKUP * 3)
  18. self.failUnless(len(locations) == 3)
  19. for i in range(3):
  20. self._location_test(locations[i])
  21. def test_one_soup(self):
  22. if BeautifulSoup:
  23. locations = self.parser.find_all(BeautifulSoup(self.MARKUP))
  24. self.failUnless(len(locations) == 1)
  25. self._location_test(locations[0])
  26. def test_multi_soup(self):
  27. if BeautifulSoup:
  28. locations = self.parser.find_all(BeautifulSoup(self.MARKUP * 3))
  29. self.failUnless(len(locations) == 3)
  30. for i in range(3):
  31. self._location_test(locations[i])
  32. def _location_test(self, location):
  33. self.failUnless(location.name == self.NAME)
  34. self.failUnless(location.point == self.POINT)
  35. class GeoMicroformatNotFound(object):
  36. def setUp(self):
  37. self.parser = GeoMicroformat()
  38. def test_none_str(self):
  39. locations = self.parser.find_all(self.MARKUP)
  40. self.failUnless(len(locations) == 0)
  41. def test_none_soup(self):
  42. if BeautifulSoup:
  43. locations = self.parser.find_all(BeautifulSoup(self.MARKUP))
  44. self.failUnless(len(locations) == 0)
  45. class GeoMicroformatFoundTest(GeoMicroformatFound, unittest.TestCase):
  46. MARKUP = """
  47. <span class="geo">
  48. <span class="latitude">41.4924</span>;
  49. <span class="longitude">-81.7239</span>
  50. </span>
  51. """
  52. NAME = "41.4924; -81.7239"
  53. POINT = Point(41.4924, -81.7239)
  54. class GeoMicroformatNotFoundTest(GeoMicroformatNotFound, unittest.TestCase):
  55. MARKUP = """
  56. <span>
  57. <span class="latitude">41.4924</span>;
  58. <span class="longitude">-81.7239</span>
  59. </span>
  60. """
  61. class FindAbbrLatLongTest(GeoMicroformatFoundTest):
  62. MARKUP = """
  63. <span class="geo">
  64. <abbr class="latitude" title="41.4924">N 41.5</abbr>,
  65. <abbr class="longitude" title="-81.7239">W 81.7</abbr>
  66. </span>
  67. """
  68. NAME = "N 41.5, W 81.7"
  69. class FindAbbrShorthandTest(GeoMicroformatFoundTest):
  70. MARKUP = """
  71. <abbr class="geo" title="41.4924;-81.7239">N 41.5, W 81.7</abbr>
  72. """
  73. NAME = "N 41.5, W 81.7"
  74. class NoShorthandNotFoundTest(GeoMicroformatNotFoundTest):
  75. def setUp(self):
  76. self.parser = GeoMicroformat(shorthand=False)
  77. MARKUP = """<span class="geo">41.4924;-81.7239</span>"""
  78. class NoShorthandFoundTest(GeoMicroformatFoundTest):
  79. def setUp(self):
  80. self.parser = GeoMicroformat(shorthand=False)
  81. MARKUP = """
  82. <span class="geo">41.4924;-81.7239</span>
  83. <span class="geo">
  84. <span class="latitude">41.4924</span>;
  85. <span class="longitude">-81.7239</span>
  86. </span>
  87. <abbr class="geo" title="41.4924;-81.7239">N 41.5, W 81.7</abbr>
  88. """
  89. class FindNestedDefListTest(GeoMicroformatFoundTest):
  90. MARKUP = """
  91. <dl>
  92. <dt>Geo</dt>
  93. <dd class="geo">
  94. <dl>
  95. <dt>Latitude</dt>
  96. <dd><abbr class="latitude" title="12.3456789">12&deg;20' 44" N</abbr></dd>
  97. <dt>Longitude</dt>
  98. <dd><abbr class="longitude" title="-123.456789">123&deg;27' 24" W</abbr></dd>
  99. </dl>
  100. </dd>
  101. </dl>
  102. """
  103. NAME = "Latitude 12%s20' 44\" N" \
  104. " Longitude 123%s27' 24\" W" % (format.DEGREE, format.DEGREE)
  105. POINT = Point(12.3456789, -123.456789)
  106. def get_suite():
  107. """
  108. Returns a TestSuite containing all of the TestCases for microformats. If BeautifulSoup
  109. isn't installed, then tests against that library are skipped.
  110. """
  111. geofound_test_methods = [
  112. 'test_one_str',
  113. 'test_multi_str',
  114. ]
  115. geonotfound_test_methods = [
  116. 'test_none_str',
  117. ]
  118. if BeautifulSoup:
  119. geofound_test_methods.extend(['test_one_soup','test_multi_soup',])
  120. geonotfound_test_methods.append('test_none_soup')
  121. tests = []
  122. tests.extend(map(GeoMicroformatFoundTest,geofound_test_methods))
  123. tests.extend(map(FindAbbrLatLongTest,geofound_test_methods))
  124. tests.extend(map(FindAbbrShorthandTest,geofound_test_methods))
  125. tests.extend(map(NoShorthandFoundTest,geofound_test_methods))
  126. tests.extend(map(FindNestedDefListTest,geofound_test_methods))
  127. tests.extend(map(GeoMicroformatNotFoundTest,geonotfound_test_methods))
  128. tests.extend(map(NoShorthandNotFoundTest,geonotfound_test_methods))
  129. return unittest.TestSuite(tests)
  130. if __name__ == '__main__':
  131. unittest.main()