|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +from pathlib import Path |
| 16 | + |
15 | 17 | import pytest |
16 | 18 |
|
17 | | -from newrelic.common.encoding_utils import camel_case, snake_case |
| 19 | +from newrelic.common.encoding_utils import camel_case, json_encode, snake_case |
18 | 20 |
|
19 | 21 |
|
20 | 22 | @pytest.mark.parametrize( |
@@ -55,3 +57,27 @@ def test_camel_case(input_, expected, upper): |
55 | 57 | def test_snake_case(input_, expected): |
56 | 58 | output = snake_case(input_) |
57 | 59 | assert output == expected |
| 60 | + |
| 61 | + |
| 62 | +def _generator(): |
| 63 | + # range() itself is not a generator |
| 64 | + yield from range(1, 4) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "input_,expected", |
| 69 | + [ |
| 70 | + (10, "10"), |
| 71 | + (10.0, "10.0"), |
| 72 | + ("my_string", '"my_string"'), |
| 73 | + (b"my_bytes", '"my_bytes"'), |
| 74 | + ({"id": 1, "name": "test", "NoneType": None}, '{"id":1,"name":"test","NoneType":null}'), |
| 75 | + (_generator(), "[1,2,3]"), |
| 76 | + (tuple(range(4, 7)), "[4,5,6]"), |
| 77 | + (Path("test/path/file.txt"), '"test/path/file.txt"'), |
| 78 | + ], |
| 79 | + ids=["int", "float", "str", "bytes", "dict", "generator", "iterable", "Path"], |
| 80 | +) |
| 81 | +def test_json_encode(input_, expected): |
| 82 | + output = json_encode(input_) |
| 83 | + assert output == expected |
0 commit comments