Skip to content

Commit

Permalink
Tests for import history command
Browse files Browse the repository at this point in the history
  • Loading branch information
carsongee committed Jul 26, 2015
1 parent 26f7b0f commit fe1354d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
11 changes: 6 additions & 5 deletions archelonc/archelonc/command.py
Expand Up @@ -86,9 +86,9 @@ def update():
)
except ArcheloncConnectionException as ex:
print(ex)
sys.exit(1)
sys.exit(3)
if not success:
print('Failed to add commands, got:\n {}'.format(
print('Failed to upload commands, got:\n {}'.format(
response
))
sys.exit(2)
Expand Down Expand Up @@ -122,12 +122,13 @@ def import_history():
success, response = web_history.bulk_add(commands.keys())
except ArcheloncConnectionException as ex:
print(ex)
sys.exit(1)
sys.exit(4)

if not success:
print('Failed to download commands, got:\n {}'.format(
print('Failed to upload commands, got:\n {}'.format(
response
))
sys.exit(6)
# Make copy of imported history so we only have to track
# changes from here on out when archelon is invoked
shutil.copy(hist_file_path, HISTORY_FILE)
Expand All @@ -152,7 +153,7 @@ def export_history():
results = web_history.all(page)
except ArcheloncConnectionException as ex:
print(ex)
sys.exit(1)
sys.exit(5)
while len(results) > 0:
output_file.write('\n'.join(results))
output_file.write('\n')
Expand Down
104 changes: 103 additions & 1 deletion archelonc/archelonc/tests/test_command.py
Expand Up @@ -35,6 +35,11 @@ class TestCommands(WebTest):
"testdata",
"history"
)
TEST_BASH_HISTORY_ALT = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
"testdata",
"history_alt"
)

def test_web_setup(self):
"""
Expand Down Expand Up @@ -156,7 +161,7 @@ def test_update_not_success(self, mock_web_setup):
# Test with connection error
with self.assertRaises(SystemExit) as exception_context:
update()
self.assertEqual(exception_context.exception.code, 1)
self.assertEqual(exception_context.exception.code, 3)

@mock.patch.dict('os.environ', {'HISTFILE': TEST_BASH_HISTORY}, clear=True)
@mock.patch('archelonc.command.HISTORY_FILE', TEST_ARCHELON_HISTORY)
Expand All @@ -176,6 +181,63 @@ def test_update_large_indicator(self, mock_stdout, mock_web_setup):
self.assertEqual(exception_context.exception.code, 2)
self.assertIn('This may take a while', mock_stdout.getvalue())

@mock.patch.dict('os.environ', {'HISTFILE': TEST_BASH_HISTORY}, clear=True)
@mock.patch('archelonc.command.HISTORY_FILE', TEST_ARCHELON_HISTORY)
@mock.patch('archelonc.command._get_web_setup')
def test_import_success(self, mock_web_setup):
"""
Verify the uploading of our history file to the server.
"""
self.addCleanup(os.remove, self.TEST_ARCHELON_HISTORY)
mock_web = mock.MagicMock()
mock_web.bulk_add.return_value = True, 'foo'
mock_web_setup.return_value = mock_web
with mock.patch('sys.argv', []):
import_history()
self.assertTrue(
filecmp.cmp(self.TEST_ARCHELON_HISTORY, self.TEST_BASH_HISTORY)
)

@mock.patch.dict('os.environ', {'HISTFILE': TEST_BASH_HISTORY}, clear=True)
@mock.patch('archelonc.command.HISTORY_FILE', TEST_ARCHELON_HISTORY)
@mock.patch('archelonc.command._get_web_setup')
def test_import_success_specified(self, mock_web_setup):
"""
Verify the uploading of our history file to the server
from a specified file.
"""
self.addCleanup(os.remove, self.TEST_ARCHELON_HISTORY)
mock_web = mock.MagicMock()
mock_web.bulk_add.return_value = True, 'foo'
mock_web_setup.return_value = mock_web
with mock.patch('sys.argv', ['a', self.TEST_BASH_HISTORY_ALT]):
import_history()
self.assertTrue(
filecmp.cmp(self.TEST_ARCHELON_HISTORY, self.TEST_BASH_HISTORY_ALT)
)

@mock.patch.dict('os.environ', {'HISTFILE': TEST_BASH_HISTORY}, clear=True)
@mock.patch('archelonc.command._get_web_setup')
def test_import_errors(self, mock_web_setup):
"""
Test handling of connection error handling.
"""
mock_web = mock.MagicMock()
mock_web.bulk_add.side_effect = ArcheloncConnectionException()
mock_web_setup.return_value = mock_web
with mock.patch('sys.argv', []):
with self.assertRaises(SystemExit) as exception_context:
import_history()
self.assertEqual(exception_context.exception.code, 4)

# Test bad results coming back
mock_web.bulk_add.side_effect = None
mock_web.bulk_add.return_value = False, 'foo'
with mock.patch('sys.argv', []):
with self.assertRaises(SystemExit) as exception_context:
import_history()
self.assertEqual(exception_context.exception.code, 6)

@mock.patch('archelonc.command._get_web_setup')
@mock.patch('sys.stdout', new_callable=StringIO)
def test_export_success_stdout(self, mock_stdout, mock_web_setup):
Expand All @@ -200,3 +262,43 @@ def side_effect(*args):
mock_stdout.getvalue(),
'\n'.join(test_list * 2) + '\n'
)

@mock.patch('archelonc.command._get_web_setup')
def test_export_success_file(self, mock_web_setup):
"""
Validate that we can export all commands successfully to a specified
file.
"""
self.addCleanup(os.remove, self.TEST_ARCHELON_HISTORY)
test_list = ['testing-export1', 'testing-export2']
mock_web = mock.MagicMock()

def side_effect(*args):
"""Do two pages of the same results."""
if args[0] < 2:
return test_list
else:
return []

mock_web.all.side_effect = side_effect
mock_web_setup.return_value = mock_web
with mock.patch('sys.argv', ['a', self.TEST_ARCHELON_HISTORY]):
export_history()
with open(self.TEST_ARCHELON_HISTORY) as output_file:
self.assertEqual(
output_file.read(),
'\n'.join(test_list * 2) + '\n'
)

@mock.patch('archelonc.command._get_web_setup')
def test_export_connection_error(self, mock_web_setup):
"""
Test handling of connection error handling.
"""
mock_web = mock.MagicMock()
mock_web.all.side_effect = ArcheloncConnectionException()
mock_web_setup.return_value = mock_web
with mock.patch('sys.argv', []):
with self.assertRaises(SystemExit) as exception_context:
export_history()
self.assertEqual(exception_context.exception.code, 5)
2 changes: 2 additions & 0 deletions archelonc/archelonc/tests/testdata/history_alt
@@ -0,0 +1,2 @@
echo 'Go down, go down, go down!'
export something='Need doing?!'

0 comments on commit fe1354d

Please sign in to comment.