def test_import_from_postgresql_retrieve_desired_data( self, mocked_create_table): mocked_create_table.return_value = 42 rows.export_to_postgresql(utils.table, self.uri, close_connection=True, table_name="rows_2") # import using uri table_1 = rows.import_from_postgresql(self.uri, close_connection=True, table_name="rows_2") call_args = mocked_create_table.call_args_list[0] self.assert_create_table_data(call_args, expected_meta=self.meta) # import using connection connection = pgconnect(self.uri) table_2 = rows.import_from_postgresql(connection, close_connection=False, table_name="rows_2") call_args = mocked_create_table.call_args_list[1] meta = self.meta.copy() meta["source"] = connection self.assert_create_table_data(call_args, expected_meta=meta) connection.close()
def test_export_to_postgresql_create_unique_table_name(self): first_table = utils.table second_table = utils.table + utils.table table_names_before = self.get_table_names() rows.export_to_postgresql(first_table, self.uri, table_name_format="rows_{index}") table_names_after = self.get_table_names() rows.export_to_postgresql(second_table, self.uri, table_name_format="rows_{index}") table_names_final = self.get_table_names() diff_1 = list(set(table_names_after) - set(table_names_before)) diff_2 = list(set(table_names_final) - set(table_names_after)) self.assertEqual(len(diff_1), 1) self.assertEqual(len(diff_2), 1) new_table_1 = diff_1[0] new_table_2 = diff_2[0] result_first_table = rows.import_from_postgresql( self.uri, table_name=new_table_1) result_second_table = rows.import_from_postgresql( self.uri, table_name=new_table_2) self.assert_table_equal(result_first_table, first_table) self.assert_table_equal(result_second_table, second_table)
def test_export_to_postgresql_create_unique_table_name(self): first_table = utils.table second_table = utils.table + utils.table table_names_before = self.get_table_names() rows.export_to_postgresql( first_table, self.uri, table_name_format="rows_{index}" ) table_names_after = self.get_table_names() rows.export_to_postgresql( second_table, self.uri, table_name_format="rows_{index}" ) table_names_final = self.get_table_names() diff_1 = list(set(table_names_after) - set(table_names_before)) diff_2 = list(set(table_names_final) - set(table_names_after)) self.assertEqual(len(diff_1), 1) self.assertEqual(len(diff_2), 1) new_table_1 = diff_1[0] new_table_2 = diff_2[0] result_first_table = rows.import_from_postgresql( self.uri, table_name=new_table_1 ) result_second_table = rows.import_from_postgresql( self.uri, table_name=new_table_2 ) self.assert_table_equal(result_first_table, first_table) self.assert_table_equal(result_second_table, second_table)
def test_postgresql_injection(self): with self.assertRaises(ValueError): rows.import_from_postgresql( self.uri, table_name=('table1","postgresql_master')) with self.assertRaises(ValueError): rows.export_to_postgresql(utils.table, self.uri, table_name='table1", "postgresql_master')
def test_postgresql_injection(self): with self.assertRaises(ValueError): rows.import_from_postgresql( self.uri, table_name=('table1","postgresql_master') ) with self.assertRaises(ValueError): rows.export_to_postgresql( utils.table, self.uri, table_name='table1", "postgresql_master' )
def test_export_to_postgresql_connection(self): connection = pgconnect(self.uri) rows.export_to_postgresql( utils.table, connection, close_connection=True, table_name="rows_4" ) table = rows.import_from_postgresql(self.uri, table_name="rows_4") self.assert_table_equal(table, utils.table)
def test_export_to_postgresql_connection(self): connection = pgconnect(self.uri) rows.export_to_postgresql(utils.table, connection, close_connection=True, table_name="rows_4") table = rows.import_from_postgresql(self.uri, table_name="rows_4") self.assert_table_equal(table, utils.table)
def test_import_from_postgresql_query_args(self): connection, table_name = rows.export_to_postgresql( utils.table, self.uri, close_connection=False, table_name="rows_9") table = rows.import_from_postgresql( connection, query="SELECT * FROM rows_9 WHERE float_column > %s", query_args=(3, ), ) for row in table: self.assertTrue(row.float_column > 3)
def test_import_from_postgresql_query_args(self): connection, table_name = rows.export_to_postgresql( utils.table, self.uri, close_connection=False, table_name="rows_9" ) table = rows.import_from_postgresql( connection, query="SELECT * FROM rows_9 WHERE float_column > %s", query_args=(3,), ) for row in table: self.assertTrue(row.float_column > 3)
def test_import_from_postgresql_uses_create_table(self, mocked_create_table): mocked_create_table.return_value = 42 kwargs = {"encoding": "test", "some_key": 123, "other": 456} rows.export_to_postgresql(utils.table, self.uri, table_name="rows_1") result = rows.import_from_postgresql(self.uri, table_name="rows_1", **kwargs) self.assertTrue(mocked_create_table.called) self.assertEqual(mocked_create_table.call_count, 1) self.assertEqual(result, 42) call = mocked_create_table.call_args kwargs["meta"] = self.meta self.assertEqual(call[1], kwargs)
def test_export_to_postgresql_forcing_table_name_appends_rows(self): repeat = 3 for _ in range(repeat): rows.export_to_postgresql(utils.table, self.uri, table_name="rows_7") expected_table = utils.table for _ in range(repeat - 1): expected_table += utils.table result_table = rows.import_from_postgresql(self.uri, table_name="rows_7") self.assertEqual(len(result_table), repeat * len(utils.table)) self.assert_table_equal(result_table, expected_table)
def test_import_from_postgresql_retrieve_desired_data(self, mocked_create_table): mocked_create_table.return_value = 42 rows.export_to_postgresql( utils.table, self.uri, close_connection=True, table_name="rows_2" ) # import using uri table_1 = rows.import_from_postgresql( self.uri, close_connection=True, table_name="rows_2" ) call_args = mocked_create_table.call_args_list[0] self.assert_create_table_data(call_args, expected_meta=self.meta) # import using connection connection = pgconnect(self.uri) table_2 = rows.import_from_postgresql( connection, close_connection=False, table_name="rows_2" ) call_args = mocked_create_table.call_args_list[1] meta = self.meta.copy() meta["source"] = connection self.assert_create_table_data(call_args, expected_meta=meta) connection.close()
def test_export_to_postgresql_uri(self): rows.export_to_postgresql(utils.table, self.uri, table_name="rows_3") table = rows.import_from_postgresql(self.uri, table_name="rows_3") self.assert_table_equal(table, utils.table)