示例#1
0
 def test_NWIS_service_defaults_to_dv(self):
     site = "01582500"
     service = "dv"
     start = "2011-01-01"
     end = "2011-01-02"
     actual = station.NWIS(site, service, start, end)
     self.assertEqual(actual.service, "dv")
示例#2
0
    def test_hf_get_nwis_accepts_countyCd_array(self, mock_get_prop, mock_get):
        start = "2017-01-01"
        end = "2017-12-31"
        cnty = ['51059', '51061']
        cnty = typing.check_NWIS_site(cnty)
        service2 = 'dv'

        expected_url = 'http://waterservices.usgs.gov/nwis/dv/?'
        expected_headers = {'max-age': '120', 'Accept-encoding': 'gzip'}
        expected_params = {
            'format': 'json,1.1',
            'sites': None,
            'stateCd': None,
            'countyCd': cnty,
            'bBox': None,
            'parameterCd': '00060',
            'period': None,
            'startDT': start,
            'endDT': end
        }

        expected = fakeResponse(200)

        mock_get.return_value = expected
        actual = station.NWIS(None,
                              service2,
                              start,
                              countyCd=cnty,
                              end_date=end).get_data()
        mock_get.assert_called_once_with(expected_url,
                                         params=expected_params,
                                         headers=expected_headers)
示例#3
0
    def test_NWIS_init_sets_url_ok_json(self, mock_extract_nwis_df,
                                        mock_get_nwis_property, mock_get_nwis):
        expected_url = "expected url"
        expected_ok = True
        expected_json = "expected json"

        mock_get_nwis.return_value = fakeResponse(code=200,
                                                  url=expected_url,
                                                  json=expected_json)
        mock_df = pd.DataFrame(
            np.random.randn(5, 1),
            columns=["A"],
            index=pd.date_range("20130101", periods=5, freq="T"),
        )
        mock_extract_nwis_df.return_value = (mock_df, "expected_dict")

        actual = station.NWIS()
        # self.assertEqual(actual.url, expected_url, "NWIS.__init__() did not set self.url properly.")
        self.assertEqual(actual.ok, expected_ok,
                         "NWIS.__init__() did not set self.ok properly.")
        self.assertEqual(
            actual.json,
            expected_json,
            "NWIS.__init__() did not set self.json properly.",
        )
示例#4
0
    def test_NWIS_init_check_defaults(self, mock_get_nwis_property,
                                      mock_get_nwis):
        default_site = None
        default_service = "dv"
        default_start = None
        default_end = None
        default_parameterCd = "all"
        default_period = None
        default_stateCd = None
        default_countyCd = None
        default_bBox = None

        mock_get_nwis_property.return_value = "expected"
        mock_get_nwis.return_value = fakeResponse()

        station.NWIS()

        mock_get_nwis.assert_called_once_with(
            default_site,
            default_service,
            default_start,
            default_end,
            parameterCd=default_parameterCd,
            period=default_period,
            stateCd=default_stateCd,
            countyCd=default_countyCd,
            bBox=default_bBox,
        )
        self.assertTrue(mock_get_nwis_property)
示例#5
0
 def test_NWIS_setters_work(self):
     site = "01582500"
     service = "dv"
     start = "2011-01-01"
     end = "2011-01-02"
     actual = station.NWIS(site, service, start, end)
     self.assertIsInstance(actual, station.NWIS)
     self.assertEqual(actual.site, site)
     self.assertEqual(actual.service, service)
     self.assertEqual(actual.start_date, start)
     self.assertEqual(actual.end_date, end)
示例#6
0
 def test_NWIS_df_returns_df(self):
     site = "01582500"
     service = "dv"
     start = "2011-01-01"
     end = "2011-01-02"
     actual = station.NWIS(site, service, start, end)
     # You don't need to test the following like this.
     # Just test that actual.df() returns nothing if you call before get_data()
     # And actual.df() returns a df if you call after .get_data()
     actualdf = actual.get_data().df()  # returns a dataframe
     self.assertIs(type(actualdf),
                   pd.core.frame.DataFrame,
                   msg="Did not return a df")
示例#7
0
 def test_NWIS_init_request_most_recent_only(self, mock_get_nwis):
     expected_json = recent_only
     expected_url = (
         "https://waterservices.usgs.gov/nwis/dv/?format=json%2C1.1&sites=01541200"
     )
     mock_get_nwis.return_value = fakeResponse(json=expected_json,
                                               url=expected_url)
     actual = station.NWIS("01541200")
     self.assertEqual(
         actual.df().shape,
         (2, 4),
         "The dataframe should only have two rows and four columns.",
     )
示例#8
0
 def test_NWIS_init_calls_extract_nwis_df(self, mock_extract_nwis_df,
                                          mock_get_nwis_property,
                                          mock_get_nwis):
     expected_json = "expected json"
     mock_get_nwis.return_value = fakeResponse(json=expected_json)
     mock_df = pd.DataFrame(
         np.random.randn(5, 1),
         columns=["A"],
         index=pd.date_range("20130101", periods=5, freq="T"),
     )
     mock_extract_nwis_df.return_value = (mock_df, "expected dict")
     actual = station.NWIS()
     mock_extract_nwis_df.assert_called_once_with(expected_json)
