import re import pytest from pydantic import ValidationError from src.models import B4wExport, Category from tests.factories import B4wExportFactory def test_b4w_export_simple(): data = B4wExportFactory.build() assert isinstance(data, B4wExport) def test_b4w_export_category(): data = B4wExportFactory.build(Category="fun:stuff") assert isinstance(data.category, Category) assert data.category.to_str() == "fun:stuff" def test_b4w_export_account_iban_invalid(): with pytest.raises(ValidationError): _ = B4wExportFactory.build(OwnrAcctIBAN="not an IBAN") def test_b4w_export_account_iban_to_long(): with pytest.raises(ValidationError): _ = B4wExportFactory.build(OwnrAcctIBAN="DE990123456789012345678901234567890") def test_b4w_export_account_iban_to_short(): with pytest.raises(ValidationError): _ = B4wExportFactory.build(OwnrAcctIBAN="DE123") def test_b4w_export_other_iban_empty_is_none(): data = B4wExportFactory.build(RmtdAcctIBAN="") assert data.other_iban is None def test_b4w_export_other_iban_blank_is_none(): data = B4wExportFactory.build(RmtdAcctIBAN=" ") assert data.other_iban is None def test_b4w_export_other_iban_invalid(): with pytest.raises(ValidationError): _ = B4wExportFactory.build(RmtdAcctIBAN="not an IBAN") def test_b4w_export_other_iban_repr_round_trip_empty(): data = B4wExportFactory.build(RmtdAcctIBAN="") data2 = eval(str(data)) assert data == data2 def test_b4w_export_parameters_to_dict(): data = B4wExportFactory.build() params = data.parameters_to_dict() expected = sorted(field.alias for field in B4wExport.model_fields.values()) assert sorted(params) == expected assert "OwnrAcctCcy" in params assert all(isinstance(value, str) for value in params.values()) def test_b4w_export_parameters_to_str(): data = B4wExportFactory.build() params = data.parameters_to_str() components = [s.strip() for s in re.split(r",\s*(?=\w+=)", params)] param_names = [component.split("=")[0].strip() for component in components] param_values = [component.split("=")[1].strip() for component in components] assert "OwnrAcctCcy" in param_names assert all(s.startswith("'") and s.endswith("'") for s in param_values) def test_b4w_export_repr_round_trip(): data_batch = B4wExportFactory.batch(size=10) assert isinstance(data_batch, list) for data in data_batch: data_str = str(data) data2 = eval(data_str) assert data == data2 def test_b4w_export_amount_two_decimal_places(): data = B4wExportFactory.build() assert data.amount > 0 assert data.amount.as_tuple().exponent >= -2 def test_b4w_export_text_fields_non_empty(): data = B4wExportFactory.build() assert data.reference assert data.notes