Skip to content

Commit

Permalink
Merge branch 'feature/v2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb531 committed Mar 11, 2015
2 parents 2f43dec + bb5d851 commit b4b1856
Show file tree
Hide file tree
Showing 19 changed files with 640 additions and 231 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exclude_lines =
include =
yv_suggest/shared.py
yv_suggest/filter_refs.py
yv_suggest/filter_recent_refs.py
yv_suggest/filter_prefs.py
yv_suggest/view_ref.py
yv_suggest/search_ref.py
Expand Down
47 changes: 47 additions & 0 deletions tests/context_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from contextlib import contextmanager
from io import BytesIO
import sys
import module_mocks
import yv_suggest.shared as yvs


@contextmanager
Expand All @@ -16,3 +18,48 @@ def redirect_stdout():
yield out
finally:
sys.stdout = original_stdout


@contextmanager
def use_prefs(prefs):
"""temporarily use the given preferences"""
original_prefs = yvs.get_prefs()
try:
yvs.update_prefs(prefs)
yield
finally:
yvs.update_prefs(original_prefs)


@contextmanager
def use_default_prefs():
"""temporarily use the default values for all preferences"""
original_prefs = yvs.get_prefs()
try:
yvs.update_prefs(yvs.get_defaults())
yield
finally:
yvs.update_prefs(original_prefs)


@contextmanager
def use_recent_refs(recent_refs):
"""temporarily use the given list of recent references"""
original_recent_refs = yvs.get_recent_refs()
try:
yvs.update_recent_refs(recent_refs)
yield
finally:
yvs.update_recent_refs(original_recent_refs)


@contextmanager
def mock_webbrowser(yvs):
"""mock the webbrowser module for testing purposes"""
mock = module_mocks.WebbrowserMock()
original_webbrowser = yvs.webbrowser
yvs.webbrowser = mock
try:
yield mock
finally:
yvs.webbrowser = original_webbrowser
1 change: 0 additions & 1 deletion tests/webbrowser_mock.py → tests/module_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ class WebbrowserMock(object):
"""mock the builtin webbrowser module"""

def open(self, url):
"""mock the webbrowser.open() function"""
self.url = url
12 changes: 6 additions & 6 deletions tests/test_filter_prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import nose.tools as nose
import yv_suggest.filter_prefs as yvs
from xml.etree import ElementTree as ET
from context_managers import redirect_stdout
import context_managers as ctx


def test_show_languages():
Expand Down Expand Up @@ -37,21 +37,21 @@ def test_filter_versions():


def test_nonexistent():
"""should not match nonexistent preferences"""
"""should not match nonexistent preference"""
results = yvs.get_result_list('xyz', prefs={})
nose.assert_equal(len(results), 0)


def test_invalid():
"""should not match nonexistent preferences"""
"""should not match nonexistent preference"""
results = yvs.get_result_list('!@#', prefs={})
nose.assert_equal(len(results), 0)


def test_main_output():
"""should output pref result list XML"""
query_str = 'language'
with redirect_stdout() as out:
with ctx.redirect_stdout() as out:
yvs.main(query_str, prefs={})
output = out.getvalue().strip()
results = yvs.get_result_list(query_str, prefs={})
Expand All @@ -60,9 +60,9 @@ def test_main_output():


def test_null_result():
"""should output "No Results" XML item for empty pref result lists"""
"""should output "No Results" XML item for empty pref result list"""
query_str = 'xyz'
with redirect_stdout() as out:
with ctx.redirect_stdout() as out:
yvs.main(query_str, prefs={})
xml = out.getvalue().strip()
root = ET.fromstring(xml)
Expand Down
118 changes: 118 additions & 0 deletions tests/test_filter_recent_refs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import nose.tools as nose
import yv_suggest.filter_recent_refs as yvs
import context_managers as ctx
import inspect
from xml.etree import ElementTree as ET


recent_refs = ['59/psa.23', '116/1co.13', '107/psa.139', '111/psa.22',
'59/rev.5.13', '1/psa.23', '111/mat.5.3-12',
'8/rev.19.16', '111/rev.22', '8/psa.23']


def test_query_param():
"""should use received query parameter as default filter query"""
spec = inspect.getargspec(yvs.main)
default_query_str = spec.defaults[0]
nose.assert_equal(default_query_str, '{query}')


