Esempio n. 1
0
 def setUp(self):
     # データ初期化
     csv_to_db.load(
         self.conn,
         example1=
         'tests/data/test_example1/example1_test_select_example1.csv')
     self.conn.commit()
Esempio n. 2
0
    def test_load_success_multi(self):
        expected_example1 = TEST_DATA_EXAMPLE1
        expected_example2 = TEST_DATA_EXAMPLE2

        # csvファイル内容で初期化
        csv_to_db.load(self.conn,
                       example1=TEST_FILE_EXAMPLE1,
                       example2=TEST_FILE_EXAMPLE2)
        self.conn.commit()

        # csvファイルをロードした後の状態を確認
        sql = "SELECT * FROM example1 ORDER BY id"
        with self.conn.cursor() as cur:
            cur.execute(sql)
            records = cur.fetchall()
        # 件数の確認
        self.assertEqual(len(records), len(expected_example1))
        # データの確認
        for i, expected_row in enumerate(expected_example1):
            with self.subTest(i=i):
                self.assertDictEqual(records[i], expected_row)

        sql = "SELECT * FROM example2 ORDER BY id"
        with self.conn.cursor() as cur:
            cur.execute(sql)
            records = cur.fetchall()
        # 件数の確認
        self.assertEqual(len(records), len(expected_example2))
        # データの確認
        for i, expected in enumerate(expected_example2):
            with self.subTest(i=i):
                self.assertDictEqual(records[i], expected)
Esempio n. 3
0
 def setUpClass(cls):
     cls.conn = database_connection.get_connection()
     # データ初期化
     csv_to_db.load(
         cls.conn,
         example1=
         'tests/data/test_example1/example1_test_select_example1_by_id.csv')
     cls.conn.commit()
Esempio n. 4
0
    def test_select_example1_sort_parameterized(self, _, cond_file, cond_sort,
                                                expected_ids):
        # データ初期化
        csv_to_db.load(self.conn, example1=cond_file)
        self.conn.commit()

        records = example1.select_example1(self.conn, sort=cond_sort)
        # 件数の確認
        self.assertEqual(len(records), len(expected_ids))
        # データの確認
        for i, expected_id in enumerate(expected_ids):
            self.assertEqual(
                records[i]['id'], expected_id,
                'failed with cond_file={},cond_sort={},i={}'.format(
                    cond_file, cond_sort, i))
Esempio n. 5
0
    def test_load_success_no_truncate(self):
        expected = INITIAL_DATA_EXAMPLE1 + TEST_DATA_EXAMPLE1

        # csvファイル内容を登録(既存のデータは消さない)
        csv_to_db.load(self.conn, truncate=False, example1=TEST_FILE_EXAMPLE1)
        self.conn.commit()

        # csvファイルをロードした後の状態を確認
        sql = "SELECT * FROM example1 ORDER BY id"
        with self.conn.cursor() as cur:
            cur.execute(sql)
            records = cur.fetchall()
        # 件数の確認(2 + 4 -> 6)
        self.assertEqual(len(records), len(expected))
        # データの確認
        for i, expected_row in enumerate(expected):
            with self.subTest(i=i):
                self.assertDictEqual(records[i], expected_row)
Esempio n. 6
0
    def test_select_example1_sort_subtest(self):
        # test name
        # data: example1のデータ(csvファイル)
        # arg: sort
        # return: idのみのリスト
        Fixture2 = namedtuple('Fixture2',
                              ('name', 'example1_file', 'sort', 'expected'))

        fixtures = [
            # id(昇順)
            Fixture2(
                name="id",
                example1_file=
                'tests/data/test_example1/example1_test_select_example1_sort_by_id.csv',
                sort=example1.Sort.id,
                expected=[11, 12, 13]),
            # datatime_col(昇順)
            Fixture2(
                name="datatime_col",
                example1_file=
                'tests/data/test_example1/example1_test_select_example1_sort_by_datetime_col.csv',
                sort=example1.Sort.datetime_col,
                expected=[11, 13, 12, 14]),
        ]

        for fixture in fixtures:
            with self.subTest(fixture=fixture):
                # データ初期化
                csv_to_db.load(self.conn, example1=fixture.example1_file)
                self.conn.commit()

                records = example1.select_example1(self.conn,
                                                   sort=fixture.sort)
                # 件数の確認
                self.assertEqual(len(records), len(fixture.expected))
                # データの確認
                actual = [record['id'] for record in records]
                self.assertListEqual(actual, fixture.expected)
Esempio n. 7
0
    def test_load_error_table_not_found(self):
        # TRUNCATEされないので戻る
        expected = INITIAL_DATA_EXAMPLE1

        # 例外の確認
        with self.assertRaises(Exception):
            try:
                self.conn.begin()
                csv_to_db.load(
                    self.conn,
                    # 存在しないテーブル名を指定
                    example9=TEST_FILE_EXAMPLE1)
            except Exception as e:
                self.conn.rollback()
                raise e

        # 戻っていることを確認
        sql = "SELECT * FROM example1 ORDER BY id"
        with self.conn.cursor() as cur:
            cur.execute(sql)
            records = cur.fetchall()
        # 件数の確認
        self.assertEqual(len(records), len(expected))
Esempio n. 8
0
    def test_load_error_unsupported_extention(self):
        # TRUNCATEされないので2件のままになる
        expected = INITIAL_DATA_EXAMPLE1

        # 例外の確認
        with self.assertRaises(ValueError):
            try:
                self.conn.begin()
                csv_to_db.load(
                    self.conn,
                    # 拡張子がtxtのファイルを設定(csv、tsv以外)
                    example1='tests/data/csv_to_db/example1_test_load.txt')
            except Exception as e:
                self.conn.rollback()
                raise e

        # 件数が変わっていないことを確認
        sql = "SELECT * FROM example1 ORDER BY id"
        with self.conn.cursor() as cur:
            cur.execute(sql)
            records = cur.fetchall()
        # 件数の確認
        self.assertEqual(len(records), len(expected))
Esempio n. 9
0
    def test_load_error_file_not_found(self):
        # TRUNCATEされないので2件のままになる
        expected = INITIAL_DATA_EXAMPLE1

        # 例外の確認
        with self.assertRaises(FileNotFoundError):
            try:
                self.conn.begin()
                csv_to_db.load(
                    self.conn,
                    # 存在しないファイルを指定
                    example1='path/to/example1_test_load_csv.csv')
            except Exception as e:
                self.conn.rollback()
                raise e

        # 件数が変わっていないことを確認
        sql = "SELECT * FROM example1 ORDER BY id"
        with self.conn.cursor() as cur:
            cur.execute(sql)
            records = cur.fetchall()
        # 件数の確認
        self.assertEqual(len(records), len(expected))
Esempio n. 10
0
    def test_load_error_type(self):
        # TRUNCATEされてるので(rollbackしてもデータが戻らないため)0件になる
        expected = []

        # 例外の確認
        with self.assertRaises(Exception):
            try:
                self.conn.begin()
                csv_to_db.load(
                    self.conn,
                    # int型の項目に文字列を指定
                    example1=
                    'tests/data/csv_to_db/example1_test_load_csv_error.csv')
            except Exception as e:
                self.conn.rollback()
                raise e

        # 0件になっていることを確認
        sql = "SELECT * FROM example1 ORDER BY id"
        with self.conn.cursor() as cur:
            cur.execute(sql)
            records = cur.fetchall()
        # 件数の確認
        self.assertEqual(len(records), len(expected))