# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

from __future__ import unicode_literals

from collections import namedtuple

import pytest
from simplesqlite.converter import RecordConvertor


attr_list_2 = ["attr_a", "attr_b"]
attr_list_3 = ["attr_a", "attr_b", "attr_c"]

NamedTuple2 = namedtuple("NamedTuple2", " ".join(attr_list_2))
NamedTuple3 = namedtuple("NamedTuple3", " ".join(attr_list_3))


class Test_RecordConvertor_to_record(object):
    @pytest.mark.parametrize(
        ["attr_names", "value", "expeted"],
        [
            [attr_list_2, [5, 6], [5, 6]],
            [attr_list_2, (5, 6), [5, 6]],
            [attr_list_2, {"attr_a": 5, "attr_b": 6}, [5, 6]],
            [attr_list_2, {"attr_a": 5, "attr_b": 6, "not_exist_attr": 100}, [5, 6]],
            [attr_list_2, {"attr_a": 5}, [5, None]],
            [attr_list_2, {"attr_b": 6}, [None, 6]],
            [attr_list_2, {}, [None, None]],
            [attr_list_2, NamedTuple2(5, 6), [5, 6]],
            [attr_list_2, NamedTuple2(5, None), [5, None]],
            [attr_list_2, NamedTuple2(None, 6), [None, 6]],
            [attr_list_2, NamedTuple2(None, None), [None, None]],
            [attr_list_2, NamedTuple3(5, 6, 7), [5, 6]],
            [attr_list_3, NamedTuple3(5, 6, 7), [5, 6, 7]],
        ],
    )
    def test_normal(self, attr_names, value, expeted):
        assert RecordConvertor.to_record(attr_names, value) == expeted

    @pytest.mark.parametrize(
        ["attr_names", "value", "expeted"],
        [[None, [5, 6], TypeError], [attr_list_2, None, ValueError], [None, None, TypeError]],
    )
    def test_exception(self, attr_names, value, expeted):
        with pytest.raises(expeted):
            RecordConvertor.to_record(attr_names, value)


class Test_RecordConvertor_to_records(object):
    @pytest.mark.parametrize(
        ["attr_names", "value", "expeted"],
        [
            [
                attr_list_2,
                [
                    [1, 2],
                    (3, 4),
                    {"attr_a": 5, "attr_b": 6},
                    {"attr_a": 7, "attr_b": 8, "not_exist_attr": 100},
                    {"attr_a": 9},
                    {"attr_b": 10},
                    {},
                    NamedTuple2(11, None),
                ],
                [[1, 2], [3, 4], [5, 6], [7, 8], [9, None], [None, 10], [None, None], [11, None]],
            ]
        ],
    )
    def test_normal(self, attr_names, value, expeted):
        assert RecordConvertor.to_records(attr_names, value) == expeted

    @pytest.mark.parametrize(
        ["attr_names", "value", "expeted"],
        [[None, [5, 6], TypeError], [attr_list_2, None, TypeError], [None, None, TypeError]],
    )
    def test_exception(self, attr_names, value, expeted):
        with pytest.raises(expeted):
            RecordConvertor.to_records(attr_names, value)

@@ -233,21 +233,17 @@
 r%0A%0A%0Aattr
-_list
+s
 _2 = %5B%22a
@@ -264,21 +264,17 @@
 b%22%5D%0Aattr
-_list
+s
 _3 = %5B%22a
@@ -355,21 +355,17 @@
 oin(attr
-_list
+s
 _2))%0ANam
@@ -414,21 +414,17 @@
 oin(attr
-_list
+s
 _3))%0A%0A%0Ac
@@ -565,29 +565,25 @@
        %5Battr
-_list
+s
 _2, %5B5, 6%5D, 
@@ -604,29 +604,25 @@
        %5Battr
-_list
+s
 _2, (5, 6), 
@@ -639,37 +639,33 @@
            %5Battr
-_list
+s
 _2, %7B%22attr_a%22: 5
@@ -698,37 +698,33 @@
            %5Battr
-_list
+s
 _2, %7B%22attr_a%22: 5
@@ -780,37 +780,33 @@
            %5Battr
-_list
+s
 _2, %7B%22attr_a%22: 5
@@ -833,29 +833,25 @@
        %5Battr
-_list
+s
 _2, %7B%22attr_b
@@ -882,29 +882,25 @@
        %5Battr
-_list
+s
 _2, %7B%7D, %5BNon
@@ -919,37 +919,33 @@
            %5Battr
-_list
+s
 _2, NamedTuple2(
@@ -969,37 +969,33 @@
            %5Battr
-_list
+s
 _2, NamedTuple2(
@@ -1025,37 +1025,33 @@
            %5Battr
-_list
+s
 _2, NamedTuple2(
@@ -1081,37 +1081,33 @@
            %5Battr
-_list
+s
 _2, NamedTuple2(
@@ -1147,29 +1147,25 @@
        %5Battr
-_list
+s
 _2, NamedTup
@@ -1204,21 +1204,17 @@
    %5Battr
-_list
+s
 _3, Name
@@ -1494,37 +1494,33 @@
 ypeError%5D, %5Battr
-_list
+s
 _2, None, ValueE
@@ -1877,21 +1877,17 @@
     attr
-_list
+s
 _2,%0A    
@@ -2609,13 +2609,9 @@
 attr
-_list
+s
 _2, 

Rename variables