8000 tests: refactor unittests using pytest idioms by tseaver · Pull Request #235 · googleapis/python-datastore · GitHub
[go: up one dir, main page]

Skip to content

tests: refactor unittests using pytest idioms #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 63 additions & 68 deletions tests/unit/test__gapic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,86 +12,81 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import mock
import pytest

from google.cloud.datastore.client import _HAVE_GRPC


@unittest.skipUnless(_HAVE_GRPC, "No gRPC")
class Test_make_datastore_api(unittest.TestCase):
def _call_fut(self, client):
from google.cloud.datastore._gapic import make_datastore_api

return make_datastore_api(client)

@mock.patch(
"google.cloud.datastore_v1.services.datastore.client.DatastoreClient",
return_value=mock.sentinel.ds_client,
@pytest.mark.skipif(not _HAVE_GRPC, reason="No gRPC")
@mock.patch(
"google.cloud.datastore_v1.services.datastore.client.DatastoreClient",
return_value=mock.sentinel.ds_client,
)
@mock.patch(
"google.cloud.datastore_v1.services.datastore.transports.grpc.DatastoreGrpcTransport",
return_value=mock.sentinel.transport,
)
@mock.patch(
"google.cloud.datastore._gapic.make_secure_channel",
return_value=mock.sentinel.channel,
)
def test_live_api(make_chan, mock_transport, mock_klass):
from google.cloud._http import DEFAULT_USER_AGENT
from google.cloud.datastore._gapic import make_datastore_api

base_url = "https://datastore.googleapis.com:443"
client = mock.Mock(
_base_url=base_url,
_credentials=mock.sentinel.credentials,
_client_info=mock.sentinel.client_info,
spec=["_base_url", "_credentials", "_client_info"],
)
@mock.patch(
"google.cloud.datastore_v1.services.datastore.transports.grpc.DatastoreGrpcTransport",
return_value=mock.sentinel.transport,
)
@mock.patch(
"google.cloud.datastore._gapic.make_secure_channel",
return_value=mock.sentinel.channel,
)
def test_live_api(self, make_chan, mock_transport, mock_klass):
from google.cloud._http import DEFAULT_USER_AGENT
ds_api = make_datastore_api(client)
assert ds_api is mock.sentinel.ds_client

base_url = "https://datastore.googleapis.com:443"
client = mock.Mock(
_base_url=base_url,
_credentials=mock.sentinel.credentials,
_client_info=mock.sentinel.client_info,
spec=["_base_url", "_credentials", "_client_info"],
)
ds_api = self._call_fut(client)
self.assertIs(ds_api, mock.sentinel.ds_client)
mock_transport.assert_called_once_with(channel=mock.sentinel.channel)

mock_transport.assert_called_once_with(channel=mock.sentinel.channel)
make_chan.assert_called_once_with(
mock.sentinel.credentials, DEFAULT_USER_AGENT, "datastore.googleapis.com:443",
)

make_chan.assert_called_once_with(
mock.sentinel.credentials,
DEFAULT_USER_AGENT,
"datastore.googleapis.com:443",
)
mock_klass.assert_called_once_with(
transport=mock.sentinel.transport, client_info=mock.sentinel.client_info
)

mock_klass.assert_called_once_with(
transport=mock.sentinel.transport, client_info=mock.sentinel.client_info
)

@mock.patch(
"google.cloud.datastore_v1.services.datastore.client.DatastoreClient",
return_value=mock.sentinel.ds_client,
)
@mock.patch(
"google.cloud.datastore_v1.services.datastore.transports.grpc.DatastoreGrpcTransport",
return_value=mock.sentinel.transport,
)
@mock.patch(
"google.cloud.datastore._gapic.insecure_channel",
return_value=mock.sentinel.channel,
@pytest.mark.skipif(not _HAVE_GRPC, reason="No gRPC")
@mock.patch(
"google.cloud.datastore_v1.services.datastore.client.DatastoreClient",
return_value=mock.sentinel.ds_client,
)
@mock.patch(
"google.cloud.datastore_v1.services.datastore.transports.grpc.DatastoreGrpcTransport",
return_value=mock.sentinel.transport,
)
@mock.patch(
"google.cloud.datastore._gapic.insecure_channel",
return_value=mock.sentinel.channel,
)
def test_emulator(make_chan, mock_transport, mock_klass):
from google.cloud.datastore._gapic import make_datastore_api

host = "localhost:8901"
base_url = "http://" + host
client = mock.Mock(
_base_url=base_url,
_credentials=mock.sentinel.credentials,
_client_info=mock.sentinel.client_info,
spec=["_base_url", "_credentials", "_client_info"],
)
def test_emulator(self, make_chan, mock_transport, mock_klass):
ds_api = make_datastore_api(client)
assert ds_api is mock.sentinel.ds_client

host = "localhost:8901"
base_url = "http://" + host
client = mock.Mock(
_base_url=base_url,
_credentials=mock.sentinel.credentials,
_client_info=mock.sentinel.client_info,
spec=["_base_url", "_credentials", "_client_info"],
)
ds_api = self._call_fut(client)
self.assertIs(ds_api, mock.sentinel.ds_client)
mock_transport.assert_called_once_with(channel=mock.sentinel.channel)

mock_transport.assert_called_once_with(channel=mock.sentinel.channel)
make_chan.assert_called_once_with(host)

make_chan.assert_called_once_with(host)

mock_klass.assert_called_once_with(
transport=mock.sentinel.transport, client_info=mock.sentinel.client_info
)
mock_klass.assert_called_once_with(
transport=mock.sentinel.transport, client_info=mock.sentinel.client_info
)
Loading
0