def test_add_operators_to_field_reference_field():
from mongoengine import ReferenceField, Document, StringField
from graphene_mongo.operators import gen_operators_of_field, allowed_operators
from graphene_mongo.fields import respective_special_fields
class Other(Document):
name = StringField()
class Test(Document):
test = ReferenceField(Other)
field = Test.test
r_graphene = respective_special_fields[type(field)]
applied_operators = gen_operators_of_field('test', field, r_graphene('test', field), allowed_operators(field))
assert sorted(list(applied_operators.keys())) == format_fields(['in', 'nin', 'ne'])
assert isinstance(applied_operators['test__in'], graphene.List)
assert isinstance(applied_operators['test__nin'], graphene.List)
assert isinstance(applied_operators['test__ne'], graphene.ID)
python类ID的实例源码
def convert_column_to_string(type, attribute, registry=None):
if attribute.is_hash_key:
return ID(description=attribute.attr_name, required=not attribute.null)
return String(description=getattr(attribute, 'attr_name'),
required=not (getattr(attribute, 'null', True)))
def convert_column_to_int_or_id(type, attribute, registry=None):
if attribute.is_hash_key:
return ID(description=attribute.attr_name, required=not attribute.null)
return Int(description=attribute.attr_name, required=not attribute.null)
def field_to_id(m_field, g_field):
""" We need this because if we want to do a query using the id, we will pass a string to args with the id of the
document that we want, but graphene needs a ID field, instead of Field. This function convert to right thing."""
if isinstance(m_field, ReferenceField):
return graphene.ID()
elif (isinstance(m_field, ListField) or isinstance(m_field, SortedListField)) and \
isinstance(m_field.field, ReferenceField):
""" Pass here if it is a ListField or SortedListField of ReferenceField """
return graphene.List(graphene.ID)
else:
return g_field
def convert_form_field_to_list(field):
return List(ID, required=field.required)
def convert_form_field_to_id(field):
return ID(required=field.required)
def test_should_auto_convert_id():
assert_conversion(models.AutoField, graphene.ID, primary_key=True)
def test_should_manytoone_convert_connectionorlist():
field = forms.ModelChoiceField(Reporter.objects.all())
graphene_type = convert_form_field(field)
assert isinstance(graphene_type, graphene.ID)