Skip to content

Commit

Permalink
Adds tests for parse_new_metadata()
Browse files Browse the repository at this point in the history
  • Loading branch information
remram44 committed Oct 11, 2013
1 parent cf548e0 commit 9cbd7f0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
15 changes: 13 additions & 2 deletions file_archive/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,20 @@ def parse_new_metadata(args):
k, v = k
if ':' in v:
t, v = v.split(':', 1)
metadata[k] = {'type': t, 'value': v}
else:
metadata[k] = v
t = 'str'
if k in metadata:
sys.stderr.write("Multiple values for key %s\n" % k)
sys.exit(1)
if t == 'int':
v = int(v)
elif t != 'str':
sys.stderr.write("Metadata has unknown type '%s'! Only 'str' and "
"'int' are supported.\n"
"If you meant a string with a ':', use "
"'str:mystring'" % t)
sys.exit(1)
metadata[k] = {'type': t, 'value': v}
return metadata


Expand Down
20 changes: 19 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,22 @@ def error1(*args):
error1('k=burger:A')


# TODO : parse_new_metadata()
class TestParseNewData(unittest.TestCase):
def test_data(self):
self.assertEqual(file_archive.main.parse_new_metadata(
['type=a file', 'month=str:october',
'time=str:11:40', 'year=int:2013']),
{'type': {'type': 'str', 'value': u'a file'},
'month': {'type': 'str', 'value': u'october'},
'time': {'type': 'str', 'value': u'11:40'},
'year': {'type': 'int', 'value': 2013}})

def test_errors(self):
def error1(*args):
with catch_errorexit() as e:
file_archive.main.parse_query_metadata(args)
self.assertEqual(e[0], 1)

error1('a', 'b')
error1('k=burger:A')
error1('k=str:A', 'k=int:7')

0 comments on commit 9cbd7f0

Please sign in to comment.