Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chemelnucfin committed Nov 6, 2017
1 parent f7aa293 commit 9605172
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 52 deletions.
12 changes: 4 additions & 8 deletions datastore/google/cloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@

from google.cloud._helpers import _LocalStack
from google.cloud._helpers import (_determine_default_project as
_default_project)

_base_default_project)
from google.cloud.client import ClientWithProject

from google.cloud.datastore import helpers
from google.cloud.datastore._http import HTTPDatastoreAPI
from google.cloud.datastore.batch import Batch
from google.cloud.datastore.entity import Entity
from google.cloud.datastore.key import Key
from google.cloud.datastore.query import Query
from google.cloud.datastore.transaction import Transaction

from google.cloud.environment_vars import DISABLE_GRPC
from google.cloud.environment_vars import GCD_DATASET
from google.cloud.environment_vars import GCD_HOST
Expand Down Expand Up @@ -75,7 +72,7 @@ def _determine_default_project(project=None):
project = _get_gcd_project()

if project is None:
project = _default_project(project=project)
project = _base_default_project(project=project)

return project

Expand Down Expand Up @@ -306,9 +303,8 @@ def get(self, key, missing=None, deferred=None,
:type eventual: bool
:param eventual: (Optional) Defaults to strongly consistent (False).
Setting True will use eventual consistency,
but cannot be used inside a transaction or
will raise ValueError.
Setting True will use eventual consistency, but cannot
be used inside a transaction or will raise ValueError.
:rtype: :class:`google.cloud.datastore.entity.Entity` or ``NoneType``
:returns: The requested entity if it exists.
Expand Down
14 changes: 7 additions & 7 deletions datastore/google/cloud/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
from google.cloud._helpers import _pb_timestamp_to_datetime
from google.cloud.datastore.entity import Entity
from google.cloud.datastore.key import Key
from google.cloud.proto.datastore.v1 import entity_pb2 as _entity_pb2
from google.cloud.proto.datastore.v1 import datastore_pb2 as _datastore_pb2
from google.cloud.proto.datastore.v1 import entity_pb2
from google.cloud.proto.datastore.v1 import datastore_pb2

from google.protobuf import struct_pb2
from google.type import latlng_pb2
Expand Down Expand Up @@ -205,7 +205,7 @@ def entity_to_protobuf(entity):
:rtype: :class:`.entity_pb2.Entity`
:returns: The protobuf representing the entity.
"""
entity_pb = _entity_pb2.Entity()
entity_pb = entity_pb2.Entity()
if entity.key is not None:
key_pb = entity.key.to_protobuf()
entity_pb.key.CopyFrom(key_pb)
Expand Down Expand Up @@ -253,15 +253,15 @@ def get_read_options(eventual, transaction_id):
"""
if transaction_id is None:
if eventual:
return _datastore_pb2.ReadOptions(
read_consistency=_datastore_pb2.ReadOptions.EVENTUAL)
return datastore_pb2.ReadOptions(
read_consistency=datastore_pb2.ReadOptions.EVENTUAL)
else:
return _datastore_pb2.ReadOptions()
return datastore_pb2.ReadOptions()
else:
if eventual:
raise ValueError('eventual must be False when in a transaction')
else:
return _datastore_pb2.ReadOptions(
return datastore_pb2.ReadOptions(
transaction=transaction_id)


Expand Down
39 changes: 2 additions & 37 deletions datastore/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def fallback_mock(project=None):
patch = mock.patch.multiple(
'google.cloud.datastore.client',
_get_gcd_project=gcd_mock,
_default_project=fallback_mock)
_base_default_project=fallback_mock)
with patch:
returned_project = self._call_fut(project_called)

Expand Down Expand Up @@ -138,7 +138,7 @@ def test_constructor_w_project_no_environ(self):
# Some environments (e.g. AppVeyor CI) run in GCE, so
# this test would fail artificially.
patch = mock.patch(
'google.cloud.datastore.client._default_project',
'google.cloud.datastore.client._base_default_project',
return_value=None)
with patch:
self.assertRaises(EnvironmentError, self._make_one, None)
Expand Down Expand Up @@ -1010,41 +1010,6 @@ def test_query_w_namespace_collision(self):
client, project=self.PROJECT, namespace=namespace2, kind=kind)


class Test__get_read_options(unittest.TestCase):

def _call_fut(self, eventual, transaction_id):
from google.cloud.datastore.helpers import get_read_options

return get_read_options(eventual, transaction_id)

def test_eventual_w_transaction(self):
with self.assertRaises(ValueError):
self._call_fut(True, b'123')

def test_eventual_wo_transaction(self):
from google.cloud.proto.datastore.v1 import datastore_pb2

read_options = self._call_fut(True, None)
expected = datastore_pb2.ReadOptions(
read_consistency=datastore_pb2.ReadOptions.EVENTUAL)
self.assertEqual(read_options, expected)

def test_default_w_transaction(self):
from google.cloud.proto.datastore.v1 import datastore_pb2

txn_id = b'123abc-easy-as'
read_options = self._call_fut(False, txn_id)
expected = datastore_pb2.ReadOptions(transaction=txn_id)
self.assertEqual(read_options, expected)

def test_default_wo_transaction(self):
from google.cloud.proto.datastore.v1 import datastore_pb2

read_options = self._call_fut(False, None)
expected = datastore_pb2.ReadOptions()
self.assertEqual(read_options, expected)


class _NoCommitBatch(object):

def __init__(self, client):
Expand Down
35 changes: 35 additions & 0 deletions datastore/tests/unit/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,41 @@ def test_w_nothing_in_pb(self):
self.assertRaises(ValueError, self._call_fut, pb)


class Test__get_read_options(unittest.TestCase):

def _call_fut(self, eventual, transaction_id):
from google.cloud.datastore.helpers import get_read_options

return get_read_options(eventual, transaction_id)

def test_eventual_w_transaction(self):
with self.assertRaises(ValueError):
self._call_fut(True, b'123')

def test_eventual_wo_transaction(self):
from google.cloud.proto.datastore.v1 import datastore_pb2

read_options = self._call_fut(True, None)
expected = datastore_pb2.ReadOptions(
read_consistency=datastore_pb2.ReadOptions.EVENTUAL)
self.assertEqual(read_options, expected)

def test_default_w_transaction(self):
from google.cloud.proto.datastore.v1 import datastore_pb2

txn_id = b'123abc-easy-as'
read_options = self._call_fut(False, txn_id)
expected = datastore_pb2.ReadOptions(transaction=txn_id)
self.assertEqual(read_options, expected)

def test_default_wo_transaction(self):
from google.cloud.proto.datastore.v1 import datastore_pb2

read_options = self._call_fut(False, None)
expected = datastore_pb2.ReadOptions()
self.assertEqual(read_options, expected)


class Test__pb_attr_value(unittest.TestCase):

def _call_fut(self, val):
Expand Down

0 comments on commit 9605172

Please sign in to comment.