Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_Census_initialized_without_api_key(self):
census = Census()
self.assertEquals(census.api_key, 'my_fake_api_key')
def test_call_api_without_output_formatting(self):
Census().call_api('test', output_format=None)
url = called_url()
expected_url = ('http://api.usatoday.com/open/census/'
'test?api_key=my_fake_api_key')
self.assertEquals(url, expected_url)
def test_population_method_with_keypat_arg(self):
Census().population('TX')
url = called_url()
expected_url = ('http://api.usatoday.com/open/census/'
'population?api_key=my_fake_api_key&keypat=TX')
self.assertEquals(url, expected_url)
def test_empty_housing_method_url(self):
Census().population()
url = called_url()
expected_url = ('http://api.usatoday.com/open/census/'
'population?api_key=my_fake_api_key')
self.assertEquals(url, expected_url)
def test_empty_call_api_method_fails(self):
self.assertRaises(TypeError, Census().call_api)
def test_housing_method_with_keypat_arg(self):
Census().housing('TX')
url = called_url()
expected_url = ('http://api.usatoday.com/open/census/'
'housing?api_key=my_fake_api_key&keypat=TX')
self.assertEquals(url, expected_url)
def test_call_api_method_with_new_api_key(self):
Census('new_api_key').call_api('testing', hello='world')
url = called_url()
expected_url = ('http://api.usatoday.com/open/census/'
'testing?api_key=new_api_key&hello=world')
self.assertEquals(url, expected_url)
}
class CensusTestCase(unittest.TestCase):
def setUp(self):
self._client = Census(KEY)
def client(self, name):
return getattr(self._client, name)
def tearDown(self):
self._client.session.close()
class TestDataDefinitions(CensusTestCase):
def test_valid_json(self):
with closing(requests.Session()) as http:
http.headers = {
'User-Agent': ('python-census/{}/tests '.format(__version__) +
'github.com/sunlightlabs/census')
}
for name, datasets in DEFINITIONS.items():
for year, url in datasets.items():
resp = http.head(url)
self.assertEqual(resp.status_code, 200)
client = self.client('acs1dp')
self.assertRaises(UnsupportedYearException,
client.state, ('NAME', '06'))
def test_sf1(self):
client = self.client('sf1')
self.assertRaises(UnsupportedYearException,
client.state, ('NAME', '06'))
def test_sf3(self):
client = self.client('sf3')
self.assertRaises(UnsupportedYearException,
client.state, ('NAME', '06'))
class TestEndpoints(CensusTestCase):
def check_endpoints(self, client_name, tests, **kwargs):
if kwargs:
tests = ((k, kwargs.get(k, v)) for k, v in tests)
client = self.client(client_name)
fields = ('NAME',)
for method_name, expected in tests:
msg = '{}.{}'.format(client_name, method_name)
method = getattr(client, method_name)
data = method(fields, **TEST_DATA)
self.assertTrue(data, msg)
def test_valid_json(self):
with closing(requests.Session()) as http:
http.headers = {
'User-Agent': ('python-census/{}/tests '.format(__version__) +
'github.com/sunlightlabs/census')
}
for name, datasets in DEFINITIONS.items():
for year, url in datasets.items():
resp = http.head(url)
self.assertEqual(resp.status_code, 200)
class TestDefaultYears(CensusTestCase):
def test_default_year_is_supported(self):
for client_name, method_names in CLIENTS:
client = self.client(client_name)
for method_name in method_names:
method = getattr(client, method_name)
self.assertIn(client.default_year, method.supported_years)
# class TestSupportedYears(CensusTestCase):
#
# def test_acs5(self):
# pass