From 73d3263561045f996cd446c6271032d86ba29588 Mon Sep 17 00:00:00 2001 From: Nicolas Delaby Date: Fri, 3 Jan 2014 16:55:43 +0100 Subject: [PATCH 1/2] 99% coverage --- pyipptool/__init__.py | 20 ++--- tests/test_highlevel.py | 189 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 182 insertions(+), 27 deletions(-) diff --git a/pyipptool/__init__.py b/pyipptool/__init__.py index 304048f..d709918 100644 --- a/pyipptool/__init__.py +++ b/pyipptool/__init__.py @@ -38,6 +38,14 @@ config.read(['/etc/pyipptool/pyipptool.cfg', os.path.join(os.path.expanduser('~'), '.pyipptool.cfg')]) IPPTOOL_PATH = config.get('main', 'ipptool_path') +try: + LOGIN = config.get('main', 'login') +except ConfigParser.NoOptionError: + LOGIN = '' +try: + PASSWORD = config.get('main', 'password') +except ConfigParser.NoOptionError: + PASSWORD = '' try: GRACEFUL_SHUTDOWN_TIME = config.get('main', 'graceful_shutdown_time') except ConfigParser.NoOptionError: @@ -53,17 +61,9 @@ class TimeoutError(Exception): def authenticate_uri(uri): - try: - login = config.get('main', 'login') - except ConfigParser.NoOptionError: - login = '' - try: - password = config.get('main', 'password') - except ConfigParser.NoOptionError: - password = '' - if login and password: + if LOGIN and PASSWORD: parsed_url = urlparse.urlparse(uri) - authenticated_netloc = '{}:{}@{}'.format(login, password, + authenticated_netloc = '{}:{}@{}'.format(LOGIN, PASSWORD, parsed_url.netloc) authenticated_uri = urlparse.ParseResult(parsed_url[0], authenticated_netloc, diff --git a/tests/test_highlevel.py b/tests/test_highlevel.py index 543669f..46a4aaa 100644 --- a/tests/test_highlevel.py +++ b/tests/test_highlevel.py @@ -10,6 +10,24 @@ import pyipptool +def patch_config(**kwconfig): + def wrapper(fn): + @functools.wraps(fn) + def inner(*args, **kw): + import pyipptool + old_values = {} + for k, v in kwconfig.items(): + old_values[k] = getattr(pyipptool, k) + setattr(pyipptool, k, v) + try: + return fn(*args, **kw) + finally: + for k, v in old_values.items(): + setattr(pyipptool, k, v) + return inner + return wrapper + + @mock.patch.object(pyipptool, '_call_ipptool') def test_ipptool_create_printer_subscription(_call_ipptool): from pyipptool import create_printer_subscription @@ -71,22 +89,7 @@ def test_get_job_attributes_with_job_uri(_call_ipptool): assert 'printer-uri' not in request -def patch_timeout(value): - def wrapper(fn): - @functools.wraps(fn) - def inner(*args, **kw): - import pyipptool - previous = pyipptool.TIMEOUT - pyipptool.TIMEOUT = value - try: - return fn(*args, **kw) - finally: - pyipptool.TIMEOUT = previous - return inner - return wrapper - - -@patch_timeout(1) +@patch_config(TIMEOUT=1) def test_timeout(): from pyipptool import TimeoutError, _call_ipptool from pyipptool.forms import get_subscriptions_form @@ -108,7 +111,6 @@ def do_POST(self): thread = threading.Thread(target=httpd.serve_forever) thread.daemon = True thread.start() - # time.sleep(.1) # Warmup request = get_subscriptions_form.render( {'header': @@ -117,3 +119,156 @@ def do_POST(self): 'http://localhost:%s/printers/fake' % PORT}}}) with pytest.raises(TimeoutError): _call_ipptool('http://localhost:%s/' % PORT, request) + + +@patch_config(LOGIN='ezeep', PASSWORD='secret') +def test_authentication(): + from pyipptool import authenticate_uri + + assert (authenticate_uri('http://localhost:631/printers/?arg=value') == + 'http://ezeep:secret@localhost:631/printers/?arg=value') + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_release_job(_call_ipptool): + from pyipptool import release_job + _call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]} + release_job('https://localhost:631/') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_cancel_job(_call_ipptool): + from pyipptool import cancel_job + _call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]} + cancel_job('https://localhost:631/') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_cups_add_modify_class(_call_ipptool): + from pyipptool import cups_add_modify_class + _call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]} + cups_add_modify_class('https://localhost:631/', + printer_uri='') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_cups_delete_printer(_call_ipptool): + from pyipptool import cups_delete_printer + _call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]} + cups_delete_printer('https://localhost:631/') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_cups_delete_class(_call_ipptool): + from pyipptool import cups_delete_class + _call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]} + cups_delete_class('https://localhost:631/') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_cups_get_classes(_call_ipptool): + from pyipptool import cups_get_classes + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + cups_get_classes('https://localhost:631/') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_cups_get_printers(_call_ipptool): + from pyipptool import cups_get_printers + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + cups_get_printers('https://localhost:631/') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_cups_get_devices(_call_ipptool): + from pyipptool import cups_get_devices + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + cups_get_devices('https://localhost:631/') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_cups_get_ppds(_call_ipptool): + from pyipptool import cups_get_ppds + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + cups_get_ppds('https://localhost:631/') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_cups_move_job(_call_ipptool): + from pyipptool import cups_move_job + _call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]} + cups_move_job('https://localhost:631/') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_cups_reject_jobs(_call_ipptool): + from pyipptool import cups_reject_jobs + _call_ipptool.return_value = {'Tests': [{'StatusCode': 'successful-ok'}]} + cups_reject_jobs('https://localhost:631/') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_get_jobs(_call_ipptool): + from pyipptool import get_jobs + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + get_jobs('https://localhost:631/', printer_uri='') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_get_printer_attributes(_call_ipptool): + from pyipptool import get_printer_attributes + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + get_printer_attributes('https://localhost:631/', printer_uri='') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_get_subscriptions(_call_ipptool): + from pyipptool import get_subscriptions + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + get_subscriptions('https://localhost:631/', printer_uri='') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_pause_printer(_call_ipptool): + from pyipptool import pause_printer + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + pause_printer('https://localhost:631/', printer_uri='') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_hold_new_jobs(_call_ipptool): + from pyipptool import hold_new_jobs + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + hold_new_jobs('https://localhost:631/', printer_uri='') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_release_held_new_jobs(_call_ipptool): + from pyipptool import release_held_new_jobs + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + release_held_new_jobs('https://localhost:631/', printer_uri='') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' + + +@mock.patch.object(pyipptool, '_call_ipptool') +def test_resume_printer(_call_ipptool): + from pyipptool import resume_printer + _call_ipptool.return_value = {'Tests': [{'ResponseAttributes': ''}]} + resume_printer('https://localhost:631/', printer_uri='') + assert _call_ipptool._mock_mock_calls[0][1][0] == 'https://localhost:631/' From 618c5009063fea700eeed53d130fcb36f375cf7e Mon Sep 17 00:00:00 2001 From: Nicolas Delaby Date: Fri, 3 Jan 2014 18:29:49 +0100 Subject: [PATCH 2/2] Try https://coveralls.io --- .travis.yml | 6 +++++- README.rst | 4 ++++ setup.py | 3 ++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 63f9871..d2fa9a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,14 @@ python: install: - "python setup.py install" + - "pip install coverage coveralls pytest mock" - "curl http://www.cups.org/software/ipptool/ipptool-20130731-linux-ubuntu-x86_64.tar.gz | tar xvzf -" - "echo \"[main]\nipptool_path = \"$TRAVIS_BUILD_DIR\"/ipptool-20130731/ipptool\" > ~/.pyipptool.cfg" -script: "python setup.py test" +script: "coverage run --source=pyipptool setup.py test" + +after_success: + coveralls notifications: email: false diff --git a/README.rst b/README.rst index 65b0aa6..66f09ad 100644 --- a/README.rst +++ b/README.rst @@ -5,6 +5,10 @@ pyipptool https://travis-ci.org/ezeep/pyipptool.png?branch=master :target: https://travis-ci.org/ezeep/pyipptool +.. image:: https://coveralls.io/repos/ezeep/pyipptool/badge.png + :target: https://coveralls.io/r/ezeep/pyipptool + + Convenient ipp request generator to interrogate CUPS or IPP devices with the help of ipptool_. .. _ipptool: http://www.cups.org/documentation.php/doc-1.7/man-ipptool.html diff --git a/setup.py b/setup.py index 94b0dd0..ca85729 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,8 @@ def read_that_file(path): license=' Apache Software License', packages=('pyipptool',), install_requires=('deform',), - tests_require=('mock', 'pytest', 'pytest-cov'), + tests_require=('mock', 'pytest', 'coverage', + 'pytest-cov', 'coveralls'), include_package_data=True, test_suite='tests', cmdclass = {'test': PyTest},