Mercurial > hg > GlobalNeighbors
view tests/test_grid.py @ 3:49aae0c0293b
improved test coverage
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 24 Jun 2017 14:48:31 -0700 |
parents | 1b94f3bf97e5 |
children | 7e27e874655b |
line wrap: on
line source
#!/usr/bin/env python """ test that we can grid a solution """ import os import unittest from common import datafile from globalneighbors.grid import LatLonGrid from globalneighbors.locations import locations from globalneighbors.read import read_city_list class TestGrid(unittest.TestCase): """test gridding functionality""" ### test functions def test_dimensions(self): # make a 2 degree grid grid = LatLonGrid(90, 180) assert grid.n == (90, 180) assert grid.d == (2., 2.) assert len(grid.grid) == 90 for row in grid.grid: assert len(row) == 180 def test_insertion(self): coord = (-23., 122.) grid = LatLonGrid(3, 4) grid.add(1234, *coord) i, j = grid.index(*coord) assert i == 1 assert j == 3 assert grid[(i,j)] == set([1234]) def test_sample(self): samplefile = datafile('sample.tsv') assert os.path.exists(samplefile) city_locations = locations(read_city_list(samplefile)) self.grid_locations(city_locations) ### generic (utility) functions def grid_locations(self, locations): """grid locations + test created grid""" # create a grid grid = LatLonGrid(8, 8) # add the items to it for geoid, (lat, lon) in locations.items(): grid.add(geoid, lat, lon) # iterate over the grid n_locations = 0 for i in range(grid.n[0]): for j in range(grid.n[1]): n_locations += len(grid[(i,j)]) assert n_locations == len(locations) if __name__ == '__main__': unittest.main()