generated from palewire/python-open-source-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.py
110 lines (92 loc) · 3.95 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /usr/bin/env python
import shutil
import unittest
from pathlib import Path
import census_map_downloader
class BaseDownloaderTestCase(unittest.TestCase):
def test_init_unsupported_year(self):
"""Test that constructor raises an exception for an unsupported year"""
with self.assertRaises(ValueError):
census_map_downloader.BaseDownloader(year=1900)
class CountiesDownloaderTestCase(unittest.TestCase):
def setUp(self):
self.MOST_RECENT_YEAR = 2020
self.DATA_DIR = Path(__file__).resolve().parent / "test-data"
self.DATA_DIR.mkdir(exist_ok=True)
def tearDown(self):
"""Tear down test environment"""
shutil.rmtree(self.DATA_DIR)
def test_init_default_year(self):
"""
Test that a default year attribute is set when no year is passed to
constructor
"""
downloader = census_map_downloader.CountiesDownloader()
self.assertEqual(downloader.year, self.MOST_RECENT_YEAR)
def test_init_specify_year(self):
"""
Test that the year attribute is set to the year passed to the
constructor
"""
downloader = census_map_downloader.CountiesDownloader(year=2018)
self.assertEqual(downloader.year, 2018)
def test_url_default_year(self):
"""
Test that the download URL reflects the default year when no year is
passed to thee constructor
"""
downloader = census_map_downloader.CountiesDownloader()
self.assertTrue(str(self.MOST_RECENT_YEAR) in downloader.url)
def test_url_specify_year(self):
"""
Test that the download URL reflects the year passed to the constructor
"""
downloader = census_map_downloader.CountiesDownloader(year=2018)
self.assertTrue("2018" in downloader.url)
def test_run_default_year(self):
"""
Test that the default year's shapefile is downloaded and processed
when no year is passed to the constructor
"""
# TODO: This is more of an integration test because it makes a request
# and touches the filesystem. It might be better to mock
# various classes and methods to detect whether things like
# urlretrive() are called. We don't need to test their
# functionality.
downloader = census_map_downloader.CountiesDownloader(data_dir=self.DATA_DIR)
downloader.run()
shapefile_zip_path = (
self.DATA_DIR / "raw" / f"tl_{self.MOST_RECENT_YEAR}_us_county.zip"
)
self.assertTrue(shapefile_zip_path.is_file())
shapefile_path = (
self.DATA_DIR / "raw" / f"tl_{self.MOST_RECENT_YEAR}_us_county.shp"
)
self.assertTrue(shapefile_path.is_file())
geojson_path = (
self.DATA_DIR / "processed" / f"counties_{self.MOST_RECENT_YEAR}.geojson"
)
self.assertTrue(geojson_path.is_file())
def test_run_specify_year(self):
"""
Test that the shapefile that is downloaded and processed
reflects the year passed to the constructor
"""
# TODO: This is more of an integration test because it makes a request
# and touches the filesystem. It might be better to mock
# various classes and methods to detect whether things like
# urlretrive() are called. We don't need to test their
# functionality.
year = 2018
downloader = census_map_downloader.CountiesDownloader(
data_dir=self.DATA_DIR, year=year
)
downloader.run()
shapefile_zip_path = self.DATA_DIR / "raw" / f"tl_{year}_us_county.zip"
self.assertTrue(shapefile_zip_path.is_file())
shapefile_path = self.DATA_DIR / "raw" / f"tl_{year}_us_county.shp"
self.assertTrue(shapefile_path.is_file())
geojson_path = self.DATA_DIR / "processed" / f"counties_{year}.geojson"
self.assertTrue(geojson_path.is_file())
if __name__ == "__main__":
unittest.main()