Ejemplo n.º 1
0
 def test_client_key_sort_key_names(self):
     k1 = key.Key('kind', '2', project=self._PROJECT)
     k2 = key.Key('kind', '1', project=self._PROJECT)
     keys = [k1, k2]
     expected_sort = [k2, k1]
     keys.sort(key=query_splitter.client_key_sort_key)
     self.assertEqual(expected_sort, keys)
Ejemplo n.º 2
0
 def test_client_key_sort_key_ids_vs_names(self):
     # Keys with IDs always come before keys with names.
     k1 = key.Key('kind', '1', project=self._PROJECT)
     k2 = key.Key('kind', 2, project=self._PROJECT)
     keys = [k1, k2]
     expected_sort = [k2, k1]
     keys.sort(key=query_splitter.client_key_sort_key)
     self.assertEqual(expected_sort, keys)
Ejemplo n.º 3
0
 def test_client_key_sort_key(self):
   k = key.Key('kind1', 1, project=self._PROJECT, namespace=self._NAMESPACE)
   k2 = key.Key('kind2', 'a', parent=k)
   k3 = key.Key('kind2', 'b', parent=k)
   k4 = key.Key('kind1', 'a', project=self._PROJECT, namespace=self._NAMESPACE)
   k5 = key.Key('kind1', 'a', project=self._PROJECT)
   keys = [k5, k, k4, k3, k2, k2, k]
   expected_sort = [k5, k, k, k2, k2, k3, k4]
   keys.sort(key=query_splitter.client_key_sort_key)
   self.assertEqual(expected_sort, keys)
Ejemplo n.º 4
0
    def __setstate__(self, state):
        """Private API used for unpickling.

        Args:
            state (Tuple[Dict[str, Any]]): A tuple containing a single
                dictionary of pickled state. This should match the signature
                returned from :func:`__getstate__`, in particular, it should
                have three keys ``pairs``, ``app`` and ``namespace``.

        Raises:
            TypeError: If the ``state`` does not have length 1.
            TypeError: If the single element in ``state`` is not a dictionary.
        """
        if len(state) != 1:
            msg = "Invalid state length, expected 1; received {:d}".format(
                len(state))
            raise TypeError(msg)

        kwargs = state[0]
        if not isinstance(kwargs, dict):
            raise TypeError(
                "Key accepts a dict of keyword arguments as state; "
                "received {!r}".format(kwargs))

        flat = _get_path(None, kwargs["pairs"])
        project = _project_from_app(kwargs["app"])
        self._key = _key_module.Key(*flat,
                                    project=project,
                                    namespace=kwargs["namespace"])
        self._reference = None
Ejemplo n.º 5
0
 def testEmbeddedClientEntityWithoutKey(self):
     client_entity = entity.Entity(key.Key('foo', project='bar'))
     entity_without_key = entity.Entity()
     entity_without_key['test'] = True
     client_entity['embedded'] = entity_without_key
     e = Entity.from_client_entity(client_entity)
     self.assertIsInstance(e.properties['embedded'], dict)
Ejemplo n.º 6
0
    def test_datastore_api_get_version(self):
        """Test the get_version API method for the Datastore API."""
        test_response = entity.Entity(
            key=key.Key('Config', 'datastore_version', project='TEST_PROJECT'))
        test_response['integer_value'] = 1
        mock_client = mock.Mock()
        mock_client.get.return_value = test_response

        test_datastore_api = datastore.DatastoreAPI(self.config, mock_client)
        self.assertEqual(1, test_datastore_api.get_version())
Ejemplo n.º 7
0
 def to_client_key(self):
   """
   Returns a :class:`google.cloud.datastore.key.Key` instance that represents
   this key.
   """
   parent = self.parent
   if parent is not None:
     parent = parent.to_client_key()
   return key.Key(*self.path_elements, parent=parent, namespace=self.namespace,
                  project=self.project)
Ejemplo n.º 8
0
 def MockEntity(*path):
     key = ds_key_module.Key(*path, project="testing")
     return entity.Entity(key=key)
Ejemplo n.º 9
0
def make_query(qname):
    key = ds_key.Key('EntityKind', 1234, project='project')
    two_step_query = ds_entity.Entity(key=key)
    two_step_query['name'] = qname
    return two_step_query