Skip to content

Commit

Permalink
Merge pull request #289 from orlandos-nl/jw-upsert-success-check-fix-6.x
Browse files Browse the repository at this point in the history
upsert success check fails for empty db
  • Loading branch information
JaapWijnen authored Aug 8, 2022
2 parents 1cc5d76 + 2a2cad7 commit f2d245e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/Meow/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension MutableModel {
public func save(in database: MeowDatabase) -> EventLoopFuture<MeowOperationResult> {
return database.collection(for: Self.self).upsert(self).map { reply in
return MeowOperationResult(
success: reply.updatedCount == 1,
success: reply.updatedCount == 1 || reply.upserted != nil,
n: reply.updatedCount,
writeErrors: reply.writeErrors
)
Expand Down
24 changes: 19 additions & 5 deletions Tests/MongoKittenTests/CRUDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,25 @@ class CrudTests : XCTestCase {
XCTAssertEqual(result[1].name, "nameIndex")
}

// func testBuildIndexes() async throws {
// mongo.async["test"].buildIndexes {
//
// }
// }
func testBuildIndexes() async throws {
let async = mongo.async[DummyAccount.collectionName]
try await mongo.async[DummyAccount.collectionName].buildIndexes {
UniqueIndex(named: "unique-name", field: "name")
}

let dummyAccount1 = DummyAccount(name: "Dum", password: "test1", age: 1337)
let dummyAccount2 = DummyAccount(name: "Dum", password: "test2", age: 1338)

let result1 = try await async.insertEncoded(dummyAccount1)
XCTAssertEqual(result1.insertCount, 1)

let result2 = try await async.insertEncoded(dummyAccount2)
XCTAssertEqual(result2.insertCount, 0)

let count = try await async.count()

XCTAssertEqual(count, 1)
}

// TODO ON ICE: Foreach future
// TODO ON ICE: Change stream
Expand Down

0 comments on commit f2d245e

Please sign in to comment.