Skip to content

Commit

Permalink
Fix convert_camel_case_to_snake
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Dudek committed Feb 25, 2025
1 parent 9f16600 commit c0cfbc1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ariadne/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ def convert_camel_case_to_snake(graphql_name: str) -> str:
i < max_index
and graphql_name[i] != c
and graphql_name[i + 1] == lowered_name[i + 1]
and graphql_name[i + 1] != "_"
)
# test134 -> test_134
or (c.isdigit() and not graphql_name[i - 1].isdigit())
or (
c.isdigit()
and not graphql_name[i - 1].isdigit()
)
# 134test -> 134_test
or (not c.isdigit() and graphql_name[i - 1].isdigit())
or (not c.isdigit() and graphql_name[i] != "_" and graphql_name[i - 1].isdigit())
):
python_name += "_"
python_name += c
Expand Down
12 changes: 12 additions & 0 deletions tests/test_camel_case_to_snake_case_convertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ def test_no_underscore_added_if_previous_character_is_an_underscore():
assert convert_camel_case_to_snake("test__complexName") == "test__complex_name"


@pytest.mark.parametrize(
("test_str", "result"),
[
("FOO_bar", "foo_bar"),
("FOO_BAR", "foo_bar"),
("S3_BUCKET", "s_3_bucket"),
],
)
def test_no_underscore_added_if_next_character_is_an_underscore(test_str, result):
assert convert_camel_case_to_snake(test_str) == result


def test_no_underscore_added_if_previous_character_is_uppercase():
assert convert_camel_case_to_snake("testWithUPPERPart") == "test_with_upper_part"

Expand Down

0 comments on commit c0cfbc1

Please sign in to comment.