Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

NdbNode.global_id_to_key utility method #15

Merged
merged 2 commits into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
History
-------

0.1.6 (TBD)
---------------------
* Added NdbNode.global_id_to_key [PR #15](https://github.com/graphql-python/graphene-gae/pull/15)

0.1.5 (2016-06-08)
---------------------
* Fixed behavior of ndb.KeyProperty ([PR #14](https://github.com/graphql-python/graphene-gae/pull/14))
Expand Down
2 changes: 1 addition & 1 deletion graphene_gae/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)

__author__ = 'Eran Kampf'
__version__ = '0.1.5'
__version__ = '0.1.6'

__all__ = [
NdbObjectType,
Expand Down
6 changes: 6 additions & 0 deletions graphene_gae/ndb/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from graphene.core.classtypes.objecttype import ObjectType, ObjectTypeMeta

from google.appengine.ext import ndb
from graphql_relay import from_global_id

from .options import NdbOptions

Expand Down Expand Up @@ -96,6 +97,11 @@ def to_global_id(self):
entity_id = self.key.urlsafe() if self.key else None
return self.global_id(entity_id)

@classmethod
def global_id_to_key(cls, global_id):
_, urlsafe_key = from_global_id(global_id)
return ndb.Key(urlsafe=urlsafe_key)

@classmethod
def get_node(cls, urlsafe_key, info=None):
try:
Expand Down
1 change: 0 additions & 1 deletion tests/_ndb/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,3 @@ def testQuery_repeatedKeyProperty(self):
self.assertLength(article['tags'], 4)
for i in range(0, 3):
self.assertEqual(article['tags'][i]['name'], 't%s' % (i + 1))

10 changes: 10 additions & 0 deletions tests/_ndb/test_types_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ def testNdbNode_getNode_validID_entityDoes_shouldReturnEntity(self):
self.assertIsNotNone(result)
self.assertEqual(result.instance, article_key.get())

def testNdbNode_globalIdToKey_returnsNdbKey(self):
article_key = Article(
headline="TestGetNode",
summary="1",
author_key=Author(name="John Dow", email="john@dow.com").put(),
).put()

node = ArticleType.get_node(article_key.urlsafe())
self.assertEqual(NdbNode.global_id_to_key(node.to_global_id()), article_key)

def test_keyProperty(self):
Article(
headline="Test1",
Expand Down