Skip to content

Commit

Permalink
Avoid test duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Jul 17, 2024
1 parent cbe10bd commit 8d32686
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 186 deletions.
2 changes: 1 addition & 1 deletion singer_sdk/_singerlib/encoding/_msgspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def serialize_message(self, message: Message) -> bytes: # noqa: PLR6301
Returns:
A string of serialized json.
"""
return encoder.encode(message)
return encoder.encode(message.to_dict())

def write_message(self, message: Message) -> None:
"""Write a message to stdout.
Expand Down
34 changes: 0 additions & 34 deletions tests/_singerlib/encoding/conftest.py
Original file line number Diff line number Diff line change
@@ -1,35 +1 @@
from __future__ import annotations # noqa: INP001

import json

import pytest

from singer_sdk._singerlib import RecordMessage


@pytest.fixture
def bench_record():
return {
"stream": "users",
"type": "RECORD",
"record": {
"Id": 1,
"created_at": "2021-01-01T00:08:00-07:00",
"updated_at": "2022-01-02T00:09:00-07:00",
"deleted_at": "2023-01-03T00:10:00-07:00",
"value": 1.23,
"RelatedId": 32412,
"TypeId": 1,
},
"time_extracted": "2023-01-01T11:00:00.00000-07:00",
}


@pytest.fixture
def bench_record_message(bench_record):
return RecordMessage.from_dict(bench_record)


@pytest.fixture
def bench_encoded_record(bench_record):
return json.dumps(bench_record)
30 changes: 0 additions & 30 deletions tests/_singerlib/encoding/test_msgspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import decimal
import io
import itertools
from contextlib import nullcontext, redirect_stdout
from textwrap import dedent

Expand Down Expand Up @@ -136,32 +135,3 @@ def test_write_message():
assert out.read() == (
'{"type":"RECORD","stream":"test","record":{"id":1,"name":"test"}}\n'
)


# Benchmark Tests


def test_bench_format_message(benchmark, bench_record_message: RecordMessage):
"""Run benchmark for Sink._validator method validate."""
number_of_runs = 1000

writer = MsgSpecWriter()

def run_format_message():
for record in itertools.repeat(bench_record_message, number_of_runs):
writer.format_message(record)

benchmark(run_format_message)


def test_bench_deserialize_json(benchmark, bench_encoded_record: str):
"""Run benchmark for Sink._validator method validate."""
number_of_runs = 1000

reader = DummyReader()

def run_deserialize_json():
for record in itertools.repeat(bench_encoded_record, number_of_runs):
reader.deserialize_json(record)

benchmark(run_deserialize_json)
30 changes: 0 additions & 30 deletions tests/_singerlib/encoding/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import decimal
import io
import itertools
from contextlib import nullcontext, redirect_stdout
from textwrap import dedent

Expand Down Expand Up @@ -94,32 +93,3 @@ def test_write_message():
assert out.getvalue() == (
'{"type":"RECORD","stream":"test","record":{"id":1,"name":"test"}}\n'
)


# Benchmark Tests


def test_bench_format_message(benchmark, bench_record_message: RecordMessage):
"""Run benchmark for Sink._validator method validate."""
number_of_runs = 1000

writer = SingerWriter()

def run_format_message():
for record in itertools.repeat(bench_record_message, number_of_runs):
writer.format_message(record)

benchmark(run_format_message)


def test_bench_deserialize_json(benchmark, bench_encoded_record: str):
"""Run benchmark for Sink._validator method validate."""
number_of_runs = 1000

reader = DummyReader()

def run_deserialize_json():
for record in itertools.repeat(bench_encoded_record, number_of_runs):
reader.deserialize_json(record)

benchmark(run_deserialize_json)
94 changes: 3 additions & 91 deletions tests/core/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,15 @@

from __future__ import annotations

import decimal
import io
import itertools
import json
from contextlib import nullcontext, redirect_stdout
from textwrap import dedent

import pytest

from singer_sdk._singerlib import RecordMessage
from singer_sdk._singerlib.encoding._msgspec import MsgSpecReader, MsgSpecWriter
from singer_sdk._singerlib.exceptions import InvalidInputLine
from singer_sdk.io_base import SingerReader, SingerWriter


class DummyReader(SingerReader):
def _process_activate_version_message(self, message_dict: dict) -> None:
pass

def _process_batch_message(self, message_dict: dict) -> None:
pass

def _process_record_message(self, message_dict: dict) -> None:
pass

def _process_schema_message(self, message_dict: dict) -> None:
pass

def _process_state_message(self, message_dict: dict) -> None:
pass


@pytest.mark.parametrize(
"line,expected,exception",
[
pytest.param(
"not-valid-json",
None,
pytest.raises(InvalidInputLine),
id="unparsable",
),
pytest.param(
'{"type": "RECORD", "stream": "users", "record": {"id": 1, "value": 1.23}}',
{
"type": "RECORD",
"stream": "users",
"record": {"id": 1, "value": decimal.Decimal("1.23")},
},
nullcontext(),
id="record",
),
],
)
def test_deserialize(line, expected, exception):
reader = DummyReader()
with exception:
assert reader.deserialize_json(line) == expected


def test_listen():
reader = DummyReader()
input_lines = io.StringIO(
dedent("""\
{"type": "SCHEMA", "stream": "users", "schema": {"type": "object", "properties": {"id": {"type": "integer"}, "value": {"type": "number"}}}}
{"type": "RECORD", "stream": "users", "record": {"id": 1, "value": 1.23}}
{"type": "RECORD", "stream": "users", "record": {"id": 2, "value": 2.34}}
{"type": "STATE", "value": {"bookmarks": {"users": {"id": 2}}}}
{"type": "SCHEMA", "stream": "batches", "schema": {"type": "object", "properties": {"id": {"type": "integer"}, "value": {"type": "number"}}}}
{"type": "BATCH", "stream": "batches", "encoding": {"format": "jsonl", "compression": "gzip"}, "manifest": ["file1.jsonl.gz", "file2.jsonl.gz"]}
{"type": "STATE", "value": {"bookmarks": {"users": {"id": 2}, "batches": {"id": 1000000}}}}
""") # noqa: E501
)
reader.listen(input_lines)


def test_listen_unknown_message():
reader = DummyReader()
input_lines = io.StringIO('{"type": "UNKNOWN"}\n')
with pytest.raises(ValueError, match="Unknown message type"):
reader.listen(input_lines)


def test_write_message():
writer = SingerWriter()
message = RecordMessage(
stream="test",
record={"id": 1, "name": "test"},
)
with redirect_stdout(io.StringIO()) as out:
writer.write_message(message)

assert out.getvalue() == (
'{"type":"RECORD","stream":"test","record":{"id":1,"name":"test"}}\n'
)


# Benchmark Tests
# IO Benchmarks


@pytest.fixture
Expand Down Expand Up @@ -129,7 +41,7 @@ def bench_encoded_record(bench_record):
return json.dumps(bench_record)


def test_bench_format_message(benchmark, bench_record_message):
def test_bench_format_message(benchmark, bench_record_message: RecordMessage):
"""Run benchmark for Sink._validator method validate."""
number_of_runs = 1000

Expand All @@ -142,7 +54,7 @@ def run_format_message():
benchmark(run_format_message)


def test_bench_deserialize_json(benchmark, bench_encoded_record):
def test_bench_deserialize_json(benchmark, bench_encoded_record: str):
"""Run benchmark for Sink._validator method validate."""
number_of_runs = 1000

Expand Down

0 comments on commit 8d32686

Please sign in to comment.