コード例 #1
0
 def test_three_args_mappings(self):
     records = [
         self.dict_constructor([('A', 'x'), ('B', 1)]),
         self.dict_constructor([('B', 2), ('A', 'y')]),  # <- Different key order.
     ]
     load_data(self.cursor, 'testtable', records)  # <- Three args.
     self.cursor.execute('SELECT A, B FROM testtable')
     self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', 2)])
コード例 #2
0
 def test_three_args_namedtuples(self):
     ntup = collections.namedtuple('ntup', ['A', 'B'])
     records = [
         ntup('x', 1),
         ntup('y', 2),
     ]
     load_data(self.cursor, 'testtable', records)  # <- Three args.
     self.cursor.execute('SELECT A, B FROM testtable')
     self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', 2)])
コード例 #3
0
 def test_three_args(self):
     records = [
         ['A', 'B'],  # <- Used as header row.
         ('x', 1),
         ('y', 2),
     ]
     load_data(self.cursor, 'testtable', records)  # <- Three args.
     self.cursor.execute('SELECT A, B FROM testtable')
     self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', 2)])
コード例 #4
0
 def test_four_args(self):
     columns = ['A', 'B']
     records = [
         ('x', 1),
         ('y', 2),
     ]
     load_data(self.cursor, 'testtable', columns, records)  # <- Four args.
     self.cursor.execute('SELECT A, B FROM testtable')
     self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', 2)])
コード例 #5
0
    def test_empty_records(self):
        records = []

        load_data(self.cursor, 'testtable1', ['A', 'B'], records)  # <- Using four args.
        self.assertTrue(table_exists(self.cursor, 'testtable1'), 'should create table')
        self.cursor.execute('SELECT A, B FROM testtable1')
        self.assertEqual(self.cursor.fetchall(), [], 'should have zero records')

        load_data(self.cursor, 'testtable2', records)  # <- Using three args.
        self.assertFalse(table_exists(self.cursor, 'testtable2'), 'should not create table')
コード例 #6
0
ファイル: api08.py プロジェクト: drewdolan/datatest
 def from_excel(cls, path, worksheet=0):
     new_cls = cls.__new__(cls)
     new_cls._connection = DEFAULT_CONNECTION
     cursor = new_cls._connection.cursor()
     with savepoint(cursor):
         table = new_table_name(cursor)
         reader = get_reader.from_excel(path, worksheet=0)
         load_data(cursor, table, reader)
     new_cls._table = table if table_exists(cursor, table) else None
     new_cls._data = path
     new_cls._args = tuple()
     new_cls._kwds = dict()
     if worksheet != 0:
         new_cls._kwds['worksheet'] = worksheet
     new_cls._update_list = []
     return new_cls
コード例 #7
0
    def test_bad_columns_object(self):
        records = [('x', 1), ('y', 2)]
        columns = 'bad columns object'  # <- Expects iterable of names, not this str.

        with self.assertRaises(TypeError):
            load_data(self.cursor, 'testtable', columns, records)
コード例 #8
0
    def test_column_default(self):
        load_data(self.cursor, 'testtable1', ['A', 'B'], [('x', 1)])
        load_data(self.cursor, 'testtable1', ['A'], [('y',)])
        load_data(self.cursor, 'testtable1', ['B'], [(3,)])
        self.cursor.execute('SELECT A, B FROM testtable1')
        self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', ''), ('', 3)])

        load_data(self.cursor, 'testtable2', ['A', 'B'], [('x', 1)], default=None)
        load_data(self.cursor, 'testtable2', ['A'], [('y',)])
        load_data(self.cursor, 'testtable2', ['B'], [(3,)])
        self.cursor.execute('SELECT A, B FROM testtable2')
        self.assertEqual(self.cursor.fetchall(), [('x', 1), ('y', None), (None, 3)])