# -*- coding: utf-8 -*-


from __future__ import unicode_literals

from datetime import datetime

from dateutil.tz import tz

import neon

NEON_ENTITY = """
entity: Column(something, type=int)
"""


def test_entity():
    expected = {"entity": neon.entity.Entity("Column", {0: "something", "type": "int"})}
    assert neon.decode(NEON_ENTITY) == expected


NEON_TYPES = """
string: "a () #' text"
integer: 5902
hexint: 0xAA
float: 5.234
floatbig: 5e10
nones: [NULL, null, Null]
bools: [TRUE, True, true, YES, Yes, yes, ON, On, on,
        FALSE, False, false, NO, No, no, OFF, Off, off]
"""


def test_types():
    expected = {
        "string": "a () #' text",
        "integer": 5902,
        "hexint": 0xAA,
        "float": 5.234,
        "floatbig": 5e10,
        "nones": [None] * 3,
        "bools": [True] * 9 + [False] * 9,
    }
    result = neon.decode(NEON_TYPES)
    for key in expected:
        assert result[key] == expected[key]


NEON_DATETIME = """
- 2013-04-23 13:24:55.123456+0000
- 2015-01-20
- 2015-5-10
"""


def test_datetime():
    expected = [
        datetime(2013, 4, 23, 13, 24, 55, 123456, tzinfo=tz.tzutc()),
        datetime(2015, 1, 20),
        datetime(2015, 5, 10),
    ]
    assert neon.decode(NEON_DATETIME) == expected


def test_string_escaping():
    assert neon.decode('key: "msg"') == {"key": "msg"}
    assert neon.decode(r'key: "msg \" end"') == {"key": 'msg " end'}
    assert neon.decode(r"key: 'msg \' end'") == {"key": "msg ' end"}
    assert neon.decode(r'src: "\\usr\\share"') == {"src": r"\usr\share"}

@@ -1359,17 +1359,16 @@
 .decode(
-r
 'key: %22m
@@ -1370,16 +1370,17 @@
 y: %22msg 
+%5C
 %5C%22 end%22'
@@ -1428,17 +1428,16 @@
 .decode(
-r
 %22key: 'm
@@ -1439,16 +1439,17 @@
 y: 'msg 
+%5C
 %5C' end'%22
@@ -1497,17 +1497,16 @@
 .decode(
-r
 'src: %22%5C
@@ -1510,13 +1510,17 @@
  %22%5C%5C
+%5C
+%5C
 usr%5C%5C
+%5C%5C
 shar
@@ -1539,15 +1539,16 @@
 c%22: 
-r
 %22
+%5C
 %5Cusr%5C
+%5C
 shar

Apply suggestions from code review