import os.path as path
from datetime import datetime

import pytest

from src.batch.common.batch_context import BatchContext
from src.batch.ultmarc.utmp_tables.table_mapper.concrete import com_post_mapper
from src.db.database import Database
from tests.testing_utility import (assert_table_results,
                                   create_db_data_from_csv,
                                   create_delete_sql_with_parameter,
                                   create_insert_sql_with_parameter,
                                   create_ultmarc_table_mapper_sut,
                                   create_ultmarc_test_data_from_csv)


class TestComPostMapper:
    """レイアウト区分005: COM_役職"""

    db: Database
    batch_context: BatchContext
    test_file_path: str = path.dirname(__file__)

    @pytest.fixture(autouse=True, scope='function')
    def pre_test(self, database: Database):
        """テスト実行前後処理"""
        self.batch_context = BatchContext.get_instance()
        # setup
        self.db = database
        self.db.connect()
        self.db.begin()

        # testing
        yield

        # teardown
        self.db.rollback()
        self.db.disconnect()

    def test_insert_record(self):
        """
        Cases:
            COM_役職テーブルにレコードを登録する
        Arranges:
            - CSVデータを用意し、読み込む
            - 追加対象となるレコードを削除する
        Expects:
            - 登録内容が期待値と一致すること
        """

        # Arrange
        # 処理日設定
        self.batch_context.syor_date = datetime.strftime(datetime.now(), '%Y/%m/%d')
        # テスト用のCSVを読み込む
        test_dat_file = create_ultmarc_test_data_from_csv(path.join(self.test_file_path, 'com_post_insert.csv'))
        # 一旦全データをDBから削除
        delete_sql, delete_parameter = create_delete_sql_with_parameter('src05.com_post', {'1': '1'})
        self.db.execute(delete_sql, delete_parameter)

        # Act
        for line_number, line in enumerate(test_dat_file, start=1):
            sut: com_post_mapper.ComPostMapper = create_ultmarc_table_mapper_sut(line, self.db)
            assert type(sut) is com_post_mapper.ComPostMapper, f'{line_number}行目:マッパークラスが期通りか'

            sut.make_query()
            sut.execute_queries()

        # Assert
        # 期待値ファイルを読み込む
        expect_data_list = create_db_data_from_csv(path.join(self.test_file_path, 'expect_com_post_insert.csv'))
        primary_keys = [f"'{primary_key['post_cd']}'" for primary_key in expect_data_list]
        actual_select_sql = f"SELECT * FROM src05.com_post WHERE post_cd IN ({','.join(primary_keys)})"
        actual_data_list = self.db.execute_select(actual_select_sql)
        # 期待値検査
        ignore_columns = ['regist_ymd', 'sys_update_date', 'sys_regist_date']
        assert_table_results(actual_data_list, expect_data_list, ignore_col_name=ignore_columns)
        # 動的日付項目の個別確認
        line_number = 0
        for actual_row, expect_row in zip(actual_data_list, expect_data_list):
            line_number += 1
            for actual_col_name, expect_col_name in zip(actual_row, expect_row):
                if actual_col_name in ignore_columns:
                    assert actual_row[actual_col_name] >= expect_row[expect_col_name], f'{line_number}行目:{actual_col_name}が、期待値以降であること'

    def test_update_record(self):
        """
        Cases:
            COM_役職テーブルのレコードを更新する
        Arranges:
            - CSVデータを用意し、読み込む
            - 更新対象となるレコードを登録する
        Expects:
            - 登録内容が期待値と一致すること
        """

        # Arrange
        # 処理日設定
        self.batch_context.syor_date = datetime.strftime(datetime.now(), '%Y/%m/%d')
        # テスト用のCSVを読み込む
        test_dat_file = create_ultmarc_test_data_from_csv(path.join(self.test_file_path, 'com_post_update.csv'))
        # 一旦全データをDBから削除
        delete_sql, delete_parameter = create_delete_sql_with_parameter('src05.com_post', {'1': '1'})
        self.db.execute(delete_sql, delete_parameter)
        # テストデータをDBに登録
        # DBデータを読み込む
        test_sql_data_list = create_db_data_from_csv(path.join(self.test_file_path, 'db_com_post_before_update.csv'))
        for test_data in test_sql_data_list:
            insert_sql, insert_parameter = create_insert_sql_with_parameter(
                'src05.com_post',
                test_data.keys(),
                test_data.values()
            )
            self.db.execute(insert_sql, insert_parameter)

        # Act
        for line_number, line in enumerate(test_dat_file, start=1):
            sut: com_post_mapper.ComPostMapper = create_ultmarc_table_mapper_sut(line, self.db)
            assert type(sut) is com_post_mapper.ComPostMapper, f'{line_number}行目:マッパークラスが期通りか'
            sut.make_query()
            sut.execute_queries()

        # Assert
        # 期待値ファイルを読み込む
        expect_data_list = create_db_data_from_csv(path.join(self.test_file_path, 'expect_com_post_update.csv'))
        primary_keys = [f"'{primary_key['post_cd']}'" for primary_key in expect_data_list]
        actual_select_sql = f"SELECT * FROM src05.com_post WHERE post_cd IN ({','.join(primary_keys)})"
        actual_data_list = self.db.execute_select(actual_select_sql)
        # 期待値検査
        ignore_columns = ['regist_ymd', 'update_ymd', 'sys_update_date', 'sys_regist_date']
        assert_table_results(actual_data_list, expect_data_list, ignore_col_name=ignore_columns)
        # 動的日付項目の個別確認
        line_number = 0
        for actual_row, expect_row in zip(actual_data_list, expect_data_list):
            line_number += 1
            for actual_col_name, expect_col_name in zip(actual_row, expect_row):
                if actual_col_name in ignore_columns:
                    if expect_row[expect_col_name] is None:
                        assert actual_row[actual_col_name] is None, f'{line_number}行目:{actual_col_name}が、登録されていないこと'
                    else:
                        assert actual_row[actual_col_name] >= expect_row[expect_col_name], f'{line_number}行目:{actual_col_name}が、期待値以降であること'

    def test_logical_delete(self):
        """
        Cases:
            COM_役職テーブルのレコードを1件論理削除する
        Arranges:
            - CSVデータを用意し、読み込む
            - 削除対象となるレコードを登録する
        Expects:
            - 登録内容が期待値と一致すること
        """

        # Arrange
        # 処理日設定
        self.batch_context.syor_date = datetime.strftime(datetime.now(), '%Y/%m/%d')
        # テスト用のCSVを読み込む
        test_dat_file = create_ultmarc_test_data_from_csv(path.join(self.test_file_path, 'com_post_delete.csv'))
        # 一旦全データをDBから削除
        delete_sql, delete_parameter = create_delete_sql_with_parameter('src05.com_post', {'1': '1'})
        self.db.execute(delete_sql, delete_parameter)
        # テストデータをDBに登録
        # DBデータを読み込む
        test_sql_data_list = create_db_data_from_csv(path.join(self.test_file_path, 'db_com_post_before_delete.csv'))
        for test_data in test_sql_data_list:
            insert_sql, insert_parameter = create_insert_sql_with_parameter(
                'src05.com_post',
                test_data.keys(),
                test_data.values()
            )
            self.db.execute(insert_sql, insert_parameter)

        # Act
        for line_number, line in enumerate(test_dat_file, start=1):
            sut: com_post_mapper.ComPostMapper = create_ultmarc_table_mapper_sut(line, self.db)
            assert type(sut) is com_post_mapper.ComPostMapper, f'{line_number}行目:マッパークラスが期通りか'
            sut.make_query()
            sut.execute_queries()

        # Assert
        # 期待値ファイルを読み込む
        expect_data_list = create_db_data_from_csv(path.join(self.test_file_path, 'expect_com_post_delete.csv'))
        primary_keys = [f"'{primary_key['post_cd']}'" for primary_key in expect_data_list]
        actual_select_sql = f"SELECT * FROM src05.com_post WHERE post_cd IN ({','.join(primary_keys)})"
        actual_data_list = self.db.execute_select(actual_select_sql)
        # 期待値検査
        ignore_columns = ['regist_ymd', 'update_ymd', 'delete_ymd', 'sys_update_date', 'sys_regist_date']
        assert_table_results(actual_data_list, expect_data_list, ignore_col_name=ignore_columns)
        # 動的日付項目の個別確認
        line_number = 0
        for actual_row, expect_row in zip(actual_data_list, expect_data_list):
            line_number += 1
            for actual_col_name, expect_col_name in zip(actual_row, expect_row):
                if actual_col_name in ignore_columns:
                    if expect_row[expect_col_name] is None:
                        assert actual_row[actual_col_name] is None, f'{line_number}行目:{actual_col_name}が、登録されていないこと'
                    else:
                        assert actual_row[actual_col_name] >= expect_row[expect_col_name], f'{line_number}行目:{actual_col_name}が、期待値以降であること'
