From c6c78b928fe0a3da1c7475de4d2cb22b2f30ca26 Mon Sep 17 00:00:00 2001 From: Tim Cera Date: Mon, 22 Dec 2014 21:01:36 -0500 Subject: [PATCH] Added API test for catalog and extract. --- VERSION | 2 +- hspfbintoolbox/hspfbintoolbox.py | 4 ++-- tests/test_catalog.py | 25 +++++++++++++++++++++---- tests/test_extract.py | 8 +++++++- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/VERSION b/VERSION index 42045ac..c2c0004 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.4 +0.3.5 diff --git a/hspfbintoolbox/hspfbintoolbox.py b/hspfbintoolbox/hspfbintoolbox.py index 0dca773..2bbfb1c 100644 --- a/hspfbintoolbox/hspfbintoolbox.py +++ b/hspfbintoolbox/hspfbintoolbox.py @@ -219,7 +219,7 @@ def _get_data(binfilename, else: mindate = min(mindate, ndate) maxdate = max(maxdate, ndate) - collect_dict[tmpkey] = (mindate, maxdate) + collect_dict[tmpkey[1:]] = (mindate, maxdate) tindex = fl.tell() @@ -369,7 +369,7 @@ def catalog(hbnfilename): catkeys.sort() for cat in catkeys: print('{0},{1},{2},{3} ,{5}, {6}, {7}'.format( - *(cat[1:] + catlog[cat] + + *(cat + catlog[cat] + (code2intervalmap[cat[-1]],)))) diff --git a/tests/test_catalog.py b/tests/test_catalog.py index 8dc60db..4c81fbd 100644 --- a/tests/test_catalog.py +++ b/tests/test_catalog.py @@ -11,7 +11,7 @@ import shlex import subprocess from pandas.util.testing import TestCase -from pandas.util.testing import assert_frame_equal +from pandas.util.testing import assert_frame_equal, assert_equal import sys try: from cStringIO import StringIO @@ -21,6 +21,11 @@ import pandas as pd from hspfbintoolbox import hspfbintoolbox +interval2codemap = {'yearly': 5, + 'monthly': 4, + 'daily': 3, + 'bivl': 2} + def capture(func, *args, **kwds): sys.stdout = StringIO() # capture output out = func(*args, **kwds) @@ -2196,9 +2201,21 @@ def setUp(self): PERLND,905,PWATER,UZS ,1951-01-01 00:00:00, 2001-01-01 00:00:00, yearly ''' - #def catalog(self): - # out = hspfbintoolbox.catalog('tests/6b_np1.hbn') - # assert_frame_equal(out, self.catalog) + def test_catalog_api(self): + out = hspfbintoolbox.catalog('tests/6b_np1.hbn') + import csv + import datetime + import re + ndict = {} + rd = csv.reader(self.catalog.decode().split('\n')) + for row in rd: + if len(row) == 0: + continue + nrow = [i.strip() for i in row] + sdate = datetime.datetime(*map(int, re.findall('\d+', nrow[4]))) + edate = datetime.datetime(*map(int, re.findall('\d+', nrow[5]))) + ndict[(nrow[0], int(nrow[1]), nrow[2], nrow[3], interval2codemap[nrow[6]])] = (sdate, edate) + assert_equal(out, ndict) def test_catalog_cli(self): args = 'hspfbintoolbox catalog tests/6b_np1.hbn' diff --git a/tests/test_extract.py b/tests/test_extract.py index 5764937..60ccdbc 100644 --- a/tests/test_extract.py +++ b/tests/test_extract.py @@ -14,7 +14,7 @@ from pandas.util.testing import assert_frame_equal import sys try: - from cStringIO import StringIO + from StringIO import StringIO except: from io import StringIO @@ -87,9 +87,15 @@ def setUp(self): 1999-01-01,1.40244 2000-01-01,0.0191165 ''' + self.extract_api = StringIO(self.extract.decode()) def test_extract_cli(self): args = 'hspfbintoolbox extract tests/6b_np1.hbn yearly ,905,,AGWS' args = shlex.split(args) out = subprocess.Popen(args ,stdout=subprocess.PIPE, stdin=subprocess.PIPE).communicate()[0] self.assertEqual(out, self.extract) + + def test_extract_sub(self): + import io + out = hspfbintoolbox.extract('tests/6b_np1.hbn', 'yearly', ',905,,AGWS') + assert_frame_equal(out, pd.DataFrame.from_csv(self.extract_api))