Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #2

Merged
merged 14 commits into from
Sep 10, 2016
Prev Previous commit
Next Next commit
Station.fetch now checks user input types.
  • Loading branch information
mroberge committed Sep 6, 2016
commit 310c921288327ac77ab6026a16a80639145bc799
17 changes: 15 additions & 2 deletions hydrofunctions/station.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@
station.py
"""
from __future__ import absolute_import, print_function
from hydrofunctions import typing
from hydrofunctions import hydrofunctions as hf


class Station(object):
"""A class for organizing stream gauge data for a single site."""

def fetch2(self):
"""Will request data from NWIS after checking input types."""
# raise TypeError if parameters are wrong
typing.check_NWIS_station_id(self.site)
typing.check_datestr(self.start_date)
typing.check_datestr(self.end_date)
hf.get_nwis(self.site, self.service, self.start_date, self.end_date)

def __init__(self, site=None, service="dv", start_date=None, end_date=None):
self.site = site
self.service = service
self.start_date = start_date
self.end_date = end_date
#self.fetch = hf.get_nwis(self.site, self.service, self.start_date, self.end_date)
self.fetch = self.fetch2



def fetch(self):
get_nwis(self.site, self.service, self.start_date, self.end_date)
8 changes: 4 additions & 4 deletions hydrofunctions/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def check_NWIS_station_id(input):
if isinstance(input, str):
return True
else:
raise TypeError("NWIS station id should be a string, often in the" +
"form of an eight digit number enclosed in quotes")
raise TypeError("NWIS station id should be a string, often in the \
form of an eight digit number enclosed in quotes")


def check_datestr(input):
Expand All @@ -34,5 +34,5 @@ def check_datestr(input):
if isinstance(input, str) and datestr.match(input):
return True
else:
raise TypeError('dates should be in the form of "YYYY-MM-DD" ' +
"enclosed in quotes")
raise TypeError('dates should be in the form of "YYYY-MM-DD" \
enclosed in quotes. Actual: {}'.format(input))
26 changes: 26 additions & 0 deletions tests/test_station.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

from __future__ import absolute_import, print_function
import unittest
from unittest import mock
from hydrofunctions import station



class TestStation(unittest.TestCase):

def test_station_is_obj(self):
Expand All @@ -29,6 +31,7 @@ def test_station_site_sets(self):

def test_station_service_defaults_to_dv(self):
actual = station.Station()
service = "problem!"
self.assertEqual(actual.service, "dv")

def test_station_start_defaults_to_None(self):
Expand All @@ -39,5 +42,28 @@ def test_station_end_defaults_to_None(self):
actual = station.Station()
self.assertIsNone(actual.end_date)

def test_station_setters_work(self):
actual = station.Station("A", "B", "C", "D")
self.assertIsInstance(actual, station.Station)
self.assertEqual(actual.site, "A")
self.assertEqual(actual.service, "B")
self.assertEqual(actual.start_date, "C")
self.assertEqual(actual.end_date, "D")
# self.assertIs(type(actual.fetch), function)

@mock.patch("hydrofunctions.hydrofunctions.get_nwis")
def test_station_fetch_calls_get_nwis_correctly(self, mock_get_nwis):
actual = station.Station("A", "B", "1111-11-11", "1111-11-11")
try_it_out = actual.fetch()
mock_get_nwis.assert_called_once_with("A", "B", "1111-11-11", "1111-11-11")

@mock.patch("hydrofunctions.hydrofunctions.get_nwis")
def test_station_fetch2_calls_get_nwis_correctly(self, mock_get_nwis):
actual = station.Station("A", "B", "2002-03-03", "2002-03-03")
try_it_out = actual.fetch()
# This won't raise TypeError for "C" not matching "yyyy-mm-dd"
mock_get_nwis.assert_called_once_with("A", "B", "2002-03-03", "2002-03-03")


if __name__ == '__main__':
unittest.main(verbosity=2)