Skip to content

Commit

Permalink
fix: Serialization of decimal.Decimal (#1826)
Browse files Browse the repository at this point in the history
Co-authored-by: Pat Nadolny <[email protected]>
  • Loading branch information
edgarrmondragon and pnadolny13 authored Jul 10, 2023
1 parent 5a1190a commit 8fb30f3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion singer_sdk/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
30 changes: 30 additions & 0 deletions tests/core/test_batch.py
Original file line number Diff line number Diff line change
@@ -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,
)
Expand Down Expand Up @@ -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
)

0 comments on commit 8fb30f3

Please sign in to comment.