Skip to content

Commit

Permalink
More tests, more fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
btimby committed May 11, 2017
1 parent 266e0e5 commit 669484c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
24 changes: 22 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import threading
import time
import tempfile
import unittest

from io import StringIO
Expand Down Expand Up @@ -345,8 +346,8 @@ def test_main_get(self):
self.fail('Did not raise SystemExit')
self.assertEqual(item_put, json.loads(stdout.getvalue()))

def test_main_put(self):
"""Ensure we can put to a queue using CLI."""
def test_main_put_stdin(self):
"""Ensure we can put to a queue from stdin using CLI."""
item_put = {'test': 'test'}
main({
'--debug': False,
Expand All @@ -359,6 +360,25 @@ def test_main_put(self):
with self.queue.get() as item_get:
self.assertEqual(item_put, item_get)

def test_main_put_file(self):
"""Ensure we can put to a queue from a file using CLI."""
"""Ensure we can put to a queue using CLI."""
item_put = {'test': 'test'}
with tempfile.NamedTemporaryFile() as t:
t.write(json.dumps(item_put).encode('utf-8'))
t.flush()

main({
'--debug': False,
'<name>': 'test',
'consume': False,
'produce': True,
'--file': t.name,
}, stdin=StringIO(json.dumps(item_put)))

with self.queue.get() as item_get:
self.assertEqual(item_put, item_get)


if __name__ == '__main__':
unittest.main()
10 changes: 9 additions & 1 deletion tpq/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ def produce(opt, stdin):
data = stdin.read()

else:
with open(opt['--file'], 'r') as f:
with open(opt['--file'], 'rb') as f:
data = f.read()

try:
data = data.decode('utf-8')
except AttributeError:
pass
except Exception as e:
LOGGER.exception(e, exc_info=True)
return

try:
json.loads(data)
except ValueError as e:
Expand Down

0 comments on commit 669484c

Please sign in to comment.