Skip to content

Commit

Permalink
text_type -> string_type. add test codes
Browse files Browse the repository at this point in the history
  • Loading branch information
tribela committed Aug 16, 2013
1 parent da84593 commit f2980cd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
12 changes: 6 additions & 6 deletions libearth/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import datetime
import re

from .compat import text_type
from .compat import string_type
from .schema import Codec, DecodeError, EncodeError
from .tz import FixedOffset, utc

Expand Down Expand Up @@ -146,7 +146,7 @@ def encode(self, value):
if value is None:
return ""
else:
return str(value)
return str(int(value))

def decode(self, text):
if not self.PATTERN.match(text):
Expand Down Expand Up @@ -177,9 +177,9 @@ def encode(self, value):
if not isinstance(value, bool) and value is not None:
raise EncodeError("type of {0} must be bool".format(value))

true = (self.true if isinstance(self.true, text_type)
true = (self.true if isinstance(self.true, string_type)
else self.true[0])
false = (self.false if isinstance(self.true, text_type)
false = (self.false if isinstance(self.true, string_type)
else self.false[0])

if value is True:
Expand All @@ -190,9 +190,9 @@ def encode(self, value):
return None

def decode(self, text):
true = (self.true if not isinstance(self.true, text_type)
true = (self.true if not isinstance(self.true, string_type)
else [self.true])
false = (self.false if not isinstance(self.false, text_type)
false = (self.false if not isinstance(self.false, string_type)
else [self.false])

if text in true:
Expand Down
55 changes: 53 additions & 2 deletions tests/codecs_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import datetime

from pytest import mark
from pytest import mark, raises

from libearth.codecs import Rfc3339
from libearth.codecs import Boolean, Integer, Rfc3339
from libearth.schema import DecodeError, EncodeError
from libearth.tz import FixedOffset, utc


Expand Down Expand Up @@ -35,3 +36,53 @@ def test_rfc3339_encode(rfc3339_string, dt):
assert codec.encode(dt) == rfc3339_string
assert (Rfc3339(prefer_utc=True).encode(dt) ==
codec.encode(dt.astimezone(utc)))


def test_integer():
codec = Integer()
assert codec.encode(42) == "42"
assert codec.decode("42") == 42


def test_integer_raises():
codec = Integer()
with raises(EncodeError):
print(codec.encode("string"))
with raises(DecodeError):
print(codec.decode("aaa"))


def test_boolean():
codec = Boolean(true="true", false="false", default_value=False)

assert codec.encode(True) is "true"
assert codec.encode(False) is "false"

assert codec.decode("true") is True
assert codec.decode("false") is False
assert codec.decode(None) is False

with raises(EncodeError):
print(codec.encode("string"))

with raises(DecodeError):
print(codec.decode("another"))


def test_boolean_tuple():
true = ("true", "on", "yes")
false = ("false", "off", "no")

codec = Boolean(true=true, false=false, default_value=False)

assert codec.encode(True) in true
assert codec.encode(False) in false

assert codec.decode("on") is True
assert codec.decode("no") is False

with raises(EncodeError):
print(codec.encode(111))

with raises(DecodeError):
print(codec.decode("is one true?"))

0 comments on commit f2980cd

Please sign in to comment.