-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_parse_test.go
More file actions
230 lines (209 loc) · 7.36 KB
/
Copy pathparse_parse_test.go
File metadata and controls
230 lines (209 loc) · 7.36 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2025 SIXT SE
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tensorlake
import (
"os"
"path/filepath"
"slices"
"testing"
"github.com/google/jsonschema-go/jsonschema"
)
func TestParseDocumentRemote(t *testing.T) {
c := initializeTestClient(t)
tests := []struct {
req *ParseDocumentRequest
}{
{
req: &ParseDocumentRequest{
FileSource: FileSource{
FileURL: "https://www.sixt.de/shared/t-c/sixt_DE_de.pdf",
},
ParsingOptions: &ParsingOptions{
ChunkingStrategy: ChunkingStrategyNone,
},
EnrichmentOptions: &EnrichmentOptions{
TableSummarization: true,
},
Labels: map[string]string{"category": "terms-and-conditions"},
},
},
}
for _, tt := range tests {
t.Run(tt.req.FileSource.FileURL, func(t *testing.T) {
func() {
// Trigger read document operation.
r, err := c.ParseDocument(t.Context(), tt.req)
if err != nil {
t.Fatalf("failed to parse document: %v", err)
}
if r == nil {
t.Fatal("response is nil")
}
t.Logf("read document done, parse ID: %s", r.ParseId)
// Read document status.
result, err := c.GetParseResult(t.Context(), r.ParseId, WithSSE(true), WithOnUpdate(func(name ParseEventName, _ *ParseResult) {
t.Logf("parse status: %s", name)
}))
if err != nil {
t.Fatalf("failed to get parse result: %v", err)
}
if len(result.Chunks) == 0 {
t.Fatalf("no chunks found")
}
peak := result.Chunks[0].Content
if len(peak) > 100 {
peak = peak[:100]
}
t.Logf("parse result: %+v", peak)
// Validate parse results.
jobs := []string{}
for j, err := range c.IterParseJobs(t.Context(), 1) {
if err != nil {
t.Fatalf("failed to list parse jobs: %v", err)
}
jobs = append(jobs, j.ParseId)
}
t.Logf("listed %d parse jobs: %v", len(jobs), jobs)
if !slices.Contains(jobs, r.ParseId) {
t.Fatalf("parse job is not found in list: %v", jobs)
}
testCleanupFileAndParseJob(t, c, "", r.ParseId)
}()
})
}
}
type Address struct {
Street string `json:"street" jsonschema:"Street address"`
City string `json:"city" jsonschema:"City"`
State string `json:"state" jsonschema:"State/Province code or name"`
ZipCode string `json:"zip_code" jsonschema:"Postal code"`
}
type BankTransaction struct {
TransactionDeposit float64 `json:"transaction_deposit" jsonschema:"Deposit amount"`
TransactionDepositDate string `json:"transaction_deposit_date" jsonschema:"Date of the deposit"`
TransactionDepositDescription string `json:"transaction_deposit_description" jsonschema:"Description of the deposit"`
TransactionWithdrawal float64 `json:"transaction_withdrawal" jsonschema:"Withdrawal amount"`
TransactionWithdrawalDate string `json:"transaction_withdrawal_date" jsonschema:"Date of the withdrawal"`
TransactionWithdrawalDescription string `json:"transaction_withdrawal_description" jsonschema:"Description of the withdrawal"`
}
type BankStatement struct {
AccountNumber string `json:"account_number" jsonschema:"Bank account number"`
AccountType string `json:"account_type" jsonschema:"Type of the bank account (e.g. Checking/Savings)"`
BankAddress Address `json:"bank_address" jsonschema:"Address of the bank"`
BankName string `json:"bank_name" jsonschema:"Name of the bank"`
ClientAddress Address `json:"client_address" jsonschema:"Address of the client"`
ClientName string `json:"client_name" jsonschema:"Name of the client"`
EndingBalance float64 `json:"ending_balance" jsonschema:"Ending balance for the period"`
StartingBalance float64 `json:"starting_balance" jsonschema:"Starting balance for the period"`
StatementDate string `json:"statement_date" jsonschema:"Overall statement date if applicable"`
StatementStartDate string `json:"statement_start_date" jsonschema:"Start date of the bank statement"`
StatementEndDate string `json:"statement_end_date" jsonschema:"End date of the bank statement"`
TableItem []BankTransaction `json:"table_item" jsonschema:"List of transactions in the statement"`
Others map[string]any `json:"others" jsonschema:"Any other additional data from the statement"`
}
func TestParseDocumentStructuredExtraction(t *testing.T) {
c := initializeTestClient(t)
tests := []struct {
filepath string
req *ParseDocumentRequest
}{
{
filepath: "testdata/bank_statement.pdf",
req: &ParseDocumentRequest{
FileSource: FileSource{
FileId: "", // fill later.
},
ParsingOptions: &ParsingOptions{
ChunkingStrategy: ChunkingStrategyNone,
},
StructuredExtractionOptions: []StructuredExtractionOptions{
{
SchemaName: "form125-basic",
JSONSchema: func() *jsonschema.Schema {
s, err := jsonschema.For[BankStatement](nil)
if err != nil {
t.Fatalf("failed to get JSON schema: %v", err)
}
return s
}(),
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.filepath, func(t *testing.T) {
func() {
// Upload file.
file, err := os.Open(tt.filepath)
if err != nil {
t.Fatalf("failed to open file: %v", err)
}
defer file.Close()
// Upload file.
resp, err := c.UploadFile(t.Context(), &UploadFileRequest{
FileBytes: file,
FileName: filepath.Base(tt.filepath),
})
if err != nil {
t.Fatalf("failed to upload file: %v", err)
}
if resp == nil {
t.Fatal("response is nil")
}
if resp.FileId == "" {
t.Fatal("file ID is empty")
}
tt.req.FileSource.FileId = resp.FileId
// Parse document.
r, err := c.ParseDocument(t.Context(), tt.req)
if err != nil {
t.Fatalf("failed to parse document: %v", err)
}
if r == nil {
t.Fatal("response is nil")
}
t.Logf("parse document done, parse ID: %s", r.ParseId)
// Get parse result.
result, err := c.GetParseResult(t.Context(), r.ParseId, WithSSE(true), WithOnUpdate(func(name ParseEventName, _ *ParseResult) {
t.Logf("parse status: %s", name)
}))
if err != nil {
t.Fatalf("failed to get parse result: %v", err)
}
if len(result.Chunks) == 0 {
t.Fatalf("no chunks found")
}
peak := result.Chunks[0].Content
if len(peak) > 100 {
peak = peak[:100]
}
t.Logf("parse result: %+v", peak)
// Validate parse results.
jobs := []string{}
for j, err := range c.IterParseJobs(t.Context(), 1) {
if err != nil {
t.Fatalf("failed to list parse jobs: %v", err)
}
jobs = append(jobs, j.ParseId)
}
t.Logf("listed %d parse jobs: %v", len(jobs), jobs)
if !slices.Contains(jobs, r.ParseId) {
t.Fatalf("parse job is not found in list: %v", jobs)
}
testCleanupFileAndParseJob(t, c, resp.FileId, r.ParseId)
}()
})
}
}