Example #1
0
    def test_put_entity_w_completed_key(self):
        from gcloud.datastore.helpers import _property_tuples

        _PROJECT = 'PROJECT'
        _PROPERTIES = {
            'foo': 'bar',
            'baz': 'qux',
            'spam': [1, 2, 3],
            'frotz': [],  # will be ignored
        }
        connection = _Connection()
        client = _Client(_PROJECT, connection)
        batch = self._makeOne(client)
        entity = _Entity(_PROPERTIES)
        entity.exclude_from_indexes = ('baz', 'spam')
        key = entity.key = _Key(_PROJECT)

        batch.put(entity)

        mutated_entity = _mutated_pb(self, batch.mutations, 'upsert')
        self.assertEqual(mutated_entity.key, key._key)

        prop_dict = dict(_property_tuples(mutated_entity))
        self.assertEqual(len(prop_dict), 3)
        self.assertFalse(prop_dict['foo'].exclude_from_indexes)
        self.assertTrue(prop_dict['baz'].exclude_from_indexes)
        self.assertFalse(prop_dict['spam'].exclude_from_indexes)
        spam_values = prop_dict['spam'].array_value.values
        self.assertTrue(spam_values[0].exclude_from_indexes)
        self.assertTrue(spam_values[1].exclude_from_indexes)
        self.assertTrue(spam_values[2].exclude_from_indexes)
        self.assertFalse('frotz' in prop_dict)
Example #2
0
    def test_put_multi_no_batch_w_partial_key(self):
        from gcloud.datastore.helpers import _property_tuples
        from gcloud.datastore.test_batch import _Entity
        from gcloud.datastore.test_batch import _Key
        from gcloud.datastore.test_batch import _KeyPB

        entity = _Entity(foo=u'bar')
        key = entity.key = _Key(self.PROJECT)
        key._id = None

        creds = object()
        client = self._makeOne(credentials=creds)
        client.connection._commit.append([_KeyPB(key)])

        result = client.put_multi([entity])
        self.assertTrue(result is None)

        self.assertEqual(len(client.connection._commit_cw), 1)
        (project, commit_req, transaction_id) = client.connection._commit_cw[0]
        self.assertEqual(project, self.PROJECT)
        inserts = list(commit_req.mutation.insert_auto_id)
        self.assertEqual(len(inserts), 1)
        self.assertEqual(inserts[0].key, key.to_protobuf())

        prop_list = list(_property_tuples(inserts[0]))
        self.assertTrue(len(prop_list), 1)
        name, value_pb = prop_list[0]
        self.assertEqual(name, 'foo')
        self.assertEqual(value_pb.string_value, u'bar')
        self.assertTrue(transaction_id is None)
Example #3
0
    def _compareEntityProto(self, entity_pb1, entity_pb2):
        from gcloud.datastore.helpers import _property_tuples

        self.assertEqual(entity_pb1.key, entity_pb2.key)
        value_list1 = sorted(_property_tuples(entity_pb1))
        value_list2 = sorted(_property_tuples(entity_pb2))
        self.assertEqual(len(value_list1), len(value_list2))
        for pair1, pair2 in zip(value_list1, value_list2):
            name1, val1 = pair1
            name2, val2 = pair2
            self.assertEqual(name1, name2)
            if val1.HasField('entity_value'):  # Message field (Entity)
                self.assertEqual(val1.meaning, val2.meaning)
                self._compareEntityProto(val1.entity_value, val2.entity_value)
            else:
                self.assertEqual(val1, val2)
Example #4
0
    def test_put_multi_no_batch_w_partial_key(self):
        from gcloud.datastore.helpers import _property_tuples
        from gcloud.datastore.test_batch import _Entity
        from gcloud.datastore.test_batch import _Key
        from gcloud.datastore.test_batch import _KeyPB
        from gcloud.datastore.test_batch import _mutated_pb

        entity = _Entity(foo=u"bar")
        key = entity.key = _Key(self.PROJECT)
        key._id = None

        creds = object()
        client = self._makeOne(credentials=creds)
        client.connection._commit.append([_KeyPB(key)])

        result = client.put_multi([entity])
        self.assertTrue(result is None)

        self.assertEqual(len(client.connection._commit_cw), 1)
        (project, commit_req, transaction_id) = client.connection._commit_cw[0]
        self.assertEqual(project, self.PROJECT)

        mutated_entity = _mutated_pb(self, commit_req.mutations, "insert")
        self.assertEqual(mutated_entity.key, key.to_protobuf())

        prop_list = list(_property_tuples(mutated_entity))
        self.assertTrue(len(prop_list), 1)
        name, value_pb = prop_list[0]
        self.assertEqual(name, "foo")
        self.assertEqual(value_pb.string_value, u"bar")

        self.assertTrue(transaction_id is None)
