Skip to content

Commit

Permalink
tree-wide: fixes after upgrade to iso8601==0.1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
ngnpope committed Sep 6, 2017
1 parent 8e045a1 commit 2b1691d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 38 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,4 +1,4 @@
iso8601
iso8601>=0.1.12
jinja2
lxml
pythonic_testcase
Expand Down
11 changes: 4 additions & 7 deletions soapfish/xsd.py
Expand Up @@ -59,9 +59,6 @@
from .utils import timezone_offset_to_string
from .xsd_types import XSDDate

# TODO: Change import we update to iso8601 > 0.1.11 (fixed in 031688e)
from iso8601.iso8601 import UTC, FixedOffset # isort:skip


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -291,7 +288,7 @@ def xmlvalue(self, value):
tz = getattr(value, 'tzinfo', None)
if not tz:
return timestring_without_tz
utc_offset = tz.utcoffset(value)
utc_offset = tz.utcoffset(None)
formatted_tz = timezone_offset_to_string(utc_offset)
return timestring_without_tz + formatted_tz

Expand All @@ -315,12 +312,12 @@ def _parse_tz(self, match):
if not offset_string:
return None
elif offset_string == 'Z':
return UTC
return iso8601.UTC

sign = 1 if (match.group('tz_sign') == '+') else -1
offset_hours = sign * int(match.group('tz_hour'))
offset_minutes = sign * int(match.group('tz_minute'))
return FixedOffset(offset_hours, offset_minutes, name='UTC' + offset_string)
return iso8601.FixedOffset(offset_hours, offset_minutes, name='UTC' + offset_string)


class DateTime(SimpleType):
Expand Down Expand Up @@ -378,7 +375,7 @@ def xmlvalue(self, value):
tz = value.tzinfo
if not tz:
return timestring_without_tz
utc_offset = tz.utcoffset(value)
utc_offset = tz.utcoffset(None)
formatted_tz = timezone_offset_to_string(utc_offset)
return timestring_without_tz + formatted_tz

Expand Down
23 changes: 7 additions & 16 deletions tests/xsd/date_test.py
Expand Up @@ -2,16 +2,14 @@

from datetime import date, datetime, timedelta, tzinfo

import iso8601
from lxml import etree
from pythonic_testcase import assert_equals, assert_raises

from soapfish import xsd
from soapfish.testutil import SimpleTypeTestCase
from soapfish.xsd_types import XSDDate

# TODO: Change import we update to iso8601 > 0.1.11 (fixed in 031688e)
from iso8601.iso8601 import FixedOffset, UTC # isort:skip


class DateTest(SimpleTypeTestCase):
xsd_type = xsd.Date
Expand All @@ -24,7 +22,7 @@ def test_can_render_python_date(self):
assert_equals(b'<foo><bar>2001-10-26</bar></foo>', xml)

def test_rendering_timezones(self):
date_ = XSDDate(2001, 10, 26, tzinfo=FixedOffset(1, 15, 'dummy zone'))
date_ = XSDDate(2001, 10, 26, tzinfo=iso8601.FixedOffset(1, 15, 'dummy zone'))
rendered_xml = self.xsd_type().xmlvalue(date_)
assert_equals('2001-10-26+01:15', rendered_xml)

Expand All @@ -37,13 +35,13 @@ def test_parsing_utctimezone(self):
class Test(xsd.ComplexType):
datetime_ = xsd.Element(xsd.DateTime, tagname='datetime')
parsed = Test.parsexml('<root><datetime>2011-06-30T00:19:00+0000</datetime></root>')
assert_equals(datetime(2011, 6, 30, 0, 19, 0, tzinfo=UTC), parsed.datetime_)
assert_equals(datetime(2011, 6, 30, 0, 19, 0, tzinfo=iso8601.UTC), parsed.datetime_)

def test_parsing_timezone(self):
class Test(xsd.ComplexType):
datetime_ = xsd.Element(xsd.DateTime, tagname='datetime')
parsed = Test.parsexml('<root><datetime>2011-06-30T20:19:00+01:00</datetime></root>')
assert_equals(datetime(2011, 6, 30, 20, 19, 0, tzinfo=FixedOffset(1, 0, '+01:00')), parsed.datetime_)
assert_equals(datetime(2011, 6, 30, 20, 19, 0, tzinfo=iso8601.FixedOffset(1, 0, '+01:00')), parsed.datetime_)

def test_can_correctly_determine_utc_offset(self):
# Ensure that the DateTime type really uses the correct UTC offset
Expand Down Expand Up @@ -75,18 +73,11 @@ def test_parsing(self):
self.assert_parse(None, None)
self.assert_parse(None, 'nil')
self.assert_parse(XSDDate(2012, 10, 26), '2012-10-26')
self.assert_parse(XSDDate(2016, 2, 29, tzinfo=UTC), '2016-02-29Z')
self.assert_parse(XSDDate(2016, 2, 29, tzinfo=iso8601.UTC), '2016-02-29Z')
parsed_date = self._parse('2012-02-29+01:00')
assert_equals(date(2012, 2, 29), parsed_date.as_datetime_date())
self.assert_same_tz(FixedOffset(1, 0, '+01:00'), parsed_date.tzinfo)
assert_equals(iso8601.FixedOffset(1, 0, '+01:00').utcoffset(None), parsed_date.tzinfo.utcoffset(None))

