66
77import pytest
88
9+ from bankstatements_core .domain .models .extraction_warning import (
10+ CODE_DATE_PROPAGATED ,
11+ ExtractionWarning ,
12+ )
913from bankstatements_core .domain .models .transaction import Transaction
1014
1115
@@ -602,17 +606,21 @@ def test_extraction_warnings_defaults_to_empty_list(self):
602606 assert tx .extraction_warnings == []
603607
604608 def test_extraction_warnings_can_be_set (self ):
605- """TXEN-02: Transaction(extraction_warnings=['missing balance']) stores value."""
609+ """TXEN-02: Transaction(extraction_warnings=[ExtractionWarning(...)]) stores value."""
610+ w = ExtractionWarning (
611+ code = CODE_DATE_PROPAGATED ,
612+ message = "date propagated from previous row ('01 Jan 2024')" ,
613+ )
606614 tx = Transaction (
607615 date = "01/01/2024" ,
608616 details = "Test" ,
609617 debit = None ,
610618 credit = "10.00" ,
611619 balance = "100.00" ,
612620 filename = "test.pdf" ,
613- extraction_warnings = ["missing balance" ],
621+ extraction_warnings = [w ],
614622 )
615- assert tx .extraction_warnings == ["missing balance" ]
623+ assert tx .extraction_warnings == [w ]
616624
617625 def test_extraction_warnings_no_shared_mutable_default (self ):
618626 """TXEN-02: Two Transaction() instances have separate extraction_warnings lists."""
@@ -632,21 +640,36 @@ def test_extraction_warnings_no_shared_mutable_default(self):
632640 balance = "120.00" ,
633641 filename = "test.pdf" ,
634642 )
635- tx1 .extraction_warnings .append ("warning" )
643+ tx1 .extraction_warnings .append (
644+ ExtractionWarning (code = CODE_DATE_PROPAGATED , message = "test" )
645+ )
636646 assert tx2 .extraction_warnings == []
637647
638648 def test_to_dict_extraction_warnings_serialises_as_json_string (self ):
639- """TXEN-02: to_dict() with extraction_warnings=['missing balance'] → JSON string."""
649+ """TXEN-02: to_dict() with an ExtractionWarning → JSON string of list of dicts."""
650+ w = ExtractionWarning (
651+ code = CODE_DATE_PROPAGATED ,
652+ message = "date propagated from previous row ('01 Jan 2024')" ,
653+ )
640654 tx = Transaction (
641655 date = "01/01/2024" ,
642656 details = "Test" ,
643657 debit = None ,
644658 credit = "10.00" ,
645659 balance = "100.00" ,
646660 filename = "test.pdf" ,
647- extraction_warnings = ["missing balance" ],
661+ extraction_warnings = [w ],
648662 )
649- assert tx .to_dict ()["extraction_warnings" ] == '["missing balance"]'
663+ import json
664+
665+ serialised = json .loads (tx .to_dict ()["extraction_warnings" ])
666+ assert serialised == [
667+ {
668+ "code" : CODE_DATE_PROPAGATED ,
669+ "message" : "date propagated from previous row ('01 Jan 2024')" ,
670+ "page" : None ,
671+ }
672+ ]
650673
651674 def test_to_dict_extraction_warnings_empty_serialises_as_json_array (self ):
652675 """TXEN-02: to_dict() with extraction_warnings=[] → '[]'."""
@@ -661,16 +684,25 @@ def test_to_dict_extraction_warnings_empty_serialises_as_json_array(self):
661684 assert tx .to_dict ()["extraction_warnings" ] == "[]"
662685
663686 def test_from_dict_extraction_warnings_parses_json_string (self ):
664- """TXEN-02: from_dict({'extraction_warnings': '["missing balance"]'}) → list."""
687+ """TXEN-02: from_dict with a JSON-encoded ExtractionWarning → list[ExtractionWarning]."""
688+ import json
689+
690+ w = {
691+ "code" : CODE_DATE_PROPAGATED ,
692+ "message" : "date propagated from previous row ('01 Jan 2024')" ,
693+ "page" : None ,
694+ }
665695 tx = Transaction .from_dict (
666696 {
667697 "Date" : "01/01/2024" ,
668698 "Details" : "Test" ,
669699 "Filename" : "test.pdf" ,
670- "extraction_warnings" : '["missing balance"]' ,
700+ "extraction_warnings" : json . dumps ([ w ]) ,
671701 }
672702 )
673- assert tx .extraction_warnings == ["missing balance" ]
703+ assert len (tx .extraction_warnings ) == 1
704+ assert isinstance (tx .extraction_warnings [0 ], ExtractionWarning )
705+ assert tx .extraction_warnings [0 ].code == CODE_DATE_PROPAGATED
674706
675707 def test_from_dict_extraction_warnings_absent_defaults_to_empty_list (self ):
676708 """TXEN-02: from_dict({}) (key absent) → extraction_warnings == []."""
@@ -684,26 +716,35 @@ def test_from_dict_extraction_warnings_absent_defaults_to_empty_list(self):
684716
685717 def test_extraction_warnings_roundtrip (self ):
686718 """TXEN-02: from_dict(tx.to_dict()) preserves extraction_warnings."""
719+ w = ExtractionWarning (
720+ code = CODE_DATE_PROPAGATED ,
721+ message = "date propagated from previous row ('01 Jan 2024')" ,
722+ )
687723 original = Transaction (
688724 date = "01/01/2024" ,
689725 details = "Test" ,
690726 debit = None ,
691727 credit = "10.00" ,
692728 balance = "100.00" ,
693729 filename = "test.pdf" ,
694- extraction_warnings = ["missing balance" ],
730+ extraction_warnings = [w ],
695731 )
696732 restored = Transaction .from_dict (original .to_dict ())
697- assert restored .extraction_warnings == ["missing balance" ]
733+ assert len (restored .extraction_warnings ) == 1
734+ assert restored .extraction_warnings [0 ].code == CODE_DATE_PROPAGATED
735+ assert restored .extraction_warnings [0 ].message == w .message
698736
699737 def test_extraction_warnings_not_in_additional_fields (self ):
700738 """TXEN-02: extraction_warnings JSON key is gated by standard_keys — not absorbed into additional_fields."""
739+ import json
740+
741+ w = {"code" : CODE_DATE_PROPAGATED , "message" : "test" , "page" : None }
701742 tx = Transaction .from_dict (
702743 {
703744 "Date" : "01/01/2024" ,
704745 "Details" : "Test" ,
705746 "Filename" : "test.pdf" ,
706- "extraction_warnings" : '["x"]' ,
747+ "extraction_warnings" : json . dumps ([ w ]) ,
707748 }
708749 )
709750 assert "extraction_warnings" not in tx .additional_fields
@@ -822,12 +863,15 @@ def test_full_roundtrip_all_three_enrichment_fields(self):
822863 filename = "statement.pdf" ,
823864 source_page = 3 ,
824865 confidence_score = 0.8 ,
825- extraction_warnings = ["missing balance" ],
866+ extraction_warnings = [
867+ ExtractionWarning (code = CODE_DATE_PROPAGATED , message = "missing balance" )
868+ ],
826869 )
827870 restored = Transaction .from_dict (original .to_dict ())
828871 assert restored .source_page == 3
829872 assert restored .confidence_score == 0.8
830- assert restored .extraction_warnings == ["missing balance" ]
873+ assert len (restored .extraction_warnings ) == 1
874+ assert restored .extraction_warnings [0 ].code == CODE_DATE_PROPAGATED
831875
832876 def test_backward_compat_old_dict_without_new_keys (self ):
833877 """TXEN-04: Old dict without new keys → from_dict() succeeds with defaults."""
0 commit comments