From 8fb30f37ac9fa107237d3c197c5c9b4e8b931132 Mon Sep 17 00:00:00 2001 From: "Edgar R. M" Date: Mon, 10 Jul 2023 11:45:25 -0600 Subject: [PATCH] fix: Serialization of `decimal.Decimal` (#1826) Co-authored-by: Pat Nadolny --- singer_sdk/batch.py | 3 ++- tests/core/test_batch.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/singer_sdk/batch.py b/singer_sdk/batch.py index f6bc966ab..0cbf11917 100644 --- a/singer_sdk/batch.py +++ b/singer_sdk/batch.py @@ -104,7 +104,8 @@ def get_batches( mode="wb", ) as gz: gz.writelines( - (json.dumps(record) + "\n").encode() for record in chunk + (json.dumps(record, default=str) + "\n").encode() + for record in chunk ) file_url = fs.geturl(filename) yield [file_url] diff --git a/tests/core/test_batch.py b/tests/core/test_batch.py index f818cb2e9..6efb3b34a 100644 --- a/tests/core/test_batch.py +++ b/tests/core/test_batch.py @@ -1,11 +1,15 @@ from __future__ import annotations +import decimal +import re from dataclasses import asdict import pytest +from singer_sdk.batch import JSONLinesBatcher from singer_sdk.helpers._batch import ( BaseBatchFileEncoding, + BatchConfig, JSONLinesEncoding, StorageTarget, ) @@ -95,3 +99,29 @@ def test_storage_from_url(file_url: str, root: str): def test_storage_split_url(file_url: str, expected: tuple): """Test storage target split URL.""" assert StorageTarget.split_url(file_url) == expected + + +def test_json_lines_batcher(): + batcher = JSONLinesBatcher( + "tap-test", + "stream-test", + batch_config=BatchConfig( + encoding=JSONLinesEncoding("gzip"), + storage=StorageTarget("file:///tmp/sdk-batches"), + batch_size=2, + ), + ) + records = [ + {"id": 1, "numeric": decimal.Decimal("1.0")}, + {"id": 2, "numeric": decimal.Decimal("2.0")}, + {"id": 3, "numeric": decimal.Decimal("3.0")}, + ] + + batches = list(batcher.get_batches(records)) + assert len(batches) == 2 + assert all(len(batch) == 1 for batch in batches) + assert all( + re.match(r".*tap-test--stream-test-.*\.json.gz", filepath) + for batch in batches + for filepath in batch + )