Skip to content

Commit d009140

Browse files
committed
Restore original enum handling
Move tests that takes cares of testing the GraphQL enum experimental feature at the end of the test file to avoid breaking the test suite.
1 parent b7d26f5 commit d009140

File tree

4 files changed

+134
-135
lines changed

4 files changed

+134
-135
lines changed

backend/infrahub/core/enums.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def generate_python_enum(name: str, options: list[Any]) -> type[enum.Enum]:
99
main_attrs = {}
1010
for option in options:
1111
if isinstance(option, int):
12-
enum_name = f"Value_{option!s}"
12+
enum_name = str(option)
1313
else:
1414
enum_name = "_".join(re.findall(ENUM_NAME_REGEX, option)).upper()
1515

backend/infrahub/graphql/enums.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ def get_enum_attribute_type_name(node_schema: MainSchemaTypes, attr_schema: Attr
1616
def generate_graphql_enum(name: str, options: List[Any]) -> graphene.Enum:
1717
def description_func(value: Any) -> str:
1818
if value:
19-
try:
20-
int(value.value)
21-
return value.name
22-
except ValueError:
23-
return value.value
19+
return value.value
2420
return f"Enum for {name}"
2521

2622
py_enum = generate_python_enum(name=name, options=options)

backend/tests/unit/core/test_enums.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ def test_generate_python_enum_with_integers():
1818

1919
enum_two = enum_class(2)
2020
assert isinstance(enum_two, enum.Enum)
21-
assert {enum.name for enum in enum_class} == {"Value_2", "Value_5", "Value_14"}
21+
assert {enum.name for enum in enum_class} == {"2", "5", "14"}
2222
assert {enum.value for enum in enum_class} == {2, 5, 14}

backend/tests/unit/graphql/test_mutation_create.py

Lines changed: 131 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -62,134 +62,6 @@ async def test_create_simple_object_with_ok_return(db: InfrahubDatabase, default
6262
assert result.data["TestPersonCreate"]["ok"] is True
6363

6464

65-
@pytest.mark.parametrize(
66-
"graphql_enums_on,enum_value,response_value", [(True, "MANUAL", "MANUAL"), (False, '"manual"', "manual")]
67-
)
68-
async def test_create_simple_object_with_enum(
69-
db: InfrahubDatabase,
70-
default_branch,
71-
person_john_main,
72-
car_person_schema,
73-
graphql_enums_on,
74-
enum_value,
75-
response_value,
76-
):
77-
config.SETTINGS.experimental_features.graphql_enums = graphql_enums_on
78-
query = """
79-
mutation {
80-
TestCarCreate(data: {
81-
name: { value: "JetTricycle"},
82-
nbr_seats: { value: 1 },
83-
is_electric: { value: false },
84-
transmission: { value: %s },
85-
owner: { id: "John" }
86-
}) {
87-
ok
88-
object {
89-
id
90-
transmission {
91-
value
92-
}
93-
}
94-
}
95-
}
96-
""" % (enum_value)
97-
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
98-
result = await graphql(
99-
schema=gql_params.schema,
100-
source=query,
101-
context_value=gql_params.context,
102-
root_value=None,
103-
variable_values={},
104-
)
105-
106-
assert result.errors is None
107-
assert result.data["TestCarCreate"]["ok"] is True
108-
assert result.data["TestCarCreate"]["object"]["transmission"]["value"] == response_value
109-
110-
car_id = result.data["TestCarCreate"]["object"]["id"]
111-
database_car = await NodeManager.get_one(db=db, id=car_id)
112-
assert database_car.transmission.value.value == "manual"
113-
114-
115-
async def test_create_enum_when_enums_off_fails(
116-
db: InfrahubDatabase,
117-
default_branch,
118-
person_john_main,
119-
car_person_schema,
120-
):
121-
config.SETTINGS.experimental_features.graphql_enums = False
122-
query = """
123-
mutation {
124-
TestCarCreate(data: {
125-
name: { value: "JetTricycle"},
126-
nbr_seats: { value: 1 },
127-
is_electric: { value: false },
128-
transmission: { value: MANUAL },
129-
owner: { id: "John" }
130-
}) {
131-
ok
132-
object {
133-
id
134-
transmission {
135-
value
136-
}
137-
}
138-
}
139-
}
140-
"""
141-
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
142-
result = await graphql(
143-
schema=gql_params.schema,
144-
source=query,
145-
context_value=gql_params.context,
146-
root_value=None,
147-
variable_values={},
148-
)
149-
150-
assert len(result.errors) == 1
151-
assert "String cannot represent a non string value" in result.errors[0].message
152-
153-
154-
async def test_create_string_when_enums_on_fails(
155-
db: InfrahubDatabase,
156-
default_branch,
157-
person_john_main,
158-
car_person_schema,
159-
):
160-
config.SETTINGS.experimental_features.graphql_enums = True
161-
query = """
162-
mutation {
163-
TestCarCreate(data: {
164-
name: { value: "JetTricycle"},
165-
nbr_seats: { value: 1 },
166-
is_electric: { value: false },
167-
transmission: { value: "manual" },
168-
owner: { id: "John" }
169-
}) {
170-
ok
171-
object {
172-
id
173-
transmission {
174-
value
175-
}
176-
}
177-
}
178-
}
179-
"""
180-
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
181-
result = await graphql(
182-
schema=gql_params.schema,
183-
source=query,
184-
context_value=gql_params.context,
185-
root_value=None,
186-
variable_values={},
187-
)
188-
189-
assert len(result.errors) == 1
190-
assert "'TestCarTransmissionValue' cannot represent non-enum value" in result.errors[0].message
191-
192-
19365
async def test_create_with_id(db: InfrahubDatabase, default_branch, car_person_schema):
19466
uuid1 = "79c83773-6b23-4537-a3ce-b214b625ff1d"
19567
query = (
@@ -1141,3 +1013,134 @@ async def test_create_valid_datetime_failure(db: InfrahubDatabase, default_branc
11411013
)
11421014
assert result.errors[0].args[0] == "10:1010 is not a valid DateTime at time"
11431015
assert result.data["TestCriticalityCreate"] is None
1016+
1017+
1018+
# These tests have been moved at the end of the file to avoid colliding with other and breaking them
1019+
1020+
1021+
@pytest.mark.parametrize(
1022+
"graphql_enums_on,enum_value,response_value", [(True, "MANUAL", "MANUAL"), (False, '"manual"', "manual")]
1023+
)
1024+
async def test_create_simple_object_with_enum(
1025+
db: InfrahubDatabase,
1026+
default_branch,
1027+
person_john_main,
1028+
car_person_schema,
1029+
graphql_enums_on,
1030+
enum_value,
1031+
response_value,
1032+
):
1033+
config.SETTINGS.experimental_features.graphql_enums = graphql_enums_on
1034+
query = """
1035+
mutation {
1036+
TestCarCreate(data: {
1037+
name: { value: "JetTricycle"},
1038+
nbr_seats: { value: 1 },
1039+
is_electric: { value: false },
1040+
transmission: { value: %s },
1041+
owner: { id: "John" }
1042+
}) {
1043+
ok
1044+
object {
1045+
id
1046+
transmission {
1047+
value
1048+
}
1049+
}
1050+
}
1051+
}
1052+
""" % (enum_value)
1053+
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
1054+
result = await graphql(
1055+
schema=gql_params.schema,
1056+
source=query,
1057+
context_value=gql_params.context,
1058+
root_value=None,
1059+
variable_values={},
1060+
)
1061+
1062+
assert result.errors is None
1063+
assert result.data["TestCarCreate"]["ok"] is True
1064+
assert result.data["TestCarCreate"]["object"]["transmission"]["value"] == response_value
1065+
1066+
car_id = result.data["TestCarCreate"]["object"]["id"]
1067+
database_car = await NodeManager.get_one(db=db, id=car_id)
1068+
assert database_car.transmission.value.value == "manual"
1069+
1070+
1071+
async def test_create_enum_when_enums_off_fails(
1072+
db: InfrahubDatabase,
1073+
default_branch,
1074+
person_john_main,
1075+
car_person_schema,
1076+
):
1077+
config.SETTINGS.experimental_features.graphql_enums = False
1078+
query = """
1079+
mutation {
1080+
TestCarCreate(data: {
1081+
name: { value: "JetTricycle"},
1082+
nbr_seats: { value: 1 },
1083+
is_electric: { value: false },
1084+
transmission: { value: MANUAL },
1085+
owner: { id: "John" }
1086+
}) {
1087+
ok
1088+
object {
1089+
id
1090+
transmission {
1091+
value
1092+
}
1093+
}
1094+
}
1095+
}
1096+
"""
1097+
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
1098+
result = await graphql(
1099+
schema=gql_params.schema,
1100+
source=query,
1101+
context_value=gql_params.context,
1102+
root_value=None,
1103+
variable_values={},
1104+
)
1105+
1106+
assert len(result.errors) == 1
1107+
assert "String cannot represent a non string value" in result.errors[0].message
1108+
1109+
1110+
async def test_create_string_when_enums_on_fails(
1111+
db: InfrahubDatabase,
1112+
default_branch,
1113+
person_john_main,
1114+
car_person_schema,
1115+
):
1116+
config.SETTINGS.experimental_features.graphql_enums = True
1117+
query = """
1118+
mutation {
1119+
TestCarCreate(data: {
1120+
name: { value: "JetTricycle"},
1121+
nbr_seats: { value: 1 },
1122+
is_electric: { value: false },
1123+
transmission: { value: "manual" },
1124+
owner: { id: "John" }
1125+
}) {
1126+
ok
1127+
object {
1128+
id
1129+
transmission {
1130+
value
1131+
}
1132+
}
1133+
}
1134+
}
1135+
"""
1136+
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
1137+
result = await graphql(
1138+
schema=gql_params.schema,
1139+
source=query,
1140+
context_value=gql_params.context,
1141+
root_value=None,
1142+
variable_values={},
1143+
)
1144+
1145+
assert len(result.errors) == 1
1146+
assert "'TestCarTransmissionValue' cannot represent non-enum value" in result.errors[0].message

0 commit comments

Comments
 (0)