-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_extraction_test.go
More file actions
117 lines (99 loc) · 2.99 KB
/
Copy pathtext_extraction_test.go
File metadata and controls
117 lines (99 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package edocuenta_test
import (
"context"
"errors"
"strings"
"testing"
edocuenta "github.com/DavidSerranoG/go-estado-cuenta-mx"
)
func TestProcessorDefaultExtractionReportsLedongthucAttempt(t *testing.T) {
t.Parallel()
processor := edocuenta.New()
_, err := processor.ParsePDF(context.Background(), []byte("not a pdf"))
if err == nil {
t.Fatal("expected extraction error")
}
if !errors.Is(err, edocuenta.ErrLedongthucExtractor) {
t.Fatalf("expected ledongthuc extractor failure, got %v", err)
}
var extractionErr *edocuenta.TextExtractionError
if !errors.As(err, &extractionErr) {
t.Fatalf("expected TextExtractionError, got %T", err)
}
if len(extractionErr.Attempts) == 0 {
t.Fatal("expected at least one extraction attempt")
}
if extractionErr.Attempts[0].Extractor != "ledongthuc" || extractionErr.Attempts[0].Status != edocuenta.TextExtractionAttemptFailed {
t.Fatalf("unexpected first attempt %+v", extractionErr.Attempts[0])
}
for _, attempt := range extractionErr.Attempts {
if attempt.Extractor == "tesseract" || attempt.Extractor == "vision" {
t.Fatalf("default extraction path should not use OCR, got %+v", attempt)
}
}
}
func TestProcessorUsesCustomExtractorOverride(t *testing.T) {
t.Parallel()
processor := edocuenta.New(
edocuenta.WithExtractor(staticExtractor{text: "FAKE BANK\n"}),
edocuenta.WithParser(matchingParser{
bank: "fake",
match: "FAKE BANK",
statement: edocuenta.Statement{
Bank: "fake",
Transactions: []edocuenta.Transaction{{Description: "fake"}},
},
}),
)
result, err := processor.ParsePDFResult(context.Background(), []byte("not a pdf"))
if err != nil {
t.Fatalf("parse pdf with explicit extractor: %v", err)
}
statement := result.Statement
if statement.Bank != "fake" {
t.Fatalf("expected fake bank, got %q", statement.Bank)
}
if result.ExtractedText != "FAKE BANK\n" {
t.Fatalf("unexpected extracted text %q", result.ExtractedText)
}
if result.Extraction.SelectedExtractor != "static" {
t.Fatalf("unexpected selected extractor %q", result.Extraction.SelectedExtractor)
}
}
func TestPublicExtractorChainUsesFirstUsableText(t *testing.T) {
t.Parallel()
extractor := edocuenta.NewTextExtractorChain(
staticExtractor{text: " "},
staticExtractor{text: "BBVA\n"},
)
text, err := extractor.ExtractText(context.Background(), []byte("pdf"))
if err != nil {
t.Fatalf("extract text: %v", err)
}
if text != "BBVA\n" {
t.Fatalf("unexpected text %q", text)
}
}
type staticExtractor struct {
text string
}
func (e staticExtractor) Name() string {
return "static"
}
func (e staticExtractor) ExtractText(context.Context, []byte) (string, error) {
return e.text, nil
}
type matchingParser struct {
bank string
match string
statement edocuenta.Statement
}
func (p matchingParser) Bank() string {
return p.bank
}
func (p matchingParser) CanParse(text string) bool {
return strings.Contains(text, p.match)
}
func (p matchingParser) Parse(string) (edocuenta.Statement, error) {
return p.statement, nil
}