forked from danielgtaylor/python-betterproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes danielgtaylor#93 to_dict returns wrong enum fields when numberi…
…ng is not consecutive
- Loading branch information
1 parent
42e197f
commit a945246
Showing
7 changed files
with
148 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"choice": "FOUR", | ||
"choices": [ | ||
"ZERO", | ||
"ONE", | ||
"THREE", | ||
"FOUR" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
syntax = "proto3"; | ||
|
||
// Tests that enums are correctly serialized and that it correctly handles skipped and out-of-order enum values | ||
message Test { | ||
Choice choice = 1; | ||
repeated Choice choices = 2; | ||
} | ||
|
||
enum Choice { | ||
ZERO = 0; | ||
ONE = 1; | ||
// TWO = 2; | ||
FOUR = 4; | ||
THREE = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from tests.output_betterproto.enum import ( | ||
Test, | ||
Choice, | ||
) | ||
|
||
|
||
def test_enum_set_and_get(): | ||
assert Test(choice=Choice.ZERO).choice == Choice.ZERO | ||
assert Test(choice=Choice.ONE).choice == Choice.ONE | ||
assert Test(choice=Choice.THREE).choice == Choice.THREE | ||
assert Test(choice=Choice.FOUR).choice == Choice.FOUR | ||
|
||
|
||
def test_enum_set_with_int(): | ||
assert Test(choice=0).choice == Choice.ZERO | ||
assert Test(choice=1).choice == Choice.ONE | ||
assert Test(choice=3).choice == Choice.THREE | ||
assert Test(choice=4).choice == Choice.FOUR | ||
|
||
|
||
def test_enum_is_comparable_with_int(): | ||
assert Test(choice=Choice.ZERO).choice == 0 | ||
assert Test(choice=Choice.ONE).choice == 1 | ||
assert Test(choice=Choice.THREE).choice == 3 | ||
assert Test(choice=Choice.FOUR).choice == 4 | ||
|
||
|
||
def test_enum_to_dict(): | ||
assert ( | ||
"choice" not in Test(choice=Choice.ZERO).to_dict() | ||
), "Default enum value is not serialized" | ||
assert ( | ||
Test(choice=Choice.ZERO).to_dict(include_default_values=True)["choice"] | ||
== "ZERO" | ||
) | ||
assert Test(choice=Choice.ONE).to_dict()["choice"] == "ONE" | ||
assert Test(choice=Choice.THREE).to_dict()["choice"] == "THREE" | ||
assert Test(choice=Choice.FOUR).to_dict()["choice"] == "FOUR" | ||
|
||
|
||
def test_repeated_enum_is_comparable_with_int(): | ||
assert Test(choices=[Choice.ZERO]).choices == [0] | ||
assert Test(choices=[Choice.ONE]).choices == [1] | ||
assert Test(choices=[Choice.THREE]).choices == [3] | ||
assert Test(choices=[Choice.FOUR]).choices == [4] | ||
|
||
|
||
def test_repeated_enum_set_and_get(): | ||
assert Test(choices=[Choice.ZERO]).choices == [Choice.ZERO] | ||
assert Test(choices=[Choice.ONE]).choices == [Choice.ONE] | ||
assert Test(choices=[Choice.THREE]).choices == [Choice.THREE] | ||
assert Test(choices=[Choice.FOUR]).choices == [Choice.FOUR] | ||
|
||
|
||
def test_repeated_enum_to_dict(): | ||
assert Test(choices=[Choice.ZERO]).to_dict()["choices"] == ["ZERO"] | ||
assert Test(choices=[Choice.ONE]).to_dict()["choices"] == ["ONE"] | ||
assert Test(choices=[Choice.THREE]).to_dict()["choices"] == ["THREE"] | ||
assert Test(choices=[Choice.FOUR]).to_dict()["choices"] == ["FOUR"] | ||
|
||
all_enums_dict = Test( | ||
choices=[Choice.ZERO, Choice.ONE, Choice.THREE, Choice.FOUR] | ||
).to_dict() | ||
assert (all_enums_dict["choices"]) == ["ZERO", "ONE", "THREE", "FOUR"] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.