Commit 3e96e166 authored by Ayush Tiwari's avatar Ayush Tiwari Committed by Douglas

erp5-data-notebook bt5: Refactor testExecuteJupyter to new JupyterCompile...

erp5-data-notebook bt5: Refactor testExecuteJupyter to new JupyterCompile where we are letting errors fails loudly in erp5
parent 7be32a3f
...@@ -27,10 +27,11 @@ ...@@ -27,10 +27,11 @@
from Products.ERP5Type.tests.SecurityTestCase import SecurityTestCase from Products.ERP5Type.tests.SecurityTestCase import SecurityTestCase
from Products.ERP5Type.tests.utils import addUserToDeveloperRole from Products.ERP5Type.tests.utils import addUserToDeveloperRole
from Products.ERP5Type.tests.utils import createZODBPythonScript from Products.ERP5Type.tests.utils import createZODBPythonScript, removeZODBPythonScript
import time import time
import json import json
import transaction
class TestExecuteJupyter(SecurityTestCase): class TestExecuteJupyter(SecurityTestCase):
...@@ -69,27 +70,29 @@ class TestExecuteJupyter(SecurityTestCase): ...@@ -69,27 +70,29 @@ class TestExecuteJupyter(SecurityTestCase):
batch_mode=True batch_mode=True
) )
def testJupyterCompileErrorHandling(self): def testJupyterCompileErrorRaise(self):
""" """
Test if JupyterCompile portal_component can handle error in efficinet way. Test if JupyterCompile portal_component raises error on the server side.
Take the case in wich one line in a statement is valid and another is not. Take the case in which one line in a statement is valid and another is not.
""" """
portal = self.getPortalObject() portal = self.getPortalObject()
script_id = "JupyterCompile_errorResult" script_id = "JupyterCompile_errorResult"
script_container = portal.portal_skins.custom script_container = portal.portal_skins.custom
new_test_title = "Wendelin Test" new_test_title = "Wendelin Test 1"
# Check if the existing title is different from new_test_title or not # Check if the existing title is different from new_test_title or not
if portal.getTitle()==new_test_title: if portal.getTitle()==new_test_title:
new_test_title = "Wenedlin" new_test_title = "Wendelin"
python_script = """ python_script = """
context.setTitle('%s') portal = context.getPortalObject()
portal.setTitle('%s')
print an_undefined_variable print an_undefined_variable
"""%new_test_title """%new_test_title
# Create python_script object with the above given code and containers # Create python_script object with the above given code and containers
createZODBPythonScript(script_container, script_id, '', python_script) createZODBPythonScript(script_container, script_id, '', python_script)
self.tic()
# Call the above created script in jupyter_code # Call the above created script in jupyter_code
jupyter_code = """ jupyter_code = """
...@@ -98,49 +101,23 @@ portal.%s() ...@@ -98,49 +101,23 @@ portal.%s()
"""%script_id """%script_id
# Make call to Base_runJupyter to run the jupyter code which is making # Make call to Base_runJupyter to run the jupyter code which is making
# a call to the newly created ZODB pyton_script # a call to the newly created ZODB python_script and assert if the call raises
result = portal.Base_runJupyter(jupyter_code=jupyter_code, old_local_variable_dict={}) # NameError as we are sending an invalid python_code to it
self.assertRaises(
# Test if the status of returned messgae from Base_runJupyter call is 'error' NameError,
self.assertEquals(result['status'], 'error') portal.Base_runJupyter,
# Test that calling Base_runJupyter shouldn't change the context Title jupyter_code=jupyter_code,
self.assertNotEqual(portal.getTitle(), new_test_title) old_local_variable_dict={}
)
def testJupyterCompileErrorHandlingForTransactionAndNormalPythonScript(self): # Abort the current transaction of test so that we can proceed to new one
""" transaction.abort()
Test Base_runJupyter for error handling in case python_script contains simple # Clear the portal cache from previous transaction
python code(addition an setting varibale value) as well as transaction. self.portal.portal_caches.clearAllCache()
We expect that the local varibles a,b,c shouldn't set themselves due to failure # Remove the ZODB python script created above
in transaction. removeZODBPythonScript(script_container, script_id)
"""
portal = self.getPortalObject()
new_test_title = "Wendelin Test"
# Check if the existing title is different from new_test_title or not
if portal.getTitle()==new_test_title:
new_test_title = "Wenedlin"
# Python script where we use transaction as well as normal python variables
jupyter_code = """
a = 1
b = 2
context.setTitle('%s')
c = a+b
print c
print an_undefined_variable
"""%new_test_title
# Make call to Base_runJupyter to run the jupyter code which is making
# a call to the newly created ZODB pyton_script
result = portal.Base_runJupyter(jupyter_code=jupyter_code, old_local_variable_dict={})
# Test if the status of returned messgae from Base_runJupyter call is 'error'
self.assertEquals(result['status'], 'error')
# Test that calling Base_runJupyter shouldn't change the context Title # Test that calling Base_runJupyter shouldn't change the context Title
self.assertNotEqual(portal.getTitle(), new_test_title) self.assertNotEqual(portal.getTitle(), new_test_title)
# Test if the local variables a, b, c are set for there values or not
local_variable_list = ['a', 'b', 'c']
self.assertFalse(set(local_variable_list).issubset(set(result['local_variable_dict'].keys())))
def testUserCannotAccessBaseExecuteJupyter(self): def testUserCannotAccessBaseExecuteJupyter(self):
""" """
...@@ -255,10 +232,9 @@ print an_undefined_variable ...@@ -255,10 +232,9 @@ print an_undefined_variable
def testBaseExecuteJupyterErrorHandling(self): def testBaseExecuteJupyterErrorHandling(self):
""" """
Test if the error message are saved by the Data Notebook Message portal Test if the Base_executeJupyter with invalid python code raises error on
type. We don't want to ignore the error message or show the error message server side. We are not catching the exception here. Expected result is
as erp5 html. Best way would be to catch te exception and display it on raise of exception.
notebook frontend.
""" """
portal = self.portal portal = self.portal
self.login('dev_user') self.login('dev_user')
...@@ -266,15 +242,13 @@ print an_undefined_variable ...@@ -266,15 +242,13 @@ print an_undefined_variable
reference = 'Test.Notebook.ExecuteJupyterErrorHandling %s' % time.time() reference = 'Test.Notebook.ExecuteJupyterErrorHandling %s' % time.time()
title = 'Test NB Title %s' % time.time() title = 'Test NB Title %s' % time.time()
result = portal.Base_executeJupyter( self.assertRaises(
NameError,
portal.Base_executeJupyter,
title=title, title=title,
reference=reference, reference=reference,
python_expression=python_expression python_expression=python_expression
) )
self.tic()
expected_status = 'error'
self.assertEquals(json.loads(result)['status'].rstrip(), expected_status)
def testBaseExecuteJupyterSaveActiveResult(self): def testBaseExecuteJupyterSaveActiveResult(self):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment