Skip to content

Commit

Permalink
Fix Py4JJavaError serialization issue (#60)
Browse files Browse the repository at this point in the history
* Toc Update (#30)

* Release v0.1.34 (#52)

* Improved the error message when the call to the parent class constructor is missing in a test fixture

* Fixing the Environment variale setting documentation for Windows Powershell

* Apply suggestions from code review

Co-authored-by: Omri Mendels <[email protected]>

* Feature: Discover test files with '_test' suffix (#47)

* Enable test discovery for test names with suffix 'test'

* Combine redundant suffix tests

* Remove old suffix tests

* Encapsulate test name parsing and have nuttercli call test name validation from api

* Have api client results call _is_valid_test_name from api

Co-authored-by: Quan Nguyen <[email protected]>

* Invalid State response is retriable (#49)

* fixed import error and refactoring

* invalid state is retriable, pull sleep is 5 seconds

* Poll wait time as flag (#51)

* poll wait time as flag

* lint fixes

Co-authored-by: RobBagby <[email protected]>
Co-authored-by: Prakash Kudkuli Vishnu <[email protected]>
Co-authored-by: Omri Mendels <[email protected]>
Co-authored-by: quanuw <[email protected]>
Co-authored-by: Quan Nguyen <[email protected]>

* Update README.md

* Fix Py4JJavaError serialization issue

* Test Py4JJavaError using mocks

* Remove Py4J stuff

* Still need to install py4j to run tests

Co-authored-by: Jesus Aguilar <[email protected]>
Co-authored-by: RobBagby <[email protected]>
Co-authored-by: Prakash Kudkuli Vishnu <[email protected]>
Co-authored-by: Omri Mendels <[email protected]>
Co-authored-by: quanuw <[email protected]>
Co-authored-by: Quan Nguyen <[email protected]>
  • Loading branch information
7 people committed Dec 15, 2022
1 parent 8747087 commit bc0bb37
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pip install pytest
pip install pytest py4j
pytest
11 changes: 9 additions & 2 deletions common/testresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
Licensed under the MIT license.
"""

from .pickleserializable import PickleSerializable
import pickle
import base64
import pickle

from py4j.protocol import Py4JJavaError

from .pickleserializable import PickleSerializable


def get_test_results():
return TestResults()
Expand All @@ -30,6 +34,9 @@ def append(self, testresult):
self.total_execution_time = total_execution_time

def serialize(self):
for i in self.results:
if isinstance(i.exception, Py4JJavaError):
i.exception = Exception(str(i.exception))
bin_data = pickle.dumps(self)
return str(base64.encodebytes(bin_data), "utf-8")

Expand Down
47 changes: 44 additions & 3 deletions tests/nutter/test_testresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
Licensed under the MIT license.
"""

import pytest
import base64
import json
from common.testresult import TestResults, TestResult
import pickle
import base64

import mock
import pytest
from common.testresult import TestResult, TestResults
from py4j.protocol import Py4JError, Py4JJavaError


def test__testresults_append__type_not_testresult__throws_error():
# Arrange
Expand Down Expand Up @@ -74,6 +78,21 @@ def test__deserialize__invalid_pickle_data__throws_Exception():
with pytest.raises(Exception):
test_results.deserialize(invalid_pickle)

def test__deserialize__p4jjavaerror__is_serializable_and_deserializable():
# Arrange
test_results = TestResults()

py4j_exception = get_mock_py4j_error_exception(get_mock_gateway_client(), mock_target_id="o123")

test_results.append(TestResult("Test Name", True, 1, [], py4j_exception))

with mock.patch('py4j.protocol.get_return_value') as mock_get_return_value:
mock_get_return_value.return_value = 'foo'
serialized_data = test_results.serialize()
deserialized_data = TestResults().deserialize(serialized_data)

assert test_results == deserialized_data


def test__eq__test_results_equal_but_not_same_ref__are_equal():
# Arrange
Expand Down Expand Up @@ -139,3 +158,25 @@ def test__deserialize__data_is_base64_str__can_deserialize():
test_results_from_data = TestResults().deserialize(serialized_str)

assert test_results == test_results_from_data


def get_mock_gateway_client():
mock_client = mock.Mock()
mock_client.send_command.return_value = "0"
mock_client.converters = []
mock_client.is_connected.return_value = True
mock_client.deque = mock.Mock()
return mock_client


def get_mock_java_object(mock_client, mock_target_id):
mock_java_object = mock.Mock()
mock_java_object._target_id = mock_target_id
mock_java_object._gateway_client = mock_client
return mock_java_object


def get_mock_py4j_error_exception(mock_client, mock_target_id):
mock_java_object = get_mock_java_object(mock_client, mock_target_id)
mock_errmsg = "An error occurred while calling {}.load.".format(mock_target_id)
return Py4JJavaError(mock_errmsg, java_exception=mock_java_object)

0 comments on commit bc0bb37

Please sign in to comment.