Source

redismap / tests / converters.py

Full commit
# -*- coding: utf-8 -*-
"""
Tests for `redismap.converters`.
"""
from attest import Tests, raises, assert_hook
from datetime import datetime, date
from decimal import Decimal as decimal
from redismap import (UTF8, Bytes, Integer, Boolean, DateTime, Float, Decimal,
                      Timestamp, Date, UTF16, TextConverter)

convertertests = Tests()

@convertertests.test
def test_utf8():
    # encoding
    assert UTF8.to_redis(u"Hello") == "Hello"
    assert UTF8.to_redis(u"¿Cómo estás?") == "\xc2\xbfC\xc3\xb3mo est\xc3\xa1s?"
    assert UTF8.to_redis("Hello") == "Hello"
    # decoding
    assert UTF8.from_redis("Hello") == u"Hello"
    assert UTF8.from_redis("\xc2\xbfC\xc3\xb3mo est\xc3\xa1s?") == u"¿Cómo estás?"
    assert isinstance(UTF8.from_redis("Hello"), unicode)


@convertertests.test
def test_utf16():
    neko = u"ねこ"
    neko_encoded = neko.encode("utf16") # avoids byte order issues
    neko_le = "\xff\xfem0S0"
    neko_be = "\xfe\xff0m0S"
    # encoding
    assert UTF16.to_redis(neko) == neko_encoded
    assert UTF16.to_redis(neko_encoded) == neko_encoded
    assert UTF16.to_redis(neko_le) == neko_le
    assert UTF16.to_redis(neko_be) == neko_be
    with raises(TypeError):
        UTF16.to_redis("Hello")   # not valid UTF-16
    # decoding
    assert UTF16.from_redis(neko_encoded) == neko
    assert UTF16.from_redis(neko_le) == neko
    assert UTF16.from_redis(neko_be) == neko


@convertertests.test
def test_textconverter():
    Latin1 = TextConverter("latin1")
    como = u"¿Cómo estás?"
    como_encoded = "\xbfC\xf3mo est\xe1s?"
    neko = u"ねこ"
    # encoding
    assert Latin1.to_redis(u"Hello") == "Hello"
    assert Latin1.to_redis(como) == como_encoded
    assert Latin1.to_redis(como_encoded) == como_encoded
    with raises(UnicodeEncodeError):
        Latin1.to_redis(neko)
    # decoding
    assert Latin1.from_redis(como_encoded) == como
    assert isinstance(Latin1.from_redis(como_encoded), unicode)
    assert Latin1.from_redis(como_encoded) == como


@convertertests.test
def test_bytes():
    # encoding
    assert Bytes.to_redis("Hello") == "Hello"
    assert Bytes.to_redis(u"Hello") == "Hello"
    assert Bytes.to_redis(1) == "1"
    # decoding
    assert Bytes.from_redis("Hello") == "Hello"
    assert Bytes.from_redis("1") == "1"


SUPER_LONG = 2 ** 65

@convertertests.test
def test_integer():
    # encoding
    assert Integer.to_redis(42) == "42"
    assert Integer.to_redis(42L) == "42"
    assert Integer.to_redis(-31) == "-31"
    assert Integer.to_redis(SUPER_LONG) == str(SUPER_LONG)
    # decoding
    assert Integer.from_redis("42") == 42
    assert Integer.from_redis("-31") == -31
    assert Integer.from_redis(str(SUPER_LONG)) == SUPER_LONG
    # zscore encoding
    assert Integer.to_zscore(32) == 32.0
    assert Integer.to_zscore(SUPER_LONG) == float(SUPER_LONG)
    # zscore decoding
    assert Integer.from_zscore(32.0) == 32
    assert Integer.from_zscore(19.5) == 19


@convertertests.test
def test_float():
    # encoding
    assert Float.to_redis(42.0) == "42.0"
    assert Float.to_redis(30) == "30.0"
    assert Float.to_redis("19.95") == "19.95"
    # decoding
    assert Float.from_redis("42.0") == 42.0
    assert Float.from_redis("19.95") == 19.95
    # zscore encoding
    assert Float.to_zscore(42.0) == 42.0
    assert Float.to_zscore(30) == 30.0
    assert Float.to_zscore("19.95") == 19.95
    # zscore decoding
    assert Float.from_zscore(42.0) == 42.0
    assert Float.from_zscore(19.95) == 19.95


