-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vdk-core: add datetime to decimal json encoder
Why? As part of the work for vdk-oracle, we need to be able to pass datetime objects for ingestion. This is not possible with the current verification. What? Add datetime to the decimal json encoder Add bytes to the decimal json encoder Make the encoder return timestamp for the datetime object Make the encoder serialize bytes as a list of ints Rename DecimalJsonEncoder to IngesterJsonEncoder How was this tested Ran locally with vdk-oracle Unit and functional tests CI tests What kind of change is this? Feature/non-breaking Signed-off-by: Dilyan Marinov <[email protected]>
- Loading branch information
Dilyan Marinov
committed
Nov 22, 2023
1 parent
74d567c
commit 095a5ef
Showing
5 changed files
with
48 additions
and
14 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
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
42 changes: 36 additions & 6 deletions
42
projects/vdk-core/tests/vdk/internal/builtin_plugins/ingestion/test_ingester_utils.py
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,24 +1,54 @@ | ||
# Copyright 2021-2023 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import datetime | ||
import json | ||
from decimal import Decimal | ||
|
||
from pytest import raises | ||
from vdk.internal.builtin_plugins.ingestion.ingester_utils import DecimalJsonEncoder | ||
from vdk.internal.builtin_plugins.ingestion.ingester_utils import IngesterJsonEncoder | ||
|
||
|
||
def test_decimal_json_encoder(): | ||
payload_no_decimal = {"a": 1, "b": 2} | ||
def test_ingester_json_encoder(): | ||
payload_no_specials = {"a": 1, "b": 2} | ||
payload_with_decimal = {"a": Decimal(1), "b": Decimal(2)} | ||
payload_with_datetime = { | ||
"a": datetime.datetime.fromtimestamp(1700641925), | ||
"b": datetime.datetime.fromtimestamp(1700641925), | ||
} | ||
payload_with_bytes = { | ||
"a": b"enoded string bla bla", | ||
"b": b"another encoded string, look at me, I'm so special", | ||
} | ||
|
||
assert json.dumps(payload_no_decimal) == '{"a": 1, "b": 2}' | ||
assert json.dumps(payload_no_specials) == '{"a": 1, "b": 2}' | ||
|
||
with raises(TypeError): | ||
json.dumps(payload_with_decimal) | ||
|
||
assert json.dumps(payload_no_decimal, cls=DecimalJsonEncoder) == '{"a": 1, "b": 2}' | ||
with raises(TypeError): | ||
json.dumps(payload_with_datetime) | ||
|
||
with raises(TypeError): | ||
json.dumps(payload_with_bytes) | ||
|
||
assert ( | ||
json.dumps(payload_no_specials, cls=IngesterJsonEncoder) == '{"a": 1, "b": 2}' | ||
) | ||
|
||
assert ( | ||
json.dumps(payload_with_decimal, cls=DecimalJsonEncoder) | ||
json.dumps(payload_with_decimal, cls=IngesterJsonEncoder) | ||
== '{"a": 1.0, "b": 2.0}' | ||
) | ||
|
||
assert ( | ||
json.dumps(payload_with_datetime, cls=IngesterJsonEncoder) | ||
== '{"a": 1700641925.0, "b": 1700641925.0}' | ||
) | ||
|
||
assert json.dumps(payload_with_bytes, cls=IngesterJsonEncoder) == ( | ||
'{"a": [101, 110, 111, 100, 101, 100, 32, 115, 116, 114, 105, 110, 103, 32, ' | ||
'98, 108, 97, 32, 98, 108, 97], "b": [97, 110, 111, 116, 104, 101, 114, 32, ' | ||
"101, 110, 99, 111, 100, 101, 100, 32, 115, 116, 114, 105, 110, 103, 44, 32, " | ||
"108, 111, 111, 107, 32, 97, 116, 32, 109, 101, 44, 32, 73, 39, 109, 32, 115, " | ||
"111, 32, 115, 112, 101, 99, 105, 97, 108]}" | ||
) |