|
1 | 1 | import io
|
| 2 | +import json |
2 | 3 |
|
3 | 4 | import pandas as pd
|
4 | 5 | import pytest
|
@@ -87,3 +88,28 @@ def test_write_vanilla_bytes(tmpdir):
|
87 | 88 | with open(path, "rb") as f:
|
88 | 89 | extracted = f.read()
|
89 | 90 | assert extracted == data
|
| 91 | + |
| 92 | + |
| 93 | +def test_write_json_s3(helper): |
| 94 | + """Writes a dataframe as json, tests that read works.""" |
| 95 | + bucket = "test-bouquet" |
| 96 | + fs = helper.s3fs |
| 97 | + fs.mkdir(bucket) |
| 98 | + |
| 99 | + input = pd.DataFrame({"k1": [1, 2], "k2": [3, 4]}, index=["one", "two"]) |
| 100 | + write_object("s3://test-bouquet/my_df.json", input, format="json") |
| 101 | + output = helper.read_json_file("s3://test-bouquet/my_df.json") |
| 102 | + expected_output = {"k1": {"one": 1, "two": 2}, "k2": {"one": 3, "two": 4}} |
| 103 | + assert output == expected_output |
| 104 | + |
| 105 | + |
| 106 | +def test_write_json(tmpdir): |
| 107 | + """Writes a json file, tests that read works.""" |
| 108 | + input = pd.DataFrame({"k1": [1, 2], "k2": [3, 4]}, index=["one", "two"]) |
| 109 | + path_base = tmpdir.join("my_file.json") |
| 110 | + url = f"file://{path_base}" |
| 111 | + write_object(url, input, format="json") |
| 112 | + with open(path_base, "r") as f: |
| 113 | + output = json.load(f) |
| 114 | + expected_output = {"k1": {"one": 1, "two": 2}, "k2": {"one": 3, "two": 4}} |
| 115 | + assert output == expected_output |
0 commit comments