|
| 1 | +import csv |
| 2 | +import json |
| 3 | +import pytest |
| 4 | + |
| 5 | +from unittest.mock import MagicMock |
| 6 | + |
| 7 | +from quixstreams.sources import CSVSource |
| 8 | +from quixstreams.rowproducer import RowProducer |
| 9 | + |
| 10 | + |
| 11 | +class TestCSVSource: |
| 12 | + |
| 13 | + @pytest.fixture |
| 14 | + def producer(self): |
| 15 | + producer = MagicMock(spec=RowProducer) |
| 16 | + producer.flush.return_value = 0 |
| 17 | + return producer |
| 18 | + |
| 19 | + def test_read(self, tmp_path, producer): |
| 20 | + path = tmp_path / "source.csv" |
| 21 | + with open(path, "w") as f: |
| 22 | + writer = csv.DictWriter( |
| 23 | + f, dialect="excel", fieldnames=("key", "value", "timestamp") |
| 24 | + ) |
| 25 | + writer.writeheader() |
| 26 | + writer.writerows( |
| 27 | + [ |
| 28 | + {"key": "key1", "value": json.dumps({"value": "value1"})}, |
| 29 | + {"key": "key2", "value": json.dumps({"value": "value2"})}, |
| 30 | + {"key": "key3", "value": json.dumps({"value": "value3"})}, |
| 31 | + {"key": "key4", "value": json.dumps({"value": "value4"})}, |
| 32 | + { |
| 33 | + "key": "key5", |
| 34 | + "value": json.dumps({"value": "value5"}), |
| 35 | + "timestamp": 10000, |
| 36 | + }, |
| 37 | + ] |
| 38 | + ) |
| 39 | + |
| 40 | + source = CSVSource(path) |
| 41 | + source.configure(source.default_topic(), producer) |
| 42 | + source.start() |
| 43 | + |
| 44 | + assert producer.produce.called |
| 45 | + assert producer.produce.call_count == 5 |
| 46 | + assert producer.produce.call_args.kwargs == { |
| 47 | + "buffer_error_max_tries": 3, |
| 48 | + "headers": None, |
| 49 | + "key": b"key5", |
| 50 | + "partition": None, |
| 51 | + "poll_timeout": 5.0, |
| 52 | + "timestamp": 10000, |
| 53 | + "topic": path, |
| 54 | + "value": b'{"value":"value5"}', |
| 55 | + } |
| 56 | + |
| 57 | + def test_read_no_timestamp(self, tmp_path, producer): |
| 58 | + path = tmp_path / "source.csv" |
| 59 | + with open(path, "w") as f: |
| 60 | + writer = csv.DictWriter(f, dialect="excel", fieldnames=("key", "value")) |
| 61 | + writer.writeheader() |
| 62 | + writer.writerows( |
| 63 | + [ |
| 64 | + {"key": "key1", "value": json.dumps({"value": "value1"})}, |
| 65 | + {"key": "key2", "value": json.dumps({"value": "value2"})}, |
| 66 | + {"key": "key3", "value": json.dumps({"value": "value3"})}, |
| 67 | + {"key": "key4", "value": json.dumps({"value": "value4"})}, |
| 68 | + {"key": "key5", "value": json.dumps({"value": "value5"})}, |
| 69 | + ] |
| 70 | + ) |
| 71 | + |
| 72 | + source = CSVSource(path) |
| 73 | + source.configure(source.default_topic(), producer) |
| 74 | + source.start() |
| 75 | + |
| 76 | + assert producer.produce.called |
| 77 | + assert producer.produce.call_count == 5 |
| 78 | + assert producer.produce.call_args.kwargs == { |
| 79 | + "buffer_error_max_tries": 3, |
| 80 | + "headers": None, |
| 81 | + "key": b"key5", |
| 82 | + "partition": None, |
| 83 | + "poll_timeout": 5.0, |
| 84 | + "timestamp": None, |
| 85 | + "topic": path, |
| 86 | + "value": b'{"value":"value5"}', |
| 87 | + } |
0 commit comments