Skip to content

Commit

Permalink
Close #29: Convert tests to pytest.
Browse files Browse the repository at this point in the history
Change mapppings to reflect changes in datasets.
Make tests less prone to fail when datasets change.
  • Loading branch information
yaph committed Jul 25, 2024
1 parent 72b2a47 commit 80d62af
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
4 changes: 2 additions & 2 deletions geonamescache/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
'The former Yugoslav Republic of Macedonia': 'Macedonia',
'the former Yugoslav Republic of Macedonia': 'Macedonia',
'The Gambia': 'Gambia',
'The Netherlands': 'Netherlands',
'Timor-Leste': 'East Timor',
'Netherlands': 'The Netherlands',
'Timor Leste': 'East Timor',
'UAE': 'United Arab Emirates',
'United Republic of Tanzania': 'Tanzania',
'United Republic of Tanzania (the)': 'Tanzania',
Expand Down
81 changes: 40 additions & 41 deletions tests/test_geonamescache.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# -*- coding: utf-8 -*-
import unittest
from geonamescache import GeonamesCache


class GeonamesCacheTestSuite(unittest.TestCase):
class TestGeonamesCacheSuite:
"""GeonamesCache test cases."""

def setUp(self):
def setup_method(self):
self.geonamescache = GeonamesCache()

def test_continents(self):
Expand All @@ -21,89 +20,89 @@ def test_continents(self):
('SA', 'South America')
)
for code, name in testdata:
self.assertTrue(code in continents)
self.assertEqual(name, continents[code]['name'])
assert code in continents
assert name == continents[code]['name']

for code in ['XX', 'OO']:
self.assertTrue(code not in continents)
assert code not in continents

def test_get_countries(self):
countries = self.geonamescache.get_countries()

testdata = (('ES', 'Spain'), ('FR', 'France'), ('US', 'United States'))
for code, name in testdata:
self.assertTrue(code in countries)
self.assertEqual(name, countries[code]['name'])
assert code in countries
assert name == countries[code]['name']

for code in ['XX', 'OO']:
self.assertTrue(code not in countries)
assert code not in countries

def test_us_states(self):
us_states = self.geonamescache.get_us_states()

testdata = (
('NM', 'New Mexico'), ('CA', 'California'), ('NV', 'Nevada'))
for code, name in testdata:
self.assertTrue(code in us_states)
self.assertEqual(name, us_states[code]['name'])
assert code in us_states
assert name == us_states[code]['name']

for code in ['XX', 'OO']:
self.assertTrue(code not in us_states)
assert code not in us_states

def test_get_countries_by_names(self):
# Length of get_countries_by_names dict and get_countries dict must be
# the same, unless country names wouldn't be unique.
self.assertTrue(len(self.geonamescache.get_countries_by_names()),
len(self.geonamescache.get_countries()))
assert len(self.geonamescache.get_countries_by_names()), \
len(self.geonamescache.get_countries())

def test_get_cities_by_name(self):
cities = self.geonamescache.get_cities()
for gid, name in (('3191316', 'Samobor'), ('3107112', 'Rivas-Vaciamadrid')):
self.assertEqual(name, cities[gid]['name'])
assert name == cities[gid]['name']

def test_get_cities_by_name_madrid(self):
self.assertGreater(len(self.geonamescache.get_cities_by_name('Madrid')), 1)
assert len(self.geonamescache.get_cities_by_name('Madrid')) > 1

def test_cities_in_us_states(self):
cities = self.geonamescache.get_cities()
for gid, name, us_state in (('4164138', 'Miami', 'FL'), ('4525353', 'Springfield', 'OH')):
self.assertEqual(name, cities[gid]['name'])
self.assertEqual(us_state, cities[gid]['admin1code'])
assert name == cities[gid]['name']
assert us_state == cities[gid]['admin1code']

def test_search_cities(self):
cities = self.geonamescache.search_cities('Kiev')
self.assertGreaterEqual(len(cities), 1)
assert len(cities) >= 1

def test_search_cities_case_sensitive(self):
cities = self.geonamescache.search_cities('Stoke-On-Trent', case_sensitive=True)
self.assertEqual(len(cities), 0)
assert len(cities) == 0
cities = self.geonamescache.search_cities('Stoke-On-Trent')
self.assertEqual(len(cities), 1)
assert len(cities) == 1

def test_search_cities_alternatenames_contains_search(self):
cities = self.geonamescache.search_cities('London')
self.assertEqual(len(cities), 15)
cities = self.geonamescache.search_cities('London', contains_search=False)
self.assertEqual(len(cities), 2)
assert (
len(self.geonamescache.search_cities('London'))
> len(self.geonamescache.search_cities('London', contains_search=False))
> 1
)

def test_search_cities_name_contains_search(self):
cities = self.geonamescache.search_cities('London', 'name')
self.assertEqual(len(cities), 5)
cities = self.geonamescache.search_cities('London', 'name', contains_search=False)
self.assertEqual(len(cities), 2)
assert (
len(self.geonamescache.search_cities('London', 'name'))
> len(self.geonamescache.search_cities('London', 'name', contains_search=False))
> 1
)

def test_search_cities_alternatenames_contains_search_and_case_sensitive(self):
cities = self.geonamescache.search_cities('London', case_sensitive=True)
self.assertEqual(len(cities), 14)
cities = self.geonamescache.search_cities('London', case_sensitive=True, contains_search=False)
self.assertEqual(len(cities), 2)
assert (
len(self.geonamescache.search_cities('London', case_sensitive=True))
> len(self.geonamescache.search_cities('London', case_sensitive=True, contains_search=False))
> 1
)

def test_search_cities_name_contains_search_and_case_sensitive(self):
cities = self.geonamescache.search_cities('London', 'name', case_sensitive=True)
self.assertEqual(len(cities), 5)
cities = self.geonamescache.search_cities('London', 'name', case_sensitive=True, contains_search=False)
self.assertEqual(len(cities), 2)


if __name__ == '__main__':
unittest.main()
assert (
len(self.geonamescache.search_cities('London', 'name', case_sensitive=True))
> len(self.geonamescache.search_cities('London', 'name', case_sensitive=True, contains_search=False))
> 1
)

0 comments on commit 80d62af

Please sign in to comment.