Skip to content

Commit

Permalink
tests: doctest addition
Browse files Browse the repository at this point in the history
* Adds doctests to ensure documentation examples works.

Signed-off-by: Lars Holm Nielsen <lars.holm.nielsen@cern.ch>
  • Loading branch information
lnielsen committed Jun 17, 2015
1 parent a7bf19c commit ffc3445
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 63 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ python:
- "3.4"

install:
- pip install coveralls pep257 Sphinx
- pip install coveralls pep257 Sphinx httpretty
- pip install pytest pytest-pep8 pytest-cov pytest-cache
- pip install -e .

script:
- pep257 cernservicexml
- "sphinx-build -qnNW docs docs/_build/html"
- python setup.py test
- "sphinx-build -qnNW -b doctest docs docs/_build/doctest"

after_success:
- coveralls
Expand Down
42 changes: 42 additions & 0 deletions cernservicexml/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,59 @@

from __future__ import absolute_import, print_function, unicode_literals

import functools
import sys

# Useful for very coarse version differentiation.
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
PY34 = sys.version_info[0:2] >= (3, 4)

if PY3: # pragma: no cover
string_types = str,
text_type = str
binary_type = bytes
from io import StringIO
else: # pragma: no cover
string_types = basestring,
text_type = unicode
binary_type = str
from StringIO import StringIO


def import_httpretty(): # pragma: no cover
"""Import HTTPretty and monkey patch Python 3.4 issue.
See https://github.com/gabrielfalcao/HTTPretty/pull/193 and
as well as https://github.com/gabrielfalcao/HTTPretty/issues/221.
"""
if not PY34:
import httpretty
else:
import socket
old_SocketType = socket.SocketType

import httpretty
from httpretty import core

def sockettype_patch(f):
@functools.wraps(f)
def inner(*args, **kwargs):
f(*args, **kwargs)
socket.SocketType = old_SocketType
socket.__dict__['SocketType'] = old_SocketType
return inner

core.httpretty.disable = sockettype_patch(
httpretty.httpretty.disable
)

return httpretty

__all__ = (
'binary_type',
'import_httpretty',
'string_types',
'StringIO',
'text_type',
)
10 changes: 4 additions & 6 deletions cernservicexml/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
Usage:
.. code-block:: python
>>> from cernservicexml import ServiceDocument
>>> doc = ServiceDocument('zenodo')
>>> doc.add_numericvalue('users', 1000, description="Number of users")
>>> xml = doc.to_xml()
>>> from cernservicexml import ServiceDocument
>>> doc = ServiceDocument('zenodo')
>>> doc.add_numericvalue('users', 1000, desc="Number of users")
>>> xml = doc.to_xml()
"""

from __future__ import absolute_import, print_function, unicode_literals
Expand Down
22 changes: 17 additions & 5 deletions cernservicexml/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,25 @@

"""Publishing of service documents.
Usage:
.. testsetup:: *
from cernservicexml._compat import import_httpretty
httpretty = import_httpretty()
httpretty.enable()
httpretty.register_uri(httpretty.POST, 'http://xsls.cern.ch',
body="", status=200)
.. testcleanup::
.. code-block:: python
httpretty.disable()
Usage:
>>> from cernservicexml import ServiceDocument, XSLSPublisher
>>> doc = ServiceDocument('myserviceid')
>>> XSLSPublisher.send(doc)
>>> from cernservicexml import ServiceDocument, XSLSPublisher
>>> doc = ServiceDocument('myserviceid')
>>> XSLSPublisher.send(doc)
<Response [200]>
"""

from __future__ import absolute_import, print_function, unicode_literals
Expand Down
48 changes: 0 additions & 48 deletions tests/helpers.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from decimal import Decimal
from os.path import dirname, join

from StringIO import StringIO
import pytest
from cernservicexml import ServiceDocument
from cernservicexml._compat import StringIO
from lxml import etree

schema = etree.XMLSchema(file=join(dirname(__file__), 'xsls_schema.xsd'))
Expand Down
3 changes: 1 addition & 2 deletions tests/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
from datetime import datetime

from cernservicexml import ServiceDocument, XSLSPublisher

from helpers import import_httpretty
from cernservicexml._compat import import_httpretty

httpretty = import_httpretty()

Expand Down

0 comments on commit ffc3445

Please sign in to comment.