def get_table(self, process_names: Callable[[str], str] ) -> SQLTable: return SQLTable( name=process_names(self._table_name), fields=[field.process(process_names) for field in self.get_fields()], indices=[index.process(process_names) for index in self.get_indices()] )
def setUp(self): print("SETUP MDB", unix_dialect.__dict__) self.field1 = SQLField("table", "field1", SQLTypes.BOOLEAN, comment="comment") self.field2 = SQLField("table", "field2", SQLTypes.NUMERIC) self.index = SQLIndex("table", "field1", SQLIndexTypes.HASH) self.table = SQLTable("table", [self.field1, self.field2], []) self.query_provider = MariaDBQueryProvider()
def test_copy_dialect(self): dialect = deepcopy(csv.excel) dialect.delimiter = '\t' dialect.doublequote = False dialect.escapechar = '\\' dialect.quotechar = "'" self.assertEqual(("COPY t FROM STDIN WITH (FORMAT CSV, HEADER TRUE, " "ENCODING 'UTF_8', DELIMITER E'\t', ESCAPE E'\\\\', " "QUOTE E'\\'')", ), self.provider.copy_stream(SQLTable("t", [], []), "utf-8", dialect))
def test_copy_stream(self): self.executor.copy_stream(SQLTable("table", [], []), BytesIO(b"data"), "utf-8", unix_dialect) self.assertEqual([ call.debug('%s (%s, %s)', 'INSERT INTO table VALUES ()', mock.ANY, {}) ], self.logger.mock_calls) self.assertEqual([ call.cursor(), call.cursor().executemany('INSERT INTO table VALUES ()', mock.ANY) ], self.connection.mock_calls)
def test_create_three(self): provider = PostgreSQLQueryProvider() self.assertEqual( ('CREATE TABLE t (\n' ' f1 text, -- comment1\n' ' field_with_long_name2 numeric,\n' ' f3 text -- comment2\n' ')', ), provider.create_table( SQLTable("t", [self.sql_field1, self.sql_field2, self.sql_field3], [])))
def test_copy_stream(self): print(csv.unix_dialect.__dict__) self.executor.copy_stream(SQLTable("table", [], []), BytesIO(b"data"), "utf-8", csv.unix_dialect) # self.assertEqual([call.debug("COPY table FROM STDIN WITH # (FORMAT CSV, " # "HEADER TRUE, ENCODING 'UTF_8')")], # self.logger.mock_calls) self.assertEqual([ call.cursor(), call.cursor().execute( "COPY table FROM STDIN WITH (FORMAT CSV, " "HEADER TRUE, ENCODING 'UTF_8')", stream=mock.ANY) ], self.connection.mock_calls)
def test_prepare_copy(self): self.table = SQLTable("table", [], []) self.assertEqual((), self.provider.prepare_copy(self.table))
def test_create_two_fields_table(self): self.table = SQLTable("table", [self.field1, self.field2], []) self.assertEqual(('CREATE TABLE table (\n' ' field1 boolean, -- comment\n' ' field2 numeric\n' ')', ), self.provider.create_table(self.table))
def test_create_one_field_table(self): self.table = SQLTable("table", [self.field1], []) self.assertEqual(('CREATE TABLE table (\n' ' field1 boolean -- comment\n' ')', ), self.provider.create_table(self.table))
def test_create_empty_table(self): self.table = SQLTable("table", [], []) self.assertEqual(('CREATE TABLE table ()', ), self.provider.create_table(self.table))
def test_drop_table(self): self.table = SQLTable("table", [], []) self.assertEqual(('DROP TABLE IF EXISTS table', ), self.provider.drop_table(self.table))
def test_create_empty(self): self.assertEqual(('CREATE TABLE t ()', ), self.provider.create_table(SQLTable("t", [], [])))
def test_create_one(self): self.assertEqual( ('CREATE TABLE t (\n' ' f1 text -- comment1\n' ')', ), self.provider.create_table(SQLTable("t", [self.sql_field1], [])))
def test_finalize_copy(self): self.assertEqual(('ANALYZE t', ), self.provider.finalize_copy(SQLTable("t", [], [])))
def test_copy(self): self.assertEqual(("COPY t FROM STDIN WITH " "(FORMAT CSV, HEADER TRUE, ENCODING 'UTF_8')", ), self.provider.copy_stream(SQLTable("t", [], []), "utf-8", csv.excel))
def test_prepare_copy(self): self.assertEqual(('TRUNCATE t', ), self.provider.prepare_copy(SQLTable("t", [], [])))
def test_insert_all(self): self.table = SQLTable("table", [self.field1, self.field2], []) self.assertEqual('INSERT INTO table VALUES (?, ?)', self.provider.insert_all(self.table))
def test_finalize_copy(self): self.table = SQLTable("table", [], []) self.assertEqual((), self.provider.finalize_copy(self.table))
def test_create_other_index(self): self.table = SQLTable("other_table", [], [self.index]) with self.assertRaises(AssertionError): self.query_provider.create_index(self.table, self.index)
def get_table(record_format: RecordFormat) -> SQLTable: fields = [ SQLField(record_format.name, field, SQLTypes.TEXT, i) for i, field in enumerate(record_format.header) ] return SQLTable(record_format.name, fields, [])
def test_drop(self): self.assertEqual(('DROP TABLE IF EXISTS t', ), self.provider.drop_table(SQLTable("t", (), ())))
def test_create_index(self): self.table = SQLTable("table", [], [self.index]) self.assertEqual( ('DROP INDEX IF EXISTS field1_table_idx ON table', 'CREATE INDEX field1_table_idx ON table(field1(255))'), self.query_provider.create_index(self.table, self.index))
def test_one_with_index(self): sql_index = SQLIndex("t", "f", SQLIndexTypes.HASH) self.assertEqual(('DROP INDEX IF EXISTS f_t_idx', 'CREATE INDEX f_t_idx ON t USING hash(f)'), self.provider.create_index(SQLTable("t", [], []), sql_index))