コード例 #1
0
    def testUnknownProperty_raisesException(self):
        with self.assertRaises(Exception) as context:
            prop = SomeWeirdUnknownProperty()
            prop._code_name = "my_prop"
            convert_ndb_property(prop)

        self.assertTrue("Don't know how to convert" in context.exception.message, msg=context.exception.message)
コード例 #2
0
    def testUnknownProperty_raisesException(self):
        with self.assertRaises(Exception) as context:
            prop = SomeWeirdUnknownProperty()
            prop._code_name = "my_prop"
            convert_ndb_property(prop)

        self.assertTrue("Don't know how to convert" in context.exception.message, msg=context.exception.message)
コード例 #3
0
    def testNoneResult_raisesException(self, patch_convert):
        from graphene_gae.ndb.converter import convert_ndb_property
        patch_convert.get.return_value = lambda _, __: None
        with self.assertRaises(Exception) as context:
            prop = ndb.StringProperty()
            prop._code_name = "my_prop"
            convert_ndb_property(prop)

        self.assertTrue("Failed to convert NDB field my_prop" in context.exception.message, msg=context.exception.message)
コード例 #4
0
    def testNoneResult_raisesException(self, patch_convert):
        from graphene_gae.ndb.converter import convert_ndb_property
        patch_convert.get.return_value = lambda _: None
        with self.assertRaises(Exception) as context:
            prop = ndb.StringProperty()
            prop._code_name = "my_prop"
            convert_ndb_property(prop)

        expected_message = 'Failed to convert NDB propeerty to a GraphQL field my_prop (StringProperty())'
        self.assertTrue(expected_message in context.exception.message, msg=context.exception.message)
コード例 #5
0
    def testKeyProperty_withSuffix(self):
        my_registry = Registry()

        class User(ndb.Model):
            name = ndb.StringProperty()

        class UserType(NdbObjectType):
            class Meta:
                model = User
                registry = my_registry

        prop = ndb.KeyProperty(kind='User')
        prop._code_name = 'user_key'

        conversion = convert_ndb_property(prop, my_registry)

        self.assertLength(conversion, 2)

        self.assertEqual(conversion[0].name, 'user_id')
        self.assertIsInstance(conversion[0].field, DynamicNdbKeyStringField)
        _type = conversion[0].field.get_type()
        self.assertIsInstance(_type, NdbKeyStringField)
        self.assertEqual(_type._type, String)

        self.assertEqual(conversion[1].name, 'user')
        self.assertIsInstance(conversion[1].field, DynamicNdbKeyReferenceField)
        _type = conversion[1].field.get_type()
        self.assertIsInstance(_type, NdbKeyReferenceField)
        self.assertEqual(_type._type, UserType)
コード例 #6
0
    def testStringProperty_required_shouldConvertToList(self):
        ndb_prop = ndb.StringProperty(required=True)
        result = convert_ndb_property(ndb_prop)
        graphene_type = result.field._type

        self.assertIsInstance(graphene_type, graphene.NonNull)
        self.assertEqual(graphene_type.of_type, graphene.String)
コード例 #7
0
    def testKeyProperty_withSuffix(self):
        my_registry = Registry()

        class User(ndb.Model):
            name = ndb.StringProperty()

        class UserType(NdbObjectType):
            class Meta:
                model = User
                registry = my_registry

        prop = ndb.KeyProperty(kind='User')
        prop._code_name = 'user_key'

        conversion = convert_ndb_property(prop, my_registry)

        self.assertLength(conversion, 2)

        self.assertEqual(conversion[0].name, 'user_id')
        self.assertIsInstance(conversion[0].field, DynamicNdbKeyStringField)
        _type = conversion[0].field.get_type()
        self.assertIsInstance(_type, NdbKeyStringField)
        self.assertEqual(_type._type, String)

        self.assertEqual(conversion[1].name, 'user')
        self.assertIsInstance(conversion[1].field, DynamicNdbKeyReferenceField)
        _type = conversion[1].field.get_type()
        self.assertIsInstance(_type, NdbKeyReferenceField)
        self.assertEqual(_type._type, UserType)
コード例 #8
0
    def testStringProperty_required_shouldConvertToList(self):
        ndb_prop = ndb.StringProperty(required=True)
        result = convert_ndb_property(ndb_prop)
        graphene_type = result.field._type

        self.assertIsInstance(graphene_type, graphene.NonNull)
        self.assertEqual(graphene_type.of_type, graphene.String)
