diff --git a/web/pgadmin/feature_tests/test_data.json b/web/pgadmin/feature_tests/test_data.json new file mode 100644 index 0000000..2f60de6 --- /dev/null +++ b/web/pgadmin/feature_tests/test_data.json @@ -0,0 +1,18 @@ +{ + "table_insert_update_cases": { + "add_update": { + "1": [1, 1, "int", "Value at 0 index is actual value(to be inserted), Value at index 1 is expected value, int is column type"], + "2": ["", 1, "int"], + "3": ["", "[null]", "int"], + "4": ["", "Hello World", "text"], + "5": ["\"\"", "", "text", "Two double quotes"], + "6": ["\\\"\\\"", "\"\"", "text", "double backslash followed by a double quote"], + "7": ["\\\\\"\\\\\"", "\\\"\\\"", "text", "double backslash followed by a double quote"], + "8": ["", "[null]", "text"], + "9": ["", "[51,52]", "json"], + "10": ["[61,62]", "[61,62]", "json"], + "11": ["", "true", "bool"], + "12": ["", "[null]", "bool"] + } + } +} \ No newline at end of file diff --git a/web/pgadmin/feature_tests/view_data_dml_queries.py b/web/pgadmin/feature_tests/view_data_dml_queries.py new file mode 100644 index 0000000..8c3fba8 --- /dev/null +++ b/web/pgadmin/feature_tests/view_data_dml_queries.py @@ -0,0 +1,323 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2017, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +import json +import os +from selenium.webdriver import ActionChains +from regression.python_test_utils import test_utils +from regression.feature_utils.base_feature_test import BaseFeatureTest +import time +from selenium.webdriver.common.by import By +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.support.ui import WebDriverWait + + +CURRENT_PATH = os.path.dirname(os.path.realpath(__file__)) + +try: + with open(CURRENT_PATH + '/test_data.json') as data_file: + config_data = json.load(data_file)['table_insert_update_cases']['add_update'] +except Exception as e: + print(str(e)) + + +class CheckForViewDataTest(BaseFeatureTest): + """ + Test cases to validate insert, update operations in table + with input test data + + First of all, the test data is inserted/updated into table and then + inserted data is compared with original data to check if expected data + is returned from table or not. + + We will cover test cases for, + 1) Insert with default values + 2) Update with null values + 3) Update with blank string + 4) Copy/Paste row + """ + + scenarios = [ + ("Validate Insert, Update operations in View data with given test " + "data", + dict()) + ] + + TABLE_NAME = 'defaults' + # To create column id with nextval, first a sequence must be created. + create_sequence = """ +CREATE SEQUENCE public.defaults_id_seq + INCREMENT 1 + START 9 + MINVALUE 1 + MAXVALUE 9223372036854775807 + CACHE 1; + +ALTER SEQUENCE public.defaults_id_seq + OWNER TO postgres; +""" + + # query for creating 'defaults_text' table + defaults_query = """ +CREATE TABLE public.defaults +( + id integer NOT NULL DEFAULT nextval('defaults_id_seq'::regclass), + number_defaults numeric(100) DEFAULT 1, + number_null numeric(100), + text_defaults text COLLATE pg_catalog."default" DEFAULT 'Hello World'::text, + text_null1 text COLLATE pg_catalog."default", + text_null2 text COLLATE pg_catalog."default", + text_null3 text COLLATE pg_catalog."default", + text_null4 text COLLATE pg_catalog."default", + json_defaults json DEFAULT '[51, 52]'::json, + json_null json, + boolean_defaults boolean DEFAULT true, + boolean_null boolean, + CONSTRAINT defaults_pkey PRIMARY KEY (id) +) +""" + + def before(self): + connection = test_utils.get_db_connection(self.server['db'], + self.server['username'], + self.server['db_password'], + self.server['host'], + self.server['port']) + test_utils.drop_database(connection, "acceptance_test_db") + test_utils.create_database(self.server, "acceptance_test_db") + test_utils.create_table_with_query( + self.server, + "acceptance_test_db", + CheckForViewDataTest.create_sequence) + + # Create pre-requisite table + test_utils.create_table_with_query( + self.server, + "acceptance_test_db", + CheckForViewDataTest.defaults_query) + self.wait = WebDriverWait(self.driver, 3) + + def runTest(self): + self.page.wait_for_spinner_to_disappear() + self._connects_to_server() + + self._tables_node_expandable() + + # Open View data + self.driver.switch_to.default_content() + self.page.select_tree_item(CheckForViewDataTest.TABLE_NAME) + # Open Object -> View data + self._check_xss_in_view_data() + + # Run test to insert a new row in table with default values + self._add_row() + self._verify_insert_data() + + # Run test to copy/paste a row + self._copy_paste_row() + + self._close_query_tool() + + def after(self): + time.sleep(1) + self.page.remove_server(self.server) + connection = test_utils.get_db_connection(self.server['db'], + self.server['username'], + self.server['db_password'], + self.server['host'], + self.server['port']) + test_utils.drop_database(connection, "acceptance_test_db") + + @staticmethod + def _get_cell_xpath(cell, row): + if str(row) == 'new-row' or str(row) == 'active': + xpath_grid_row = '//*[@id="0"]//*[@id="datagrid"]' \ + '//div[contains(@class, "'+row+'")]' + else: + xpath_grid_row = '//*[@id="0"]//*[@id="datagrid"]//div[' \ + 'contains(@class, "grid-canvas")]/div['+row+']' + xpath_row_cell = '//div[contains(@class, "'+cell+'")]' + + xpath_cell = '{0}{1}'.format(xpath_grid_row, xpath_row_cell) + + return xpath_cell + + def _wait(self, xpath): + self.wait.until(EC.visibility_of_element_located( + (By.XPATH, xpath)), "Timed out waiting for element to appear" + ) + + def _compare_cell_value(self, xpath, value): + wait = WebDriverWait(self.driver, 0.3) + try: + wait.until(EC.text_to_be_present_in_element( + (By.XPATH, xpath+"//span"), str(value)) + ) + except Exception: + wait.until(EC.text_to_be_present_in_element( + (By.XPATH, xpath), str(value)), + "Timed out waiting for element to appear" + ) + + def _update_cell(self, xpath, data): + """ + This function updates the given cell(xpath) with + given value + Args: + xpath: xpath of cell element + data: list with cell related data + + Returns: None + + """ + self._wait(xpath) + cell_el = self.page.find_by_xpath(xpath) + ActionChains(self.driver).move_to_element(cell_el).double_click( + cell_el + ).perform() + + cell_type = data[2] + value = data[0] + + if cell_type == 'int': + if value == 'clear': + cell_el.find_element_by_css_selector('input').clear() + else: + ActionChains(self.driver).send_keys(value).perform() + + elif cell_type in ['text', 'json']: + self.page.driver.find_element_by_css_selector( + "div[style*='z-index: 1000'] textarea" + ).click() + ActionChains(self.driver).send_keys(value).perform() + save_btn_xpath = "div[style*='z-index: 1000'] " \ + "div button:first-child" + self.wait.until( + EC.visibility_of_element_located( + (By.CSS_SELECTOR, save_btn_xpath) + ), "Timed out waiting for element to appear" + ) + self.page.driver.find_element_by_css_selector( + save_btn_xpath + ).click() # Click on editor's Save button + else: + if data[1] == 'true': + checkbox_el = cell_el.find_element_by_xpath(".//input") + checkbox_el.click() + ActionChains(self.driver).move_to_element(checkbox_el).double_click( + checkbox_el + ).perform() + + def _connects_to_server(self): + self.page.find_by_xpath("//*[@class='aciTreeText' and .='Servers']").click() + self.page.driver.find_element_by_link_text("Object").click() + ActionChains(self.page.driver) \ + .move_to_element(self.page.driver.find_element_by_link_text("Create")) \ + .perform() + self.page.find_by_partial_link_text("Server...").click() + + server_config = self.server + self.page.fill_input_by_field_name("name", server_config['name']) + self.page.find_by_partial_link_text("Connection").click() + self.page.fill_input_by_field_name("host", server_config['host']) + self.page.fill_input_by_field_name("port", server_config['port']) + self.page.fill_input_by_field_name("username", server_config['username']) + self.page.fill_input_by_field_name("password", server_config['db_password']) + self.page.find_by_xpath("//button[contains(.,'Save')]").click() + + def _tables_node_expandable(self): + self.page.toggle_open_tree_item(self.server['name']) + self.page.toggle_open_tree_item('Databases') + self.page.toggle_open_tree_item('acceptance_test_db') + self.page.toggle_open_tree_item('Schemas') + self.page.toggle_open_tree_item('public') + self.page.toggle_open_tree_item('Tables') + + def _check_xss_in_view_data(self): + self.page.driver.find_element_by_link_text("Object").click() + ActionChains(self.page.driver) \ + .move_to_element( + self.page.driver.find_element_by_link_text("View Data")) \ + .perform() + self.page.find_by_partial_link_text("View All Rows").click() + self.wait.until( + EC.visibility_of_element_located( + (By.CSS_SELECTOR, 'iframe') + ), "Timed out waiting for iframe to appear" + ) + self.page.driver.switch_to.frame( + self.page.driver.find_element_by_tag_name('iframe') + ) + + def _copy_paste_row(self): + row0_cell0_xpath = CheckForViewDataTest._get_cell_xpath("r0", "1") + row1_cell1_xpath = CheckForViewDataTest._get_cell_xpath("r1", "2") + row1_cell2_xpath = CheckForViewDataTest._get_cell_xpath("r2", "2") + + self.page.find_by_xpath(row0_cell0_xpath).click() + self.page.find_by_xpath("//*[@id='btn-copy-row']").click() + self.page.find_by_xpath("//*[@id='btn-paste-row']").click() + + # Update primary key of copied cell + self._update_cell(row1_cell1_xpath, [2, "", "int"]) + self.page.find_by_xpath( + CheckForViewDataTest._get_cell_xpath("r1", "3") + ).click() + + # Check if removing a cell value with default value sets + # markup to [default] if cell is cleared + self._update_cell(row1_cell2_xpath, ["clear", "", "int"]) + # click outside + self.page.find_by_xpath( + CheckForViewDataTest._get_cell_xpath("r1", "3") + ).click() + + self._compare_cell_value(row1_cell2_xpath, "[default]") + + self._update_cell(row1_cell2_xpath, ["1", "", "int"]) + + self.page.find_by_id("btn-save").click() # Save data + self.page.find_by_id("btn-flash").click() + + # Verify row 1 and row 2 data + for i in range(1, len(config_data.keys())): + cell2_xpath = CheckForViewDataTest._get_cell_xpath( + 'r'+str(i), "2" + ) + # after copy paste first cell of row 1 and + # row 2(p-keys) won't match + if i != 1: + self._compare_cell_value(cell2_xpath, config_data[str(i)][1]) + + def _add_row(self): + last_row = 'new-row' + for idx in range(1, len(config_data.keys())): + cell_xpath = CheckForViewDataTest._get_cell_xpath( + 'r'+str(idx), last_row + ) + self._update_cell(cell_xpath, config_data[str(idx)]) + last_row = 'active' if last_row == 'new-row' else last_row + + self.page.find_by_id("btn-save").click() # Save data + + def _verify_insert_data(self): + self.page.find_by_id("btn-flash").click() + for idx in range(1, len(config_data.keys())): + cell_xpath = CheckForViewDataTest._get_cell_xpath( + 'r'+str(idx), "1" + ) + # see if cell values matched to actual value + self._compare_cell_value(cell_xpath, config_data[str(idx)][1]) + + def _close_query_tool(self): + self.page.driver.switch_to_default_content() + xpath = "//*[@id='dockerContainer']/div/div[3]" \ + "/div/div[2]/div[1]" + self._wait(xpath) + self.page.click_element(self.page.find_by_xpath(xpath)) \ No newline at end of file diff --git a/web/regression/python_test_utils/test_utils.py b/web/regression/python_test_utils/test_utils.py index 2b7c695..c50dd31 100644 --- a/web/regression/python_test_utils/test_utils.py +++ b/web/regression/python_test_utils/test_utils.py @@ -172,6 +172,35 @@ def create_table(server, db_name, table_name): except Exception: traceback.print_exc(file=sys.stderr) + +def create_table_with_query(server, db_name, query): + """ + This function create the table in given database name + :param server: server details + :type server: dict + :param db_name: database name + :type db_name: str + :param query: create table query + :type query: str + :return: None + """ + try: + connection = 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() + pg_cursor.execute(query) + connection.set_isolation_level(old_isolation_level) + connection.commit() + + except Exception: + traceback.print_exc(file=sys.stderr) + + def create_constraint( server, db_name, table_name, constraint_type="unique", constraint_name="test_unique"): @@ -274,7 +303,6 @@ def drop_database(connection, database_name): connection.commit() connection.close() - def drop_tablespace(connection): """This function used to drop the tablespace""" pg_cursor = connection.cursor()