@convertertests.test
def test_decimal():
    # encoding
    assert Decimal.to_redis(decimal("42.00")) == "42.00"
    assert Decimal.to_redis(42) == "42"
    # decoding
    assert Decimal.from_redis("42.00") == decimal("42.00")
    assert Decimal.from_redis("42") == decimal("42")


@convertertests.test
def test_boolean():
    # encoding
    assert Boolean.to_redis(True) == "1"
    assert Boolean.to_redis(False) == "0"
    assert Boolean.to_redis("0") == "1"
    assert Boolean.to_redis(None) == "0"
    # decoding
    assert Boolean.from_redis("1") is True
    assert Boolean.from_redis("0") is False
    with raises(ValueError):
        Boolean.from_redis("True")


@convertertests.test
def test_datetime():
    march = datetime(2011, 3, 1)
    january = datetime(2008, 1, 10, 17, 43, 20)
    moonlanding = datetime(1969, 7, 20, 22, 56) # EDT
    # encoding
    assert DateTime.to_redis(march) == "2011-03-01T00:00:00"
    assert DateTime.to_redis(1200005000) == "2008-01-10T17:43:20"
    assert DateTime.to_redis(moonlanding) == "1969-07-20T22:56:00"
    with raises(TypeError):
        DateTime.to_redis(date.today())
    # decoding
    assert DateTime.from_redis("2011-03-01T00:00:00") == march
    assert DateTime.from_redis("2008-01-10T17:43:20") == january
    assert DateTime.from_redis("1969-07-20T22:56:00") == moonlanding
    # zscore encoding
    assert DateTime.to_zscore(march) == 1298955600.0
    assert DateTime.to_zscore(1200005000) == 1200005000.0
    assert DateTime.to_zscore(moonlanding) == -14159040.0
    # zscore decoding
    assert DateTime.from_zscore(1298955600.0) == march
    assert DateTime.from_zscore(1200005000.0) == january
    assert DateTime.from_zscore(-14159040.0) == moonlanding


@convertertests.test
def test_timestamp():
    march = datetime(2011, 3, 1)
    january = datetime(2008, 1, 10, 17, 43, 20)
    moonlanding = datetime(1969, 7, 20, 22, 56) # EDT
    # encoding
    assert Timestamp.to_redis(march) == "1298955600"
    assert Timestamp.to_redis(1200005000) == "1200005000"
    assert Timestamp.to_redis(moonlanding) == "-14159040"
    with raises(TypeError):
        Timestamp.to_redis(date.today())
    # decoding
    assert Timestamp.from_redis("1298955600") == march
    assert Timestamp.from_redis("1200005000") == january
    assert Timestamp.from_redis("-14159040") == moonlanding
    # zscore encoding
    assert Timestamp.to_zscore(march) == 1298955600.0
    assert Timestamp.to_zscore(1200005000) == 1200005000.0
    assert Timestamp.to_zscore(moonlanding) == -14159040.0
    # zscore decoding
    assert Timestamp.from_zscore(1298955600.0) == march
    assert Timestamp.from_zscore(1200005000.0) == january
    assert Timestamp.from_zscore(-14159040.0) == moonlanding


@convertertests.test
def test_date():
    piday = date(2011, 3, 14)
    christmas = date(2011, 12, 25)
    moonlanding = date(1969, 7, 20)
    # encoding
    assert Date.to_redis(piday) == "2011-03-14"
    assert Date.to_redis(christmas) == "2011-12-25"
    assert Date.to_redis(moonlanding) == "1969-07-20"
    # decoding
    assert Date.from_redis("2011-03-14") == piday
    assert Date.from_redis("2011-12-25") == christmas
    assert Date.from_redis("1969-07-20") == moonlanding
    # zscore encoding
    assert Date.to_zscore(piday) == 734210.0
    assert Date.to_zscore(moonlanding) == 718998.0
    # zscore decoding
    assert Date.from_zscore(734210.0) == piday
    assert Date.from_zscore(718998.0) == moonlanding