コード例 #9
0
    def testStringProperty_repeated_shouldConvertToList(self):
        ndb_prop = ndb.StringProperty(repeated=True)
        conversion_result = convert_ndb_property(ndb_prop)
        graphene_type = conversion_result.field

        self.assertIsInstance(graphene_type, graphene.List)
        self.assertIsInstance(graphene_type.of_type, graphene.String)
コード例 #10
0
    def fields_for_ndb_model(options):
        ndb_model = options.model
        only_fields = options.only_fields
        already_created_fields = {
            name
            for name, _ in options.local_fields.iteritems()
        }

        ndb_fields = OrderedDict()
        for prop_name, prop in ndb_model._properties.iteritems():
            name = prop._code_name

            is_not_in_only = only_fields and name not in only_fields
            is_excluded = name in options.exclude_fields or name in already_created_fields
            if is_not_in_only or is_excluded:
                continue

            results = convert_ndb_property(prop)
            if not results:
                continue

            if not isinstance(results, list):
                results = [results]

            for r in results:
                ndb_fields[r.name] = r.field

        ndb_fields['ndb_id'] = Field(
            String, resolver=lambda entity, *_: str(entity.key.id()))

        return ndb_fields
コード例 #11
0
    def testKeyProperty_withSuffix_repeated(self):
        class User(ndb.Model):
            name = ndb.StringProperty()

        class UserType(NdbObjectType):
            class Meta:
                model = User

        prop = ndb.KeyProperty(kind='User', repeated=True)
        prop._code_name = 'user_keys'

        conversion = convert_ndb_property(prop)

        self.assertLength(conversion, 2)

        self.assertEqual(conversion[0].name, 'user_ids')
        self.assertIsInstance(conversion[0].field, DynamicNdbKeyStringField)
        _type = conversion[0].field.get_type()
        self.assertIsInstance(_type, NdbKeyStringField)
        self.assertIsInstance(_type._type, List)
        self.assertEqual(_type._type.of_type, String)

        self.assertEqual(conversion[1].name, 'users')
        self.assertIsInstance(conversion[1].field, DynamicNdbKeyReferenceField)
        _type = conversion[1].field.get_type()
        self.assertIsInstance(_type, NdbKeyReferenceField)
        self.assertIsInstance(_type._type, List)
        self.assertEqual(_type._type.of_type, UserType)
コード例 #12
0
    def testKeyProperty_repeatedPlural_withSuffixRemoval_removesSuffixAndPluralName(self):
        prop = ndb.KeyProperty()
        prop._code_name = "user_keys"
        conversion = convert_ndb_property(prop)
        self.assertEqual(conversion.name, "users")

        prop = ndb.KeyProperty()
        prop._code_name = "tag_name_keys"
        conversion = convert_ndb_property(prop)
        self.assertEqual(conversion.name, "tag_names")

        prop = ndb.KeyProperty()
        prop._code_name = "person_keys"
        conversion = convert_ndb_property(prop)
        self.assertEqual(conversion.name, "people")

        prop = ndb.KeyProperty()
        prop._code_name = "universal_category_keys"
        conversion = convert_ndb_property(prop)
        self.assertEqual(conversion.name, "universal_categories")
コード例 #13
0
 def __assert_conversion(self, ndb_property_type, expected_graphene_type,
                         *args, **kwargs):
     ndb_property = ndb_property_type(*args, **kwargs)
     result = convert_ndb_property(ndb_property)
     graphene_field = result.field
     self.assertEqual(graphene_field._type, expected_graphene_type)
コード例 #14
0
 def __assert_conversion(self, ndb_property_type, expected_graphene_type, *args, **kwargs):
     ndb_property = ndb_property_type(*args, **kwargs)
     result = convert_ndb_property(ndb_property)
     graphene_field = result.field
     self.assertEqual(graphene_field._type, expected_graphene_type)
コード例 #15
0
    def testKeyProperty_withSuffixRemoval_removesSuffix(self):
        prop = ndb.KeyProperty()
        prop._code_name = "user_key"

        conversion = convert_ndb_property(prop)
        self.assertEqual(conversion.name, "user")
コード例 #16
0
 def __assert_conversion(self, ndb_property_type, expected_graphene_type, *args, **kwargs):
     ndb_property = ndb_property_type(*args, **kwargs)
     conversion_result = convert_ndb_property(ndb_property)
     graphene_field_type = conversion_result.field
     self.assertIsInstance(graphene_field_type, expected_graphene_type)