Skip to content

Commit

Permalink
group by id if no rawComputedFields are specified
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriomazzeo authored and vzsg committed Oct 26, 2018
1 parent c6582b2 commit 0413a39
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/MongoDriver/MongoDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ extension MongoKitten.Database : Fluent.Driver, Connection {

if query.isDistinct {
let groupId: ExpressionRepresentable = rawComputedProperties.isEmpty
? "null"
? "$\(E.name)._id"
: Document(rawComputedProperties.reduce([:], { result, value in
var mutableResult = result
mutableResult[value] = "$\(E.name).\(value)"
Expand Down
14 changes: 9 additions & 5 deletions Tests/MongoDriverTests/DriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,16 @@ class DriverTests: XCTestCase {
XCTAssertNoThrow(try charlie.save())

let molly = Child(name: "Molly", age: 2, parentId: try alice.assertExists())
let kevin = Child(name: "Rex", age: 1, parentId: try bob.assertExists())
let bill = Child(name: "Sparky", age: 1, parentId: try bob.assertExists())
let kevin = Child(name: "Kevin", age: 1, parentId: try bob.assertExists())
let bill = Child(name: "Bill", age: 1, parentId: try bob.assertExists())
let simon = Child(name: "Simon", age: 1, parentId: try charlie.assertExists())
let kelly = Child(name: "Kelly", age: 1, parentId: try charlie.assertExists())

XCTAssertNoThrow(try molly.save())
XCTAssertNoThrow(try kevin.save())
XCTAssertNoThrow(try bill.save())
XCTAssertNoThrow(try simon.save())
XCTAssertNoThrow(try kelly.save())

let adults = try Adult
.makeQuery()
Expand All @@ -146,9 +150,9 @@ class DriverTests: XCTestCase {
.distinct()
.all()

XCTAssertEqual(adults.count, 1)
XCTAssertEqual(adults.first?.id, try bob.assertExists())
XCTAssertEqual(adults.first?.name, "Bob")
XCTAssertEqual(adults.count, 2)
XCTAssertTrue(adults.contains(where: { $0.id == bob.id }))
XCTAssertTrue(adults.contains(where: { $0.id == charlie.id }))
}

func testOuterJoin() throws {
Expand Down

0 comments on commit 0413a39

Please sign in to comment.