def test_hf_get_nwis_converts_parameterCd_all_to_None(self, mock_get): site = "01541200" service = "iv" parameterCd = "all" expected_parameterCd = None expected_url = "https://waterservices.usgs.gov/nwis/" + service + "/?" expected_headers = {"max-age": "120", "Accept-encoding": "gzip"} expected_params = { "format": "json,1.1", "sites": site, "stateCd": None, "countyCd": None, "bBox": None, "parameterCd": None, "period": None, "startDT": None, "endDT": None, } expected = fakeResponse() expected.status_code = 200 expected.reason = "any text" mock_get.return_value = expected actual = hf.get_nwis(site, service, parameterCd=parameterCd) mock_get.assert_called_once_with( expected_url, params=expected_params, headers=expected_headers ) self.assertEqual(actual, expected)
def test_hf_extract_nwis_df(self): # I need to make a response fixture to test this out!! test = hf.get_nwis("01589440", "dv", "2013-01-01", "2013-01-05") actual = hf.extract_nwis_df(test) self.assertIs(type(actual), pd.core.frame.DataFrame, msg="Did not return a df")
def test_hf_get_nwis_calls_correct_url(self, mock_get): """ Thanks to http://engineroom.trackmaven.com/blog/making-a-mockery-of-python/ """ site = 'A' service = 'B' start = 'C' end = 'D' expected_url = 'http://waterservices.usgs.gov/nwis/B/?' expected_headers = {'max-age': '120', 'Accept-encoding': 'gzip'} expected_params = { 'format': 'json,1.1', 'sites': 'A', 'endDT': 'D', 'startDT': 'C', 'parameterCd': '00060' } expected = 'mock data' mock_get.return_value = expected actual = hf.get_nwis(site, service, start, end) mock_get.assert_called_once_with(expected_url, params=expected_params, headers=expected_headers) self.assertEqual(actual, expected)
def test_hf_get_nwis_calls_correct_url_multiple_sites(self, mock_get): site = ['site1', 'site2'] parsed_site = hf.check_NWIS_site(site) service = 'iv' start = 'C' end = 'D' expected_url = 'http://waterservices.usgs.gov/nwis/' + service + '/?' expected_headers = {'max-age': '120', 'Accept-encoding': 'gzip'} expected_params = { 'format': 'json,1.1', 'sites': parsed_site, 'stateCd': None, 'countyCd': None, 'bBox': None, 'parameterCd': '00060', 'period': None, 'startDT': 'C', 'endDT': 'D' } expected = fakeResponse() expected.status_code = 200 expected.reason = "any text" mock_get.return_value = expected actual = hf.get_nwis(site, service, start, end) mock_get.assert_called_once_with(expected_url, params=expected_params, headers=expected_headers) self.assertEqual(actual, expected)
def test_hf_get_nwis_calls_correct_url_multiple_sites(self, mock_get): site = ["site1", "site2"] parsed_site = hf.check_parameter_string(site, "site") service = "iv" start = "C" end = "D" expected_url = "https://waterservices.usgs.gov/nwis/" + service + "/?" expected_headers = {"max-age": "120", "Accept-encoding": "gzip"} expected_params = { "format": "json,1.1", "sites": parsed_site, "stateCd": None, "countyCd": None, "bBox": None, "parameterCd": None, "period": None, "startDT": "C", "endDT": "D", } expected = fakeResponse() expected.status_code = 200 expected.reason = "any text" mock_get.return_value = expected actual = hf.get_nwis(site, service, start, end) mock_get.assert_called_once_with( expected_url, params=expected_params, headers=expected_headers ) self.assertEqual(actual, expected)
def test_hf_get_nwis_calls_correct_url(self, mock_get): """ Thanks to http://engineroom.trackmaven.com/blog/making-a-mockery-of-python/ """ site = 'A' service = 'iv' start = 'C' end = 'D' expected_url = 'http://waterservices.usgs.gov/nwis/' + service + '/?' expected_headers = {'Accept-encoding': 'gzip', 'max-age': '120'} expected_params = { 'format': 'json,1.1', 'sites': 'A', 'stateCd': None, 'countyCd': None, 'bBox': None, 'parameterCd': '00060', 'period': None, 'startDT': 'C', 'endDT': 'D' } expected = fakeResponse() expected.status_code = 200 expected.reason = "any text" mock_get.return_value = expected actual = hf.get_nwis(site, service, start, end) mock_get.assert_called_once_with(expected_url, params=expected_params, headers=expected_headers) self.assertEqual(actual, expected)
def test_hf_extract_nwis_stations_df(self): sites = ["01638500", "01646502"] # TODO: test should be the json for a multiple site request. test = hf.get_nwis(sites, "dv", "2013-01-01", "2013-01-05") actual = hf.extract_nwis_df(test) vD = hf.get_nwis_property(test, key='variableDescription') self.assertIs(type(actual), pd.core.frame.DataFrame, msg="Did not return a df")
def test_hf_extract_nwis_bBox2_df(self): sites = None bBox = '-105.430,39.655,-104,39.863' # TODO: test should be the json for a multiple site request. test = hf.get_nwis(sites, "dv", "2013-01-01", "2013-01-05", bBox=bBox) names = hf.get_nwis_property(test, key='name') actual = hf.extract_nwis_df(test) self.assertIs(type(actual), pd.core.frame.DataFrame, msg="Did not return a df")
def test_hf_extract_nwis_iv_gwstations_df(self): # TODO: I need to make a response fixture to test this out!! sites = ["380616075380701", "394008077005601"] test = hf.get_nwis(sites, "iv", "2018-01-01", "2018-01-05", parameterCd='72019') actual = hf.extract_nwis_df(test) self.assertIs(type(actual), pd.core.frame.DataFrame, msg="Did not return a df")
def test_hf_get_nwis_calls_correct_url(self, mock_get): """ Thanks to http://engineroom.trackmaven.com/blog/making-a-mockery-of-python/ """ site = "A" service = "iv" start = "C" end = "D" expected_url = "https://waterservices.usgs.gov/nwis/" + service + "/?" expected_headers = {"Accept-encoding": "gzip", "max-age": "120"} expected_params = { "format": "json,1.1", "sites": "A", "stateCd": None, "countyCd": None, "bBox": None, "parameterCd": None, "period": None, "startDT": "C", "endDT": "D", } expected = fakeResponse() expected.status_code = 200 expected.reason = "any text" mock_get.return_value = expected actual = hf.get_nwis(site, service, start, end) mock_get.assert_called_once_with( expected_url, params=expected_params, headers=expected_headers ) self.assertEqual(actual, expected)
def test_hf_extract_nwis_dict(self): # I need to make a response fixture to test this out!! test = hf.get_nwis("01589440", "dv", "2013-01-01", "2013-01-05") actual = hf.extract_nwis_dict(test) self.assertIs(type(actual), dict, msg="Did not return a dict")
def test_hf_get_nwis_raises_ValueError_start_and_period(self): with self.assertRaises(ValueError): hf.get_nwis("01541000", start_date="2014-01-01", period="P1D")
def test_hf_get_nwis_raises_ValueError_too_many_locations(self): with self.assertRaises(ValueError): hf.get_nwis("01541000", stateCd="MD")