Python graphene.Enum() Examples
The following are 30
code examples of graphene.Enum().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
graphene
, or try the search function
.
Example #1
Source File: test_converter.py From graphene-sqlalchemy with MIT License | 6 votes |
def test_should_enum_convert_enum(): field = get_field(types.Enum(enum.Enum("TwoNumbers", ("one", "two")))) field_type = field.type() assert isinstance(field_type, graphene.Enum) assert field_type._meta.name == "TwoNumbers" assert hasattr(field_type, "ONE") assert not hasattr(field_type, "one") assert hasattr(field_type, "TWO") assert not hasattr(field_type, "two") field = get_field(types.Enum("one", "two", name="two_numbers")) field_type = field.type() assert isinstance(field_type, graphene.Enum) assert field_type._meta.name == "TwoNumbers" assert hasattr(field_type, "ONE") assert not hasattr(field_type, "one") assert hasattr(field_type, "TWO") assert not hasattr(field_type, "two")
Example #2
Source File: filters.py From caluma with GNU General Public License v3.0 | 6 votes |
def case_status_filter(*args, **kwargs): case_status_descriptions = { s.upper(): d for s, d in models.Case.STATUS_CHOICE_TUPLE } class EnumWithDescriptionsType(object): @property def description(self): return case_status_descriptions[self.name] enum = graphene.Enum( "CaseStatusArgument", [(i.upper(), i) for i in models.Case.STATUS_CHOICES], type=EnumWithDescriptionsType, ) return generate_list_filter_class(enum)(*args, **kwargs)
Example #3
Source File: test_converter.py From graphene-django with MIT License | 6 votes |
def test_field_with_choices_convert_enum(): field = models.CharField( help_text="Language", choices=(("es", "Spanish"), ("en", "English")) ) class TranslatedModel(models.Model): language = field class Meta: app_label = "test" graphene_type = convert_django_field_with_choices(field) assert isinstance(graphene_type, graphene.Enum) assert graphene_type._meta.name == "TranslatedModelLanguage" assert graphene_type._meta.enum.__members__["ES"].value == "es" assert graphene_type._meta.enum.__members__["ES"].description == "Spanish" assert graphene_type._meta.enum.__members__["EN"].value == "en" assert graphene_type._meta.enum.__members__["EN"].description == "English"
Example #4
Source File: dauphin_registry.py From dagster with Apache License 2.0 | 6 votes |
def create_enum(metaclass): class EnumRegisteringMetaclass(metaclass, EnumMeta): pass def from_enum(cls, enum, description=None, deprecation_reason=None): description = description or enum.__doc__ meta_dict = { "enum": enum, "description": description, "deprecation_reason": deprecation_reason, } meta_class = type("Meta", (object,), meta_dict) return type(meta_class.enum.__name__, (cls,), {"Meta": meta_class}) Enum = EnumRegisteringMetaclass('Enum', (graphene.Enum,), {'from_enum': classmethod(from_enum)}) setattr(Enum, '__dauphinCoreType', True) return Enum
Example #5
Source File: test_sort_enums.py From graphene-sqlalchemy with MIT License | 6 votes |
def test_sort_enum(): class PetType(SQLAlchemyObjectType): class Meta: model = Pet sort_enum = PetType.sort_enum() assert isinstance(sort_enum, type(Enum)) assert sort_enum._meta.name == "PetTypeSortEnum" assert list(sort_enum._meta.enum.__members__) == [ "ID_ASC", "ID_DESC", "NAME_ASC", "NAME_DESC", "PET_KIND_ASC", "PET_KIND_DESC", "HAIR_KIND_ASC", "HAIR_KIND_DESC", "REPORTER_ID_ASC", "REPORTER_ID_DESC", ] assert str(sort_enum.ID_ASC.value.value) == "pets.id ASC" assert str(sort_enum.ID_DESC.value.value) == "pets.id DESC" assert str(sort_enum.HAIR_KIND_ASC.value.value) == "pets.hair_kind ASC" assert str(sort_enum.HAIR_KIND_DESC.value.value) == "pets.hair_kind DESC"
Example #6
Source File: test_registry.py From graphene-sqlalchemy with MIT License | 6 votes |
def test_register_sort_enum_incorrect_types(): reg = Registry() class PetType(SQLAlchemyObjectType): class Meta: model = Pet registry = reg sort_enum = GrapheneEnum( "PetSort", [("ID", EnumValue("id", Pet.id)), ("NAME", EnumValue("name", Pet.name))], ) re_err = r"Expected SQLAlchemyObjectType, but got: .*PetSort.*" with pytest.raises(TypeError, match=re_err): reg.register_sort_enum(sort_enum, sort_enum) re_err = r"Expected Graphene Enum, but got: .*PetType.*" with pytest.raises(TypeError, match=re_err): reg.register_sort_enum(PetType, PetType)
Example #7
Source File: registry.py From graphene-sqlalchemy with MIT License | 5 votes |
def register_enum(self, sa_enum, graphene_enum): if not isinstance(sa_enum, SQLAlchemyEnumType): raise TypeError( "Expected SQLAlchemyEnumType, but got: {!r}".format(sa_enum) ) if not isinstance(graphene_enum, type(Enum)): raise TypeError( "Expected Graphene Enum, but got: {!r}".format(graphene_enum) ) self._registry_enums[sa_enum] = graphene_enum
Example #8
Source File: test_converter.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_should_intenum_choice_convert_enum(): class TestEnum(enum.IntEnum): one = 1 two = 2 field = get_field(ChoiceType(TestEnum, impl=types.String())) graphene_type = field.type assert issubclass(graphene_type, graphene.Enum) assert graphene_type._meta.name == "MODEL_COLUMN" assert graphene_type._meta.enum.__members__["one"].value == 1 assert graphene_type._meta.enum.__members__["two"].value == 2
Example #9
Source File: test_converter.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_should_postgresql_enum_convert(): field = get_field(postgresql.ENUM("one", "two", name="two_numbers")) field_type = field.type() assert isinstance(field_type, graphene.Enum) assert field_type._meta.name == "TwoNumbers" assert hasattr(field_type, "ONE") assert not hasattr(field_type, "one") assert hasattr(field_type, "TWO") assert not hasattr(field_type, "two")
Example #10
Source File: test_converter.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_should_postgresql_py_enum_convert(): field = get_field(postgresql.ENUM(enum.Enum("TwoNumbers", "one two"), name="two_numbers")) field_type = field.type() assert field_type._meta.name == "TwoNumbers" assert isinstance(field_type, graphene.Enum) assert hasattr(field_type, "ONE") assert not hasattr(field_type, "one") assert hasattr(field_type, "TWO") assert not hasattr(field_type, "two")
Example #11
Source File: test_registry.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_register_enum(): reg = Registry() sa_enum = SQLAlchemyEnum("cat", "dog") graphene_enum = GrapheneEnum("PetKind", [("CAT", 1), ("DOG", 2)]) reg.register_enum(sa_enum, graphene_enum) assert reg.get_graphene_enum_for_sa_enum(sa_enum) is graphene_enum
Example #12
Source File: test_registry.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_register_enum_incorrect_types(): reg = Registry() sa_enum = SQLAlchemyEnum("cat", "dog") graphene_enum = GrapheneEnum("PetKind", [("CAT", 1), ("DOG", 2)]) re_err = r"Expected Graphene Enum, but got: Enum\('cat', 'dog'\)" with pytest.raises(TypeError, match=re_err): reg.register_enum(sa_enum, sa_enum) re_err = r"Expected SQLAlchemyEnumType, but got: .*PetKind.*" with pytest.raises(TypeError, match=re_err): reg.register_enum(graphene_enum, graphene_enum)
Example #13
Source File: test_registry.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_register_sort_enum(): reg = Registry() class PetType(SQLAlchemyObjectType): class Meta: model = Pet registry = reg sort_enum = GrapheneEnum( "PetSort", [("ID", EnumValue("id", Pet.id)), ("NAME", EnumValue("name", Pet.name))], ) reg.register_sort_enum(PetType, sort_enum) assert reg.get_sort_enum_for_object_type(PetType) is sort_enum
Example #14
Source File: test_converter.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_should_choice_convert_enum(): field = get_field(ChoiceType([(u"es", u"Spanish"), (u"en", u"English")])) graphene_type = field.type assert issubclass(graphene_type, graphene.Enum) assert graphene_type._meta.name == "MODEL_COLUMN" assert graphene_type._meta.enum.__members__["es"].value == "Spanish" assert graphene_type._meta.enum.__members__["en"].value == "English"
Example #15
Source File: test_field_converter.py From graphene-django with MIT License | 5 votes |
def test_should_choice_convert_enum(): field = assert_conversion( serializers.ChoiceField, graphene.Enum, choices=[("h", "Hello"), ("w", "World")], source="word", ) assert field._meta.enum.__members__["H"].value == "h" assert field._meta.enum.__members__["H"].description == "Hello" assert field._meta.enum.__members__["W"].value == "w" assert field._meta.enum.__members__["W"].description == "World"
Example #16
Source File: test_field_converter.py From graphene-django with MIT License | 5 votes |
def test_should_filepath_convert_string(): assert_conversion(serializers.FilePathField, graphene.Enum, path="/")
Example #17
Source File: test_field_converter.py From graphene-django with MIT License | 5 votes |
def test_should_multiplechoicefield_convert_to_list_of_enum(): field = assert_conversion( serializers.MultipleChoiceField, graphene.List, choices=[1, 2, 3] ) assert issubclass(field.of_type, graphene.Enum)
Example #18
Source File: filters.py From caluma with GNU General Public License v3.0 | 5 votes |
def convert_ordering_field_to_enum(field): """ Add support to convert ordering choices to Graphql enum. Label is used as enum name. """ registry = get_global_registry() converted = registry.get_converted_field(field) if converted: return converted if field.label in convert_ordering_field_to_enum._cache: return convert_ordering_field_to_enum._cache[field.label] def get_choices(choices): for value, help_text in choices: if value[0] != "-": name = convert_choice_name(value) + "_ASC" else: name = convert_choice_name(value[1:]) + "_DESC" description = help_text yield name, value, description name = to_camel_case(field.label) choices = list(get_choices(field.choices)) named_choices = [(c[0], c[1]) for c in choices] named_choices_descriptions = {c[0]: c[2] for c in choices} class EnumWithDescriptionsType(object): @property def description(self): return named_choices_descriptions[self.name] enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType) converted = List(enum, description=field.help_text, required=field.required) registry.register_converted_field(field, converted) convert_ordering_field_to_enum._cache[field.label] = converted return converted
Example #19
Source File: filters.py From caluma with GNU General Public License v3.0 | 5 votes |
def convert_choice_field_to_enum(field): """ Add support to convert ordering choices to Graphql enum. Label is used as enum name. """ registry = get_global_registry() # field label of enum needs to be unique so stored it likewise converted = registry.get_converted_field(field.label) if converted: return converted def get_choices(choices): for value, help_text in choices: if value: name = convert_choice_name(value) description = help_text yield name, value, description name = to_camel_case(field.label) choices = list(get_choices(field.choices)) named_choices = [(c[0], c[1]) for c in choices] named_choices_descriptions = {c[0]: c[2] for c in choices} class EnumWithDescriptionsType(object): @property def description(self): return named_choices_descriptions[self.name] enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType) converted = enum(description=field.help_text, required=field.required) registry.register_converted_field(field.label, converted) return converted
Example #20
Source File: ordering.py From caluma with GNU General Public License v3.0 | 5 votes |
def AttributeOrderingFactory(model, fields=None, exclude_fields=None): """Build ordering field for a given model. Used to define an ordering field that is presented as an enum """ if not exclude_fields: exclude_fields = [] if not fields: fields = [f.name for f in model._meta.fields if f.name not in exclude_fields] field_enum = type( f"Sortable{model.__name__}Attributes", (Enum,), {field.upper(): field for field in fields}, ) field_class = type(f"{model.__name__}AttributeField", (ChoiceField,), {}) ordering_class = type( f"{model.__name__}AttributeOrdering", (AttributeOrderingMixin, CalumaOrdering), {"field_class": field_class, "_fields": fields}, ) @convert_form_field.register(field_class) def to_enum(field): registry = get_global_registry() converted = registry.get_converted_field(field) if converted: # pragma: no cover return converted converted = field_enum() registry.register_converted_field(field, converted) return converted return ordering_class()
Example #21
Source File: test_converter.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_should_enum_choice_convert_enum(): class TestEnum(enum.Enum): es = u"Spanish" en = u"English" field = get_field(ChoiceType(TestEnum, impl=types.String())) graphene_type = field.type assert issubclass(graphene_type, graphene.Enum) assert graphene_type._meta.name == "MODEL_COLUMN" assert graphene_type._meta.enum.__members__["es"].value == "Spanish" assert graphene_type._meta.enum.__members__["en"].value == "English"
Example #22
Source File: enum.py From graphene with MIT License | 5 votes |
def __call__(cls, *args, **kwargs): # noqa: N805 if cls is Enum: description = kwargs.pop("description", None) deprecation_reason = kwargs.pop("deprecation_reason", None) return cls.from_enum( PyEnum(*args, **kwargs), description=description, deprecation_reason=deprecation_reason, ) return super(EnumMeta, cls).__call__(*args, **kwargs) # return cls._meta.enum(*args, **kwargs)
Example #23
Source File: test_sort_enums.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_sort_argument(): class PetType(SQLAlchemyObjectType): class Meta: model = Pet sort_arg = PetType.sort_argument() assert isinstance(sort_arg, Argument) assert isinstance(sort_arg.type, List) sort_enum = sort_arg.type._of_type assert isinstance(sort_enum, type(Enum)) assert sort_enum._meta.name == "PetTypeSortEnum" assert list(sort_enum._meta.enum.__members__) == [ "ID_ASC", "ID_DESC", "NAME_ASC", "NAME_DESC", "PET_KIND_ASC", "PET_KIND_DESC", "HAIR_KIND_ASC", "HAIR_KIND_DESC", "REPORTER_ID_ASC", "REPORTER_ID_DESC", ] assert str(sort_enum.ID_ASC.value.value) == "pets.id ASC" assert str(sort_enum.ID_DESC.value.value) == "pets.id DESC" assert str(sort_enum.HAIR_KIND_ASC.value.value) == "pets.hair_kind ASC" assert str(sort_enum.HAIR_KIND_DESC.value.value) == "pets.hair_kind DESC" assert sort_arg.default_value == ["ID_ASC"] assert str(sort_enum.ID_ASC.value.value) == "pets.id ASC"
Example #24
Source File: test_sort_enums.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_sort_enum_with_custom_name(): class PetType(SQLAlchemyObjectType): class Meta: model = Pet sort_enum = PetType.sort_enum(name="CustomSortName") assert isinstance(sort_enum, type(Enum)) assert sort_enum._meta.name == "CustomSortName"
Example #25
Source File: test_enums.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_convert_sa_enum_to_graphene_enum_based_on_list_unnamed(): sa_enum = SQLAlchemyEnumType("red", "green", "blue") graphene_enum = _convert_sa_to_graphene_enum(sa_enum, "FallbackName") assert isinstance(graphene_enum, type(Enum)) assert graphene_enum._meta.name == "FallbackName" assert [ (key, value.value) for key, value in graphene_enum._meta.enum.__members__.items() ] == [("RED", 'red'), ("GREEN", 'green'), ("BLUE", 'blue')]
Example #26
Source File: test_enums.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_convert_sa_enum_to_graphene_enum_based_on_list_named(): sa_enum = SQLAlchemyEnumType("red", "green", "blue", name="color_values") graphene_enum = _convert_sa_to_graphene_enum(sa_enum, "FallbackName") assert isinstance(graphene_enum, type(Enum)) assert graphene_enum._meta.name == "ColorValues" assert [ (key, value.value) for key, value in graphene_enum._meta.enum.__members__.items() ] == [("RED", 'red'), ("GREEN", 'green'), ("BLUE", 'blue')]
Example #27
Source File: test_enums.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_convert_sa_to_graphene_enum_based_on_py_enum_with_bad_names(): class Color(PyEnum): red = 1 green = 2 blue = 3 sa_enum = SQLAlchemyEnumType(Color) graphene_enum = _convert_sa_to_graphene_enum(sa_enum, "FallbackName") assert isinstance(graphene_enum, type(Enum)) assert graphene_enum._meta.name == "Color" assert graphene_enum._meta.enum is not Color assert [ (key, value.value) for key, value in graphene_enum._meta.enum.__members__.items() ] == [("RED", 1), ("GREEN", 2), ("BLUE", 3)]
Example #28
Source File: test_enums.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_convert_sa_to_graphene_enum_based_on_py_enum(): class Color(PyEnum): RED = 1 GREEN = 2 BLUE = 3 sa_enum = SQLAlchemyEnumType(Color) graphene_enum = _convert_sa_to_graphene_enum(sa_enum, "FallbackName") assert isinstance(graphene_enum, type(Enum)) assert graphene_enum._meta.name == "Color" assert graphene_enum._meta.enum is Color
Example #29
Source File: test_enums.py From graphene-sqlalchemy with MIT License | 5 votes |
def test_convert_sa_to_graphene_enum_bad_type(): re_err = "Expected sqlalchemy.types.Enum, but got: 'foo'" with pytest.raises(TypeError, match=re_err): _convert_sa_to_graphene_enum("foo")
Example #30
Source File: filters.py From graphene-sqlalchemy-filter with MIT License | 5 votes |
def _get_enum_from_field( enum: 'Union[Callable, graphene.Field]', ) -> graphene.Enum: """ Get graphene enum. Args: enum: lambda or graphene.Field Returns: Graphene enum. """ if gqls_version < (2, 2, 0): # AssertionError: Found different types # with the same name in the schema: ... raise AssertionError( 'Enum is not supported. ' 'Requires graphene-sqlalchemy 2.2.0 or higher.' ) elif gqls_version == (2, 2, 0): # https://github.com/graphql-python/graphene-sqlalchemy/compare/2.1.2...2.2.0#diff-9202780f6bf4790a0d960de553c086f1L155 return enum._type()() else: # https://github.com/graphql-python/graphene-sqlalchemy/compare/2.2.0...2.2.1#diff-9202780f6bf4790a0d960de553c086f1L150 return enum()()