Skip to content

Commit

Permalink
Merge pull request #29 from roanutil/bugfix/aggregate-unified-endpoin…
Browse files Browse the repository at this point in the history
…t-is-hard-coded-to-use-sum

Bugfix/aggregate unified endpoint is hard coded to use sum
  • Loading branch information
roanutil authored Jul 22, 2024
2 parents 742aa43 + dc094d0 commit a472822
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension CoreDataRepository {
return await count(predicate: predicate, entityDesc: entityDesc, as: valueType)
default:
return await Self.send(
function: .sum,
function: function,
context: context,
predicate: predicate,
entityDesc: entityDesc,
Expand Down
107 changes: 107 additions & 0 deletions Tests/CoreDataRepositoryTests/AggregateRepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ final class AggregateRepositoryTests: CoreDataXCTestCase {
}
}

func testCountSuccess_UnifiedEndpoint() async throws {
let result = try await repository().aggregate(
function: .count,
predicate: NSPredicate(value: true),
entityDesc: ManagedMovie.entity(),
attributeDesc: XCTUnwrap(
ManagedMovie.entity().attributesByName.values
.first(where: { $0.name == "boxOffice" })
),
as: Double.self
)
switch result {
case let .success(value):
XCTAssertEqual(value, 5, "Result value (count) should equal number of movies.")
case .failure:
XCTFail("Not expecting failure")
}
}

func testCountSubscription() async throws {
let task = Task {
var resultCount = 0
Expand Down Expand Up @@ -128,6 +147,25 @@ final class AggregateRepositoryTests: CoreDataXCTestCase {
}
}

func testSumSuccess_UnifiedEndpoint() async throws {
let result = try await repository().aggregate(
function: .sum,
predicate: NSPredicate(value: true),
entityDesc: ManagedMovie.entity(),
attributeDesc: XCTUnwrap(
ManagedMovie.entity().attributesByName.values
.first(where: { $0.name == "boxOffice" })
),
as: Decimal.self
)
switch result {
case let .success(value):
XCTAssertEqual(value, 150, "Result value (sum) should equal number of movies.")
case .failure:
XCTFail("Not expecting failure")
}
}

func testSumSubscription() async throws {
let task = Task {
var resultCount = 0
Expand Down Expand Up @@ -219,6 +257,29 @@ final class AggregateRepositoryTests: CoreDataXCTestCase {
}
}

func testAverageSuccess_UnifiedEndpoint() async throws {
let result = try await repository().aggregate(
function: .average,
predicate: NSPredicate(value: true),
entityDesc: ManagedMovie.entity(),
attributeDesc: XCTUnwrap(
ManagedMovie.entity().attributesByName.values
.first(where: { $0.name == "boxOffice" })
),
as: Decimal.self
)
switch result {
case let .success(value):
XCTAssertEqual(
value,
30,
"Result value should equal average of movies box office."
)
case .failure:
XCTFail("Not expecting failure")
}
}

func testAverageSubscription() async throws {
let task = Task {
var resultCount = 0
Expand Down Expand Up @@ -318,6 +379,29 @@ final class AggregateRepositoryTests: CoreDataXCTestCase {
}
}

func testMinSuccess_UnifiedEndpoint() async throws {
let result = try await repository().aggregate(
function: .min,
predicate: NSPredicate(value: true),
entityDesc: ManagedMovie.entity(),
attributeDesc: XCTUnwrap(
ManagedMovie.entity().attributesByName.values
.first(where: { $0.name == "boxOffice" })
),
as: Decimal.self
)
switch result {
case let .success(value):
XCTAssertEqual(
value,
10,
"Result value should equal min of movies box office."
)
case .failure:
XCTFail("Not expecting failure")
}
}

func testMinSubscription() async throws {
let task = Task {
var resultCount = 0
Expand Down Expand Up @@ -409,6 +493,29 @@ final class AggregateRepositoryTests: CoreDataXCTestCase {
}
}

func testMaxSuccess_UnifiedEndpoint() async throws {
let result = try await repository().aggregate(
function: .max,
predicate: NSPredicate(value: true),
entityDesc: ManagedMovie.entity(),
attributeDesc: XCTUnwrap(
ManagedMovie.entity().attributesByName.values
.first(where: { $0.name == "boxOffice" })
),
as: Decimal.self
)
switch result {
case let .success(value):
XCTAssertEqual(
value,
50,
"Result value should equal max of movies box office."
)
case .failure:
XCTFail("Not expecting failure")
}
}

func testMaxSubscription() async throws {
let task = Task {
var resultCount = 0
Expand Down

0 comments on commit a472822

Please sign in to comment.