Skip to content

Commit

Permalink
Restore original enum handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gmazoyer committed Oct 15, 2024
1 parent 05aab68 commit db0e849
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 135 deletions.
2 changes: 1 addition & 1 deletion backend/infrahub/core/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def generate_python_enum(name: str, options: list[Any]) -> type[enum.Enum]:
main_attrs = {}
for option in options:
if isinstance(option, int):
enum_name = f"Value_{option!s}"
enum_name = str(option)
else:
enum_name = "_".join(re.findall(ENUM_NAME_REGEX, option)).upper()

Expand Down
6 changes: 1 addition & 5 deletions backend/infrahub/graphql/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ def get_enum_attribute_type_name(node_schema: MainSchemaTypes, attr_schema: Attr
def generate_graphql_enum(name: str, options: List[Any]) -> graphene.Enum:
def description_func(value: Any) -> str:
if value:
try:
int(value.value)
return value.name
except ValueError:
return value.value
return value.value
return f"Enum for {name}"

py_enum = generate_python_enum(name=name, options=options)
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/unit/core/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ def test_generate_python_enum_with_integers():

enum_two = enum_class(2)
assert isinstance(enum_two, enum.Enum)
assert {enum.name for enum in enum_class} == {"Value_2", "Value_5", "Value_14"}
assert {enum.name for enum in enum_class} == {"2", "5", "14"}
assert {enum.value for enum in enum_class} == {2, 5, 14}
259 changes: 131 additions & 128 deletions backend/tests/unit/graphql/test_mutation_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,134 +62,6 @@ async def test_create_simple_object_with_ok_return(db: InfrahubDatabase, default
assert result.data["TestPersonCreate"]["ok"] is True


@pytest.mark.parametrize(
"graphql_enums_on,enum_value,response_value", [(True, "MANUAL", "MANUAL"), (False, '"manual"', "manual")]
)
async def test_create_simple_object_with_enum(
db: InfrahubDatabase,
default_branch,
person_john_main,
car_person_schema,
graphql_enums_on,
enum_value,
response_value,
):
config.SETTINGS.experimental_features.graphql_enums = graphql_enums_on
query = """
mutation {
TestCarCreate(data: {
name: { value: "JetTricycle"},
nbr_seats: { value: 1 },
is_electric: { value: false },
transmission: { value: %s },
owner: { id: "John" }
}) {
ok
object {
id
transmission {
value
}
}
}
}
""" % (enum_value)
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={},
)

assert result.errors is None
assert result.data["TestCarCreate"]["ok"] is True
assert result.data["TestCarCreate"]["object"]["transmission"]["value"] == response_value

car_id = result.data["TestCarCreate"]["object"]["id"]
database_car = await NodeManager.get_one(db=db, id=car_id)
assert database_car.transmission.value.value == "manual"


async def test_create_enum_when_enums_off_fails(
db: InfrahubDatabase,
default_branch,
person_john_main,
car_person_schema,
):
config.SETTINGS.experimental_features.graphql_enums = False
query = """
mutation {
TestCarCreate(data: {
name: { value: "JetTricycle"},
nbr_seats: { value: 1 },
is_electric: { value: false },
transmission: { value: MANUAL },
owner: { id: "John" }
}) {
ok
object {
id
transmission {
value
}
}
}
}
"""
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={},
)

assert len(result.errors) == 1
assert "String cannot represent a non string value" in result.errors[0].message


async def test_create_string_when_enums_on_fails(
db: InfrahubDatabase,
default_branch,
person_john_main,
car_person_schema,
):
config.SETTINGS.experimental_features.graphql_enums = True
query = """
mutation {
TestCarCreate(data: {
name: { value: "JetTricycle"},
nbr_seats: { value: 1 },
is_electric: { value: false },
transmission: { value: "manual" },
owner: { id: "John" }
}) {
ok
object {
id
transmission {
value
}
}
}
}
"""
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={},
)

assert len(result.errors) == 1
assert "'TestCarTransmissionValue' cannot represent non-enum value" in result.errors[0].message


async def test_create_with_id(db: InfrahubDatabase, default_branch, car_person_schema):
uuid1 = "79c83773-6b23-4537-a3ce-b214b625ff1d"
query = (
Expand Down Expand Up @@ -1141,3 +1013,134 @@ async def test_create_valid_datetime_failure(db: InfrahubDatabase, default_branc
)
assert result.errors[0].args[0] == "10:1010 is not a valid DateTime at time"
assert result.data["TestCriticalityCreate"] is None


# These tests have been moved at the end of the file to avoid colliding with other and breaking them


@pytest.mark.parametrize(
"graphql_enums_on,enum_value,response_value", [(True, "MANUAL", "MANUAL"), (False, '"manual"', "manual")]
)
async def test_create_simple_object_with_enum(
db: InfrahubDatabase,
default_branch,
person_john_main,
car_person_schema,
graphql_enums_on,
enum_value,
response_value,
):
config.SETTINGS.experimental_features.graphql_enums = graphql_enums_on
query = """
mutation {
TestCarCreate(data: {
name: { value: "JetTricycle"},
nbr_seats: { value: 1 },
is_electric: { value: false },
transmission: { value: %s },
owner: { id: "John" }
}) {
ok
object {
id
transmission {
value
}
}
}
}
""" % (enum_value)
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={},
)

assert result.errors is None
assert result.data["TestCarCreate"]["ok"] is True
assert result.data["TestCarCreate"]["object"]["transmission"]["value"] == response_value

car_id = result.data["TestCarCreate"]["object"]["id"]
database_car = await NodeManager.get_one(db=db, id=car_id)
assert database_car.transmission.value.value == "manual"


async def test_create_enum_when_enums_off_fails(
db: InfrahubDatabase,
default_branch,
person_john_main,
car_person_schema,
):
config.SETTINGS.experimental_features.graphql_enums = False
query = """
mutation {
TestCarCreate(data: {
name: { value: "JetTricycle"},
nbr_seats: { value: 1 },
is_electric: { value: false },
transmission: { value: MANUAL },
owner: { id: "John" }
}) {
ok
object {
id
transmission {
value
}
}
}
}
"""
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={},
)

assert len(result.errors) == 1
assert "String cannot represent a non string value" in result.errors[0].message


async def test_create_string_when_enums_on_fails(
db: InfrahubDatabase,
default_branch,
person_john_main,
car_person_schema,
):
config.SETTINGS.experimental_features.graphql_enums = True
query = """
mutation {
TestCarCreate(data: {
name: { value: "JetTricycle"},
nbr_seats: { value: 1 },
is_electric: { value: false },
transmission: { value: "manual" },
owner: { id: "John" }
}) {
ok
object {
id
transmission {
value
}
}
}
}
"""
gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=default_branch)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={},
)

assert len(result.errors) == 1
assert "'TestCarTransmissionValue' cannot represent non-enum value" in result.errors[0].message

0 comments on commit db0e849

Please sign in to comment.