Fix Py4JJavaError serialization issue (#60)

* 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 <omri374@users.noreply.github.com>

* 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 <qunguyen@microsoft.com>

* 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 <rob@robbagby.com>
Co-authored-by: Prakash Kudkuli Vishnu <prvishnu@microsoft.com>
Co-authored-by: Omri Mendels <omri374@users.noreply.github.com>
Co-authored-by: quanuw <quanuw@gmail.com>
Co-authored-by: Quan Nguyen <qunguyen@microsoft.com>

* 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 <3589801+giventocode@users.noreply.github.com>
Co-authored-by: RobBagby <rob@robbagby.com>
Co-authored-by: Prakash Kudkuli Vishnu <prvishnu@microsoft.com>
Co-authored-by: Omri Mendels <omri374@users.noreply.github.com>
Co-authored-by: quanuw <quanuw@gmail.com>
Co-authored-by: Quan Nguyen <qunguyen@microsoft.com>
This commit is contained in:
Thomas Conté 2021-07-19 18:13:58 +02:00 коммит произвёл GitHub
Родитель 51a4357cbd
Коммит 1683ea1b53
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 55 добавлений и 7 удалений

2
.github/workflows/pythonpackage.yml поставляемый
Просмотреть файл

@ -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

Просмотреть файл

@ -3,9 +3,13 @@ Copyright (c) Microsoft Corporation.
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()
@ -30,6 +34,9 @@ class TestResults(PickleSerializable):
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")

Просмотреть файл

@ -3,11 +3,15 @@ Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
"""
import pytest
import json
from common.testresult import TestResults, TestResult
import pickle
import base64
import json
import pickle
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
@ -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
@ -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)