|
4 | 4 |
|
5 | 5 | import strawberry |
6 | 6 | from strawberry.exceptions import ObjectIsNotAnEnumError |
7 | | -from strawberry.exceptions.not_a_strawberry_enum import NotAStrawberryEnumError |
| 7 | +from strawberry.types.base import get_object_definition |
8 | 8 | from strawberry.types.enum import EnumDefinition |
9 | 9 |
|
10 | 10 |
|
@@ -120,25 +120,6 @@ class IceCreamFlavour(Enum): |
120 | 120 | assert definition.values[2].description is None |
121 | 121 |
|
122 | 122 |
|
123 | | -@pytest.mark.raises_strawberry_exception( |
124 | | - NotAStrawberryEnumError, match='Enum "IceCreamFlavour" is not a Strawberry enum' |
125 | | -) |
126 | | -def test_raises_error_when_using_enum_not_decorated(): |
127 | | - class IceCreamFlavour(Enum): |
128 | | - VANILLA = strawberry.enum_value("vanilla") |
129 | | - STRAWBERRY = strawberry.enum_value( |
130 | | - "strawberry", |
131 | | - description="Our favourite", |
132 | | - ) |
133 | | - CHOCOLATE = "chocolate" |
134 | | - |
135 | | - @strawberry.type |
136 | | - class Query: |
137 | | - flavour: IceCreamFlavour |
138 | | - |
139 | | - strawberry.Schema(query=Query) |
140 | | - |
141 | | - |
142 | 123 | def test_can_use_enum_values(): |
143 | 124 | @strawberry.enum |
144 | 125 | class TestEnum(Enum): |
@@ -169,3 +150,53 @@ class TestEnum(IntEnum): |
169 | 150 | assert TestEnum.D.value == 4 |
170 | 151 |
|
171 | 152 | assert [x.value for x in TestEnum.__members__.values()] == [1, 2, 3, 4] |
| 153 | + |
| 154 | + |
| 155 | +def test_default_enum_implementation() -> None: |
| 156 | + class Foo(Enum): |
| 157 | + BAR = "bar" |
| 158 | + BAZ = "baz" |
| 159 | + |
| 160 | + @strawberry.type |
| 161 | + class Query: |
| 162 | + @strawberry.field |
| 163 | + def foo(self, foo: Foo) -> Foo: |
| 164 | + return foo |
| 165 | + |
| 166 | + schema = strawberry.Schema(Query) |
| 167 | + res = schema.execute_sync("{ foo(foo: BAR) }") |
| 168 | + assert not res.errors |
| 169 | + assert res.data |
| 170 | + assert res.data["foo"] == "BAR" |
| 171 | + |
| 172 | + |
| 173 | +def test_default_enum_reuse() -> None: |
| 174 | + class Foo(Enum): |
| 175 | + BAR = "bar" |
| 176 | + BAZ = "baz" |
| 177 | + |
| 178 | + @strawberry.type |
| 179 | + class SomeType: |
| 180 | + foo: Foo |
| 181 | + bar: Foo |
| 182 | + |
| 183 | + definition = get_object_definition(SomeType, strict=True) |
| 184 | + assert definition.fields[1].type is definition.fields[1].type |
| 185 | + |
| 186 | + |
| 187 | +def test_default_enum_with_enum_value() -> None: |
| 188 | + class Foo(Enum): |
| 189 | + BAR = "bar" |
| 190 | + BAZ = strawberry.enum_value("baz") |
| 191 | + |
| 192 | + @strawberry.type |
| 193 | + class Query: |
| 194 | + @strawberry.field |
| 195 | + def foo(self, foo: Foo) -> str: |
| 196 | + return foo.value |
| 197 | + |
| 198 | + schema = strawberry.Schema(Query) |
| 199 | + res = schema.execute_sync("{ foo(foo: BAZ) }") |
| 200 | + assert not res.errors |
| 201 | + assert res.data |
| 202 | + assert res.data["foo"] == "baz" |
0 commit comments