def test_init_id_required(self): with self.assertRaises(Exception): dataset = Dataset() dataset = Dataset('dataset-id-here') self.assertEqual('dataset-id-here', dataset.id()) self.assertEqual(None, dataset.connection())
def _makeOne(self, dataset=_MARKER, kind=_KIND, exclude_from_indexes=()): from gcloud.datastore.dataset import Dataset klass = self._getTargetClass() if dataset is _MARKER: dataset = Dataset(_DATASET_ID) return klass(dataset, kind, exclude_from_indexes)
def test_from_key(self): key = Key(dataset=Dataset('test-dataset')).kind('TestKind').id(1234) entity = Entity.from_key(key) self.assertEqual('test-dataset', entity.dataset().id()) self.assertEqual('TestKind', entity.key().kind()) self.assertEqual(entity.key().kind(), entity.kind()) self.assertEqual(1234, entity.key().id())
def test_ctor_explicit(self): from gcloud.datastore.dataset import Dataset dataset = Dataset(_DATASET_ID) _EXCLUDE_FROM_INDEXES = ['foo', 'bar'] entity = self._makeOne(dataset, _KIND, _EXCLUDE_FROM_INDEXES) self.assertTrue(entity.dataset() is dataset) self.assertEqual(sorted(entity.exclude_from_indexes()), sorted(_EXCLUDE_FROM_INDEXES))
def test_ctor_explicit(self): from gcloud.datastore.dataset import Dataset _DATASET = 'DATASET' _KIND = 'KIND' _NAMESPACE = 'NAMESPACE' dataset = Dataset(_DATASET) query = self._makeOne(_KIND, dataset, _NAMESPACE) self.assertTrue(query.dataset() is dataset) self.assertEqual(query.kind(), _KIND) self.assertEqual(query.namespace(), _NAMESPACE)
def test_dataset_setter(self): from gcloud.datastore.dataset import Dataset _DATASET = 'DATASET' _KIND = 'KIND' dataset = Dataset(_DATASET) query = self._makeOne(_KIND) after = query.dataset(dataset) self.assertFalse(after is query) self.assertTrue(isinstance(after, self._getTargetClass())) self.assertTrue(after.dataset() is dataset) self.assertEqual(query.kind(), _KIND)
def dataset(self, *args, **kwargs): """Factory method for Dataset objects. :param args: All args and kwargs will be passed along to the :class:`gcloud.datastore.dataset.Dataset` initializer. :rtype: :class:`gcloud.datastore.dataset.Dataset` :returns: A dataset object that will use this connection as its transport. """ kwargs['connection'] = self return Dataset(*args, **kwargs)
def test_kind_setter_wo_existing(self): from gcloud.datastore.dataset import Dataset _DATASET = 'DATASET' _KIND = 'KIND' dataset = Dataset(_DATASET) query = self._makeOne(dataset=dataset) after = query.kind(_KIND) self.assertFalse(after is query) self.assertTrue(isinstance(after, self._getTargetClass())) self.assertTrue(after.dataset() is dataset) kq_pb, = list(after.kind()) self.assertEqual(kq_pb.name, _KIND)
def test_kind_setter_w_existing(self): from gcloud.datastore.dataset import Dataset _DATASET = 'DATASET' _KIND_BEFORE = 'KIND_BEFORE' _KIND_AFTER = 'KIND_AFTER' dataset = Dataset(_DATASET) query = self._makeOne(_KIND_BEFORE, dataset) self.assertEqual(query.kind(), _KIND_BEFORE) after = query.kind(_KIND_AFTER) self.assertFalse(after is query) self.assertTrue(isinstance(after, self._getTargetClass())) self.assertTrue(after.dataset() is dataset) self.assertEqual(after.kind(), _KIND_AFTER)
def test_limit_setter_wo_existing(self): from gcloud.datastore.dataset import Dataset _DATASET = 'DATASET' _KIND = 'KIND' _LIMIT = 42 dataset = Dataset(_DATASET) query = self._makeOne(_KIND, dataset) after = query.limit(_LIMIT) self.assertFalse(after is query) self.assertTrue(isinstance(after, self._getTargetClass())) self.assertTrue(after.dataset() is dataset) self.assertEqual(after.limit(), _LIMIT) self.assertEqual(after.kind(), _KIND)
def test_from_key_w_dataset(self): from gcloud.datastore.dataset import Dataset from gcloud.datastore.key import Key klass = self._getTargetClass() dataset = Dataset(_DATASET_ID) key = Key().kind(_KIND).id(_ID) entity = klass.from_key(key, dataset) self.assertTrue(entity.dataset() is dataset) self.assertEqual(entity.kind(), _KIND) key = entity.key() self.assertEqual(key.kind(), _KIND) self.assertEqual(key.id(), _ID)
def test__clone(self): from gcloud.datastore.dataset import Dataset _DATASET = 'DATASET' _KIND = 'KIND' _NAMESPACE = 'NAMESPACE' dataset = Dataset(_DATASET) query = self._makeOne(_KIND, dataset, _NAMESPACE) clone = query._clone() self.assertFalse(clone is query) self.assertTrue(isinstance(clone, self._getTargetClass())) self.assertTrue(clone.dataset() is dataset) self.assertEqual(clone.namespace(), _NAMESPACE) self.assertEqual(clone.kind(), _KIND)
def from_protobuf(cls, pb, dataset=None): path = [] for element in pb.path_element: element_dict = {'kind': element.kind} if element.HasField('id'): element_dict['id'] = element.id elif element.HasField('name'): element_dict['name'] = element.name path.append(element_dict) if not dataset: dataset = Dataset(id=pb.partition_id.dataset_id) return cls(path=path, dataset=dataset)
def test__clone(self): from gcloud.datastore.dataset import Dataset _DATASET = 'DATASET' _KIND = 'KIND' _CURSOR = 'DEADBEEF' _NAMESPACE = 'NAMESPACE' dataset = Dataset(_DATASET) query = self._makeOne(_KIND, dataset, _NAMESPACE) query._cursor = _CURSOR clone = query._clone() self.assertFalse(clone is query) self.assertTrue(isinstance(clone, self._getTargetClass())) self.assertTrue(clone.dataset() is dataset) self.assertEqual(clone.namespace(), _NAMESPACE) kq_pb, = list(clone.kind()) self.assertEqual(kq_pb.name, _KIND) self.assertEqual(clone._cursor, _CURSOR)
def test_w_dataset(self): from gcloud.datastore import datastore_v1_pb2 as datastore_pb from gcloud.datastore.dataset import Dataset _DATASET_ID = 'DATASET' _KIND = 'KIND' _ID = 1234 entity_pb = datastore_pb.Entity() entity_pb.key.partition_id.dataset_id = _DATASET_ID entity_pb.key.path_element.add(kind=_KIND, id=_ID) prop_pb = entity_pb.property.add() prop_pb.name = 'foo' prop_pb.value.string_value = 'Foo' dataset = Dataset(_DATASET_ID) entity = self._callFUT(entity_pb, dataset) self.assertTrue(entity.dataset() is dataset) self.assertEqual(entity.kind(), _KIND) self.assertEqual(entity['foo'], 'Foo') key = entity.key() self.assertEqual(key._dataset_id, _DATASET_ID) self.assertEqual(key.namespace(), None) self.assertEqual(key.kind(), _KIND) self.assertEqual(key.id(), _ID)
def test_entity_factory(self): dataset = Dataset('test') entity = dataset.entity('TestKind') self.assertIsInstance(entity, Entity) self.assertEqual('TestKind', entity.kind())
def test_query_factory(self): dataset = Dataset('test') query = dataset.query() self.assertIsInstance(query, Query) self.assertEqual(dataset, query.dataset())
def test_init_sets_proper_values(self): dataset = Dataset(id='test-dataset') entity = Entity(dataset, 'TestKind') self.assertEqual('test-dataset', entity.dataset().id()) self.assertEqual('TestKind', entity.kind())
def test_key(self): dataset = Dataset(id='test-dataset') entity = Entity(dataset, 'TestKind') self.assertIsInstance(entity.key(), Key)