def test_show_all():
"""should show all recent references when given empty query"""
with ctx.use_recent_refs(recent_refs):
results = yvs.get_result_list('', prefs={})
nose.assert_equal(len(results), len(recent_refs))


def test_filter_book():
"""should filter recent references by book name"""
with ctx.use_recent_refs(recent_refs):
results = yvs.get_result_list('ps', prefs={})
nose.assert_equal(len(results), 5)
nose.assert_equal(results[0]['title'], 'Psalm 23 (ESV)')
nose.assert_equal(results[1]['title'], 'Psalm 139 (NET)')
nose.assert_equal(results[2]['title'], 'Psalm 22 (NIV)')
nose.assert_equal(results[3]['title'], 'Psalm 23 (KJV)')
nose.assert_equal(results[4]['title'], 'Psalm 23 (AMP)')


def test_filter_book_numbered():
"""should filter recent references by numbered book name"""
with ctx.use_recent_refs(recent_refs):
results = yvs.get_result_list('1c', prefs={})
nose.assert_equal(len(results), 1)
nose.assert_equal(results[0]['title'], '1 Corinthians 13 (NLT)')


def test_filter_chapter():
"""should filter recent references by chapter"""
with ctx.use_recent_refs(recent_refs):
results = yvs.get_result_list('re1', prefs={})
nose.assert_equal(len(results), 1)
nose.assert_equal(results[0]['title'], 'Revelation 19:16 (AMP)')


def test_filter_verse():
"""should filter recent references by verse"""
with ctx.use_recent_refs(recent_refs):
results = yvs.get_result_list('re5.1', prefs={})
nose.assert_equal(len(results), 1)
nose.assert_equal(results[0]['title'], 'Revelation 5:13 (ESV)')


def test_filter_verse_range():
"""should filter recent references by verse range"""
with ctx.use_recent_refs(recent_refs):
results = yvs.get_result_list('m5:3-1', prefs={})
nose.assert_equal(len(results), 1)
nose.assert_equal(results[0]['title'], 'Matthew 5:3-12 (NIV)')


def test_filter_verse_range_nonexistent():
"""should not match verse range not apart of recent reference"""
with ctx.use_recent_refs(recent_refs):
results = yvs.get_result_list('m5:3-4', prefs={})
nose.assert_equal(len(results), 0)


def test_filter_version():
"""should filter recent references by version"""
with ctx.use_recent_refs(recent_refs):
results = yvs.get_result_list('ps2k', prefs={})
nose.assert_equal(len(results), 1)
nose.assert_equal(results[0]['title'], 'Psalm 23 (KJV)')


def test_filter_empty():
"""should return empty list for nonexistent references"""
with ctx.use_recent_refs(recent_refs):
results = yvs.get_result_list('xyz', prefs={})
nose.assert_equal(len(results), 0)


def test_main_output():
"""should output recent list XML"""
with ctx.use_recent_refs(recent_refs):
with ctx.redirect_stdout() as out:
query_str = 'p22'
yvs.main(query_str, prefs={})
output = out.getvalue().strip()
results = yvs.get_result_list(query_str, prefs={})
xml = yvs.shared.get_result_list_xml(results).strip()
nose.assert_equal(output, xml)


def test_null_result():
"""should output "No Results" XML item for empty recent recent list"""
with ctx.use_recent_refs(recent_refs):
with ctx.redirect_stdout() as out:
yvs.main('xyz', prefs={})
xml = out.getvalue().strip()
root = ET.fromstring(xml)
item = root.find('item')
nose.assert_is_not_none(item, '<item> element is missing')
nose.assert_equal(item.get('valid'), 'no')
8 changes: 4 additions & 4 deletions tests/test_filter_refs_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import nose.tools as nose
import yv_suggest.filter_refs as yvs
from xml.etree import ElementTree as ET
from context_managers import redirect_stdout
import context_managers as ctx
import inspect
import sys


def test_output():
"""should output ref result list XML"""
query_str = 'genesis 50:20'
with redirect_stdout() as out:
with ctx.redirect_stdout() as out:
yvs.main(query_str, prefs={})
output = out.getvalue().strip()
results = yvs.get_result_list(query_str, prefs={})
Expand All @@ -22,9 +22,9 @@ def test_output():


