feat: add clickhouse micro-batch storage
This commit is contained in:
101
tests/unit/test_batch.py
Normal file
101
tests/unit/test_batch.py
Normal file
@@ -0,0 +1,101 @@
|
||||
from datetime import datetime, timezone
|
||||
from ipaddress import IPv4Address
|
||||
from uuid import uuid4
|
||||
|
||||
from core.domain.models import NormalizedCDR
|
||||
from core.worker.batch import (
|
||||
BufferedCDR,
|
||||
KafkaPosition,
|
||||
MicroBatch,
|
||||
)
|
||||
|
||||
|
||||
def normalized_cdr(
|
||||
raw_id: str,
|
||||
) -> NormalizedCDR:
|
||||
now = datetime.now(
|
||||
timezone.utc
|
||||
)
|
||||
|
||||
return NormalizedCDR(
|
||||
cdr_id=uuid4(),
|
||||
raw_id=raw_id,
|
||||
|
||||
msisdn="+19875551234",
|
||||
imsi="310410998877665",
|
||||
|
||||
service_type="4G_DATA",
|
||||
radio_access_type="LTE_4G",
|
||||
cell_id="4021_8812",
|
||||
|
||||
bytes_uploaded=100,
|
||||
bytes_downloaded=200,
|
||||
total_bytes=300,
|
||||
duration_seconds=300,
|
||||
|
||||
session_start_time=now,
|
||||
session_end_time=now,
|
||||
|
||||
source_ip=IPv4Address(
|
||||
"10.240.12.1"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def buffered_item(
|
||||
raw_id: str,
|
||||
offset: int,
|
||||
) -> BufferedCDR:
|
||||
return BufferedCDR(
|
||||
cdr=normalized_cdr(raw_id),
|
||||
position=KafkaPosition(
|
||||
topic="cdr.raw",
|
||||
partition=0,
|
||||
offset=offset,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_batch_flushes_when_full() -> None:
|
||||
batch = MicroBatch(
|
||||
max_size=2,
|
||||
flush_interval_seconds=10,
|
||||
)
|
||||
|
||||
batch.add(
|
||||
buffered_item(
|
||||
raw_id="raw-1",
|
||||
offset=1,
|
||||
)
|
||||
)
|
||||
|
||||
assert batch.is_full is False
|
||||
|
||||
batch.add(
|
||||
buffered_item(
|
||||
raw_id="raw-2",
|
||||
offset=2,
|
||||
)
|
||||
)
|
||||
|
||||
assert batch.is_full is True
|
||||
|
||||
|
||||
def test_batch_clear_resets_state() -> None:
|
||||
batch = MicroBatch(
|
||||
max_size=2,
|
||||
flush_interval_seconds=10,
|
||||
)
|
||||
|
||||
batch.add(
|
||||
buffered_item(
|
||||
raw_id="raw-1",
|
||||
offset=1,
|
||||
)
|
||||
)
|
||||
|
||||
batch.clear()
|
||||
|
||||
assert batch.size == 0
|
||||
assert batch.is_empty is True
|
||||
assert batch.is_due() is False
|
||||
Reference in New Issue
Block a user