Skip to content

Commit

Permalink
Added API test for catalog and extract.
Browse files Browse the repository at this point in the history
  • Loading branch information
timcera committed Dec 23, 2014
1 parent d09fb69 commit c6c78b9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.3.4
0.3.5
4 changes: 2 additions & 2 deletions hspfbintoolbox/hspfbintoolbox.py
Expand Up @@ -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()


Expand Down Expand Up @@ -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]],))))


Expand Down
25 changes: 21 additions & 4 deletions tests/test_catalog.py
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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'
Expand Down
8 changes: 7 additions & 1 deletion tests/test_extract.py
Expand Up @@ -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

Expand Down Expand Up @@ -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))

0 comments on commit c6c78b9

Please sign in to comment.