parsed_date = self._parse('2012-02-29-02:30')
assert_equals(date(2012, 2, 29), parsed_date.as_datetime_date())
self.assert_same_tz(FixedOffset(-2, -30, '-02:30'), parsed_date.tzinfo)

# --- custom assertions ---------------------------------------------------
def assert_same_tz(self, tz, other_tz):
assert_equals(
(tz._FixedOffset__offset_hours, tz._FixedOffset__offset_minutes),
(other_tz._FixedOffset__offset_hours, other_tz._FixedOffset__offset_minutes),
)
assert_equals(iso8601.FixedOffset(-2, -30, '-02:30').utcoffset(None), parsed_date.tzinfo.utcoffset(None))
14 changes: 6 additions & 8 deletions tests/xsd/time_test.py
Expand Up @@ -2,15 +2,13 @@

from datetime import time

import iso8601
from lxml import etree
from pythonic_testcase import assert_equals, assert_raises

from soapfish import xsd
from soapfish.testutil import SimpleTypeTestCase

# TODO: Change import we update to iso8601 > 0.1.11 (fixed in 031688e)
from iso8601.iso8601 import FixedOffset, UTC # isort:skip


class TimeTest(SimpleTypeTestCase):
xsd_type = xsd.Time
Expand All @@ -23,7 +21,7 @@ def test_can_render_python_time(self):
assert_equals(b'<foo><bar>23:59:59</bar></foo>', xml)

def test_rendering_timezones(self):
time_ = time(10, 15, 20, tzinfo=FixedOffset(1, 15, 'dummy zone'))
time_ = time(10, 15, 20, tzinfo=iso8601.FixedOffset(1, 15, 'dummy zone'))
rendered_xml = self.xsd_type().xmlvalue(time_)
assert_equals('10:15:20+01:15', rendered_xml)

Expand All @@ -36,13 +34,13 @@ def test_parsing_utctimezone(self):
class Test(xsd.ComplexType):
time_ = xsd.Element(self.xsd_type, tagname='time')
parsed = Test.parsexml('<root><time>00:19:00Z</time></root>')
assert_equals(time(0, 19, 0, tzinfo=UTC), parsed.time_)
assert_equals(time(0, 19, 0, tzinfo=iso8601.UTC), parsed.time_)

def test_parsing_timezone(self):
class Test(xsd.ComplexType):
time_ = xsd.Element(self.xsd_type, tagname='time')
parsed = Test.parsexml('<root><time>20:19:00+01:00</time></root>')
assert_equals(time(20, 19, 0, tzinfo=FixedOffset(1, 0, '+01:00')), parsed.time_)
assert_equals(time(20, 19, 0, tzinfo=iso8601.FixedOffset(1, 0, '+01:00')), parsed.time_)

def test_accepts_only_compatible_types(self):
self.assert_can_set(None)
Expand All @@ -56,6 +54,6 @@ def test_parsing(self):
self.assert_parse(None, None)
self.assert_parse(None, 'nil')
parsed_time = self._parse('23:59:59+01:00')
assert_equals(time(23, 59, 59, tzinfo=FixedOffset(1, 0, '+01:00')), parsed_time)
assert_equals(time(23, 59, 59, tzinfo=iso8601.FixedOffset(1, 0, '+01:00')), parsed_time)
parsed_time = self._parse('23:59:59-02:30')
assert_equals(time(23, 59, 59, tzinfo=FixedOffset(-2, -30, '-02:30')), parsed_time)
assert_equals(time(23, 59, 59, tzinfo=iso8601.FixedOffset(-2, -30, '-02:30')), parsed_time)
9 changes: 3 additions & 6 deletions tests/xsd/xsd_datetime_test.py
@@ -1,14 +1,11 @@
import unittest
from datetime import datetime, timedelta, tzinfo

import iso8601
from lxml import etree

from soapfish import xsd

# TODO: Change import on update to iso8601 > 0.1.11 (fixed in 031688e)
from iso8601.iso8601 import FixedOffset, UTC # isort:skip


class DatetimeTest(unittest.TestCase):

def test_rendering(self):
Expand All @@ -24,7 +21,7 @@ def test_rendering(self):
self.assertEqual(expected_xml, xml)

def test_rendering_timezones(self):
fake_tz = FixedOffset(1, 15, 'dummy zone')
fake_tz = iso8601.FixedOffset(1, 15, 'dummy zone')
dt = datetime(2001, 10, 26, 21, 32, 52, tzinfo=fake_tz)
rendered_xml = xsd.DateTime().xmlvalue(dt)
self.assertEqual('2001-10-26T21:32:52+01:15', rendered_xml)
Expand All @@ -46,7 +43,7 @@ class Test(xsd.ComplexType):
datetime = xsd.Element(xsd.DateTime)
XML = '<root><datetime>2011-06-30T20:19:00+01:00</datetime></root>'
test = Test.parsexml(XML)
self.assertEqual(datetime(2011, 6, 30, 19, 19, 0), test.datetime.astimezone(UTC).replace(tzinfo=None))
self.assertEqual(datetime(2011, 6, 30, 19, 19, 0), test.datetime.astimezone(iso8601.UTC).replace(tzinfo=None))

def test_can_correctly_determine_utc_offset(self):
# Ensure that the DateTime type really uses the correct UTC offset
Expand Down

0 comments on commit 2b1691d

Please sign in to comment.