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,50 +101,24 @@ portal.%s() ...@@ -98,50 +101,24 @@ 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(
NameError,
portal.Base_runJupyter,
jupyter_code=jupyter_code,
old_local_variable_dict={}
)
# Abort the current transaction of test so that we can proceed to new one
transaction.abort()
# Clear the portal cache from previous transaction
self.portal.portal_caches.clearAllCache()
# Remove the ZODB python script created above
removeZODBPythonScript(script_container, script_id)
# 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)
def testJupyterCompileErrorHandlingForTransactionAndNormalPythonScript(self):
"""
Test Base_runJupyter for error handling in case python_script contains simple
python code(addition an setting varibale value) as well as transaction.
We expect that the local varibles a,b,c shouldn't set themselves due to failure
in transaction.
"""
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
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):
""" """
Test if non developer user can't access Base_executeJupyter Test if non developer user can't access Base_executeJupyter
...@@ -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(
title=title, NameError,
reference=reference, portal.Base_executeJupyter,
python_expression=python_expression title=title,
) reference=reference,
self.tic() python_expression=python_expression
)
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