Skip to content

Commit

Permalink
make application test python 3.8 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
HomelessDinosaur committed May 8, 2023
1 parent 27ed61f commit 4e17012
Showing 1 changed file with 43 additions and 39 deletions.
82 changes: 43 additions & 39 deletions tests/test_application.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#
# Copyright (c) 2021 Nitric Technologies Pty Ltd.
#
# This file is part of Nitric Python 3 SDK.
# See https://github.com/nitrictech/python-sdk for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# #
# # Copyright (c) 2021 Nitric Technologies Pty Ltd.
# #
# # This file is part of Nitric Python 3 SDK.
# # See https://github.com/nitrictech/python-sdk for further info.
# #
# # Licensed under the Apache License, Version 2.0 (the "License");
# # you may not use this file except in compliance with the License.
# # You may obtain a copy of the License at
# #
# # http://www.apache.org/licenses/LICENSE-2.0
# #
# # Unless required by applicable law or agreed to in writing, software
# # distributed under the License is distributed on an "AS IS" BASIS,
# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# # See the License for the specific language governing permissions and
# # limitations under the License.
# #
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, AsyncMock, Mock, call
from unittest.mock import patch, AsyncMock, Mock

from grpclib import GRPCError, Status
from opentelemetry.sdk.trace import TracerProvider, sampling
Expand Down Expand Up @@ -66,11 +66,12 @@ def test_run(self):
mock_running_loop = Mock()
mock_event_loop = Mock()

with (patch("asyncio.get_event_loop", mock_event_loop), patch("asyncio.get_running_loop", mock_running_loop)):
application.run()
with patch("asyncio.get_event_loop", mock_event_loop):
with patch("asyncio.get_running_loop", mock_running_loop):
application.run()

mock_running_loop.assert_called_once()
mock_event_loop.assert_not_called()
mock_running_loop.assert_called_once()
mock_event_loop.assert_not_called()

def test_run_with_no_active_event_loop(self):
application = Nitric()
Expand All @@ -80,11 +81,12 @@ def test_run_with_no_active_event_loop(self):

mock_event_loop = Mock()

with (patch("asyncio.get_event_loop", mock_event_loop), patch("asyncio.get_running_loop", mock_running_loop)):
application.run()
with patch("asyncio.get_event_loop", mock_event_loop):
with patch("asyncio.get_running_loop", mock_running_loop):
application.run()

mock_running_loop.assert_called_once()
mock_event_loop.assert_called_once()
mock_running_loop.assert_called_once()
mock_event_loop.assert_called_once()

def test_run_with_keyboard_interrupt(self):
application = Nitric()
Expand All @@ -94,11 +96,12 @@ def test_run_with_keyboard_interrupt(self):

mock_event_loop = Mock()

with (patch("asyncio.get_event_loop", mock_event_loop), patch("asyncio.get_running_loop", mock_running_loop)):
application.run()
with patch("asyncio.get_event_loop", mock_event_loop):
with patch("asyncio.get_running_loop", mock_running_loop):
application.run()

mock_running_loop.assert_called_once()
mock_event_loop.assert_not_called()
mock_running_loop.assert_called_once()
mock_event_loop.assert_not_called()

def test_run_with_connection_refused(self):
application = Nitric()
Expand All @@ -108,12 +111,13 @@ def test_run_with_connection_refused(self):

mock_event_loop = Mock()

with (patch("asyncio.get_event_loop", mock_event_loop), patch("asyncio.get_running_loop", mock_running_loop)):
try:
application.run()
pytest.fail()
except NitricUnavailableException as e:
assert str(e).startswith("Unable to connect to a nitric server!")
with patch("asyncio.get_event_loop", mock_event_loop):
with patch("asyncio.get_running_loop", mock_running_loop):
try:
application.run()
pytest.fail()
except NitricUnavailableException as e:
assert str(e).startswith("Unable to connect to a nitric server!")

mock_running_loop.assert_called_once()
mock_event_loop.assert_not_called()
mock_running_loop.assert_called_once()
mock_event_loop.assert_not_called()

0 comments on commit 4e17012

Please sign in to comment.