Example #5
0
    def test_put_multi_no_batch_w_partial_key(self):
        from gcloud.datastore.helpers import _property_tuples
        from gcloud.datastore.test_batch import _Entity
        from gcloud.datastore.test_batch import _Key
        from gcloud.datastore.test_batch import _KeyPB

        entity = _Entity(foo=u'bar')
        key = entity.key = _Key(self.DATASET_ID)
        key._id = None

        creds = object()
        client = self._makeOne(credentials=creds)
        client.connection._commit.append([_KeyPB(key)])

        result = client.put_multi([entity])
        self.assertTrue(result is None)

        self.assertEqual(len(client.connection._commit_cw), 1)
        (dataset_id,
         commit_req, transaction_id) = client.connection._commit_cw[0]
        self.assertEqual(dataset_id, self.DATASET_ID)
        inserts = list(commit_req.mutation.insert_auto_id)
        self.assertEqual(len(inserts), 1)
        self.assertEqual(inserts[0].key, key.to_protobuf())

        prop_list = list(_property_tuples(inserts[0]))
        self.assertTrue(len(prop_list), 1)
        name, value_pb = prop_list[0]
        self.assertEqual(name, 'foo')
        self.assertEqual(value_pb.string_value, u'bar')
        self.assertTrue(transaction_id is None)
Example #6
0
    def test_put_entity_w_completed_key(self):
        from gcloud.datastore.helpers import _property_tuples

        _PROJECT = 'PROJECT'
        _PROPERTIES = {
            'foo': 'bar',
            'baz': 'qux',
            'spam': [1, 2, 3],
            'frotz': [],  # will be ignored
            }
        connection = _Connection()
        client = _Client(_PROJECT, connection)
        batch = self._makeOne(client)
        entity = _Entity(_PROPERTIES)
        entity.exclude_from_indexes = ('baz', 'spam')
        key = entity.key = _Key(_PROJECT)

        batch.put(entity)

        mutated_entity = _mutated_pb(self, batch.mutations, 'upsert')
        self.assertEqual(mutated_entity.key, key._key)

        prop_dict = dict(_property_tuples(mutated_entity))
        self.assertEqual(len(prop_dict), 3)
        self.assertFalse(prop_dict['foo'].exclude_from_indexes)
        self.assertTrue(prop_dict['baz'].exclude_from_indexes)
        self.assertFalse(prop_dict['spam'].exclude_from_indexes)
        spam_values = prop_dict['spam'].array_value.values
        self.assertTrue(spam_values[0].exclude_from_indexes)
        self.assertTrue(spam_values[1].exclude_from_indexes)
        self.assertTrue(spam_values[2].exclude_from_indexes)
        self.assertFalse('frotz' in prop_dict)
Example #7
0
    def _compareEntityProto(self, entity_pb1, entity_pb2):
        from gcloud.datastore.helpers import _property_tuples

        self.assertEqual(entity_pb1.key, entity_pb2.key)
        value_list1 = sorted(_property_tuples(entity_pb1))
        value_list2 = sorted(_property_tuples(entity_pb2))
        self.assertEqual(len(value_list1), len(value_list2))
        for pair1, pair2 in zip(value_list1, value_list2):
            name1, val1 = pair1
            name2, val2 = pair2
            self.assertEqual(name1, name2)
            if val1.HasField('entity_value'):  # Message field (Entity)
                self.assertEqual(val1.meaning, val2.meaning)
                self._compareEntityProto(val1.entity_value,
                                         val2.entity_value)
            else:
                self.assertEqual(val1, val2)
Example #8
0
    def test_entity_empty_wo_key(self):
        from gcloud.datastore.entity import Entity
        from gcloud.datastore.helpers import _property_tuples

        pb = self._makePB()
        entity = Entity()
        self._callFUT(pb, entity)
        value = pb.entity_value
        self.assertEqual(value.key.SerializeToString(), b'')
        self.assertEqual(len(list(_property_tuples(value))), 0)
