Hi, I tried writing tests in the web/pgadmin/tools/sqleditor/ utils/tests/test_save_query_to_file_utils for the file web/pgadmin/tools/sqleditor/utils/tests/save_query_to_ file_utils.py
But I am getting a error, ERROR: runTest (pgadmin.tools.sqleditor.utils.tests.test_save_query_ to_file_utils.TestSaveQueryToFile) When user has entered the extension .sql to the file while saving ---------------------------------------------------------------------- Traceback (most recent call last): File "/var/www/flask/pgadmin4/pgadmin4/web/pgadmin/tools/ sqleditor/utils/tests/test_save_query_to_file_utils.py", line 42, in runTest file_path_result = save_query_to_file(self.file_data) File "/var/www/flask/pgadmin4/pgadmin4/web/pgadmin/tools/ sqleditor/utils/save_query_to_file_utils.py", line 15, in save_query_to_file storage_manager_path = get_storage_directory() File "/var/www/flask/pgadmin4/local/lib/python2.7/site-packages/flask_login.py", line 788, in decorated_view if current_app.login_manager._login_disabled: File "/var/www/flask/pgadmin4/local/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__ return getattr(self._get_current_object(), name) File "/var/www/flask/pgadmin4/local/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object return self.__local() File "/var/www/flask/pgadmin4/local/lib/python2.7/site-packages/flask/globals.py", line 51, in _find_app raise RuntimeError(_app_ctx_err_msg) RuntimeError: Working outside of application context. How do I test the extracted code inside context? How do I resolve this error. I have attached test_save_query_to_file_utils.py and save_query_to_file_utils.py Murtuza, Actually I didnt find any toggable button in the File Dialog Box So I made it general purpose ( I guess I will have to make one then and then if I select SQL all .sql files should be listed, and if I select All files then every type of file is shown in the File Dialog Box,this will be a new feature, wouldnt it ? ) On Fri, Mar 30, 2018 at 4:10 PM, Murtuza Zabuawala < murtuza.zabuaw...@enterprisedb.com> wrote: > > > On Thu, Mar 29, 2018 at 11:45 PM, Joao De Almeida Pereira < > jdealmeidapere...@pivotal.io> wrote: > >> Hi Rahul, >> I see you extracted some code, that is a pretty good move :D >> >> We run the patch through the testing pipeline and everything is green GJ >> :D >> Also tested the functionality by hand and looks like it is working except >> for "add the .sql extension when format is set to SQL." if you set it to >> All Files the extension is also added. Not sure if this is a big deal or >> not. Lets see what other people think. >> > Yes, I also think it should append .sql only if the sql extension is > selected and user has not provided extension. > > Let say If I want to save the file with .txt extension then I can use > All Files. > > >> Codewise here are some of my comments: >> . You added the yarn-error.log file and a migration to the patch doesn't >> look intentional. Can you please remove them? >> . Also in the patch there are 2 file (moc_LogWindow.cpp and >> ui_LogWindow.h) that look like they do not belong to the patch (Did you >> rebase your branch before trying to create the patch? >> >> The test file: test_save_query_to_file.py is empty, it is missing some >> tests there. >> >> As a convention we user lower case names for functions and UpperCase for >> class >> >> Please, regenerate the patch following my previous comments. >> >> Thanks >> Joao >> >> On Thu, Mar 29, 2018 at 12:54 PM Rahul Soshte <rahulsoshte...@gmail.com> >> wrote: >> >>> Hi, >>> When using save or save as feature if .sql is not provided this Patch >>> appends it. >>> as clearly mentioned in this link. >>> >>> https://redmine.postgresql.org/issues/1998 >>> >>> I have ran pep8,regression and Jasmine tests too. >>> >>> I have primarily changed these files >>> web/pgadmin/tools/sqleditor/__init__.py >>> web/pgadmin/tools/sqleditor/static/js/sqleditor.js >>> web/pgadmin/tools/sqleditor/utils/save_query_to_file.py >>> >>> >>> Regards, >>> Rahul Soshte (Hunter) >>> >>> >
import os from pgadmin.misc.file_manager import Filemanager from pgadmin.utils import get_storage_directory from pgadmin.utils.ajax import make_json_response, bad_request, \ success_return, internal_server_error, unauthorized try: from urllib import unquote except ImportError: from urllib.parse import unquote def save_query_to_file(file_data): # retrieve storage directory path storage_manager_path = get_storage_directory() # generate full path of file file_path = unquote(file_data['file_name']) if hasattr(str, 'decode'): file_path = unquote( file_data['file_name'] ).encode('utf-8').decode('utf-8') if not file_path.endswith('.sql'): file_path = file_path + ".sql" try: Filemanager.check_access_permission(storage_manager_path, file_path) except Exception as e: return internal_server_error(errormsg=str(e)) # lstrip() returns a copy of the string # in which all chars have been stripped # from the beginning of the string (default whitespace characters). if storage_manager_path is not None: file_path = os.path.join( storage_manager_path, file_path.lstrip('/').lstrip('\\') ) if hasattr(str, 'decode'): file_content = file_data['file_content'] else: file_content = file_data['file_content'].encode() # write to file try: with open(file_path, 'wb+') as output_file: if hasattr(str, 'decode'): output_file.write(file_content.encode('utf-8')) return file_path else: output_file.write(file_content) return file_path except IOError as e: if e.strerror == 'Permission denied': err_msg = "Error: {0}".format(e.strerror) else: err_msg = "Error: {0}".format(e.strerror) return internal_server_error(errormsg=err_msg) except Exception as e: err_msg = "Error: {0}".format(e.strerror) return internal_server_error(errormsg=err_msg)
import sys from pgadmin.utils.route import BaseTestGenerator from pgadmin.tools.sqleditor.utils.save_query_to_file_utils import \ save_query_to_file from flask import Flask if sys.version_info < (3, 3): from mock import patch, ANY else: from unittest.mock import patch, ANY class TestSaveQueryToFile(BaseTestGenerator): """ Check that the save_query method works as intended """ scenarios = [ ( 'When user has not entered the extension .sql while saving the file', dict( file_data= { 'file_name': '/abc/xyz', 'file_content': 'some data here', }, expected_return_value='/abc/xyz.sql' ) ), ( 'When user has entered the extension .sql to the file while saving', dict( file_data= { 'file_name': '/abc/xyx.sql', 'file_content': 'some data here', }, expected_return_value='/abc/xyx.sql' ) ), ] def runTest(self): file_path_result = save_query_to_file(self.file_data) self.assertEquals(file_path_result, self.expected_return_value)