Skip to content

Commit

Permalink
Merge b45a823 into 3f1691a
Browse files Browse the repository at this point in the history
  • Loading branch information
lnielsen committed Jun 17, 2015
2 parents 3f1691a + b45a823 commit ff46996
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 76 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
32 changes: 32 additions & 0 deletions cernservicexml/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

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,
Expand All @@ -25,3 +27,33 @@
string_types = basestring,
text_type = unicode
binary_type = str


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
11 changes: 4 additions & 7 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 Expand Up @@ -105,5 +103,4 @@ def to_xml(self):
if isinstance(res, binary_type):
res = res.decode('utf-8')

print(type(res))
return res
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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def run_tests(self):
'coverage<4.0a1',
'httpretty>=0.8.0',
'mock>=1.0',
'lxml>=3.4',
]

setup(
Expand Down
48 changes: 0 additions & 48 deletions tests/helpers.py

This file was deleted.

17 changes: 15 additions & 2 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@

from datetime import datetime
from decimal import Decimal
from os.path import dirname, join
from StringIO import StringIO

import pytest
from cernservicexml import ServiceDocument
from lxml import etree

schema = etree.XMLSchema(file=join(dirname(__file__), 'xsls_schema.xsd'))


def validate_xsd(xmlstr):
"""Assert if xml is valid according to XSD schema."""
schema.assertValid(etree.parse(StringIO(xmlstr)))
return True


def test_creation_simple():
Expand All @@ -25,8 +36,7 @@ def test_creation_simple():
assert isinstance(doc.timestamp, datetime)
assert doc.availability == 100

print(doc.to_xml())

assert validate_xsd(doc.to_xml())
assert doc.to_xml() == \
'<serviceupdate xmlns="http://sls.cern.ch/SLS/XML/update">' \
'<id>myserviceid</id>' \
Expand All @@ -44,6 +54,7 @@ def test_creation_simple():
'<availability>99</availability>' \
'<timestamp>2015-01-01T00:00:00</timestamp>' \
'</serviceupdate>'
assert validate_xsd(doc.to_xml())


def test_creation_attrs():
Expand All @@ -68,6 +79,7 @@ def test_creation_attrs():
'<webpage>http://example.org</webpage>' \
'<availabilityinfo>Extra info</availabilityinfo>' \
'</serviceupdate>'
assert validate_xsd(doc.to_xml())


def test_outofbounds():
Expand Down Expand Up @@ -116,3 +128,4 @@ def test_numericvalue():
'<numericvalue desc="a desc" name="val5">1234</numericvalue>' \
'</data>' \
'</serviceupdate>'
assert validate_xsd(doc.to_xml())
14 changes: 1 addition & 13 deletions tests/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,13 @@
from datetime import datetime

from cernservicexml import ServiceDocument, XSLSPublisher
from cernservicexml._compat import binary_type

from helpers import import_httpretty
from cernservicexml._compat import import_httpretty

httpretty = import_httpretty()

EXAMPLE_URL = "http://xsls.example.org"


# @patch("datacite.request.requests")
# def test_connection_error(requests):
# """Test connection error."""
# requests.get.side_effect = ConnectionError()

# c = get_client()
# with pytest.raises(DataCiteHttpError):
# c.doi_get("10.1234/foo.bar")


@httpretty.activate
def test_xslspublisher_send():
"""."""
Expand Down
77 changes: 77 additions & 0 deletions tests/xsls_schema.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
# This file is part of CERN Service XML
# Copyright (C) 2015 CERN.
#
# CERN Service XML is free software; you can redistribute it and/or modify
# it under the terms of the Revised BSD License; see LICENSE file for
# more details.
-->
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://sls.cern.ch/SLS/XML/update"
targetNamespace="http://sls.cern.ch/SLS/XML/update"
elementFormDefault="qualified"
attributeFormDefault="unqualified">

<!-- Types -->
<xs:simpleType name="singleId">
<xs:restriction base="xs:string">
<xs:pattern value="([a-zA-Z0-9\.\-_])+" />
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="stringNonEmpty">
<xs:restriction base="xs:string">
<xs:minLength value="1" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="percentage">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="0" />
<xs:maxInclusive value="100" />
</xs:restriction>
</xs:simpleType>

<!-- Schema -->
<xs:element name="serviceupdate">
<xs:complexType>
<xs:all>
<!-- Mandatory fields -->
<xs:element name="id" type="tns:singleId" />
<xs:element name="timestamp" type="xs:dateTime" />
<xs:element name="availability" type="tns:percentage" />

<!-- Optional fields -->
<xs:element name="availabilityinfo" minOccurs="0" maxOccurs="1">
<xs:complexType mixed="true">
<xs:sequence>
<xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="availabilitydesc" type="tns:stringNonEmpty" minOccurs="0" maxOccurs="1" />
<xs:element name="contact" type="tns:stringNonEmpty" minOccurs="0" maxOccurs="1"/>
<xs:element name="webpage" type="xs:anyURI" minOccurs="0" maxOccurs="1"/>

<xs:element name="data" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="numericvalue" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="name" type="tns:stringNonEmpty" use="required" />
<xs:attribute name="desc" type="tns:stringNonEmpty" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>

0 comments on commit ff46996

Please sign in to comment.