# -*- coding: utf-8 -*-
# Monary - Copyright 2011-2014 David J. C. Beach
# Please see the included LICENSE.TXT and NOTICE.TXT for licensing information.

import pymongo

import monary


expected = ["aあ".decode('utf-8'),
            "âéÇ".decode('utf-8'),
            "αλΩ".decode('utf-8'),
            "çœ¥¨≠".decode('utf-8')]


def setup():
    with pymongo.MongoClient() as c:
        c.drop_database("monary_test")
        c.monary_test.data.insert(
            {"test": my_str, "sequence": i}
            for i, my_str in enumerate(expected))


def teardown():
    with pymongo.MongoClient() as c:
        c.drop_database("monary_test")


def test_utf8():
    with monary.Monary("127.0.0.1") as m:
        data, lens, sizes = m.query(
            "monary_test", "data", {}, ["test", "test", "test"],
            ["string:12", "length", "size"], sort="sequence")
        assert (lens < sizes).all()

    for monary_bytes, monary_len, expected_str, in zip(data, lens, expected):
        monary_str = monary_bytes.decode('utf8')

        # We got the same string out from Monary as we inserted w/ PyMongo.
        assert monary_str == expected_str

        # Monary's idea of "length" == len(string).
        assert monary_len == len(expected_str)

@@ -147,16 +147,28 @@
 ation.%0A%0A
+import sys%0A%0A
 import p
@@ -211,131 +211,157 @@
 %22a%E3%81%82%22
-.decode('utf-8'),%0A            %22%C3%A2%C3%A9%C3%87%22.decode('utf-8'),%0A            %22%CE%B1%CE%BB%CE%A9%22.decode('utf-8'),%0A            %22%C3%A7%C5%93%C2%A5%C2%A8%E2%89%A0%22.decode('utf-8')
+, %22%C3%A2%C3%A9%C3%87%22, %22%CE%B1%CE%BB%CE%A9%22, %22%C3%A7%C5%93%C2%A5%C2%A8%E2%89%A0%22%5D%0Aif sys.version_info%5B0%5D %3C 3:%0A    # Python 2: convert from str to unicode.%0A    expected = %5Bs.decode('utf-8') for s in expected
 %5D%0A%0A%0A

Fix test_utf8 for Python 3.