-
-
Notifications
You must be signed in to change notification settings - Fork 0
tests: add fixture corpus quality coverage #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import FetchCore | ||
| import Testing | ||
| @testable import FetchKit | ||
|
|
||
| @Suite("FetchKit fixture corpus quality", .serialized) | ||
| struct FixtureCorpusQualityTests { | ||
| @Test("Fixture corpus records carry source attribution") | ||
| func fixtureCorpusRecordsCarrySourceAttribution() { | ||
| #expect(GutenbergMiniCorpus.source.datasetID == "zkeown/gutenberg-corpus") | ||
| #expect(GutenbergMiniCorpus.source.config == "chapters") | ||
| #expect(GutenbergMiniCorpus.source.split == "train") | ||
| #expect(GutenbergMiniCorpus.records.allSatisfy { $0.sourceURI == GutenbergMiniCorpus.source.url }) | ||
| #expect(GutenbergMiniCorpus.records.allSatisfy { $0.metadata["fixture.dataset"] == GutenbergMiniCorpus.source.datasetID }) | ||
| } | ||
|
|
||
| @Test("Fixture corpus retrieves a body-driven chapter hit") | ||
| func fixtureCorpusRetrievesBodyDrivenChapterHit() async throws { | ||
| let library = try await indexedFixtureLibrary() | ||
|
|
||
| let results = try await library.search( | ||
| "storage food seeds", | ||
| kind: .allTerms, | ||
| fields: [.title, .body], | ||
| limit: 3 | ||
| ) | ||
| let firstResult = try #require(results.first) | ||
|
|
||
| #expect(firstResult.document.id == "gutenberg-78430-chapter-1") | ||
| #expect(firstResult.snippet?.text.localizedCaseInsensitiveContains("storage") == true) | ||
| #expect(firstResult.snippet?.text.localizedCaseInsensitiveContains("food") == true) | ||
| #expect(firstResult.snippet?.text.localizedCaseInsensitiveContains("seeds") == true) | ||
| #expect((firstResult.snippet?.matchRanges.count ?? 0) >= 3) | ||
| #expect(firstResult.matchedFields == [.body]) | ||
| #expect(firstResult.snippetField == .body) | ||
| } | ||
|
|
||
| @Test("Fixture corpus keeps closely related chapters separate") | ||
| func fixtureCorpusKeepsRelatedChaptersSeparate() async throws { | ||
| let library = try await indexedFixtureLibrary() | ||
|
|
||
| let foodStorageResults = try await library.search( | ||
| "storage food seeds", | ||
| kind: .allTerms, | ||
| fields: [.body], | ||
| limit: 4 | ||
| ) | ||
| let germinationResults = try await library.search( | ||
| "germinating seed organic", | ||
| kind: .allTerms, | ||
| fields: [.body], | ||
| limit: 4 | ||
| ) | ||
|
|
||
| #expect(foodStorageResults.map(\.document.id) == ["gutenberg-78430-chapter-1"]) | ||
| #expect(germinationResults.map(\.document.id) == ["gutenberg-78430-chapter-2"]) | ||
| } | ||
|
|
||
| @Test("Fixture corpus title-only hits use the title as the current snippet source") | ||
| func fixtureCorpusTitleOnlyHitUsesTitleSnippet() async throws { | ||
| let library = try await indexedFixtureLibrary() | ||
|
|
||
| let results = try await library.search( | ||
| "rocket test pilot", | ||
| kind: .allTerms, | ||
| fields: [.title, .body], | ||
| limit: 3 | ||
| ) | ||
| let firstResult = try #require(results.first) | ||
| let snippet = try #require(firstResult.snippet) | ||
|
|
||
| #expect(firstResult.document.id == "gutenberg-78431-book") | ||
| #expect(firstResult.matchedFields == [.title]) | ||
| #expect(firstResult.snippetField == .title) | ||
| #expect(snippet.text.localizedCaseInsensitiveContains("rocket test pilot")) | ||
| #expect(!snippet.text.localizedCaseInsensitiveContains("Transcriber's Note")) | ||
| } | ||
|
|
||
| private func indexedFixtureLibrary() async throws -> FetchKitLibrary { | ||
| let library = FetchKitLibrary() | ||
| try await library.addDocuments(GutenbergMiniCorpus.records) | ||
| return library | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import FetchCore | ||
|
|
||
| enum GutenbergMiniCorpus { | ||
| struct Source: Hashable, Sendable { | ||
| let datasetID: String | ||
| let config: String | ||
| let split: String | ||
| let license: String | ||
| let url: String | ||
| } | ||
|
|
||
| static let source = Source( | ||
| datasetID: "zkeown/gutenberg-corpus", | ||
| config: "chapters", | ||
| split: "train", | ||
| license: "Apache-2.0 dataset packaging; source texts marked public domain in the USA", | ||
| url: "https://huggingface.co/datasets/zkeown/gutenberg-corpus" | ||
| ) | ||
|
|
||
| static let records: [FetchDocumentRecord] = [ | ||
| FetchDocumentRecord( | ||
| id: "gutenberg-78430-chapter-1", | ||
| title: "A practical course in botany: Chapter I. The Seed", | ||
| body: """ | ||
| I. The storage of food in seeds. | ||
|
|
||
| Material. In addition to the four food tests described in the course, provide raw starch, grape sugar, the white of a hard-boiled egg, and a fatty substance such as lard or oil. Living material includes grains of corn and wheat, and seeds of some kind of bean. | ||
| """, | ||
| kind: .reference, | ||
| language: "en", | ||
| sourceURI: source.url, | ||
| metadata: [ | ||
| "fixture.dataset": source.datasetID, | ||
| "fixture.config": source.config, | ||
| "fixture.split": source.split, | ||
| "fixture.row": "2", | ||
| "fixture.gutenbergID": "78430", | ||
| ] | ||
| ), | ||
| FetchDocumentRecord( | ||
| id: "gutenberg-78430-chapter-2", | ||
| title: "A practical course in botany: Chapter II. Germination and Growth", | ||
| body: """ | ||
| Processes accompanying germination. | ||
|
|
||
| Material includes corn, peas, beans, or any quickly germinating seed. Before taking up the study of germinating seeds, it is important to learn from what sources the organic substances used by the growing plant are derived. | ||
| """, | ||
| kind: .reference, | ||
| language: "en", | ||
| sourceURI: source.url, | ||
| metadata: [ | ||
| "fixture.dataset": source.datasetID, | ||
| "fixture.config": source.config, | ||
| "fixture.split": source.split, | ||
| "fixture.row": "3", | ||
| "fixture.gutenbergID": "78430", | ||
| ] | ||
| ), | ||
| FetchDocumentRecord( | ||
| id: "gutenberg-78431-book", | ||
| title: "Always Another Dawn: The Story of a Rocket Test Pilot", | ||
| body: """ | ||
| Transcriber's Note: Italicized text is surrounded by underscores. The opening material identifies A. Scott Crossfield with Clay Blair, Jr. and includes publisher front matter before the main narrative begins. | ||
| """, | ||
| kind: .article, | ||
| language: "en", | ||
| sourceURI: source.url, | ||
| metadata: [ | ||
| "fixture.dataset": source.datasetID, | ||
| "fixture.config": "books", | ||
| "fixture.split": "train", | ||
| "fixture.row": "2", | ||
| "fixture.gutenbergID": "78431", | ||
| ] | ||
| ), | ||
| FetchDocumentRecord( | ||
| id: "gutenberg-78432-book", | ||
| title: "The young pioneers of the North-west", | ||
| body: """ | ||
| Transcriber's note: Unusual and inconsistent spelling is as printed. The frontier series opening material introduces a juvenile fiction setting around pioneer children, conduct of life, and frontier life. | ||
| """, | ||
| kind: .article, | ||
| language: "en", | ||
| sourceURI: source.url, | ||
| metadata: [ | ||
| "fixture.dataset": source.datasetID, | ||
| "fixture.config": "books", | ||
| "fixture.split": "train", | ||
| "fixture.row": "3", | ||
| "fixture.gutenbergID": "78432", | ||
| ] | ||
| ), | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding
matchedFieldsas a non-optional stored property on this publicCodabletype causes synthesized decoding to require the key, so JSON produced before this change will now fail withkeyNotFoundwhen decoded. This impacts any caller that persists or cachesFetchSearchResultacross app/package upgrades; a manualinit(from:)should decodematchedFieldswith a fallback (for exampledecodeIfPresent(... ) ?? []) to keep older payloads readable.Useful? React with 👍 / 👎.