Example #9
0
    def test_entity_empty_wo_key(self):
        from gcloud.datastore.entity import Entity
        from gcloud.datastore.helpers import _property_tuples

        pb = self._makePB()
        entity = Entity()
        self._callFUT(pb, entity)
        value = pb.entity_value
        self.assertEqual(value.key.SerializeToString(), b'')
        self.assertEqual(len(list(_property_tuples(value))), 0)
Example #10
0
    def test_entity_w_key(self):
        from gcloud.datastore.entity import Entity
        from gcloud.datastore.helpers import _property_tuples
        from gcloud.datastore.key import Key

        name = 'foo'
        value = u'Foo'
        pb = self._makePB()
        key = Key('KIND', 123, project='PROJECT')
        entity = Entity(key=key)
        entity[name] = value
        self._callFUT(pb, entity)
        entity_pb = pb.entity_value
        self.assertEqual(entity_pb.key, key.to_protobuf())

        prop_dict = dict(_property_tuples(entity_pb))
        self.assertEqual(len(prop_dict), 1)
        self.assertEqual(list(prop_dict.keys()), [name])
        self.assertEqual(prop_dict[name].string_value, value)
Example #11
0
    def test_entity_w_key(self):
        from gcloud.datastore.entity import Entity
        from gcloud.datastore.helpers import _property_tuples
        from gcloud.datastore.key import Key

        name = 'foo'
        value = u'Foo'
        pb = self._makePB()
        key = Key('KIND', 123, project='PROJECT')
        entity = Entity(key=key)
        entity[name] = value
        self._callFUT(pb, entity)
        entity_pb = pb.entity_value
        self.assertEqual(entity_pb.key, key.to_protobuf())

        prop_dict = dict(_property_tuples(entity_pb))
        self.assertEqual(len(prop_dict), 1)
        self.assertEqual(list(prop_dict.keys()), [name])
        self.assertEqual(prop_dict[name].string_value, value)
Example #12
0
    def test_put_multi_existing_batch_w_completed_key(self):
        from gcloud.datastore.helpers import _property_tuples
        from gcloud.datastore.test_batch import _Entity
        from gcloud.datastore.test_batch import _Key
        from gcloud.datastore.test_batch import _mutated_pb

        creds = object()
        client = self._makeOne(credentials=creds)
        entity = _Entity(foo=u'bar')
        key = entity.key = _Key(self.PROJECT)

        with _NoCommitBatch(client) as CURR_BATCH:
            result = client.put_multi([entity])

        self.assertEqual(result, None)
        mutated_entity = _mutated_pb(self, CURR_BATCH.mutations, 'upsert')
        self.assertEqual(mutated_entity.key, key.to_protobuf())

        prop_list = list(_property_tuples(mutated_entity))
        self.assertTrue(len(prop_list), 1)
        name, value_pb = prop_list[0]
        self.assertEqual(name, 'foo')
        self.assertEqual(value_pb.string_value, u'bar')
Example #13
0
    def test_put_multi_existing_batch_w_completed_key(self):
        from gcloud.datastore.helpers import _property_tuples
        from gcloud.datastore.test_batch import _Entity
        from gcloud.datastore.test_batch import _Key
        from gcloud.datastore.test_batch import _mutated_pb

        creds = object()
        client = self._makeOne(credentials=creds)
        entity = _Entity(foo=u"bar")
        key = entity.key = _Key(self.PROJECT)

        with _NoCommitBatch(client) as CURR_BATCH:
            result = client.put_multi([entity])

        self.assertEqual(result, None)
        mutated_entity = _mutated_pb(self, CURR_BATCH.mutations, "upsert")
        self.assertEqual(mutated_entity.key, key.to_protobuf())

        prop_list = list(_property_tuples(mutated_entity))
        self.assertTrue(len(prop_list), 1)
        name, value_pb = prop_list[0]
        self.assertEqual(name, "foo")
        self.assertEqual(value_pb.string_value, u"bar")
Example #14
0
 def _callFUT(self, entity_pb):
     from gcloud.datastore.helpers import _property_tuples
     return _property_tuples(entity_pb)
Example #15
0
 def _callFUT(self, entity_pb):
     from gcloud.datastore.helpers import _property_tuples
     return _property_tuples(entity_pb)