addfips
Advanced tools
| # -*- coding: utf-8 -*- | ||
| # This file is part of addfips. | ||
| # http://github.com/fitnr/addfips | ||
| # Licensed under the GPL-v3.0 license: | ||
| # http://opensource.org/licenses/GPL-3.0 | ||
| # Copyright (c) 2016, fitnr <fitnr@fakeisthenewreal> | ||
| # pylint: disable=missing-docstring,invalid-name,protected-access | ||
| import re | ||
| import unittest | ||
| try: | ||
| from importlib.resources import files | ||
| except ImportError: | ||
| from importlib_resources import files | ||
| from addfips import addfips | ||
| class TestAddFips(unittest.TestCase): | ||
| def setUp(self): | ||
| self.af = addfips.AddFIPS() | ||
| def test_basics(self): | ||
| assert isinstance(self.af._states, dict) | ||
| assert isinstance(self.af._counties, dict) | ||
| def test_empty(self): | ||
| assert self.af.get_county_fips('foo', 'bar') is None | ||
| assert self.af.get_county_fips('foo', state='New York') is None | ||
| assert self.af.get_state_fips('foo') is None | ||
| self.assertIsNone(self.af.get_state_fips(None)) | ||
| self.assertEqual(self.af.add_state_fips({"state":"Illinois"}).get("fips"), "17") | ||
| def test_vintages(self): | ||
| self.assertIn(2000, addfips.COUNTY_FILES) | ||
| self.assertIn(2010, addfips.COUNTY_FILES) | ||
| self.assertIn(2015, addfips.COUNTY_FILES) | ||
| def test_typos(self): | ||
| """Find missing or mistyped geographic names in data files.""" | ||
| for vintage in (2000, 2010, 2015, 2020): | ||
| county_csv = files('addfips').joinpath(addfips.COUNTY_FILES[vintage]) | ||
| pat = re.compile(addfips.COUNTY_PATTERN, flags=re.I) | ||
| with county_csv.open(encoding="utf8") as f: | ||
| # purge header | ||
| f.readline() | ||
| for line in f.readlines(): | ||
| try: | ||
| self.assertIsNotNone(pat.search(line.strip())) | ||
| except AssertionError as error: | ||
| if line.find('11,001') > -1 or line.find('Guam') > -1: | ||
| continue | ||
| msg = f'Did not match county regex: {line} ({vintage})' | ||
| raise AssertionError(msg) from error | ||
| class TestData(unittest.TestCase): | ||
| def setUp(self): | ||
| self.af = addfips.AddFIPS() | ||
| self.row = {'county': 'Kings', 'borough': 'Brooklyn', 'state': 'New York', 'statefp': '36', 'foo': 'bar'} | ||
| self.list = ['Kings', 'Brooklyn', 'New York', 'NY', '36'] | ||
| def test_get_state(self): | ||
| assert self.af.get_state_fips('New York') == '36' | ||
| assert self.af.get_state_fips('36') == '36' | ||
| assert self.af.get_state_fips('NY') == '36' | ||
| assert self.af.get_state_fips('ny') == '36' | ||
| assert self.af.get_state_fips('new york') == '36' | ||
| def test_county_row_dict_defaults(self): | ||
| new = self.af.add_county_fips(self.row) | ||
| self.assertEqual(new['fips'], '36047') | ||
| self.af.default_state_field = 'statefp' | ||
| self.af.default_county_field = 'borough' | ||
| new = self.af.add_county_fips(self.row) | ||
| assert new['fips'] == '36047' | ||
| def test_county_row_state_name(self): | ||
| new = self.af.add_county_fips(self.row, county_field='county', state='New York') | ||
| assert new['fips'] == '36047' | ||
| assert new['foo'] == 'bar' | ||
| def test_vintage2015(self): | ||
| self.assertIsNone(self.af.get_county_fips('Clifton Forge', 'VA')) | ||
| def test_vintage2010(self): | ||
| af2010 = addfips.AddFIPS(vintage=2010) | ||
| assert af2010.get_county_fips('Wade Hampton', 'Alaska') == '02270' | ||
| self.assertIsNone(af2010.get_county_fips('Clifton Forge', 'VA')) | ||
| def test_vintage2000(self): | ||
| af2000 = addfips.AddFIPS(vintage=2000) | ||
| assert af2000.get_county_fips('Wade Hampton', 'Alaska') == '02270' | ||
| self.assertEqual(af2000.get_county_fips('Clifton Forge city', 'Virginia'), "51560") | ||
| assert af2000.get_county_fips('Clifton Forge', 'Virginia') == "51560" | ||
| def test_get_county(self): | ||
| # Full County Name with various ways of ID'ing state | ||
| assert self.af.get_county_fips("Val Verde County", '48') == "48465" | ||
| assert self.af.get_county_fips("Johnson County", 'Kansas') == "20091" | ||
| assert self.af.get_county_fips("Fall River County", "SD") == "46047" | ||
| def test_case_insensitive(self): | ||
| assert self.af.get_county_fips('niagara', 'ny') == '36063' | ||
| def test_no_county(self): | ||
| assert self.af.get_county_fips("El Dorado", 'California') == "06017" | ||
| def test_parish(self): | ||
| assert self.af.get_county_fips('Acadia Parish', 'Louisiana') == "22001" | ||
| assert self.af.get_county_fips('Caldwell', 'Louisiana') == "22021" | ||
| def test_alaska_borough(self): | ||
| self.assertEqual(self.af.get_county_fips('Aleutians East', 'AK'), "02013") | ||
| self.assertEqual(self.af.get_county_fips("Juneau", "Alaska"), "02110") | ||
| def test_nyc_borough(self): | ||
| assert self.af.get_county_fips("Brooklyn", "NY") == "36047" | ||
| def test_diacretic(self): | ||
| assert self.af.get_county_fips('Dona Ana', '35') == "35013" | ||
| assert self.af.get_county_fips('Añasco Municipio', 'Puerto Rico') == "72011" | ||
| def test_municipio(self): | ||
| self.assertEqual(self.af.get_county_fips('Añasco Municipio', 'PR'), "72011") | ||
| self.assertEqual(self.af.get_county_fips('Añasco', 'PR'), "72011") | ||
| def test_municipality(self): | ||
| self.assertEqual(self.af.get_county_fips('Anchorage Municipality', 'AK'), "02020") | ||
| self.assertEqual(self.af.get_county_fips('Anchorage', 'AK'), "02020") | ||
| assert self.af.get_county_fips('Northern Islands', '69') == "69085" | ||
| def test_city(self): | ||
| assert self.af.get_county_fips('Emporia', 'Virginia') == "51595" | ||
| def test_state_row(self): | ||
| new = self.af.add_state_fips(self.row, state_field='state') | ||
| assert new['fips'] == '36' | ||
| assert new['foo'] == 'bar' | ||
| new = self.af.add_state_fips(self.row, state_field='statefp') | ||
| assert new['fips'] == '36' | ||
| def test_state_list(self): | ||
| new = self.af.add_state_fips(self.list, state_field=2) | ||
| assert new[0] == '36' | ||
| new = self.af.add_state_fips(self.list, state_field=3) | ||
| assert new[0] == '36' | ||
| def test_county_dict(self): | ||
| new = self.af.add_county_fips(self.row, county_field='county', state_field='state') | ||
| assert new['fips'] == '36047' | ||
| assert new['foo'] == 'bar' | ||
| new = self.af.add_county_fips(self.row, county_field='county', state_field='statefp') | ||
| assert new['fips'] == '36047' | ||
| new = self.af.add_county_fips(self.row, county_field='borough', state_field='statefp') | ||
| assert new['fips'] == '36047' | ||
| def test_county_list(self): | ||
| new = self.af.add_county_fips(self.list, county_field=1, state_field=2) | ||
| assert new[0] == '36047' | ||
| def test_saint(self): | ||
| assert self.af.get_county_fips('St. Clair County', 'AL') == "01115" | ||
| assert self.af.get_county_fips('St. Clair', 'AL') == "01115" | ||
| assert self.af.get_county_fips('St. Louis City', 'Missouri') == "29510" | ||
| self.assertEqual(self.af.get_county_fips('Saint Louis County', 'Missouri'), "29189") | ||
| assert self.af.get_county_fips('Saint Louis County', 'MO') == "29189" | ||
| assert self.af.get_county_fips('Saint Louis City', 'MO') == "29510" | ||
| assert self.af.get_county_fips('Ste. Genevieve County', 'MO') == "29186" | ||
| assert self.af.get_county_fips('Sainte Genevieve', 'MO') == "29186" | ||
| def test_fort(self): | ||
| assert self.af.get_county_fips('Ft. Bend County', 'Texas') == '48157' | ||
| assert self.af.get_county_fips('Fort Bend County', 'Texas') == '48157' | ||
| assert self.af.get_county_fips('Beaufort County', 'North Carolina') == '37013' | ||
| assert self.af.get_county_fips('Beauft. County', 'North Carolina') is None | ||
| def test_district(self): | ||
| assert self.af.get_county_fips("Manu'a District", "60") == "60020" | ||
| def test_county2020(self): | ||
| """Check 2020 addition to county list.""" | ||
| self.assertEqual(self.af.get_county_fips("Copper River Census Area", "02"), "02066") | ||
| if __name__ == '__main__': | ||
| unittest.main() |
| # -*- coding: utf-8 -*- | ||
| # This file is part of addfips. | ||
| # http://github.com/fitnr/addfips | ||
| # Licensed under the GPL-v3.0 license: | ||
| # http://opensource.org/licenses/GPL-3.0 | ||
| # Copyright (c) 2016, fitnr <fitnr@fakeisthenewreal> | ||
| # pylint: disable=missing-docstring,invalid-name | ||
| import csv | ||
| import io | ||
| import subprocess | ||
| import sys | ||
| import unittest | ||
| from os import path | ||
| from addfips import __main__ as addfips_cli | ||
| class TestCli(unittest.TestCase): | ||
| def setUp(self): | ||
| dirname = path.join(path.dirname(__file__), 'data') | ||
| self.st_args = ['addfips', path.join(dirname, 'state.csv'), '-s', 'name'] | ||
| self.co_args = ['addfips', path.join(dirname, 'county.csv'), '-c', 'county', '-s', 'state'] | ||
| def test_state_cli_subprocess(self): | ||
| out = subprocess.check_output(self.st_args) | ||
| f = io.StringIO(out.decode('utf8')) | ||
| reader = csv.DictReader(f) | ||
| row = next(reader) | ||
| self.assertIn('name', row.keys()) | ||
| self.assertIn('fips', row.keys()) | ||
| self.assertEqual(row.get('name'), 'Alabama') | ||
| assert row.get('fips') == '01' | ||
| def test_county_cli_subprocess(self): | ||
| with subprocess.Popen(self.co_args, stdout=subprocess.PIPE) as proc: | ||
| out, err = proc.communicate() | ||
| assert err is None | ||
| with io.StringIO(out.decode('utf-8')) as f: | ||
| reader = csv.DictReader(f) | ||
| row = next(reader) | ||
| self.assertIn('county', row.keys()) | ||
| self.assertIn('fips', row.keys()) | ||
| assert row.get('county') == 'Autauga County' | ||
| assert row['fips'] == '01001' | ||
| def test_county_cli_call(self): | ||
| sys.argv = self.co_args | ||
| sys.stdout = io.StringIO() | ||
| addfips_cli.main() | ||
| sys.stdout.seek(0) | ||
| reader = csv.DictReader(sys.stdout) | ||
| row = next(reader) | ||
| self.assertIn('county', row.keys()) | ||
| self.assertIn('fips', row.keys()) | ||
| assert row['county'] == 'Autauga County' | ||
| assert row['fips'] == '01001' | ||
| def test_state_cli_call(self): | ||
| sys.argv = self.st_args | ||
| sys.stdout = io.StringIO() | ||
| addfips_cli.main() | ||
| sys.stdout.seek(0) | ||
| reader = csv.DictReader(sys.stdout) | ||
| row = next(reader) | ||
| self.assertIn('name', row.keys()) | ||
| self.assertIn('fips', row.keys()) | ||
| assert row['name'] == 'Alabama' | ||
| assert row['fips'] == '01' | ||
| def test_state_name_cli_call(self): | ||
| sys.argv = self.co_args[:-2] + ['--state-name', 'Alabama'] | ||
| sys.stdout = io.StringIO() | ||
| addfips_cli.main() | ||
| sys.stdout.seek(0) | ||
| reader = csv.DictReader(sys.stdout) | ||
| row = next(reader) | ||
| self.assertIn('county', row.keys()) | ||
| self.assertIn('fips', row.keys()) | ||
| assert row['county'] == 'Autauga County' | ||
| assert row['fips'] == '01001' | ||
| def test_state_cli_call_noheader(self): | ||
| sys.argv = self.st_args[:2] + ['-s', '1', '--no-header'] | ||
| sys.stdout = io.StringIO() | ||
| addfips_cli.main() | ||
| sys.stdout.seek(0) | ||
| reader = csv.reader(sys.stdout) | ||
| next(reader) | ||
| row = next(reader) | ||
| assert row[1] == 'Alabama' | ||
| assert row[0] == '01' | ||
| def test_unmatched(self): | ||
| self.assertTrue(addfips_cli.unmatched({'fips': None})) | ||
| self.assertTrue(addfips_cli.unmatched([None, 'foo'])) | ||
| self.assertFalse(addfips_cli.unmatched(['01001', 'foo'])) | ||
| self.assertFalse(addfips_cli.unmatched({'fips': '01001'})) | ||
| if __name__ == '__main__': | ||
| unittest.main() |
+4
-2
| Metadata-Version: 2.1 | ||
| Name: addfips | ||
| Version: 0.4.0 | ||
| Version: 0.4.2 | ||
| Summary: Add county FIPS to tabular data | ||
@@ -190,4 +190,6 @@ Author-email: Neil Freeman <contact@fakeisthenewreal.org> | ||
| Description-Content-Type: text/markdown | ||
| License-File: LICENSE | ||
| Requires-Dist: importlib_resources; python_version < "3.9" | ||
| Provides-Extra: tests | ||
| License-File: LICENSE | ||
| Requires-Dist: coverage[toml]>=6; extra == "tests" | ||
@@ -194,0 +196,0 @@ # AddFIPS |
+4
-1
@@ -7,3 +7,2 @@ [build-system] | ||
| name = "addfips" | ||
| version = "0.4.0" | ||
| description = "Add county FIPS to tabular data" | ||
@@ -31,2 +30,3 @@ readme = "README.md" | ||
| ] | ||
| dynamic = ["version"] | ||
@@ -51,2 +51,5 @@ [project.urls] | ||
| [tool.setuptools.dynamic] | ||
| version = {attr = "addfips.__version__"} | ||
| [project.scripts] | ||
@@ -53,0 +56,0 @@ addfips = "addfips.__main__:main" |
| Metadata-Version: 2.1 | ||
| Name: addfips | ||
| Version: 0.4.0 | ||
| Version: 0.4.2 | ||
| Summary: Add county FIPS to tabular data | ||
@@ -190,4 +190,6 @@ Author-email: Neil Freeman <contact@fakeisthenewreal.org> | ||
| Description-Content-Type: text/markdown | ||
| License-File: LICENSE | ||
| Requires-Dist: importlib_resources; python_version < "3.9" | ||
| Provides-Extra: tests | ||
| License-File: LICENSE | ||
| Requires-Dist: coverage[toml]>=6; extra == "tests" | ||
@@ -194,0 +196,0 @@ # AddFIPS |
@@ -24,2 +24,4 @@ LICENSE | ||
| src/addfips/data/counties_2020.csv | ||
| src/addfips/data/states.csv | ||
| src/addfips/data/states.csv | ||
| tests/test_base.py | ||
| tests/test_cli.py |
@@ -14,4 +14,4 @@ # -*- coding: utf-8 -*- | ||
| __version__ = '0.4.1' | ||
| __version__ = '0.4.2' | ||
| __all__ = ['addfips'] |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
441146
2.77%22
10%488
96.77%