Skip to content

Commit

Permalink
Merge 74d3365 into 62cd5fc
Browse files Browse the repository at this point in the history
  • Loading branch information
carsongee committed Aug 2, 2015
2 parents 62cd5fc + 74d3365 commit ea6de64
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 11 deletions.
22 changes: 11 additions & 11 deletions archelonc/archelonc/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ class SearchForm(npyscreen.ActionFormWithMenus):
"""
# pylint: disable=too-many-ancestors

def __init__(self, *args, **keywords):
def __init__(self, *args, **kwargs):
"""
Add additional properties for use in state tracking
"""
super(SearchForm, self).__init__(*args, **keywords)
super(SearchForm, self).__init__(*args, **kwargs)
self.order = None

def forward_order(self):
Expand All @@ -152,14 +152,6 @@ def reverse_order(self):
self.order = 'r'
self.search_box.when_value_edited()

def afterEditing(self):
"""
This is the form to display, so set next to None
"""
# Have to use the methods I'm given.
# pylint: disable=invalid-name
self.parentApp.setNextForm(None)

def create(self):
"""
Build the form for searching
Expand Down Expand Up @@ -212,6 +204,14 @@ def beforeEditing(self):

self.preserve_selected_widget = True

def afterEditing(self):
"""
This is the only form to display, so set next to None
"""
# Have to use the methods I'm given.
# pylint: disable=invalid-name
self.parentApp.setNextForm(None)

def on_ok(self, *_):
"""
We just drop the command into a
Expand All @@ -224,7 +224,7 @@ def on_ok(self, *_):

def on_cancel(self, *_):
"""
Drop out with a non 1 exit code so the
Drop out with a non 0 exit code so the
wrapper doesn't execute anything
"""
sys.exit(1)
Expand Down
134 changes: 134 additions & 0 deletions archelonc/archelonc/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# -*- coding: utf-8 -*-
"""
Tests for the search interface and classes.
"""
from __future__ import absolute_import, unicode_literals
import os
from tempfile import NamedTemporaryFile
import unittest

import mock

from archelonc.search import Search, SearchForm
from archelonc.data import LocalHistory, WebHistory


class TestSearchMain(unittest.TestCase):
"""
Test for the main app and history initialization.
"""

def test_search_init(self):
"""
Verify main form initializes as expected.
"""
search = Search()
self.assertTrue(hasattr(search, 'data'))

def test_search_onstart(self):
"""
Verify we setup the form and data class correctly
"""
search = Search()
search.addForm = mock.MagicMock()
# Verify local data with no URLs set.
with mock.patch.dict('os.environ', {}, clear=True):
with mock.patch('archelonc.search.LocalHistory.__init__') as init:
init.return_value = None
search.onStart()
self.assertTrue(isinstance(search.data, LocalHistory))

# Verify Web data with URLs set.
with mock.patch.dict(
'os.environ',
{'ARCHELON_URL': 'http://foo', 'ARCHELON_TOKEN': 'foo'},
clear=True
):
search.onStart()
self.assertTrue(isinstance(search.data, WebHistory))


@mock.patch('npyscreen.ActionFormWithMenus.__init__')
class TestSearchForm(unittest.TestCase):
"""
Test for the main form and creation.
"""

def test_form_init(self, mocked_super):
"""
Verify we are calling super and that order is set.
"""
form = SearchForm(1, a='b')
mocked_super.assert_called_with(1, a='b')
self.assertTrue(hasattr(form, 'order'))

def test_create(self, _):
"""
Verify all of our creation code is as expected (thought this is
strictly a unit test since I am mocking everything else out, and
create is called during the initialization with the super class
that I am mocking out as well.
"""
form = SearchForm()
form.add = mock.MagicMock()
form.add_handlers = mock.MagicMock()
with mock.patch('archelonc.search.SearchBox') as _,\
mock.patch('archelonc.search.SearchResults') as _,\
mock.patch('archelonc.search.CommandBox') as _:
form.create()
self.assertEqual(form.order, 'r')
for attr in ('search_box', 'results_list', 'command_box'):
self.assertTrue(hasattr(form, attr))
self.assertEqual(len(form.menu.getItemObjects()), 2)
self.assertTrue(form.add_handlers.called_once)

def test_orders(self, _):
"""
Verify the forward and reverse orders do as expected.
"""
form = SearchForm()
form.search_box = mock.MagicMock()
form.order = 'not_real'
form.forward_order()
self.assertIsNone(form.order)
self.assertTrue(form.search_box.when_value_edited.called_once())

# Now the reverse.
form.search_box = mock.MagicMock()
form.order = 'flibber-flab'
form.reverse_order()
self.assertEqual('r', form.order)
self.assertTrue(form.search_box.when_value_edited.called_once())

def test_edit_events(self, _):
"""
Verify the event handlers (before and after).
"""
form = SearchForm()
form.preserve_selected_widget = False
form.beforeEditing()
self.assertTrue(form.preserve_selected_widget)

form.parentApp = mock.MagicMock()
form.afterEditing()
form.parentApp.setNextForm.assert_called_with(None)

def test_exit_handlers(self, _):
"""
Verify the exit handlers do what we want.
"""
form = SearchForm()
with self.assertRaises(SystemExit) as exception_context:
form.on_cancel()
self.assertEqual(exception_context.exception.code, 1)
form.command_box = mock.MagicMock()
test_command = 'Hello Testo'
form.command_box.value = test_command
with NamedTemporaryFile(mode='r+') as temp_file:
with mock.patch('os.path.expanduser') as expand_hijack:
expand_hijack.return_value = os.path.abspath(temp_file.name)
with self.assertRaises(SystemExit) as exception_context:
form.on_ok()
self.assertEqual(exception_context.exception.code, 0)
temp_file.seek(0)
self.assertEqual(test_command, temp_file.read())
1 change: 1 addition & 0 deletions archelonc/archelonc/tests/testdata/history_alt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
echo 'Go down, go down, go down!'

export something='Need doing?!'
7 changes: 7 additions & 0 deletions archelonc/test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
pytest
pytest-pep8
pytest-pylint

# Thanks to @bdero for this:
# logilab-common is only necessary because pylint's dependency chain will
# result in the latest logilab-common being fetched, but astroid
# (another pylint dependency) depends on logilab-common<=0.63.0
logilab-common==0.63.0

pytest-cov
pytest-capturelog
pytest-watch
Expand Down
7 changes: 7 additions & 0 deletions archelond/test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
pytest
pytest-pep8
pytest-pylint

# Thanks to @bdero for this:
# logilab-common is only necessary because pylint's dependency chain will
# result in the latest logilab-common being fetched, but astroid
# (another pylint dependency) depends on logilab-common<=0.63.0
logilab-common==0.63.0

pytest-cov
pytest-capturelog
pytest-watch
Expand Down

0 comments on commit ea6de64

Please sign in to comment.