示例#9
0
 def test_NWIS_setters_parameterCd(self):
     site = "01582500"
     service = "dv"
     start = "2011-01-01"
     end = "2011-01-02"
     parameterCd = "00065"
     actual = station.NWIS(site,
                           service,
                           start,
                           end,
                           parameterCd=parameterCd)
     self.assertIsInstance(actual, station.NWIS)
     self.assertEqual(actual.site, site)
     self.assertEqual(actual.service, service)
     self.assertEqual(actual.start_date, start)
     self.assertEqual(actual.parameterCd, parameterCd)
示例#10
0
 def test_NWIS_init_filename_calls_read_parquet(self, mock_read):
     expected_filename = "expected_filename"
     expected_meta = "expected meta"
     expected_df = pd.DataFrame(
         np.random.randn(5, 1),
         columns=["A"],
         index=pd.date_range("20130101", periods=5, freq="T"),
     )
     mock_start = "expected start"
     mock_end = "expected end"
     mock_read.return_value = (expected_df, expected_meta)
     actual = station.NWIS(file=expected_filename)
     mock_read.assert_called_once_with(expected_filename)
     assert_frame_equal(expected_df, actual._dataframe)
     self.assertEqual(
         expected_meta,
         actual.meta,
         "The metadata were not retrieved by NWIS.read().",
     )
示例#11
0
 def test_NWIS_get_data_calls_get_nwis_mult_sites(self, mock_get_prop,
                                                  mock_get_nwis):
     site = ["01638500", "01646502"]
     siteEx = "01638500,01646502"
     service = "dv"
     start = "2011-01-01"
     end = "2011-01-02"
     parameterCd = "00060"
     actual = station.NWIS(site, service, start, end)
     try_it_out = actual.get_data()
     # try_it_out should be an instance of NWIS.
     mock_get_nwis.assert_called_once_with(siteEx,
                                           service,
                                           start,
                                           end,
                                           parameterCd=parameterCd,
                                           period=None,
                                           stateCd=None,
                                           countyCd=None,
                                           bBox=None)
示例#12
0
    def test_NWIS_get_data_calls_get_nwis_correctly(self, mock_get_prop,
                                                    mock_get_nwis):
        site = "01582500"
        service = "dv"
        start = "2011-01-01"
        end = "2011-01-02"
        parameterCd = "00060"

        actual = station.NWIS(site, service, start, end)
        try_it_out = actual.get_data()
        # try_it_out should be a response object, I think
        mock_get_nwis.assert_called_once_with(site,
                                              service,
                                              start,
                                              end,
                                              parameterCd=parameterCd,
                                              period=None,
                                              stateCd=None,
                                              countyCd=None,
                                              bBox=None)
示例#13
0
    def test_NWIS_init_filename_calls_read_parquet_then_get_nwis(
            self, mock_save, mock_extract_nwis_df, mock_get_nwis, mock_read):
        # Mocks listed in order that they get called.

        # mock_read: pretend file doesn't exist, so return OSError
        #    file exists:
        #         mock_read.return_value = (expected_df, expected_meta)
        #    file doesn't exist, raise error:
        mock_read.side_effect = OSError()

        # mock_get_nwis
        expected_json = "expected json"
        mock_get_nwis.return_value = fakeResponse(json=expected_json)

        # mock_get_nwis_property
        # never called

        # mock_extract_nwis_df
        mock_df = pd.DataFrame(
            np.random.randn(5, 1),
            columns=["A"],
            index=pd.date_range("20130101", periods=5, freq="T"),
        )
        mock_meta = "mock meta"
        mock_extract_nwis_df.return_value = (mock_df, mock_meta)

        # mock_save
        expected_filename = "expected_filename"
        mock_save.return_value = "expected self"

        # Create an NWIS with a filename, but the filename doesn't exist.
        # so an OSError is returned.
        # So now get_nwis is called, extract_nwis_df, save().
        actual = station.NWIS(file=expected_filename)
        mock_save.assert_called_once_with(expected_filename, mock_df,
                                          mock_meta)
示例#14
0
    def test_NWIS_init_calls_get_nwis_and_get_prop(self,
                                                   mock_get_nwis_property,
                                                   mock_get_nwis):
        site = "expected site"
        service = "expected service"
        start = "expected start"
        end = "expected end"
        parameterCd = "expected pCode"
        mock_get_nwis_property.return_value = "expected"
        mock_get_nwis.return_value = fakeResponse()

        station.NWIS(site, service, start, end, parameterCd=parameterCd)
        mock_get_nwis.assert_called_once_with(
            site,
            service,
            start,
            end,
            parameterCd=parameterCd,
            period=None,
            stateCd=None,
            countyCd=None,
            bBox=None,
        )
        self.assertTrue(mock_get_nwis_property)
示例#15
0
 def test_NWIS_start_defaults_to_None(self):
     actual = station.NWIS()
     self.assertIsNone(actual.start_date)