Skip to content

Commit

Permalink
testing for proper JSON encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkinnison committed Jul 26, 2022
1 parent f586a17 commit 4f015b5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/ludwig/utils/test_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import json

import numpy as np
import pandas as pd
import pytest
Expand All @@ -24,6 +26,7 @@
get_abs_path,
hash_dict,
use_credentials,
NumpyEncoder,
)

try:
Expand Down Expand Up @@ -126,3 +129,29 @@ def test_use_credentials():
assert conf == s3_creds

assert len(conf) == 0


def test_numpy_encoder():
# Test Python builtin data type encoding.
assert json.dumps(None, cls=NumpyEncoder) == "null"
assert json.dumps({}, cls=NumpyEncoder) == "{}"
assert json.dumps(1, cls=NumpyEncoder) == "1"
assert json.dumps(1.0, cls=NumpyEncoder) == "1.0"
assert json.dumps("a", cls=NumpyEncoder) == '"a"'
assert json.dumps([0, 1, 2, 3, 4], cls=NumpyEncoder) == "[0, 1, 2, 3, 4]"
assert json.dumps((0, 1, 2, 3, 4), cls=NumpyEncoder) == "[0, 1, 2, 3, 4]"
assert json.dumps({0, 1, 2, 3, 4}, cls=NumpyEncoder) == "[0, 1, 2, 3, 4]"
assert json.dumps({"a": "b"}, cls=NumpyEncoder) == '{"a": "b"}'

# Test numpy data type encoding
for dtype in [np.byte, np.ubyte, np.short, np.ushort, np.int, np.uint, np.longlong, np.ulonglong]:
x = np.arange(5, dtype=dtype)
assert json.dumps(x, cls=NumpyEncoder) == "[0, 1, 2, 3, 4]"
for i in x:
assert json.dumps(i, cls=NumpyEncoder) == f"{i}"

for dtype in [np.half, np.single, np.double, np.longdouble]:
x = np.arange(5, dtype=dtype)
assert json.dumps(x, cls=NumpyEncoder) == "[0.0, 1.0, 2.0, 3.0, 4.0]"
for i in x:
assert json.dumps(i, cls=NumpyEncoder) == f"{i}"
32 changes: 32 additions & 0 deletions tests/ludwig/utils/test_server_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np
import pytest

from ludwig.utils.server_utils import NumpyJSONResponse


def test_numpy_json_response():
response = NumpyJSONResponse()

# Test Python builtin data type encoding.
assert response.render(None) == "null".encode("utf-8")
assert response.render({}) == "{}".encode("utf-8")
assert response.render(1) == "1".encode("utf-8")
assert response.render(1.0) == "1.0".encode("utf-8")
assert response.render("a") == '"a"'.encode("utf-8")
assert response.render([0, 1, 2, 3, 4]) == "[0,1,2,3,4]".encode("utf-8")
assert response.render((0, 1, 2, 3, 4)) == "[0,1,2,3,4]".encode("utf-8")
assert response.render({0, 1, 2, 3, 4}) == "[0,1,2,3,4]".encode("utf-8")
assert response.render({"a": "b"}) == '{"a":"b"}'.encode("utf-8")

# Test numpy data type encoding
for dtype in [np.byte, np.ubyte, np.short, np.ushort, np.int, np.uint, np.longlong, np.ulonglong]:
x = np.arange(5, dtype=dtype)
assert response.render(x) == "[0,1,2,3,4]".encode("utf-8")
for i in x:
assert response.render(i) == f"{i}".encode("utf-8")

for dtype in [np.half, np.single, np.double, np.longdouble]:
x = np.arange(5, dtype=dtype)
assert response.render(x) == "[0.0,1.0,2.0,3.0,4.0]".encode("utf-8")
for i in x:
assert response.render(i) == f"{i}".encode("utf-8")

0 comments on commit 4f015b5

Please sign in to comment.