Skip to content

Commit a472822

Browse files
authored
Merge pull request #29 from roanutil/bugfix/aggregate-unified-endpoint-is-hard-coded-to-use-sum
Bugfix/aggregate unified endpoint is hard coded to use sum
2 parents 742aa43 + dc094d0 commit a472822

2 files changed

Lines changed: 108 additions & 1 deletion

File tree

Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension CoreDataRepository {
3434
return await count(predicate: predicate, entityDesc: entityDesc, as: valueType)
3535
default:
3636
return await Self.send(
37-
function: .sum,
37+
function: function,
3838
context: context,
3939
predicate: predicate,
4040
entityDesc: entityDesc,

Tests/CoreDataRepositoryTests/AggregateRepositoryTests.swift

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ final class AggregateRepositoryTests: CoreDataXCTestCase {
5151
}
5252
}
5353

54+
func testCountSuccess_UnifiedEndpoint() async throws {
55+
let result = try await repository().aggregate(
56+
function: .count,
57+
predicate: NSPredicate(value: true),
58+
entityDesc: ManagedMovie.entity(),
59+
attributeDesc: XCTUnwrap(
60+
ManagedMovie.entity().attributesByName.values
61+
.first(where: { $0.name == "boxOffice" })
62+
),
63+
as: Double.self
64+
)
65+
switch result {
66+
case let .success(value):
67+
XCTAssertEqual(value, 5, "Result value (count) should equal number of movies.")
68+
case .failure:
69+
XCTFail("Not expecting failure")
70+
}
71+
}
72+
5473
func testCountSubscription() async throws {
5574
let task = Task {
5675
var resultCount = 0
@@ -128,6 +147,25 @@ final class AggregateRepositoryTests: CoreDataXCTestCase {
128147
}
129148
}
130149

150+
func testSumSuccess_UnifiedEndpoint() async throws {
151+
let result = try await repository().aggregate(
152+
function: .sum,
153+
predicate: NSPredicate(value: true),
154+
entityDesc: ManagedMovie.entity(),
155+
attributeDesc: XCTUnwrap(
156+
ManagedMovie.entity().attributesByName.values
157+
.first(where: { $0.name == "boxOffice" })
158+
),
159+
as: Decimal.self
160+
)
161+
switch result {
162+
case let .success(value):
163+
XCTAssertEqual(value, 150, "Result value (sum) should equal number of movies.")
164+
case .failure:
165+
XCTFail("Not expecting failure")
166+
}
167+
}
168+
131169
func testSumSubscription() async throws {
132170
let task = Task {
133171
var resultCount = 0
@@ -219,6 +257,29 @@ final class AggregateRepositoryTests: CoreDataXCTestCase {
219257
}
220258
}
221259

260+
func testAverageSuccess_UnifiedEndpoint() async throws {
261+
let result = try await repository().aggregate(
262+
function: .average,
263+
predicate: NSPredicate(value: true),
264+
entityDesc: ManagedMovie.entity(),
265+
attributeDesc: XCTUnwrap(
266+
ManagedMovie.entity().attributesByName.values
267+
.first(where: { $0.name == "boxOffice" })
268+
),
269+
as: Decimal.self
270+
)
271+
switch result {
272+
case let .success(value):
273+
XCTAssertEqual(
274+
value,
275+
30,
276+
"Result value should equal average of movies box office."
277+
)
278+
case .failure:
279+
XCTFail("Not expecting failure")
280+
}
281+
}
282+
222283
func testAverageSubscription() async throws {
223284
let task = Task {
224285
var resultCount = 0
@@ -318,6 +379,29 @@ final class AggregateRepositoryTests: CoreDataXCTestCase {
318379
}
319380
}
320381

382+
func testMinSuccess_UnifiedEndpoint() async throws {
383+
let result = try await repository().aggregate(
384+
function: .min,
385+
predicate: NSPredicate(value: true),
386+
entityDesc: ManagedMovie.entity(),
387+
attributeDesc: XCTUnwrap(
388+
ManagedMovie.entity().attributesByName.values
389+
.first(where: { $0.name == "boxOffice" })
390+
),
391+
as: Decimal.self
392+
)
393+
switch result {
394+
case let .success(value):
395+
XCTAssertEqual(
396+
value,
397+
10,
398+
"Result value should equal min of movies box office."
399+
)
400+
case .failure:
401+
XCTFail("Not expecting failure")
402+
}
403+
}
404+
321405
func testMinSubscription() async throws {
322406
let task = Task {
323407
var resultCount = 0
@@ -409,6 +493,29 @@ final class AggregateRepositoryTests: CoreDataXCTestCase {
409493
}
410494
}
411495

496+
func testMaxSuccess_UnifiedEndpoint() async throws {
497+
let result = try await repository().aggregate(
498+
function: .max,
499+
predicate: NSPredicate(value: true),
500+
entityDesc: ManagedMovie.entity(),
501+
attributeDesc: XCTUnwrap(
502+
ManagedMovie.entity().attributesByName.values
503+
.first(where: { $0.name == "boxOffice" })
504+
),
505+
as: Decimal.self
506+
)
507+
switch result {
508+
case let .success(value):
509+
XCTAssertEqual(
510+
value,
511+
50,
512+
"Result value should equal max of movies box office."
513+
)
514+
case .failure:
515+
XCTFail("Not expecting failure")
516+
}
517+
}
518+
412519
func testMaxSubscription() async throws {
413520
let task = Task {
414521
var resultCount = 0

0 commit comments

Comments
 (0)