Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/test_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ def handler(request):
assert response.ok


def test_append_ndjson(self, httpserver: HTTPServer):
ds = Datasource("mydatasource", "123456", api=httpserver.url_for("/"))

records = [{"key": "foo", "value": "bar"}, {"key": "baz", "value": "ed"}]
expected_body = '{"key": "foo", "value": "bar"}\n{"key": "baz", "value": "ed"}\n'

httpserver.expect_request(
"/v0/datasources",
query_string={
"name": "mydatasource",
"mode": "append",
"format": "ndjson",
},
data=expected_body,
headers={"Content-Length": str(len(expected_body.encode()))},
).respond_with_data("", 200)

response = ds.append_ndjson(records)
httpserver.check()
assert response.ok


class TestFileDatasource:
def test_append(self, tmp_path):
file_path = tmp_path / "myfile.csv"
Expand Down
6 changes: 2 additions & 4 deletions verdin/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ def append_ndjson(self, records: List[Dict]) -> requests.Response:
:return: The HTTP response
"""

def _ndjson_iterator():
for record in records:
yield json.dumps(record) + "\n"
data = "".join(json.dumps(record) + "\n" for record in records)

LOG.debug(
"appending %d ndjson records to %s via %s",
Expand All @@ -160,7 +158,7 @@ def _ndjson_iterator():
response = self._datasources_api.append(
name=self.canonical_name,
format="ndjson",
data=_ndjson_iterator(),
data=data,
)

return response._response
Expand Down