Пример #1
0
    def test_ctor_missing_required(self):
        from gcloud.datastore._testing import _monkey_defaults

        with _monkey_defaults():
            self.assertRaises(ValueError, self._makeOne)
            self.assertRaises(ValueError, self._makeOne, dataset_id=object())
            self.assertRaises(ValueError, self._makeOne, connection=object())
Пример #2
0
    def test_implicit_connection(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.test_batch import _Connection
        from gcloud.datastore.test_batch import _Entity
        from gcloud.datastore.test_batch import _Key

        # Build basic mocks needed to delete.
        _DATASET = 'DATASET'
        connection = _Connection()
        entity = _Entity(foo=u'bar')
        key = entity.key = _Key(_DATASET)

        with _monkey_defaults(connection=connection):
            # Set up Batch on stack so we can check it is used.
            with _NoCommitBatch(_DATASET, connection) as CURR_BATCH:
                result = self._callFUT(entity)

        self.assertEqual(result, None)
        self.assertEqual(len(CURR_BATCH.mutation.insert_auto_id), 0)
        self.assertEqual(len(CURR_BATCH.mutation.upsert), 1)
        upserts = list(CURR_BATCH.mutation.upsert)
        self.assertEqual(len(upserts), 1)
        self.assertEqual(upserts[0].key, key.to_protobuf())
        properties = list(upserts[0].property)
        self.assertEqual(properties[0].name, 'foo')
        self.assertEqual(properties[0].value.string_value, u'bar')
        self.assertEqual(len(CURR_BATCH.mutation.delete), 0)
Пример #3
0
    def test_implicit_connection(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.test_batch import _Connection
        from gcloud.datastore.test_batch import _Entity
        from gcloud.datastore.test_batch import _Key

        # Build basic mocks needed to delete.
        _DATASET = 'DATASET'
        connection = _Connection()
        entity = _Entity(foo=u'bar')
        key = entity.key = _Key(_DATASET)

        with _monkey_defaults(connection=connection):
            # Set up Batch on stack so we can check it is used.
            with _NoCommitBatch(_DATASET, connection) as CURR_BATCH:
                result = self._callFUT([entity])

        self.assertEqual(result, None)
        self.assertEqual(len(CURR_BATCH.mutation.insert_auto_id), 0)
        self.assertEqual(len(CURR_BATCH.mutation.upsert), 1)
        upserts = list(CURR_BATCH.mutation.upsert)
        self.assertEqual(len(upserts), 1)
        self.assertEqual(upserts[0].key, key.to_protobuf())
        properties = list(upserts[0].property)
        self.assertEqual(properties[0].name, 'foo')
        self.assertEqual(properties[0].value.string_value, u'bar')
        self.assertEqual(len(CURR_BATCH.mutation.delete), 0)
Пример #4
0
    def test_ctor_missing_required(self):
        from gcloud.datastore._testing import _monkey_defaults

        with _monkey_defaults():
            self.assertRaises(ValueError, self._makeOne)
            self.assertRaises(ValueError, self._makeOne, dataset_id=object())
            self.assertRaises(ValueError, self._makeOne, connection=object())
Пример #5
0
    def test_with_already_completed_key(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.key import Key
        from gcloud.datastore.test_connection import _Connection

        CUSTOM_CONNECTION = _Connection()
        with _monkey_defaults(connection=CUSTOM_CONNECTION,
                              dataset_id='DATASET'):
            COMPLETE_KEY = Key('KIND', 1234)
            self.assertRaises(ValueError, self._callFUT,
                              COMPLETE_KEY, 2)
Пример #6
0
    def test_with_already_completed_key(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.key import Key
        from gcloud.datastore.test_connection import _Connection

        CUSTOM_CONNECTION = _Connection()
        with _monkey_defaults(connection=CUSTOM_CONNECTION,
                              dataset_id='DATASET'):
            COMPLETE_KEY = Key('KIND', 1234)
            self.assertRaises(ValueError, self._callFUT,
                              COMPLETE_KEY, 2)
Пример #7
0
    def test_ctor_with_env(self):
        from gcloud.datastore._testing import _monkey_defaults

        CONNECTION = _Connection()
        DATASET_ID = 'DATASET'
        with _monkey_defaults(connection=CONNECTION, dataset_id=DATASET_ID):
            xact = self._makeOne()

        self.assertEqual(xact.id, None)
        self.assertEqual(xact.dataset_id, DATASET_ID)
        self.assertEqual(xact.connection, CONNECTION)
        self.assertEqual(xact._status, self._getTargetClass()._INITIAL)
Пример #8
0
    def test_ctor_with_env(self):
        from gcloud.datastore._testing import _monkey_defaults

        CONNECTION = _Connection()
        DATASET_ID = 'DATASET'
        with _monkey_defaults(connection=CONNECTION, dataset_id=DATASET_ID):
            xact = self._makeOne()

        self.assertEqual(xact.id, None)
        self.assertEqual(xact.dataset_id, DATASET_ID)
        self.assertEqual(xact.connection, CONNECTION)
        self.assertEqual(xact._status, self._getTargetClass()._INITIAL)
Пример #9
0
    def test_ctor_implicit(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore._datastore_v1_pb2 import Mutation
        _DATASET = 'DATASET'
        CONNECTION = _Connection()

        with _monkey_defaults(connection=CONNECTION, dataset_id=_DATASET):
            batch = self._makeOne()

        self.assertEqual(batch.dataset_id, _DATASET)
        self.assertEqual(batch.connection, CONNECTION)
        self.assertTrue(isinstance(batch.mutation, Mutation))
        self.assertEqual(batch._auto_id_entities, [])
Пример #10
0
    def test_fetch_defaults_w_implicit_connection(self):
        from gcloud.datastore._testing import _monkey_defaults

        _DATASET = 'DATASET'
        _KIND = 'KIND'
        connection = _Connection()
        query = self._makeOne(_KIND, _DATASET)

        with _monkey_defaults(connection=connection):
            iterator = query.fetch()
        self.assertTrue(iterator._query is query)
        self.assertEqual(iterator._limit, None)
        self.assertEqual(iterator._offset, 0)
Пример #11
0
    def test_fetch_defaults_w_implicit_connection(self):
        from gcloud.datastore._testing import _monkey_defaults

        _DATASET = 'DATASET'
        _KIND = 'KIND'
        connection = _Connection()
        query = self._makeOne(_KIND, _DATASET)

        with _monkey_defaults(connection=connection):
            iterator = query.fetch()
        self.assertTrue(iterator._query is query)
        self.assertEqual(iterator._limit, None)
        self.assertEqual(iterator._offset, 0)
Пример #12
0
    def test_ctor_implicit(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore._datastore_v1_pb2 import Mutation
        _DATASET = 'DATASET'
        CONNECTION = _Connection()

        with _monkey_defaults(connection=CONNECTION, dataset_id=_DATASET):
            batch = self._makeOne()

        self.assertEqual(batch.dataset_id, _DATASET)
        self.assertEqual(batch.connection, CONNECTION)
        self.assertTrue(isinstance(batch.mutation, Mutation))
        self.assertEqual(batch._auto_id_entities, [])
Пример #13
0
    def test_w_implicit_connection(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.key import Key
        from gcloud.datastore.test_connection import _Connection

        CUSTOM_CONNECTION = _Connection()
        NUM_IDS = 2
        with _monkey_defaults(connection=CUSTOM_CONNECTION,
                              dataset_id='DATASET'):
            INCOMPLETE_KEY = Key('KIND')
            result = self._callFUT(INCOMPLETE_KEY, NUM_IDS)

        # Check the IDs returned.
        self.assertEqual([key.id for key in result], list(range(NUM_IDS)))
Пример #14
0
    def test_ctor_defaults_w_implicit_dataset_id(self):
        from gcloud.datastore._testing import _monkey_defaults

        _DATASET = 'DATASET'
        with _monkey_defaults(dataset_id=_DATASET):
            query = self._makeOne()
        self.assertEqual(query.dataset_id, _DATASET)
        self.assertEqual(query.kind, None)
        self.assertEqual(query.namespace, None)
        self.assertEqual(query.ancestor, None)
        self.assertEqual(query.filters, [])
        self.assertEqual(query.projection, [])
        self.assertEqual(query.order, [])
        self.assertEqual(query.group_by, [])
Пример #15
0
    def test_ctor_defaults_w_implicit_dataset_id(self):
        from gcloud.datastore._testing import _monkey_defaults

        _DATASET = 'DATASET'
        with _monkey_defaults(dataset_id=_DATASET):
            query = self._makeOne()
        self.assertEqual(query.dataset_id, _DATASET)
        self.assertEqual(query.kind, None)
        self.assertEqual(query.namespace, None)
        self.assertEqual(query.ancestor, None)
        self.assertEqual(query.filters, [])
        self.assertEqual(query.projection, [])
        self.assertEqual(query.order, [])
        self.assertEqual(query.group_by, [])
Пример #16
0
    def test_w_implicit_connection(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.key import Key
        from gcloud.datastore.test_connection import _Connection

        CUSTOM_CONNECTION = _Connection()
        NUM_IDS = 2
        with _monkey_defaults(connection=CUSTOM_CONNECTION,
                              dataset_id='DATASET'):
            INCOMPLETE_KEY = Key('KIND')
            result = self._callFUT(INCOMPLETE_KEY, NUM_IDS)

        # Check the IDs returned.
        self.assertEqual([key.id for key in result], list(range(NUM_IDS)))
Пример #17
0
    def test_wo_batch_w_key_different_than_default_dataset_id(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.test_batch import _Connection
        from gcloud.datastore.test_batch import _Key

        # Build basic mocks needed to delete.
        _DEFAULT_DATASET = 'DEFAULT'
        _DATASET = 'DATASET'
        connection = _Connection()
        key = _Key(_DATASET)

        with _monkey_defaults(connection=connection,
                              dataset_id=_DEFAULT_DATASET):
            result = self._callFUT([key])
        self.assertEqual(result, None)
        self.assertEqual(len(connection._committed), 1)
        dataset_id, mutation = connection._committed[0]
        self.assertEqual(dataset_id, _DATASET)
        self.assertEqual(list(mutation.delete), [key.to_protobuf()])
Пример #18
0
    def test_wo_batch_w_key_different_than_default_dataset_id(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.test_batch import _Connection
        from gcloud.datastore.test_batch import _Key

        # Build basic mocks needed to delete.
        _DEFAULT_DATASET = 'DEFAULT'
        _DATASET = 'DATASET'
        connection = _Connection()
        key = _Key(_DATASET)

        with _monkey_defaults(connection=connection,
                              dataset_id=_DEFAULT_DATASET):
            result = self._callFUT([key])
        self.assertEqual(result, None)
        self.assertEqual(len(connection._committed), 1)
        dataset_id, mutation = connection._committed[0]
        self.assertEqual(dataset_id, _DATASET)
        self.assertEqual(list(mutation.delete), [key.to_protobuf()])
Пример #19
0
    def test_implicit_connection_and_dataset_id(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.test_batch import _Connection
        from gcloud.datastore.test_batch import _Key

        # Build basic mocks needed to delete.
        _DATASET = 'DATASET'
        connection = _Connection()
        key = _Key(_DATASET)

        with _monkey_defaults(connection=connection, dataset_id=_DATASET):
            # Set up Batch on stack so we can check it is used.
            with _NoCommitBatch(_DATASET, connection) as CURR_BATCH:
                result = self._callFUT([key])

        self.assertEqual(result, None)
        self.assertEqual(len(CURR_BATCH.mutation.insert_auto_id), 0)
        self.assertEqual(len(CURR_BATCH.mutation.upsert), 0)
        deletes = list(CURR_BATCH.mutation.delete)
        self.assertEqual(len(deletes), 1)
        self.assertEqual(deletes[0], key._key)
        self.assertEqual(len(connection._committed), 0)
Пример #20
0
    def test_implicit_connection_and_dataset_id(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.test_batch import _Connection
        from gcloud.datastore.test_batch import _Key

        # Build basic mocks needed to delete.
        _DATASET = 'DATASET'
        connection = _Connection()
        key = _Key(_DATASET)

        with _monkey_defaults(connection=connection, dataset_id=_DATASET):
            # Set up Batch on stack so we can check it is used.
            with _NoCommitBatch(_DATASET, connection) as CURR_BATCH:
                result = self._callFUT([key])

        self.assertEqual(result, None)
        self.assertEqual(len(CURR_BATCH.mutation.insert_auto_id), 0)
        self.assertEqual(len(CURR_BATCH.mutation.upsert), 0)
        deletes = list(CURR_BATCH.mutation.delete)
        self.assertEqual(len(deletes), 1)
        self.assertEqual(deletes[0], key._key)
        self.assertEqual(len(connection._committed), 0)
Пример #21
0
    def test_implicit_wo_transaction(self):
        from gcloud.datastore._testing import _monkey_defaults
        from gcloud.datastore.key import Key
        from gcloud.datastore.test_connection import _Connection

        DATASET_ID = 'DATASET'
        KIND = 'Kind'
        ID = 1234
        PATH = [{'kind': KIND, 'id': ID}]

        # Make a found entity pb to be returned from mock backend.
        entity_pb = self._make_entity_pb(DATASET_ID, KIND, ID,
                                         'foo', 'Foo')

        # Make a connection to return the entity pb.
        CUSTOM_CONNECTION = _Connection(entity_pb)

        key = Key(KIND, ID, dataset_id=DATASET_ID)
        with _monkey_defaults(connection=CUSTOM_CONNECTION,
                              dataset_id=DATASET_ID):
            result, = self._callFUT([key])

        expected_called_with = {
            'dataset_id': DATASET_ID,
            'key_pbs': [key.to_protobuf()],
            'transaction_id': None,
            'eventual': False,
        }
        self.assertEqual(CUSTOM_CONNECTION._called_with, expected_called_with)

        new_key = result.key
        # Check the returned value is as expected.
        self.assertFalse(new_key is key)
        self.assertEqual(new_key.dataset_id, DATASET_ID)
        self.assertEqual(new_key.path, PATH)
        self.assertEqual(list(result), ['foo'])
        self.assertEqual(result['foo'], 'Foo')
Пример #22
0
 def _monkey(self, dataset_id):
     from gcloud.datastore._testing import _monkey_defaults
     return _monkey_defaults(dataset_id=dataset_id)
    def test_preset(self):
        from gcloud.datastore._testing import _monkey_defaults

        SENTINEL = object()
        with _monkey_defaults(dataset_id=SENTINEL):
            self.assertEqual(self._callFUT(), SENTINEL)
Пример #24
0
 def _monkeyDatasetID(self, dataset_id=_DEFAULT_DATASET):
     from gcloud.datastore._testing import _monkey_defaults
     return _monkey_defaults(dataset_id=dataset_id)
Пример #25
0
 def _monkey(self, dataset_id):
     from gcloud.datastore._testing import _monkey_defaults
     return _monkey_defaults(dataset_id=dataset_id)
Пример #26
0
 def _monkey(self, connection):
     from gcloud.datastore._testing import _monkey_defaults
     return _monkey_defaults(connection=connection)
    def test_preset(self):
        from gcloud.datastore._testing import _monkey_defaults

        SENTINEL = object()
        with _monkey_defaults(dataset_id=SENTINEL):
            self.assertEqual(self._callFUT(), SENTINEL)
Пример #28
0
 def _monkey(self, connection):
     from gcloud.datastore._testing import _monkey_defaults
     return _monkey_defaults(connection=connection)