(t *testing.T)
| 91 | } |
| 92 | |
| 93 | func TestPDFTextSplit(t *testing.T) { |
| 94 | ctx := context.Background() |
| 95 | t.Parallel() |
| 96 | page1_1Content := "A Simple PDF File This is a small demonstration .pdf file - " + |
| 97 | "just for use in the Virtual Mechanics tutorials. More text. And more text. And more " + |
| 98 | "text. And more text. And more text. And more text. And more text. And more text. And " + |
| 99 | "more text. And more text. And more text. Boring, zzzzz. And more" |
| 100 | page1_2Content := "text. Boring, zzzzz. And more text. And more text. And more text. And " + |
| 101 | "more text. And more text. And more text. And more text. And more text. And more text. And " + |
| 102 | "more text. And more text. And more text. And more text. And more text. And more text. And " + |
| 103 | "more text. Even more. Continued on page 2 ..." |
| 104 | |
| 105 | page2_1Content := "Simple PDF File 2 ...continued from page 1. Yet more text. And more text. " + |
| 106 | "And more text. And more text. And more text. And more text. And more text. And more text. " + |
| 107 | "Oh, how boring typing this stuff. But not as boring as watching paint dry. And more text. " + |
| 108 | "And more text. And more text. And more" |
| 109 | page2_2Content := "text. And more text. And more text. Boring. More, a little more text. The end, and just as well." |
| 110 | |
| 111 | expectedResults := []struct { |
| 112 | content string |
| 113 | metadata map[string]any |
| 114 | }{ |
| 115 | {content: page1_1Content, metadata: map[string]any{"page": 1, "total_pages": 2}}, |
| 116 | {content: page1_2Content, metadata: map[string]any{"page": 1, "total_pages": 2}}, |
| 117 | {content: page2_1Content, metadata: map[string]any{"page": 2, "total_pages": 2}}, |
| 118 | {content: page2_2Content, metadata: map[string]any{"page": 2, "total_pages": 2}}, |
| 119 | } |
| 120 | |
| 121 | t.Run("PDFTextSplit", func(t *testing.T) { |
| 122 | t.Parallel() |
| 123 | f, err := os.Open("./testdata/sample.pdf") |
| 124 | require.NoError(t, err) |
| 125 | defer f.Close() |
| 126 | finfo, err := f.Stat() |
| 127 | require.NoError(t, err) |
| 128 | p := NewPDF(f, finfo.Size()) |
| 129 | split := textsplitter.NewRecursiveCharacter() |
| 130 | split.ChunkSize = 300 |
| 131 | split.ChunkOverlap = 30 |
| 132 | docs, err := p.LoadAndSplit(ctx, split) |
| 133 | require.NoError(t, err) |
| 134 | |
| 135 | assert.Len(t, docs, 4) |
| 136 | |
| 137 | for r := range expectedResults { |
| 138 | assert.Equal(t, expectedResults[r].content, docs[r].PageContent) |
| 139 | assert.Equal(t, expectedResults[r].metadata, docs[r].Metadata) |
| 140 | } |
| 141 | }) |
| 142 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…