<commit_before># encoding: utf-8

"""
.. codeauthor:: <NAME> <<EMAIL>>
"""

from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import collections
from decimal import Decimal
import itertools

from pytablereader import TableData
import pytest

import pytablereader as ptr
import pytablewriter as ptw

from .data import (
    header_list,
    value_matrix,
    mix_header_list,
    mix_value_matrix,
    value_matrix_iter
)


inf = Decimal('Infinity')
nan = None

Data = collections.namedtuple("Data", "table header value expected")

normal_test_data_list = [
    Data(
        table="tablename",
        header=header_list,
        value=value_matrix,
        expected=TableData(
            "tablename",
            ["a", "b", "c", "dd", "e"],
            [
                [1, 123.1, "a", 1,   1],
                [2, 2.2, "bb", 2.2, 2.2],
                [3, 3.3, "ccc", 3,   "cccc"],
            ])
    ),
    Data(
        table="mix_data",
        header=mix_header_list,
        value=mix_value_matrix,
        expected=TableData(
            "mix_data",
            [
                'i', 'f', 'c', 'if', 'ifc', 'bool',
                'inf', 'nan', 'mix_num', 'time',
            ],
            [
                [
                    1, "1.1", 'aa', 1, 1, 1, inf,
                    nan, 1, '2017-01-01T00:00:00',
                ],
                [
                    2, "2.2", 'bbb', "2.2", "2.2", 0, inf, nan,
                    inf, '2017-01-02 03:04:05+09:00',
                ],
                [
                    3, "3.33", 'cccc', -3, 'ccc', 1, inf,
                    nan, nan, '2017-01-01T00:00:00',
                ],
            ])
    ),
    Data(
        table="infnan",
        header=["inf", "nan"],
        value=[
            [inf, float("nan")],
            ["inf", "nan"],
            ["INF", "NAN"],
            ["INFINITY", "inf"],
        ],
        expected=TableData(
            "infnan",
            ["inf", "nan"],
            [
                [inf, nan],
                [inf, nan],
                [inf, nan],
                [inf, inf],
            ]
        )
    ),
]

exception_test_data_list = [
    Data(
        table="",
        header=header_list,
        value=value_matrix,
        expected=ptw.EmptyTableNameError
    ),
    Data(
        table="dummy",
        header=[],
        value=value_matrix,
        expected=ptw.EmptyHeaderError
    ),
    Data(
        table="dummy",
        header=None,
        value=value_matrix,
        expected=ptw.EmptyHeaderError
    ),
]


class Test_SqliteTableWriter_write_table:

    @pytest.mark.parametrize(
        ["table", "header", "value", "expected"],
        [
            [data.table, data.header, data.value, data.expected]
            for data in normal_test_data_list
        ]
    )
    def test_normal(self, tmpdir, table, header, value, expected):
        test_file_path = tmpdir.join("test.sqlite")

        writer = ptw.SqliteTableWriter()
        writer.open(str(test_file_path))
        writer.table_name = table
        writer.header_list = header
        writer.value_matrix = value
        writer.write_table()
        writer.close()

        print("!!", value)

        loader = ptr.SqliteFileLoader(str(test_file_path))

        for tabledata in loader.load():
            #expected_dump = ptw.dump_tabledata(expected)
            #actual_dump = ptw.dump_tabledata(tabledata)<commit_msg>Add test cases<commit_after># encoding: utf-8

"""
.. codeauthor:: <NAME> <<EMAIL>>
"""

from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import collections
from decimal import Decimal
import itertools

from pytablereader import TableData
import pytest

import pytablereader as ptr
import pytablewriter as ptw

from .data import (
    header_list,
    value_matrix,
    mix_header_list,
    mix_value_matrix,
    value_matrix_iter
)


inf = Decimal('Infinity')
nan = None

Data = collections.namedtuple("Data", "table header value expected")

normal_test_data_list = [
    Data(
        table="tablename",
        header=header_list,
        value=value_matrix,
        expected=TableData(
            "tablename",
            ["a", "b", "c", "dd", "e"],
            [
                [1, 123.1, "a", 1,   1],
                [2, 2.2, "bb", 2.2, 2.2],
                [3, 3.3, "ccc", 3,   "cccc"],
            ])
    ),
    Data(
        table="mix_data",
        header=mix_header_list,
        value=mix_value_matrix,
        expected=TableData(
            "mix_data",
            [
                'i', 'f', 'c', 'if', 'ifc', 'bool',
                'inf', 'nan', 'mix_num', 'time',
            ],
            [
                [
                    1, "1.1", 'aa', 1, 1, 1, inf,
                    nan, 1, '2017-01-01T00:00:00',
                ],
                [
                    2, "2.2", 'bbb', "2.2", "2.2", 0, inf, nan,
                    inf, '2017-01-02 03:04:05+09:00',
                ],
                [
                    3, "3.33", 'cccc', -3, 'ccc', 1, inf,
                    nan, nan, '2017-01-01T00:00:00',
                ],
            ])
    ),
    Data(
        table="infnan",
        header=["inf", "nan"],
        value=[
            [inf, float("nan")],
            ["inf", "nan"],
            ["INF", "NAN"],
            ["INFINITY", "inf"],
        ],
        expected=TableData(
            "infnan",
            ["inf", "nan"],
            [
                [inf, nan],
                [inf, nan],
                [inf, nan],
                [inf, inf],
            ]
        )
    ),
]

exception_test_data_list = [
    Data(
        table="",
        header=header_list,
        value=value_matrix,
        expected=ptw.EmptyTableNameError
    ),
    Data(
        table="dummy",
        header=[],
        value=value_matrix,
        expected=ptw.EmptyHeaderError
    ),
    Data(
        table="dummy",
        header=None,
        value=value_matrix,
        expected=ptw.EmptyHeaderError
    ),
    Data(
        table="dummy",
        header=[],
        value=[],
        expected=ptw.EmptyTableDataError
    ),
    Data(
        table="dummy",
        header=header_list,
        value=[],
        expected=ptw.EmptyValueError
    ),
]


class Test_SqliteTableWriter_write_table:

    @pytest.mark.parametrize(
        ["table", "header", "value", "expected"],
        [
            [data.table, data.header, data.value, data.expected]
            for data in normal_test_data_list
        ]
    )
    def test_normal(self, tmpdir, table, header, value, expected):
        test_file_path = tmpdir.join("test.sqlite")

        writer = ptw.SqliteTableWriter()
        writer.open(str(test_file_path))
        writer.table_name = table
        writer.header_list = header
        writer.value_matrix = value
        writer.write_table()
        writer.close()

        print("!!", value)

        loader = ptr.SqliteFileLoader(str(test_file_path))

        for tabledata in loader.load():
            #expected_dump = ptw.dump_tabledata(expected)
            #actual_dump = ptw.dump_tabledata(tabledata)
{'fraction_non_alphanumeric': 0.1361111111111111, 'fraction_numerical': 0.02388888888888889, 'mean_word_length': 1.585637342908438, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7361246', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2227, 'n_words': 451}