From 28b741a13a1eeab42e52ba75aca4881e766d8f88 Mon Sep 17 00:00:00 2001 From: Akshay Joshi Date: Tue, 27 Aug 2019 19:54:30 +0530 Subject: [PATCH] Fixed timezone issue in RE-SQL test cases for Roles. --- .../9.4_plus/alter_login_role_options.sql | 2 +- .../tests/9.4_plus/alter_role_options.sql | 2 +- .../servers/roles/tests/9.4_plus/test.json | 6 ++- web/regression/re_sql/tests/test_resql.py | 40 ++++++++++++++++++- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/alter_login_role_options.sql b/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/alter_login_role_options.sql index 046d06912..04ac56221 100644 --- a/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/alter_login_role_options.sql +++ b/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/alter_login_role_options.sql @@ -9,7 +9,7 @@ CREATE ROLE "Role2_$%{}[]()&*^!@""'`\/#" WITH CREATEROLE NOREPLICATION CONNECTION LIMIT 100 - VALID UNTIL '2050-01-01 00:00:00+05:30'; + VALID UNTIL ''; ALTER ROLE "Role2_$%{}[]()&*^!@""'`\/#" IN DATABASE postgres SET application_name TO 'pg4'; diff --git a/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/alter_role_options.sql b/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/alter_role_options.sql index fb8b74a7f..f1572037e 100644 --- a/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/alter_role_options.sql +++ b/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/alter_role_options.sql @@ -9,7 +9,7 @@ CREATE ROLE "Role2_$%{}[]()&*^!@""'`\/#" WITH NOCREATEROLE NOREPLICATION CONNECTION LIMIT 100 - VALID UNTIL '2050-01-01 00:00:00+05:30'; + VALID UNTIL ''; ALTER ROLE "Role2_$%{}[]()&*^!@""'`\/#" IN DATABASE postgres SET application_name TO 'pg4'; diff --git a/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/test.json b/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/test.json index 3a2bc9427..97ab53bae 100644 --- a/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/test.json +++ b/web/pgadmin/browser/server_groups/servers/roles/tests/9.4_plus/test.json @@ -57,7 +57,8 @@ "rolvaliduntil": "2050-01-01 00:00:00 +05:30", "variables": { "added": [{"name":"application_name","value":"pg4","database":"postgres"}] } }, - "expected_sql_file": "alter_role_options.sql" + "expected_sql_file": "alter_role_options.sql", + "convert_timestamp_columns": ["rolvaliduntil"] }, { "type": "delete", @@ -122,7 +123,8 @@ "rolvaliduntil": "2050-01-01 00:00:00 +05:30", "variables": { "added": [{"name":"application_name","value":"pg4","database":"postgres"}] } }, - "expected_sql_file": "alter_login_role_options.sql" + "expected_sql_file": "alter_login_role_options.sql", + "convert_timestamp_columns": ["rolvaliduntil"] }, { "type": "delete", diff --git a/web/regression/re_sql/tests/test_resql.py b/web/regression/re_sql/tests/test_resql.py index 81d061674..ef8692ad0 100644 --- a/web/regression/re_sql/tests/test_resql.py +++ b/web/regression/re_sql/tests/test_resql.py @@ -90,7 +90,8 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator): # Schema ID placeholder in JSON file which needs to be replaced # while running the test cases self.JSON_PLACEHOLDERS = {'schema_id': '', - 'owner': ''} + 'owner': '', + 'timestamptz': ''} resql_module_list = create_resql_module_list( BaseTestGenerator.re_sql_module_list, @@ -371,6 +372,9 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator): if 'username' in self.server: sql = sql.replace(self.JSON_PLACEHOLDERS['owner'], self.server['username']) + # Convert timestamp with timezone from json file to the + # database server's correct timestamp + sql = self.convert_timestamptz(scenario, sql) try: self.assertEquals(sql, resp_sql) except Exception as e: @@ -427,6 +431,9 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator): if 'username' in self.server: sql = sql.replace(self.JSON_PLACEHOLDERS['owner'], self.server['username']) + # Convert timestamp with timezone from json file to the + # database server's correct timestamp + sql = self.convert_timestamptz(scenario, sql) try: self.assertEquals(sql, resp_sql) except Exception as e: @@ -447,6 +454,9 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator): if 'username' in self.server: exp_sql = exp_sql.replace(self.JSON_PLACEHOLDERS['owner'], self.server['username']) + # Convert timestamp with timezone from json file to the + # database server's correct timestamp + sql = self.convert_timestamptz(scenario, exp_sql) try: self.assertEquals(exp_sql, resp_sql) except Exception as e: @@ -509,3 +519,31 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator): scenario['data']['schema_id'] == \ self.JSON_PLACEHOLDERS['schema_id']: scenario['data']['schema'] = self.schema_id + + def convert_timestamptz(self, scenario, sql): + """ + This function will convert the given timestamptz with database + servers timestamptz and replace that in given sql. + :param scenario: + :param sql: + :return: + """ + if 'convert_timestamp_columns' in scenario: + for col in scenario['convert_timestamp_columns']: + if 'data' in scenario and col in scenario['data']: + self.get_db_connection() + pg_cursor = self.connection.cursor() + try: + query = "SELECT timestamp with time zone '" \ + + scenario['data'][col] + "'" + pg_cursor.execute(query) + converted_tz = pg_cursor.fetchone() + if len(converted_tz) >= 1: + sql = sql.replace( + self.JSON_PLACEHOLDERS['timestamptz'], + converted_tz[0]) + except Exception as e: + traceback.print_exc() + pg_cursor.close() + + return sql