Subject: Package node test cases [pgadmin4]

From: Harshal Dhumal <[email-masked]>
Date: Tue, 30 Aug 2016 16:35:58 +0530

Hi,

PFA patch for package node test cases.

-- 
*Harshal Dhumal*
*Software Engineer*

EnterpriseDB India: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/__init__.py
new file mode 100644
index 0000000..133b58a
--- /dev/null
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/__init__.py
@@ -0,0 +1,8 @@
+##########################################################################
+#
+# pgAdmin 4 - PostgreSQL Tools
+#
+# Copyright (C) 2013 - 2016, The pgAdmin Development Team
+# This software is released under the PostgreSQL Licence
+#
+##########################################################################
\ No newline at end of file
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_add.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_add.py
new file mode 100644
index 0000000..8099e86
--- /dev/null
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_add.py
@@ -0,0 +1,74 @@
+# #################################################################
+#
+# pgAdmin 4 - PostgreSQL Tools
+#
+# Copyright (C) 2013 - 2016, The pgAdmin Development Team
+# This software is released under the PostgreSQL Licence
+#
+# ##################################################################
+
+from regression import test_utils as utils
+from pgadmin.utils.route import BaseTestGenerator
+from pgadmin.browser.server_groups.servers.tests import utils as server_utils
+from pgadmin.browser.server_groups.servers.databases.tests import utils as \
+    database_utils
+from pgadmin.browser.server_groups.servers.databases.schemas.tests import \
+    utils as schema_utils
+from . import utils as package_utils
+
+
+class PackageAddTestCase(BaseTestGenerator):
+    """ This class will add new package under schema node. """
+
+    scenarios = [
+        # Fetching default URL for package node.
+        ('Fetch Package Node URL', dict(
+            url='/browser/package/obj/'))
+    ]
+
+    @classmethod
+    def setUpClass(cls):
+        """
+        This function perform the three tasks
+         1. Add the test server
+         2. Connect to server
+         3. Add the databases
+         4. Add the schemas
+
+        :return: None
+        """
+
+        # Firstly, add the server
+        server_utils.add_server(cls.tester)
+        # Connect to server
+        cls.server_connect_response, cls.server_group, cls.server_ids = \
+            server_utils.connect_server(cls.tester)
+        if len(cls.server_connect_response) == 0:
+            raise Exception("No Server(s) connected to add the database!!!")
+        # Add database
+        database_utils.add_database(cls.tester, cls.server_connect_response,
+                                    cls.server_ids)
+        # Add schemas
+        schema_utils.add_schemas(cls.tester)
+
+    def runTest(self):
+        """ This function will add package under schema node. """
+
+        package_utils.add_package(
+            self.tester, self.server_connect_response, self.server_ids)
+
+    @classmethod
+    def tearDownClass(cls):
+        """
+        This function deletes the added package, schemas, database,
+        server and the 'parent_id.pkl' file which is created in setup()
+        function.
+
+        :return: None
+        """
+
+        package_utils.delete_package(cls.tester)
+        schema_utils.delete_schema(cls.tester)
+        database_utils.delete_database(cls.tester)
+        server_utils.delete_server(cls.tester)
+        utils.delete_parent_id_file()
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_delete.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_delete.py
new file mode 100644
index 0000000..0d97991
--- /dev/null
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_delete.py
@@ -0,0 +1,77 @@
+# #################################################################
+#
+# pgAdmin 4 - PostgreSQL Tools
+#
+# Copyright (C) 2013 - 2016, The pgAdmin Development Team
+# This software is released under the PostgreSQL Licence
+#
+# ##################################################################
+
+from regression import test_utils as utils
+from pgadmin.utils.route import BaseTestGenerator
+from pgadmin.browser.server_groups.servers.tests import utils as server_utils
+from pgadmin.browser.server_groups.servers.databases.tests import utils as \
+    database_utils
+from pgadmin.browser.server_groups.servers.databases.schemas.tests import \
+    utils as schema_utils
+from . import utils as package_utils
+
+
+class TriggerFuncDeleteTestCase(BaseTestGenerator):
+    """ This class will delete the package under schema node. """
+
+    scenarios = [
+        # Fetching default URL for package node.
+        ('Fetch package Node URL',
+         dict(url='/browser/package/obj/'))
+    ]
+
+    @classmethod
+    def setUpClass(cls):
+        """
+        This function perform the three tasks
+         1. Add the test server
+         2. Connect to server
+         3. Add the databases
+         4. Add the schemas
+         5. Add the packages
+
+        :return: None
+        """
+
+        # Firstly, add the server
+        server_utils.add_server(cls.tester)
+        # Connect to server
+        cls.server_connect_response, cls.server_group, cls.server_ids = \
+            server_utils.connect_server(cls.tester)
+        if len(cls.server_connect_response) == 0:
+            raise Exception("No Server(s) connected to add the database!!!")
+        # Add database
+        database_utils.add_database(cls.tester, cls.server_connect_response,
+                                    cls.server_ids)
+        # Add schemas
+        schema_utils.add_schemas(cls.tester)
+
+        # Add packages
+        package_utils.add_package(
+            cls.tester, cls.server_connect_response, cls.server_ids)
+
+    def runTest(self):
+        """ This function will delete package under database node. """
+
+        package_utils.delete_package(self.tester)
+
+    @classmethod
+    def tearDownClass(cls):
+        """
+        This function deletes the added package, schemas, database,
+        server and the 'parent_id.pkl' file which is created in setup()
+        function.
+
+        :return: None
+        """
+
+        schema_utils.delete_schema(cls.tester)
+        database_utils.delete_database(cls.tester)
+        server_utils.delete_server(cls.tester)
+        utils.delete_parent_id_file()
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_get.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_get.py
new file mode 100644
index 0000000..fb304b7
--- /dev/null
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_get.py
@@ -0,0 +1,98 @@
+# #################################################################
+#
+# pgAdmin 4 - PostgreSQL Tools
+#
+# Copyright (C) 2013 - 2016, The pgAdmin Development Team
+# This software is released under the PostgreSQL Licence
+#
+# ##################################################################
+
+from regression import test_utils as utils
+from pgadmin.utils.route import BaseTestGenerator
+from pgadmin.browser.server_groups.servers.tests import utils as server_utils
+from pgadmin.browser.server_groups.servers.databases.tests import utils as \
+    database_utils
+from pgadmin.browser.server_groups.servers.databases.schemas.tests import \
+    utils as schema_utils
+from . import utils as package_utils
+
+
+class PackageGetTestCase(BaseTestGenerator):
+    """This class will fetch added package under schema node."""
+
+    scenarios = [
+        # Fetching default URL for package node.
+        ('Fetch package Node URL',
+         dict(url='/browser/package/obj/'))
+    ]
+
+    @classmethod
+    def setUpClass(cls):
+        """
+        This function perform the three tasks
+         1. Add the test server
+         2. Connect to server
+         3. Add the databases
+         4. Add the schemas
+         5. Add the packages
+
+        :return: None
+        """
+
+        # Firstly, add the server
+        server_utils.add_server(cls.tester)
+        # Connect to server
+        cls.server_connect_response, cls.server_group, cls.server_ids = \
+            server_utils.connect_server(cls.tester)
+        if len(cls.server_connect_response) == 0:
+            raise Exception("No Server(s) connected to add the database!!!")
+        # Add database
+        database_utils.add_database(cls.tester, cls.server_connect_response,
+                                    cls.server_ids)
+        # Add schemas
+        schema_utils.add_schemas(cls.tester)
+        # Add packages
+        package_utils.add_package(
+            cls.tester, cls.server_connect_response, cls.server_ids)
+
+    def runTest(self):
+        """ This function will delete package under database node. """
+
+        all_id = utils.get_ids()
+        server_ids = all_id["sid"]
+        db_ids_dict = all_id["did"][0]
+        schema_ids_dict = all_id["scid"][0]
+        package_ids_dict = all_id["pkgid"][0]
+        for server_id in server_ids:
+            db_id = db_ids_dict[int(server_id)]
+            db_con = database_utils.verify_database(
+                self.tester, utils.SERVER_GROUP, server_id, db_id)
+            if db_con['data']["connected"]:
+                schema_id = schema_ids_dict[int(server_id)][0]
+                schema_response = schema_utils.verify_schemas(
+                    self.tester, server_id, db_id, schema_id)
+                if schema_response.status_code == 200:
+                    package_list = package_ids_dict[int(server_id)]
+                    for package in package_list:
+                        package_id = package[0]
+                        package_response = \
+                            package_utils.verify_package(
+                                self.tester, server_id, db_id, schema_id,
+                                package_id)
+                        self.assertTrue(package_response.status_code, 200)
+
+    @classmethod
+    def tearDownClass(cls):
+        """
+        This function deletes the added package, schemas, database,
+        server and the 'parent_id.pkl' file which is created in setup()
+        function.
+
+        :return: None
+        """
+
+        package_utils.delete_package(cls.tester)
+        schema_utils.delete_schema(cls.tester)
+        database_utils.delete_database(cls.tester)
+        server_utils.delete_server(cls.tester)
+        utils.delete_parent_id_file()
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_put.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_put.py
new file mode 100644
index 0000000..f2f3dd0
--- /dev/null
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/test_package_put.py
@@ -0,0 +1,120 @@
+# #################################################################
+#
+# pgAdmin 4 - PostgreSQL Tools
+#
+# Copyright (C) 2013 - 2016, The pgAdmin Development Team
+# This software is released under the PostgreSQL Licence
+#
+# ##################################################################
+
+import json
+
+from regression import test_utils as utils
+from pgadmin.utils.route import BaseTestGenerator
+from regression.test_setup import advanced_config_data
+from pgadmin.browser.server_groups.servers.tests import utils as server_utils
+from pgadmin.browser.server_groups.servers.databases.tests import utils as \
+    database_utils
+from pgadmin.browser.server_groups.servers.databases.schemas.tests import \
+    utils as schema_utils
+from . import utils as package_utils
+
+
+class PacakgePutTestCase(BaseTestGenerator):
+    """ This class will update new package under schema node. """
+
+    scenarios = [
+        # Fetching default URL for package node.
+        ('Fetch package Node URL',
+         dict(url='/browser/package/obj/'))
+    ]
+
+    @classmethod
+    def setUpClass(cls):
+        """
+        This function perform the three tasks
+         1. Add the test server
+         2. Connect to server
+         3. Add the databases
+         4. Add the schemas
+         5. Add the packages
+
+        :return: None
+        """
+
+        # Firstly, add the server
+        server_utils.add_server(cls.tester)
+        # Connect to server
+        cls.server_connect_response, cls.server_group, cls.server_ids = \
+            server_utils.connect_server(cls.tester)
+        if len(cls.server_connect_response) == 0:
+            raise Exception("No Server(s) connected to add the database!!!")
+        # Add database
+        database_utils.add_database(cls.tester, cls.server_connect_response,
+                                    cls.server_ids)
+        # Add schemas
+        schema_utils.add_schemas(cls.tester)
+        # Add packages
+        package_utils.add_package(
+            cls.tester, cls.server_connect_response, cls.server_ids)
+
+    def runTest(self):
+        """ This function will update package under database node. """
+
+        all_id = utils.get_ids()
+        server_ids = all_id["sid"]
+        db_ids_dict = all_id["did"][0]
+        schema_ids_dict = all_id["scid"][0]
+        package_ids_dict = all_id["pkgid"][0]
+        for server_id in server_ids:
+            db_id = db_ids_dict[int(server_id)]
+            db_con = database_utils.verify_database(self.tester,
+                                                    utils.SERVER_GROUP,
+                                                    server_id, db_id)
+            if db_con['data']["connected"]:
+                schema_id = schema_ids_dict[int(server_id)]
+                schema_response = schema_utils.verify_schemas(self.tester,
+                                                              server_id,
+                                                              db_id,
+                                                              schema_id)
+                if schema_response.status_code == 200:
+                    package_list = package_ids_dict[int(server_id)]
+                    for package in package_list:
+                        package_id = package[0]
+                        package_response = \
+                            package_utils.verify_package(
+                                self.tester, server_id, db_id, schema_id,
+                                package_id)
+                        if package_response.status_code == 200:
+                            data = {
+                                "description": advanced_config_data[
+                                    'package_update_data']['comment'],
+                                "id": package_id
+                            }
+
+                            put_response = self.tester.put(
+                                self.url + str(utils.SERVER_GROUP) + '/' +
+                                str(server_id) + '/' +
+                                str(db_id) + '/' +
+                                str(schema_id) + '/' +
+                                str(package_id),
+                                data=json.dumps(data),
+                                follow_redirects=True)
+
+                            self.assertEquals(put_response.status_code, 200)
+
+    @classmethod
+    def tearDownClass(cls):
+        """
+        This function deletes the added package, schemas, database,
+        server and the 'parent_id.pkl' file which is created in setup()
+        function.
+
+        :return: None
+        """
+
+        package_utils.delete_package(cls.tester)
+        schema_utils.delete_schema(cls.tester)
+        database_utils.delete_database(cls.tester)
+        server_utils.delete_server(cls.tester)
+        utils.delete_parent_id_file()
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/utils.py
new file mode 100644
index 0000000..c7c9cb2
--- /dev/null
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/tests/utils.py
@@ -0,0 +1,160 @@
+# ##########################################################################
+#
+# #pgAdmin 4 - PostgreSQL Tools
+#
+# #Copyright (C) 2013 - 2016, The pgAdmin Development Team
+# #This software is released under the PostgreSQL Licence
+#
+# ##########################################################################
+
+import json
+import os
+import pickle
+import uuid
+
+from regression.test_setup import pickle_path, advanced_config_data
+from pgadmin.browser.server_groups.servers.databases.tests import \
+    utils as database_utils
+from pgadmin.browser.server_groups.servers.databases.schemas.tests import \
+    utils as schema_utils
+from regression import test_utils as utils
+
+PACKAGE_URL = '/browser/package/obj/'
+PACKAGE_DELETE_URL = '/browser/package/delete/'
+
+
+def get_package_data(server_connect_data):
+    """This function returns the package config data"""
+
+    adv_config_data = None
+    data = None
+    db_user = server_connect_data['data']['user']['name']
+
+    # Get the config data of appropriate db user
+    for config_test_data in \
+            advanced_config_data['package_credentials']:
+        if db_user == config_test_data['owner']:
+            adv_config_data = config_test_data
+
+    if adv_config_data is not None:
+        data = {
+            "name": adv_config_data['name'],
+            "description": adv_config_data['description'],
+            "pkgacl": adv_config_data['pkgacl'],
+            "pkgbodysrc": adv_config_data['pkgbodysrc'],
+            "pkgheadsrc": adv_config_data['pkgheadsrc']
+        }
+    return data
+
+
+def write_package_id(package_ids_list, server_id):
+    """
+    This function writes the server and package related data like
+    server id and package name
+
+    :param package_ids_list: list of package ids
+    :param server_id: server id
+    :return: None
+    """
+
+    pickle_id_dict = utils.get_pickle_id_dict()
+
+    if os.path.isfile(pickle_path):
+        existing_server_id = open(pickle_path, 'rb')
+        tol_server_id = pickle.load(existing_server_id)
+        pickle_id_dict = tol_server_id
+    if 'pkgid' in pickle_id_dict:
+        if pickle_id_dict['pkgid']:
+            # Add the db_id as value in dict
+            pickle_id_dict["pkgid"][0].update(
+                {int(server_id): package_ids_list})
+        else:
+            # Create new dict with server_id and db_id
+            pickle_id_dict["pkgid"].append(
+                {int(server_id): package_ids_list})
+    db_output = open(pickle_path, 'wb')
+    pickle.dump(pickle_id_dict, db_output)
+    db_output.close()
+
+
+def add_package(tester, server_connect_response, server_ids):
+    """This function add the package to schema"""
+
+    all_id = utils.get_ids()
+    db_ids_dict = all_id["did"][0]
+    schema_ids_dict = all_id["scid"][0]
+    for server_connect_response, server_id in zip(server_connect_response,
+                                                  server_ids):
+        db_id = db_ids_dict[int(server_id)]
+        db_con = database_utils.verify_database(tester, utils.SERVER_GROUP,
+                                                server_id, db_id)
+        if db_con['data']["connected"]:
+            schema_id = schema_ids_dict[int(server_id)][0]
+            schema_utils.verify_schemas(tester, server_id, db_id,
+                                        schema_id)
+            data = get_package_data(server_connect_response)
+            package_ids_list = []
+
+            data["name"] = "package_{}".format(str(uuid.uuid4())[1:8])
+
+            response = tester.post(
+                PACKAGE_URL + str(utils.SERVER_GROUP) + '/' +
+                str(server_id) + '/' + str(db_id) + '/' + str(schema_id) +
+                '/', data=json.dumps(data), content_type='html/json')
+
+            assert response.status_code == 200
+            response_data = json.loads(response.data.decode('utf-8'))
+            package_id = response_data['node']['_id']
+            package_name = str(response_data['node']['label'])
+            package_ids_list.append(
+                (package_id, package_name))
+
+            write_package_id(package_ids_list, server_id)
+
+
+def verify_package(tester, server_id, db_id, schema_id,
+                            package_id):
+    """This function verifies the package with GET API"""
+
+    get_response = tester.get(
+        PACKAGE_URL + str(utils.SERVER_GROUP) + '/' +
+        str(server_id) + '/' + str(db_id) + '/' +
+        str(schema_id) + '/' + str(package_id),
+        content_type='html/json')
+    assert get_response.status_code == 200
+    return get_response
+
+
+def delete_package(tester):
+    """This function add the package to schema"""
+
+    all_id = utils.get_ids()
+    server_ids = all_id["sid"]
+    db_ids_dict = all_id["did"][0]
+    schema_ids_dict = all_id["scid"][0]
+    package_ids_dict = all_id["pkgid"][0]
+    for server_id in server_ids:
+        db_id = db_ids_dict[int(server_id)]
+        db_con = database_utils.verify_database(tester, utils.SERVER_GROUP,
+                                                server_id, db_id)
+        if db_con['data']["connected"]:
+            schema_id = schema_ids_dict[int(server_id)][0]
+            schema_response = schema_utils.verify_schemas(
+                tester, server_id, db_id, schema_id)
+            if schema_response.status_code == 200:
+                package_list = package_ids_dict[int(server_id)]
+                for package in package_list:
+                    package_id = package[0]
+                    package_response = verify_package(
+                        tester, server_id, db_id, schema_id, package_id)
+                    if package_response.status_code == 200:
+                        del_response = tester.delete(
+                            PACKAGE_DELETE_URL + str(
+                                utils.SERVER_GROUP) + '/' +
+                            str(server_id) + '/' + str(db_id) + '/' +
+                            str(schema_id) + '/' + str(package_id),
+                            follow_redirects=True)
+                        assert del_response.status_code == 200
+                        del_response_data = json.loads(
+                            del_response.data.decode('utf-8'))
+                        assert del_response_data['success'] == 1
diff --git a/web/regression/test_advanced_config.json.in b/web/regression/test_advanced_config.json.in
index b339c92..a62cdb1 100644
--- a/web/regression/test_advanced_config.json.in
+++ b/web/regression/test_advanced_config.json.in
@@ -488,7 +488,52 @@
     "sequnce_update_data":
   {
     "comment": "This is sequence update comment"
-  }
+  },
+
+  "package_credentials": [
+    {
+      "name": "pkg",
+      "description": "comment",
+      "owner": "enterprisedb",
+      "pkgacl": [
+        { "grantee": "enterprisedb",
+          "grantor": "enterprisedb",
+          "privileges":
+          [
+            {
+              "privilege": true,
+              "privilege_type": "X",
+              "with_grant": false
+            }
+          ]
+        }
+      ],
+      "pkgheadsrc": "FUNCTION get_dept_name(p_deptno numeric) RETURN character varying;",
+      "pkgbodysrc":"todays_date timestamp without time zone DEFAULT sysdate;\n base_sal integer DEFAULT 35525;\n FUNCTION get_dept_name(p_deptno numeric) RETURN character varying IS \n  v_dname         varchar2(14);\n t_dept_max      integer := 1;\n begin\n select dname into v_dname from dept where deptno = p_deptno;\n return v_dname;\n exception\n when no_data_found then\n dbms_output.put_line('invalid department number ' || p_deptno);\n return '';\n end;"
+    }
+  ],
+
+  "package_update_data": [
+    {
+      "description": "This is package comment",
+      "owner": "enterprisedb",
+      "pkgacl": [
+        { "grantee": "enterprisedb",
+          "grantor": "enterprisedb",
+          "privileges":
+          [
+            {
+              "privilege": true,
+              "privilege_type": "X",
+              "with_grant": true
+            }
+          ]
+        }
+      ],
+      "pkgheadsrc": "FUNCTION get_dept_name1(p_deptno numeric) RETURN character varying;",
+      "pkgbodysrc":"todays_date timestamp without time zone DEFAULT sysdate;\n base_sal integer DEFAULT 35525;\n FUNCTION get_dept_name1(p_deptno numeric) RETURN character varying IS\n v_dname         varchar2(14);\n t_dept_max      integer := 1;\n begin\n select dname into v_dname from dept where deptno = p_deptno;\n return v_dname;\n exception\n when no_data_found then\n dbms_output.put_line('invalid department number ' || p_deptno);\n return '';\n end;"
+    }
+  ]

  }

-- 
Sent via pgadmin-hackers mailing list ([email-masked])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

---

From: Dave Page <[email-masked]>
Date: Thu, 1 Sep 2016 11:27:32 +0100

Per my earlier email to Priyanka, I'm not going to commit any more test
cases until the existing issues are resolved and the enhanced summary and
object cleanup work is complete, otherwise it's just taking too much time
unnecessarily.

Please re-test and resubmit this patch once that work is complete.

Thanks.

-- 
Dave Page
Blog: http://pgsnake.blogspot.com
Twitter: @pgsnake

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

---

From: Harshal Dhumal <[email-masked]>
Date: Thu, 1 Sep 2016 16:04:23 +0530

Ok, I'll do that once that work is completed.

-- 
*Harshal Dhumal*
*Software Engineer*