diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/tests/test_column_get.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/tests/test_column_get.py index 713d999..4adfbef 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/tests/test_column_get.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/tests/test_column_get.py @@ -24,7 +24,7 @@ from . import utils as columns_utils class ColumnGetTestCase(BaseTestGenerator): """This class will get column under table node.""" scenarios = [ - ('Fetch table Node URL', dict(url='/browser/column/obj/')) + ('Fetch columns under table node', dict(url='/browser/column/obj/')) ] def setUp(self): diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_add.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_add.py index 0703cba..9e4846c 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_add.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_add.py @@ -14,6 +14,7 @@ from pgadmin.browser.server_groups.servers.databases.schemas.tests import \ utils as schema_utils from pgadmin.browser.server_groups.servers.databases.tests import utils as \ database_utils +from pgadmin.browser.server_groups.servers.tests import utils as server_utils from pgadmin.utils.route import BaseTestGenerator from regression import parent_node_dict from regression.python_test_utils import test_utils as utils @@ -23,7 +24,19 @@ class TableAddTestCase(BaseTestGenerator): """ This class will add new collation under schema node. """ scenarios = [ # Fetching default URL for table node. - ('Fetch table Node URL', dict(url='/browser/table/obj/')) + ('Create Table', dict(url='/browser/table/obj/')), + ('Create Range partitioned table with 2 partitions', + dict(url='/browser/table/obj/', + server_min_version=100000, + partition_type='range' + ) + ), + ('Create List partitioned table with 2 partitions', + dict(url='/browser/table/obj/', + server_min_version=100000, + partition_type='list' + ) + ) ] def setUp(self): @@ -43,6 +56,19 @@ class TableAddTestCase(BaseTestGenerator): if not schema_response: raise Exception("Could not find the schema to add a table.") + self.is_partition = False + if hasattr(self, 'server_min_version'): + server_con = server_utils.connect_server(self, self.server_id) + if not server_con["info"] == "Server connected.": + raise Exception("Could not connect to server to add " + "partitioned table.") + if server_con["data"]["version"] < self.server_min_version: + message = "Partitioned table are not supported by " \ + "PPAS/PG 10.0 and below." + self.skipTest(message) + else: + self.is_partition = True + def runTest(self): """ This function will add table under schema node. """ db_user = self.server["username"] @@ -68,7 +94,7 @@ class TableAddTestCase(BaseTestGenerator): "seclabels": [] }, {"name": "DOJ", - "cltype": "date[]", + "cltype": "date", "attacl": [], "is_primary_key": False, "attoptions": [], @@ -76,7 +102,7 @@ class TableAddTestCase(BaseTestGenerator): } ], "exclude_constraint": [], - "fillfactor": "11", + "fillfactor": "", "hastoasttable": True, "like_constraints": True, "like_default_value": True, @@ -166,6 +192,35 @@ class TableAddTestCase(BaseTestGenerator): } ] } + + if self.is_partition: + data['partition_type'] = self.partition_type + data['is_partitioned'] = True + if self.partition_type == 'range': + data['partitions'] = \ + [{'values_from': "'2010-01-01'", + 'values_to': "'2010-12-31'", + 'is_attach': False, + 'partition_name': 'emp_2010' + }, + {'values_from': "'2011-01-01'", + 'values_to': "'2011-12-31'", + 'is_attach': False, + 'partition_name': 'emp_2011' + }] + else: + data['partitions'] = \ + [{'values_in': "'2012-01-01', '2012-12-31'", + 'is_attach': False, + 'partition_name': 'emp_2012' + }, + {'values_in': "'2013-01-01', '2013-12-31'", + 'is_attach': False, + 'partition_name': 'emp_2013' + }] + data['partition_keys'] = \ + [{'key_type': 'column', 'pt_column': 'DOJ'}] + # Add table response = self.tester.post( self.url + str(utils.SERVER_GROUP) + '/' + diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_delete.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_delete.py index 1b2f9f5..9749fed 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_delete.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_delete.py @@ -23,7 +23,7 @@ class TableDeleteTestCase(BaseTestGenerator): """This class will delete new table under schema node.""" scenarios = [ # Fetching default URL for table node. - ('Fetch table Node URL', dict(url='/browser/table/obj/')) + ('Delete Table', dict(url='/browser/table/obj/')) ] def setUp(self): diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_put.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_put.py index bfbf884..ef476d7 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_put.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_put.py @@ -14,6 +14,7 @@ from pgadmin.browser.server_groups.servers.databases.schemas.tests import \ utils as schema_utils from pgadmin.browser.server_groups.servers.databases.tests import utils as \ database_utils +from pgadmin.browser.server_groups.servers.tests import utils as server_utils from pgadmin.utils.route import BaseTestGenerator from regression import parent_node_dict from regression.python_test_utils import test_utils as utils @@ -24,7 +25,49 @@ class TableUpdateTestCase(BaseTestGenerator): """This class will add new collation under schema node.""" scenarios = [ # Fetching default URL for table node. - ('Fetch table Node URL', dict(url='/browser/table/obj/')) + ('Update Table', dict(url='/browser/table/obj/')), + ('Create partitions of existing range partitioned table', + dict(url='/browser/table/obj/', + server_min_version=100000, + partition_type='range', + mode='create' + ) + ), + ('Create partitions of existing list partitioned table', + dict(url='/browser/table/obj/', + server_min_version=100000, + partition_type='list', + mode='create' + ) + ), + ('Detach partition from existing range partitioned table', + dict(url='/browser/table/obj/', + server_min_version=100000, + partition_type='range', + mode='detach' + ) + ), + ('Detach partition from existing list partitioned table', + dict(url='/browser/table/obj/', + server_min_version=100000, + partition_type='list', + mode='detach' + ) + ), + ('Attach partition to existing range partitioned table', + dict(url='/browser/table/obj/', + server_min_version=100000, + partition_type='range', + mode='attach' + ) + ), + ('Attach partition to existing list partitioned table', + dict(url='/browser/table/obj/', + server_min_version=100000, + partition_type='list', + mode='attach' + ) + ) ] def setUp(self): @@ -44,9 +87,31 @@ class TableUpdateTestCase(BaseTestGenerator): if not schema_response: raise Exception("Could not find the schema to add a table.") self.table_name = "test_table_put_%s" % (str(uuid.uuid4())[1:6]) - self.table_id = tables_utils.create_table(self.server, self.db_name, - self.schema_name, - self.table_name) + + self.is_partition = False + if hasattr(self, 'server_min_version'): + server_con = server_utils.connect_server(self, self.server_id) + if not server_con["info"] == "Server connected.": + raise Exception("Could not connect to server to add " + "partitioned table.") + if server_con["data"]["version"] < self.server_min_version: + message = "Partitioned table are not supported by " \ + "PPAS/PG 10.0 and below." + self.skipTest(message) + else: + self.is_partition = True + + self.table_id = tables_utils.create_table_for_partition( + self.server, + self.db_name, + self.schema_name, + self.table_name, + 'partitioned', + self.partition_type) + else: + self.table_id = tables_utils.create_table(self.server, self.db_name, + self.schema_name, + self.table_name) def runTest(self): """This function will fetch added table under schema node.""" @@ -54,10 +119,18 @@ class TableUpdateTestCase(BaseTestGenerator): self.table_id) if not table_response: raise Exception("Could not find the table to update.") - data = { - "description": "This is test comment for table", - "id": self.table_id - } + + if self.is_partition: + data = {"id": self.table_id} + tables_utils.set_partition_data( + self.server, self.db_name, self.schema_name, self.table_name, + self.partition_type, data, self.mode) + else: + data = { + "description": "This is test comment for table", + "id": self.table_id + } + response = self.tester.put( self.url + str(utils.SERVER_GROUP) + '/' + str(self.server_id) + '/' + str(self.db_id) + '/' + diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/utils.py index 456f020..0c7aede 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/utils.py @@ -85,3 +85,153 @@ def verify_table(server, db_name, table_id): except Exception: traceback.print_exc(file=sys.stderr) raise + + +def create_table_for_partition(server, db_name, schema_name, table_name, + table_type, partition_type, partition_name=None): + """ + This function creates partitioned/partition/regular table + under provided schema. + + :param server: server details + :param db_name: database name + :param schema_name: schema name + :param table_name: table name + :param table_type: regular/partitioned/partition + :param partition_type: partition table type (range/list) + :param partition_name: Partition Name + :return table_id: table id + """ + try: + connection = utils.get_db_connection(db_name, + server['username'], + server['db_password'], + server['host'], + server['port']) + old_isolation_level = connection.isolation_level + connection.set_isolation_level(0) + pg_cursor = connection.cursor() + + query = '' + if table_type == 'partitioned': + if partition_type == 'range': + query = "CREATE TABLE %s.%s(country text, sales bigint, " \ + "saledate date) PARTITION BY RANGE(saledate)" % \ + (schema_name, table_name) + else: + query = "CREATE TABLE %s.%s(country text, sales bigint, " \ + "saledate date) PARTITION BY LIST(saledate)" % \ + (schema_name, table_name) + elif table_type == 'partition': + if partition_type == 'range': + query = "CREATE TABLE %s.%s PARTITION OF %s.%s " \ + "FOR VALUES FROM ('2012-01-01') TO ('2012-12-31')" % \ + (schema_name, partition_name, schema_name, table_name) + else: + query = "CREATE TABLE %s.%s PARTITION OF %s.%s " \ + "FOR VALUES IN ('2013-01-01')" % \ + (schema_name, partition_name, schema_name, table_name) + + # To fetch OID table name is actually partition name + table_name = partition_name + elif table_type == 'regular': + query = "CREATE TABLE %s.%s(country text, sales bigint," \ + "saledate date NOT NULL)" % (schema_name, table_name) + + pg_cursor.execute(query) + connection.set_isolation_level(old_isolation_level) + connection.commit() + # Get 'oid' from newly created table + pg_cursor.execute("select oid from pg_class where relname='%s'" % + table_name) + table = pg_cursor.fetchone() + table_id = '' + if table: + table_id = table[0] + connection.close() + return table_id + except Exception: + traceback.print_exc(file=sys.stderr) + raise + + +def set_partition_data(server, db_name, schema_name, table_name, + partition_type, data, mode): + """ + This function is used to set the partitions data on the basis of + partition type and action. + + :param server: server details + :param db_name: Database Name + :param schema_name: Schema Name + :param table_name: Table Name + :param partition_type: range or list + :param data: Data + :param mode: create/detach + :return: + """ + + data['partitions'] = dict() + if partition_type == 'range' and mode == 'create': + data['partitions'].update( + {'added': [{'values_from': "'2014-01-01'", + 'values_to': "'2014-12-31'", + 'is_attach': False, + 'partition_name': 'sale_2014'}, + {'values_from': "'2015-01-01'", + 'values_to': "'2015-12-31'", + 'is_attach': False, + 'partition_name': 'sale_2015' + }] + } + ) + elif partition_type == 'list' and mode == 'create': + data['partitions'].update( + {'added': [{'values_in': "'2016-01-01', '2016-12-31'", + 'is_attach': False, + 'partition_name': 'sale_2016'}, + {'values_in': "'2017-01-01', '2017-12-31'", + 'is_attach': False, + 'partition_name': 'sale_2017' + }] + } + ) + elif partition_type == 'range' and mode == 'detach': + partition_id = create_table_for_partition(server, db_name, schema_name, + table_name, 'partition', + partition_type, 'sale_2012') + data['partitions'].update( + {'deleted': [{'oid': partition_id}] + } + ) + elif partition_type == 'list' and mode == 'detach': + partition_id = create_table_for_partition(server, db_name, schema_name, + table_name, 'partition', + partition_type, 'sale_2013') + data['partitions'].update( + {'deleted': [{'oid': partition_id}] + } + ) + elif partition_type == 'range' and mode == 'attach': + partition_id = create_table_for_partition(server, db_name, schema_name, + 'attach_sale_2010', 'regular', + partition_type) + data['partitions'].update( + {'added': [{'values_from': "'2010-01-01'", + 'values_to': "'2010-12-31'", + 'is_attach': True, + 'partition_name': partition_id + }] + } + ) + elif partition_type == 'list' and mode == 'attach': + partition_id = create_table_for_partition(server, db_name, schema_name, + 'attach_sale_2011', 'regular', + partition_type) + data['partitions'].update( + {'added': [{'values_in': "'2011-01-01'", + 'is_attach': True, + 'partition_name': partition_id + }] + } + )