import os
import tempfile
import shutil
import unittest

from autoprep.preparator import Preparator, PreparedData
from autoprep.train_data import TrainData


class TestPreparator(unittest.TestCase):
    num_test_file = 6
    num_test_group = 2
    test_content = ["""
No.,選手名,守備,生年月日,年齢,年数,身長,体重,血液型,投打,出身地,年俸(推定)
0,上本　崇司,内野手,1990/08/22,28歳,6年,170cm,71kg,A型,右両,広島,800万円
2,田中　広輔,内野手,1989/07/03,29歳,5年,171cm,85kg,A型,右左,神奈川,"14,000万円"
4,小窪　哲也,内野手,1985/04/12,33歳,11年,175cm,83kg,O型,右右,奈良,"3,300万円"
6,安部　友裕,内野手,1989/06/24,29歳,11年,181cm,81kg,O型,右左,福岡,"4,600万円"
7,堂林　翔太,内野手,1991/08/17,27歳,9年,183cm,88kg,A型,右右,愛知,"1,500万円"
9,丸　佳浩,外野手,1989/04/11,29歳,11年,177cm,90kg,AB型,右左,千葉,"21,000万円"
10,岩本　貴裕,外野手,1986/04/18,32歳,10年,182cm,96kg,A型,左左,広島,"1,600万円"
11,福井　優也,投手,1988/02/08,30歳,8年,178cm,85kg,A型,右右,岡山,"3,100万円"
12,九里　亜蓮,投手,1991/09/01,27歳,5年,187cm,92kg,A型,右右,鳥取,"3,800万円"
""", """
No.,選手名,守備,生年月日,年齢,年数,身長,体重,血液型,投打,出身地,年俸(推定)
0,吉川　尚輝,内野手,1995/02/08,23歳,2年,177cm,79kg,A型,右左,岐阜,"1,300万円"
0,寺内　崇幸,内野手,1983/05/27,35歳,12年,177cm,73kg,B型,右右,栃木,"3,200万円"
1,比嘉　賢伸,内野手,2000/01/12,18歳,1年,180cm,82kg,A型,右左,大阪,250万円
2,陽　岱鋼,外野手,1987/01/17,31歳,13年,183cm,89kg,A型,右右,台湾,"30,000万円"
2,加藤　脩平,外野手,1999/03/28,19歳,2年,178cm,79kg,O型,右左,静岡,250万円
3,山川　和大,投手,1995/01/04,24歳,2年,166cm,74kg,A型,右左,兵庫,250万円
4,笠井　駿,外野手,1995/04/20,23歳,1年,180cm,80kg,B型,右右,東京,250万円
5,ゲレーロ,外野手,1986/11/20,32歳,2年,182cm,99kg,不明,右右,キューバ,"40,000万円"
5,広畑　塁,捕手,1995/06/17,23歳,1年,177cm,72kg,O型,右左,福岡,250万円
""", """
No.,選手名,守備,生年月日,年齢,年数,身長,体重,血液型,投打,出身地,年俸(推定)
0,比屋根　渉,外野手,1987/06/20,31歳,7年,180cm,70kg,O型,右右,沖縄,"1,500万円"
1,山田　哲人,内野手,1992/07/16,26歳,8年,180cm,76kg,O型,右右,兵庫,"28,000万円"
2,大引　啓次,内野手,1984/06/29,34歳,12年,178cm,84kg,A型,右右,大阪,"6,000万円"
3,西浦　直亨,内野手,1991/04/11,27歳,5年,178cm,75kg,B型,右右,奈良,"1,700万円"
4,バレンティン,外野手,1984/07/02,34歳,8年,185cm,100kg,不明,右右,アンティル,"33,300万円"
5,川端　慎吾,内野手,1987/10/16,31歳,13年,185cm,86kg,O型,右左,大阪,"14,000万円"
8,武内　晋一,内野手,1983/12/10,35歳,13年,175cm,90kg,O型,左左,兵庫,"1,700万円"
9,塩見　泰隆,外野手,1993/06/12,25歳,1年,179cm,76kg,B型,右右,神奈川,"1,000万円"
10,荒木　貴裕,内野手,1987/07/26,31歳,9年,180cm,84kg,AB型,右右,富山,"2,300万円"
""", """
背番号,選手名,打率,試合,打席数,打数,安打,本塁打,打点,盗塁,四球,死球,三振,犠打,併殺打,出塁率,長打率,OPS,RC27,XR27
2,田中　広輔,.262,143,675,572,150,10,60,32,75,17,118,6,6,.362,.383,.745,5.22,5.23
33,菊池　涼介,.233,139,642,557,130,13,60,10,51,3,111,30,5,.301,.355,.656,3.70,3.68
9,丸　佳浩,.306,125,566,432,132,39,97,10,130,3,130,0,5,.468,.627,1.096,10.61,10.35
51,鈴木　誠也,.320,124,520,422,135,30,94,4,88,5,116,0,4,.438,.618,1.057,10.02,9.75
37,野間　峻祥,.286,126,447,405,116,5,46,17,30,6,69,4,2,.343,.393,.736,4.95,4.86
44,松山　竜平,.302,124,446,397,120,12,74,2,42,2,46,0,10,.368,.466,.834,6.24,6.07
27,會澤　翼,.305,106,377,315,96,13,42,0,39,14,56,5,6,.401,.492,.893,7.27,7.11
63,西川　龍馬,.309,107,361,327,101,6,46,5,27,2,51,4,4,.364,.450,.814,6.04,5.72
95,バティスタ,.242,99,302,273,66,25,55,0,26,1,81,0,6,.308,.546,.854,5.57,5.75
""", """
背番号,選手名,打率,試合,打席数,打数,安打,本塁打,打点,盗塁,四球,死球,三振,犠打,併殺打,出塁率,長打率,OPS,RC27,XR27
25,岡本　和真,.309,143,616,540,167,33,100,2,72,4,120,0,11,.394,.541,.935,7.77,7.53
33,マギー,.285,132,547,499,142,21,84,2,41,1,92,0,12,.336,.467,.803,5.52,5.50
6,坂本　勇人,.345,109,502,441,152,18,67,9,61,0,83,0,4,.424,.537,.962,8.85,8.30
9,亀井　善行,.254,123,461,422,107,13,49,4,36,2,70,0,6,.315,.393,.708,4.47,4.43
7,長野　久義,.290,116,426,383,111,13,52,3,41,1,69,0,5,.359,.433,.793,5.80,5.65
0,吉川　尚輝,.253,92,355,316,80,4,29,11,19,5,61,13,5,.304,.361,.665,3.71,3.68
5,ゲレーロ,.244,82,323,287,70,15,40,2,29,6,62,0,8,.325,.460,.785,4.86,4.90
22,小林　誠司,.219,119,313,265,58,2,26,0,27,4,58,16,8,.300,.275,.575,2.54,2.55
2,陽　岱鋼,.245,87,276,253,62,10,37,2,17,2,72,3,7,.297,.427,.724,3.99,4.02
""", """
背番号,選手名,打率,試合,打席数,打数,安打,本塁打,打点,盗塁,四球,死球,三振,犠打,併殺打,出塁率,長打率,OPS,RC27,XR27
1,山田　哲人,.315,140,637,524,165,34,89,33,106,4,119,0,8,.432,.582,1.014,9.69,9.36
4,バレンティン,.268,142,602,514,138,38,131,1,85,0,121,0,15,.370,.533,.904,6.74,6.74
42,坂口　智隆,.317,139,595,508,161,3,37,9,75,3,60,7,15,.406,.394,.800,5.94,5.68
23,青木　宣親,.327,127,567,495,162,10,67,3,51,19,48,0,13,.409,.475,.884,7.18,6.80
3,西浦　直亨,.242,138,550,479,116,10,55,1,40,8,88,20,10,.309,.363,.673,3.67,3.67
41,雄平,.318,125,482,446,142,11,67,6,33,1,62,0,13,.365,.439,.805,5.82,5.57
52,中村　悠平,.211,123,401,341,72,5,26,2,31,5,39,22,7,.285,.287,.572,2.61,2.71
5,川端　慎吾,.259,97,334,297,77,3,31,1,32,3,37,1,11,.336,.327,.663,3.74,3.71
33,畠山　和洋,.248,75,155,129,32,5,27,0,26,0,23,0,4,.374,.411,.785,5.49,5.45
"""]

    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        for i in range(0, self.num_test_file):
            self.test_file = os.path.join(self.test_dir, f"test_data_{i}.csv")
            with open(self.test_file, 'w') as fp:
                fp.write(self.test_content[i])
        self.raw_data = TrainData(self.test_dir)

    def tearDown(self):
        shutil.rmtree(self.test_dir)

    def __test_prepare(self):
        p = Preparator(self.raw_data)
        prepared = p.prepare()
        self.assertIsInstance(prepared, list)
        self.assertEqual(len(prepared), self.num_test_group)

        self.assertIsInstance(prepared[0], PreparedData)
        self.assertIsInstance(prepared[1], PreparedData)

        self.assertEqual(prepared[0].df.shape, (27, 13))
        self.assertEqual(prepared[1].df.shape, (27, 21))

        dtypes_1 = [
            'int64', 'object', 'object', 'datetime64[ns]',
            'int64', 'int64', 'int64', 'int64', 'object',
            'object', 'object', 'int64', 'object'
        ]
        self.assertEqual(list(prepared[0].df.dtypes.astype(str)), dtypes_1)

        self.assertFalse(prepared[0].pmetas[0].is_key)
        self.assertTrue(prepared[0].pmetas[1].is_key)
        self.assertEqual(prepared[0].pmetas[4].unit, '歳')


if __name__ == '__main__':
    unittest.main()
