-
Notifications
You must be signed in to change notification settings - Fork 67
Fix index test failing on server latest #273
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| @testable import MongoSwift | ||
| import Nimble | ||
| import XCTest | ||
|
|
||
| final class MongoCollection_IndexTests: MongoSwiftTestCase { | ||
| var collName: String = "" | ||
| var coll: MongoCollection<Document>! | ||
| let doc1: Document = ["_id": 1, "cat": "dog"] | ||
| let doc2: Document = ["_id": 2, "cat": "cat"] | ||
|
|
||
| /// Set up the entire suite - run once before all tests | ||
| override class func setUp() { | ||
| super.setUp() | ||
| do { | ||
| _client = try MongoClient() | ||
| } catch { | ||
| print("Setup failed: \(error)") | ||
| } | ||
| } | ||
|
|
||
| /// Set up a single test - run before each testX function | ||
| override func setUp() { | ||
| super.setUp() | ||
| self.continueAfterFailure = false | ||
| self.collName = self.getCollectionName() | ||
|
|
||
| do { | ||
| guard let client = _client else { | ||
| XCTFail("Invalid client") | ||
| return | ||
| } | ||
| coll = try client.db(type(of: self).testDatabase).createCollection(self.collName) | ||
| try coll.insertMany([doc1, doc2]) | ||
| } catch { | ||
| XCTFail("Setup failed: \(error)") | ||
| } | ||
| } | ||
|
|
||
| /// Teardown a single test - run after each testX function | ||
| override func tearDown() { | ||
| super.tearDown() | ||
| do { | ||
| if coll != nil { try coll.drop() } | ||
| } catch { | ||
| XCTFail("Dropping test collection \(type(of: self).testDatabase).\(self.collName) failed: \(error)") | ||
| } | ||
| } | ||
|
|
||
| /// Teardown the entire suite - run after all tests complete | ||
| override class func tearDown() { | ||
| super.tearDown() | ||
| do { | ||
| guard let client = _client else { | ||
| print("Invalid client") | ||
| return | ||
| } | ||
| try client.db(self.testDatabase).drop() | ||
| } catch { | ||
| print("Dropping test database \(self.testDatabase) failed: \(error)") | ||
| } | ||
| } | ||
|
|
||
| func testCreateIndexFromModel() throws { | ||
| let model = IndexModel(keys: ["cat": 1]) | ||
| expect(try self.coll.createIndex(model)).to(equal("cat_1")) | ||
| let indexes = try coll.listIndexes() | ||
| expect(indexes.next()?["name"]).to(bsonEqual("_id_")) | ||
| expect(indexes.next()?["name"]).to(bsonEqual("cat_1")) | ||
| expect(indexes.next()).to(beNil()) | ||
| } | ||
|
|
||
| func testIndexOptions() throws { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the only index test that actually changed. |
||
| let options = IndexOptions( | ||
| background: true, | ||
| name: "testOptions", | ||
| sparse: false, | ||
| storageEngine: ["wiredTiger": ["configString": "access_pattern_hint=random"] as Document], | ||
| unique: true, | ||
| indexVersion: 2, | ||
| defaultLanguage: "english", | ||
| languageOverride: "cat", | ||
| textIndexVersion: 2, | ||
| weights: ["cat": 0.5, "_id": 0.5], | ||
| sphereIndexVersion: 2, | ||
| bits: 32, | ||
| max: 30, | ||
| min: 0, | ||
| bucketSize: 10, | ||
| collation: ["locale": "fr"] | ||
| ) | ||
|
|
||
| let model = IndexModel(keys: ["cat": 1, "_id": -1], options: options) | ||
| expect(try self.coll.createIndex(model)).to(equal("testOptions")) | ||
|
|
||
| let ttlOptions = IndexOptions(expireAfterSeconds: 100, name: "ttl") | ||
| let ttlModel = IndexModel(keys: ["cat": 1], options: ttlOptions) | ||
| expect(try self.coll.createIndex(ttlModel)).to(equal("ttl")) | ||
|
|
||
| var indexes: [IndexOptions] = try self.coll.listIndexes().map { indexDoc in | ||
| var decoded = try BSONDecoder().decode(IndexOptions.self, from: indexDoc) | ||
| // name is not one of the CodingKeys for IndexOptions so manually pull | ||
| // it out of the doc and set it on the options. | ||
| decoded.name = indexDoc.name as? String | ||
| return decoded | ||
| } | ||
|
|
||
| indexes.sort { $0.name! < $1.name! } | ||
| expect(indexes).to(haveCount(3)) | ||
|
|
||
| // _id index | ||
| expect(indexes[0]).to(equal(IndexOptions(name: "_id_", indexVersion: 2))) | ||
|
|
||
| // testOptions index | ||
| var expectedTestOptions = options | ||
| expectedTestOptions.name = "testOptions" | ||
| expect(indexes[1]).to(equal(expectedTestOptions)) | ||
|
|
||
| // ttl index | ||
| var expectedTtlOptions = ttlOptions | ||
| expectedTtlOptions.indexVersion = 2 | ||
| expect(indexes[2]).to(equal(expectedTtlOptions)) | ||
| } | ||
|
|
||
| func testCreateIndexesFromModels() throws { | ||
| let model1 = IndexModel(keys: ["cat": 1]) | ||
| let model2 = IndexModel(keys: ["cat": -1]) | ||
| expect( try self.coll.createIndexes([model1, model2]) ).to(equal(["cat_1", "cat_-1"])) | ||
| let indexes = try coll.listIndexes() | ||
| expect(indexes.next()?["name"]).to(bsonEqual("_id_")) | ||
| expect(indexes.next()?["name"]).to(bsonEqual("cat_1")) | ||
| expect(indexes.next()?["name"]).to(bsonEqual("cat_-1")) | ||
| expect(indexes.next()).to(beNil()) | ||
| } | ||
|
|
||
| func testCreateIndexFromKeys() throws { | ||
| expect(try self.coll.createIndex(["cat": 1])).to(equal("cat_1")) | ||
|
|
||
| let indexOptions = IndexOptions(name: "blah", unique: true) | ||
| let model = IndexModel(keys: ["cat": -1], options: indexOptions) | ||
| expect(try self.coll.createIndex(model)).to(equal("blah")) | ||
|
|
||
| let indexes = try coll.listIndexes() | ||
| expect(indexes.next()?["name"]).to(bsonEqual("_id_")) | ||
| expect(indexes.next()?["name"]).to(bsonEqual("cat_1")) | ||
|
|
||
| let thirdIndex = indexes.next() | ||
| expect(thirdIndex?["name"]).to(bsonEqual("blah")) | ||
| expect(thirdIndex?["unique"]).to(bsonEqual(true)) | ||
|
|
||
| expect(indexes.next()).to(beNil()) | ||
| } | ||
|
|
||
| func testDropIndexByName() throws { | ||
| let model = IndexModel(keys: ["cat": 1]) | ||
| expect(try self.coll.createIndex(model)).to(equal("cat_1")) | ||
| expect(try self.coll.dropIndex("cat_1")).toNot(throwError()) | ||
|
|
||
| // now there should only be _id_ left | ||
| let indexes = try coll.listIndexes() | ||
| expect(indexes.next()?["name"]).to(bsonEqual("_id_")) | ||
| expect(indexes.next()).to(beNil()) | ||
| } | ||
|
|
||
| func testDropIndexByModel() throws { | ||
| let model = IndexModel(keys: ["cat": 1]) | ||
| expect(try self.coll.createIndex(model)).to(equal("cat_1")) | ||
|
|
||
| let res = try self.coll.dropIndex(model) | ||
| expect((res["ok"] as? BSONNumber)?.doubleValue).to(bsonEqual(1.0)) | ||
|
|
||
| // now there should only be _id_ left | ||
| let indexes = try coll.listIndexes() | ||
| expect(indexes).toNot(beNil()) | ||
| expect(indexes.next()?["name"]).to(bsonEqual("_id_")) | ||
| expect(indexes.next()).to(beNil()) | ||
| } | ||
|
|
||
| func testDropIndexByKeys() throws { | ||
| let model = IndexModel(keys: ["cat": 1]) | ||
| expect(try self.coll.createIndex(model)).to(equal("cat_1")) | ||
|
|
||
| let res = try self.coll.dropIndex(["cat": 1]) | ||
| expect((res["ok"] as? BSONNumber)?.doubleValue).to(bsonEqual(1.0)) | ||
|
|
||
| // now there should only be _id_ left | ||
| let indexes = try coll.listIndexes() | ||
| expect(indexes).toNot(beNil()) | ||
| expect(indexes.next()?["name"]).to(bsonEqual("_id_")) | ||
| expect(indexes.next()).to(beNil()) | ||
| } | ||
|
|
||
| func testDropAllIndexes() throws { | ||
| let model = IndexModel(keys: ["cat": 1]) | ||
| expect(try self.coll.createIndex(model)).to(equal("cat_1")) | ||
|
|
||
| let res = try self.coll.dropIndexes() | ||
| expect((res["ok"] as? BSONNumber)?.doubleValue).to(bsonEqual(1.0)) | ||
|
|
||
| // now there should only be _id_ left | ||
| let indexes = try coll.listIndexes() | ||
| expect(indexes.next()?["name"]).to(bsonEqual("_id_")) | ||
| expect(indexes.next()).to(beNil()) | ||
| } | ||
|
|
||
| func testListIndexes() throws { | ||
| let indexes = try coll.listIndexes() | ||
| // New collection, so expect just the _id_ index to exist. | ||
| expect(indexes.next()?["name"]).to(bsonEqual("_id_")) | ||
| expect(indexes.next()).to(beNil()) | ||
| } | ||
| } | ||
|
|
||
| extension IndexOptions: Equatable { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is new too. |
||
| public static func == (lhs: IndexOptions, rhs: IndexOptions) -> Bool { | ||
| return lhs.background == rhs.background && | ||
| lhs.expireAfterSeconds == rhs.expireAfterSeconds && | ||
| lhs.name == rhs.name && | ||
| lhs.sparse == rhs.sparse && | ||
| lhs.storageEngine == rhs.storageEngine && | ||
| lhs.unique == rhs.unique && | ||
| lhs.indexVersion == rhs.indexVersion && | ||
| lhs.defaultLanguage == rhs.defaultLanguage && | ||
| lhs.languageOverride == rhs.languageOverride && | ||
| lhs.textIndexVersion == rhs.textIndexVersion && | ||
| lhs.weights == rhs.weights && | ||
| lhs.sphereIndexVersion == rhs.sphereIndexVersion && | ||
| lhs.bits == rhs.bits && | ||
| lhs.max == rhs.max && | ||
| lhs.min == rhs.min && | ||
| lhs.bucketSize == rhs.bucketSize && | ||
| lhs.partialFilterExpression == rhs.partialFilterExpression && | ||
| lhs.collation?["locale"] as? String == rhs.collation?["locale"] as? String | ||
| // ^ server adds a bunch of extra fields and a version number | ||
| // to collations. rather than deal with those, just verify the | ||
| // locale matches. | ||
| } | ||
| } | ||
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.
this and
indexVersionbecame mutable for the sake of the test. I considered updating all of them but I think that ticket will make it into our next release anyway