-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f823537
commit 3972b36
Showing
13 changed files
with
170 additions
and
35 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 |
---|---|---|
@@ -1,8 +1,57 @@ | ||
# coding: utf-8 | ||
# | ||
|
||
from wdapy._utils import camel_to_snake | ||
|
||
from wdapy._utils import camel_to_snake, json_dumps_omit_empty | ||
from wdapy._proto import * | ||
from wdapy._types import * | ||
|
||
def test_camel_to_snake(): | ||
assert "this_is_my_string" == camel_to_snake("ThisIsMyString") | ||
|
||
|
||
|
||
def test_json_dumps_omit_empty(): | ||
# Test with a mix of None and non-None values | ||
data = { | ||
"a": 1, | ||
"b": None, | ||
"c": "test", | ||
"d": [1, 2, 3], | ||
"e": [{ | ||
"f": 1, | ||
"g": None | ||
}] | ||
} | ||
expected_json = '{"a": 1, "c": "test", "d": [1, 2, 3], "e": [{"f": 1}]}' | ||
assert json_dumps_omit_empty(data) == expected_json | ||
|
||
data = [Gesture( | ||
action=GestureAction.TAP, | ||
options=GestureOption( | ||
x=100, | ||
y=200 | ||
) | ||
)] | ||
expected_json = '[{"action": "tap", "options": {"x": 100, "y": 200}}]' | ||
assert json_dumps_omit_empty(data) == expected_json | ||
|
||
# Test with all values as None | ||
data_all_none = { | ||
"a": None, | ||
"b": None | ||
} | ||
expected_json_all_none = '{}' | ||
assert json_dumps_omit_empty(data_all_none) == expected_json_all_none | ||
|
||
# Test with no None values | ||
data_no_none = { | ||
"a": 1, | ||
"b": "test" | ||
} | ||
expected_json_no_none = '{"a": 1, "b": "test"}' | ||
assert json_dumps_omit_empty(data_no_none) == expected_json_no_none | ||
|
||
# Test with empty dictionary | ||
data_empty = {} | ||
expected_json_empty = '{}' | ||
assert json_dumps_omit_empty(data_empty) == expected_json_empty |
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
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
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 |
---|---|---|
@@ -1,5 +1,35 @@ | ||
# coding: utf-8 | ||
# | ||
|
||
from __future__ import annotations | ||
|
||
import dataclasses | ||
import json | ||
|
||
|
||
def camel_to_snake(s: str) -> str: | ||
return ''.join(['_'+c.lower() if c.isupper() else c for c in s]).lstrip("_") | ||
|
||
|
||
def omit_empty(d: list | dict | dataclasses.dataclass | None): | ||
if isinstance(d, list): | ||
return [omit_empty(v) for v in d] | ||
elif isinstance(d, dict): | ||
return {k: omit_empty(v) for k, v in d.items() if v is not None} | ||
elif dataclasses.is_dataclass(d): | ||
return omit_empty(dataclasses.asdict(d)) | ||
else: | ||
return d | ||
|
||
|
||
def json_dumps_omit_empty(data: dict) -> str: | ||
""" | ||
Convert a dictionary to a JSON string, omitting any items with a value of None. | ||
Parameters: | ||
data (dict): The dictionary to convert to a JSON string. | ||
Returns: | ||
str: A JSON string representation of the dictionary with None values omitted. | ||
""" | ||
return json.dumps(omit_empty(data)) |
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