def test_null_result():
"""should output "No Results" XML item for empty ref result lists"""
"""should output "No Results" XML item for empty ref result list"""
query_str = 'xyz'
with redirect_stdout() as out:
with ctx.redirect_stdout() as out:
yvs.main(query_str, prefs={})
xml = out.getvalue().strip()
root = ET.fromstring(xml)
Expand Down
54 changes: 29 additions & 25 deletions tests/test_filter_refs_prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,48 @@
import nose.tools as nose
import yv_suggest.filter_refs as yvs
import os
import context_managers as ctx


def test_version_persistence():
"""should remember version preferences"""
original_prefs = yvs.shared.get_prefs()
prefs = original_prefs.copy()
prefs['language'] = 'en'
prefs['version'] = 59
yvs.shared.update_prefs(prefs)
results = yvs.get_result_list('mat 4')
nose.assert_equal(len(results), 1)
nose.assert_equal(results[0]['title'], 'Matthew 4 (ESV)')
yvs.shared.update_prefs(original_prefs)
prefs = {
'language': 'en',
'version': 59
}
with ctx.use_prefs(prefs):
results = yvs.get_result_list('mat 4')
nose.assert_equal(len(results), 1)
nose.assert_equal(results[0]['title'], 'Matthew 4 (ESV)')


def test_language_persistence():
"""should remember language preferences"""
original_prefs = yvs.shared.get_prefs()
prefs = original_prefs.copy()
prefs['language'] = 'es'
yvs.shared.update_prefs(prefs)
results = yvs.get_result_list('gá 4')
nose.assert_equal(len(results), 1)
nose.assert_true(results[0]['title'].startswith('Gálatas 4 '))
yvs.shared.update_prefs(original_prefs)
prefs = {
'language': 'es'
}
with ctx.use_prefs(prefs):
results = yvs.get_result_list('gá 4')
nose.assert_equal(len(results), 1)
nose.assert_true(results[0]['title'].startswith('Gálatas 4 '))


def test_creation():
"""should create preferences if nonexistent"""
yvs.shared.delete_prefs()
nose.assert_false(os.path.exists(yvs.shared.prefs_path))
defaults = yvs.shared.get_defaults()
prefs = yvs.shared.get_prefs()
nose.assert_true(os.path.exists(yvs.shared.prefs_path))
nose.assert_equal(prefs, defaults)
with ctx.use_default_prefs():
yvs.shared.delete_prefs()
nose.assert_false(os.path.exists(yvs.shared.prefs_path))
defaults = yvs.shared.get_defaults()
prefs = yvs.shared.get_prefs()
nose.assert_true(os.path.exists(yvs.shared.prefs_path))
nose.assert_equal(prefs, defaults)


def test_delete_nonexistent():
"""should attempt to delete nonexistent preferences without error"""
yvs.shared.delete_prefs()
yvs.shared.delete_prefs()
with ctx.use_default_prefs():
try:
yvs.shared.delete_prefs()
yvs.shared.delete_prefs()
except Exception as error:
nose.assert_true(False, error)
47 changes: 47 additions & 0 deletions tests/test_recent_refs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import nose.tools as nose
import yv_suggest.shared as yvs
import os.path
import context_managers as ctx


def test_max_len():
"""should cap length of recent list to defined length"""
with ctx.use_recent_refs([]):
for verse in range(1, yvs.max_recent_refs + 5):
yvs.push_recent_ref('59/jhn.3.{}'.format(verse))
recent_refs = yvs.get_recent_refs()
nose.assert_equal(len(recent_refs), yvs.max_recent_refs)


def test_dup_ref():
"""should not add reference already present in recent list"""
with ctx.use_recent_refs([]):
dup_ref = '59/psa.73.26'
for i in range(2):
yvs.push_recent_ref(dup_ref)
recent_refs = yvs.get_recent_refs()
nose.assert_equal(recent_refs.count(dup_ref), 1)


def test_creation():
"""should create recent list if nonexistent"""
with ctx.use_recent_refs([]):
yvs.delete_recent_refs()
nose.assert_false(os.path.exists(yvs.recent_refs_path))
recent_refs = yvs.get_recent_refs()
nose.assert_true(os.path.exists(yvs.recent_refs_path))
nose.assert_equal(recent_refs, [])


def test_delete_nonexistent():
"""should attempt to delete nonexistent recent list without error"""
with ctx.use_recent_refs([]):
try:
yvs.delete_recent_refs()
yvs.delete_recent_refs()
except Exception as error:
nose.assert_true(False, error)
Loading

0 comments on commit b4b1856

Please sign in to comment.