Repository navigation
Add /collections endpoint #32
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
76 changes: 76 additions & 0 deletions
76
Tests/NZImageApiLambdaTests/FilteredCollectionWeightsTests.swift
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,76 @@ | ||
| // | ||
| // FilteredCollectionWeightsTests.swift | ||
| // | ||
| // Coverage for the `?exclude=` renormalization path (`NZImageApi.filteredCollectionWeights`). | ||
| // The invariant mirrors CollectionWeightsTests but over the FILTERED set: dropping collections | ||
| // must re-derive weights as `count / filtered-total` so they still sum to 1, rather than | ||
| // subtracting-then-leaving-unnormalized (which would reintroduce the weightedRandomPick | ||
| // fallthrough bug the unfiltered path already guards against). | ||
| // | ||
|
|
||
| import OrderedCollections | ||
| import XCTest | ||
| @testable import NZImageApiLambda | ||
|
|
||
| final class FilteredCollectionWeightsTests: XCTestCase { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| func testFilteredWeightsSumToOne() throws { | ||
| let exclude: Set<String> = ["Te Papa Collections Online", "Kura Heritage Collections Online"] | ||
| let weights = try NZImageApi.filteredCollectionWeights(excluding: exclude) | ||
| let total = weights.values.reduce(0, +) | ||
| XCTAssertEqual(total, 1.0, accuracy: 0.000_001) | ||
| } | ||
|
|
||
| func testExcludedCollectionsAreAbsent() throws { | ||
| let exclude: Set<String> = ["Te Papa Collections Online", "Kura Heritage Collections Online"] | ||
| let weights = try NZImageApi.filteredCollectionWeights(excluding: exclude) | ||
| for excluded in exclude { | ||
| XCTAssertNil(weights[excluded], "\(excluded) should not appear in the filtered weights") | ||
| } | ||
| } | ||
|
|
||
| func testFilteredWeightsPreserveRemainingCollections() throws { | ||
| let exclude: Set<String> = ["Te Papa Collections Online"] | ||
| let weights = try NZImageApi.filteredCollectionWeights(excluding: exclude) | ||
| XCTAssertEqual( | ||
| weights.count, | ||
| NZImageApi.collectionImageCounts.count - exclude.count | ||
| ) | ||
| // A large non-excluded collection must still be present with a positive weight, proving | ||
| // the remaining proportions were renormalized rather than dropped. | ||
| XCTAssertNotNil(weights["Kura Heritage Collections Online"]) | ||
| for (name, weight) in weights { | ||
| XCTAssertGreaterThan(weight, 0, "\(name) has a non-positive weight") | ||
| } | ||
| } | ||
|
|
||
| func testRenormalizationRedistributesProportionally() throws { | ||
| // Removing a collection should scale every survivor's weight up by exactly | ||
| // 1 / (1 - removedShare), preserving relative proportions among survivors. | ||
| let removed = "Te Papa Collections Online" | ||
| let unfiltered = NZImageApi.collectionWeights | ||
| let removedShare = unfiltered[removed]! | ||
| let filtered = try NZImageApi.filteredCollectionWeights(excluding: [removed]) | ||
|
|
||
| let scale = 1.0 / (1.0 - removedShare) | ||
| for (name, filteredWeight) in filtered { | ||
| XCTAssertEqual(filteredWeight, unfiltered[name]! * scale, accuracy: 0.000_001) | ||
| } | ||
| } | ||
|
|
||
| func testExcludingEveryCollectionThrows() { | ||
| let all = Set(NZImageApi.collectionImageCounts.keys) | ||
| XCTAssertThrowsError(try NZImageApi.filteredCollectionWeights(excluding: all)) { error in | ||
| XCTAssertTrue(error is NZImageApi.NoEligibleCollectionsError) | ||
| } | ||
| } | ||
|
|
||
| func testEmptyExcludeMatchesUnfilteredWeights() throws { | ||
| // An empty exclude set is a degenerate filter: it must reproduce the canonical | ||
| // unfiltered weights exactly (same keys, same order, same values). | ||
| let filtered = try NZImageApi.filteredCollectionWeights(excluding: []) | ||
| XCTAssertEqual(Array(filtered.keys), Array(NZImageApi.collectionWeights.keys)) | ||
| for (name, weight) in filtered { | ||
| XCTAssertEqual(weight, NZImageApi.collectionWeights[name]!, accuracy: 0.000_001) | ||
| } | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.