diff --git a/CoreStoreTests/FetchTests.swift b/CoreStoreTests/FetchTests.swift index a479de7..0190c6e 100644 --- a/CoreStoreTests/FetchTests.swift +++ b/CoreStoreTests/FetchTests.swift @@ -978,6 +978,951 @@ final class FetchTests: BaseTestCase { } } + @objc + dynamic func test_ThatTransactions_CanFetchOneFromDefaultConfiguration() { + + let configurations: [String?] = [nil] + self.prepareStack(configurations: configurations) { (stack) in + + self.prepareStubsForStack(stack, configurations: configurations) + + stack.beginSynchronous { (transaction) in + + let object2 = transaction.fetchOne( + From(TestEntity1), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(object2) + XCTAssertEqual(object2?.testString, "nil:TestEntity1:2") + + let object3 = transaction.fetchOne( + From(TestEntity1), + Where("%K < %@", "testNumber", 4), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNotNil(object3) + XCTAssertEqual(object3?.testString, "nil:TestEntity1:3") + + let nilObject = transaction.fetchOne( + From(TestEntity1), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject) + } + stack.beginSynchronous { (transaction) in + + let object2 = transaction.fetchOne( + From(nil), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(object2) + XCTAssertEqual(object2?.testString, "nil:TestEntity1:2") + + let object3 = transaction.fetchOne( + From(nil), + Where("%K < %@", "testNumber", 4), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNotNil(object3) + XCTAssertEqual(object3?.testString, "nil:TestEntity1:3") + + let nilObject = transaction.fetchOne( + From(nil), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject) + } + stack.beginSynchronous { (transaction) in + + let nilObject1 = transaction.fetchOne( + From("Config1"), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject1) + + let nilObject2 = transaction.fetchOne( + From("Config1"), + Where("%K < %@", "testNumber", 4), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNil(nilObject2) + } + } + } + + @objc + dynamic func test_ThatTransactions_CanFetchOneFromSingleConfiguration() { + + let configurations: [String?] = [nil, "Config1", "Config2"] + self.prepareStack(configurations: configurations) { (stack) in + + self.prepareStubsForStack(stack, configurations: configurations) + + stack.beginSynchronous { (transaction) in + + let object2 = transaction.fetchOne( + From(TestEntity1), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(object2) + XCTAssertEqual(object2?.testNumber, 2) // configuration ambiguous + + let object3 = transaction.fetchOne( + From(TestEntity1), + Where("%K < %@", "testNumber", 4), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNotNil(object3) + XCTAssertEqual(object3?.testNumber, 3) // configuration ambiguous + + let nilObject = transaction.fetchOne( + From(TestEntity1), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject) + } + stack.beginSynchronous { (transaction) in + + let object2 = transaction.fetchOne( + From(nil), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(object2) + XCTAssertEqual(object2?.testString, "nil:TestEntity1:2") + + let object3 = transaction.fetchOne( + From(nil), + Where("%K < %@", "testNumber", 4), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNotNil(object3) + XCTAssertEqual(object3?.testString, "nil:TestEntity1:3") + + let nilObject = transaction.fetchOne( + From(nil), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject) + } + stack.beginSynchronous { (transaction) in + + let object2 = transaction.fetchOne( + From("Config1"), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(object2) + XCTAssertEqual(object2?.testString, "Config1:TestEntity1:2") + + let object3 = transaction.fetchOne( + From("Config1"), + Where("%K < %@", "testNumber", 4), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNotNil(object3) + XCTAssertEqual(object3?.testString, "Config1:TestEntity1:3") + + let nilObject = transaction.fetchOne( + From("Config1"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject) + } + stack.beginSynchronous { (transaction) in + + let nilObject1 = transaction.fetchOne( + From("Config2"), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject1) + + let nilObject2 = transaction.fetchOne( + From("Config2"), + Where("%K < %@", "testNumber", 4), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNil(nilObject2) + } + } + } + + @objc + dynamic func test_ThatTransactions_CanFetchOneFromMultipleConfigurations() { + + let configurations: [String?] = [nil, "Config1", "Config2"] + self.prepareStack(configurations: configurations) { (stack) in + + self.prepareStubsForStack(stack, configurations: configurations) + + stack.beginSynchronous { (transaction) in + + let object2 = transaction.fetchOne( + From(nil, "Config1"), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(object2) + XCTAssertEqual(object2?.testNumber, 2) // configuration is ambiguous + + let object3 = transaction.fetchOne( + From(nil, "Config1"), + Where("%K < %@", "testNumber", 4), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNotNil(object3) + XCTAssertEqual(object3?.testNumber, 3) // configuration is ambiguous + + let nilObject = transaction.fetchOne( + From(nil, "Config1"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject) + } + stack.beginSynchronous { (transaction) in + + let object2 = transaction.fetchOne( + From(nil, "Config2"), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(object2) + XCTAssertEqual(object2?.testString, "nil:TestEntity1:2") + + let object3 = transaction.fetchOne( + From(nil, "Config2"), + Where("%K < %@", "testNumber", 4), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNotNil(object3) + XCTAssertEqual(object3?.testString, "nil:TestEntity1:3") + + let nilObject = transaction.fetchOne( + From(nil, "Config2"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject) + } + stack.beginSynchronous { (transaction) in + + let object2 = transaction.fetchOne( + From("Config1", "Config2"), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(object2) + XCTAssertEqual(object2?.testString, "Config1:TestEntity1:2") + + let object3 = transaction.fetchOne( + From("Config1", "Config2"), + Where("%K < %@", "testNumber", 4), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNotNil(object3) + XCTAssertEqual(object3?.testString, "Config1:TestEntity1:3") + + let nilObject = transaction.fetchOne( + From("Config1", "Config2"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject) + } + } + } + + @objc + dynamic func test_ThatTransactions_CanFetchAllFromDefaultConfiguration() { + + let configurations: [String?] = [nil] + self.prepareStack(configurations: configurations) { (stack) in + + self.prepareStubsForStack(stack, configurations: configurations) + + stack.beginSynchronous { (transaction) in + + let objects2to4 = transaction.fetchAll( + From(TestEntity1), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects2to4) + XCTAssertEqual(objects2to4?.count, 3) + XCTAssertEqual( + (objects2to4 ?? []).map { $0.testString ?? "" }, + [ + "nil:TestEntity1:2", + "nil:TestEntity1:3", + "nil:TestEntity1:4" + ] + ) + + let objects4to2 = transaction.fetchAll( + From(TestEntity1), + Where("%K < %@", "testNumber", 5), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects4to2) + XCTAssertEqual(objects4to2?.count, 3) + XCTAssertEqual( + (objects4to2 ?? []).map { $0.testString ?? "" }, + [ + "nil:TestEntity1:4", + "nil:TestEntity1:3", + "nil:TestEntity1:2" + ] + ) + + let emptyObjects = transaction.fetchAll( + From(TestEntity1), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyObjects) + XCTAssertEqual(emptyObjects?.count, 0) + } + stack.beginSynchronous { (transaction) in + + let objects2to4 = transaction.fetchAll( + From(nil), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects2to4) + XCTAssertEqual(objects2to4?.count, 3) + XCTAssertEqual( + (objects2to4 ?? []).map { $0.testString ?? "" }, + [ + "nil:TestEntity1:2", + "nil:TestEntity1:3", + "nil:TestEntity1:4" + ] + ) + + let objects4to2 = transaction.fetchAll( + From(nil), + Where("%K < %@", "testNumber", 5), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects4to2) + XCTAssertEqual(objects4to2?.count, 3) + XCTAssertEqual( + (objects4to2 ?? []).map { $0.testString ?? "" }, + [ + "nil:TestEntity1:4", + "nil:TestEntity1:3", + "nil:TestEntity1:2" + ] + ) + + let emptyObjects = transaction.fetchAll( + From(nil), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyObjects) + XCTAssertEqual(emptyObjects?.count, 0) + } + stack.beginSynchronous { (transaction) in + + let nilObject1 = transaction.fetchAll( + From("Config1"), + Where("%K < %@", "testNumber", 4), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilObject1) + + let nilObject2 = transaction.fetchAll( + From("Config1"), + Where("testNumber", isEqualTo: 0), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNil(nilObject2) + } + } + } + + @objc + dynamic func test_ThatTransactions_CanFetchAllFromSingleConfiguration() { + + let configurations: [String?] = [nil, "Config1", "Config2"] + self.prepareStack(configurations: configurations) { (stack) in + + self.prepareStubsForStack(stack, configurations: configurations) + + stack.beginSynchronous { (transaction) in + + let objects4to5 = transaction.fetchAll( + From(TestEntity1), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects4to5) + XCTAssertEqual(objects4to5?.count, 3) + XCTAssertEqual( + Set((objects4to5 ?? []).map { $0.testNumber!.integerValue }), + [4, 5] as Set + ) // configuration is ambiguous + + let objects2to1 = transaction.fetchAll( + From(TestEntity1), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects2to1) + XCTAssertEqual(objects2to1?.count, 3) + XCTAssertEqual( + Set((objects2to1 ?? []).map { $0.testNumber!.integerValue }), + [1, 2] as Set + ) // configuration is ambiguous + + let emptyObjects = transaction.fetchAll( + From(TestEntity1), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyObjects) + XCTAssertEqual(emptyObjects?.count, 0) + } + stack.beginSynchronous { (transaction) in + + let objects4to5 = transaction.fetchAll( + From(nil), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects4to5) + XCTAssertEqual(objects4to5?.count, 2) + XCTAssertEqual( + (objects4to5 ?? []).map { $0.testString ?? "" }, + [ + "nil:TestEntity1:4", + "nil:TestEntity1:5" + ] + ) + + let objects2to1 = transaction.fetchAll( + From(nil), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects2to1) + XCTAssertEqual(objects2to1?.count, 2) + XCTAssertEqual( + (objects2to1 ?? []).map { $0.testString ?? "" }, + [ + "nil:TestEntity1:2", + "nil:TestEntity1:1" + ] + ) + + let emptyObjects = transaction.fetchAll( + From(nil), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyObjects) + XCTAssertEqual(emptyObjects?.count, 0) + } + stack.beginSynchronous { (transaction) in + + let objects4to5 = transaction.fetchAll( + From("Config1"), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects4to5) + XCTAssertEqual(objects4to5?.count, 2) + XCTAssertEqual( + (objects4to5 ?? []).map { $0.testString ?? "" }, + [ + "Config1:TestEntity1:4", + "Config1:TestEntity1:5" + ] + ) + + let objects2to1 = transaction.fetchAll( + From("Config1"), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects2to1) + XCTAssertEqual(objects2to1?.count, 2) + XCTAssertEqual( + (objects2to1 ?? []).map { $0.testString ?? "" }, + [ + "Config1:TestEntity1:2", + "Config1:TestEntity1:1" + ] + ) + + let emptyObjects = transaction.fetchAll( + From("Config1"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyObjects) + XCTAssertEqual(emptyObjects?.count, 0) + } + stack.beginSynchronous { (transaction) in + + let object1 = transaction.fetchOne( + From("Config2"), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(object1) + + let object5 = transaction.fetchOne( + From("Config2"), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNil(object5) + } + } + } + + @objc + dynamic func test_ThatTransactions_CanFetchAllFromMultipleConfigurations() { + + let configurations: [String?] = [nil, "Config1", "Config2"] + self.prepareStack(configurations: configurations) { (stack) in + + self.prepareStubsForStack(stack, configurations: configurations) + + stack.beginSynchronous { (transaction) in + + let objects4to5 = transaction.fetchAll( + From(nil, "Config1"), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects4to5) + XCTAssertEqual(objects4to5?.count, 3) + XCTAssertEqual( + Set((objects4to5 ?? []).map { $0.testNumber!.integerValue }), + [4, 5] as Set + ) // configuration is ambiguous + + let objects2to1 = transaction.fetchAll( + From(nil, "Config1"), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects2to1) + XCTAssertEqual(objects2to1?.count, 3) + XCTAssertEqual( + Set((objects2to1 ?? []).map { $0.testNumber!.integerValue }), + [1, 2] as Set + ) // configuration is ambiguous + + let emptyObjects = transaction.fetchAll( + From(nil, "Config1"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyObjects) + XCTAssertEqual(emptyObjects?.count, 0) + } + stack.beginSynchronous { (transaction) in + + let objects4to5 = transaction.fetchAll( + From(nil, "Config2"), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects4to5) + XCTAssertEqual(objects4to5?.count, 2) + XCTAssertEqual( + (objects4to5 ?? []).map { $0.testString ?? "" }, + [ + "nil:TestEntity1:4", + "nil:TestEntity1:5" + ] + ) + + let objects2to1 = transaction.fetchAll( + From(nil, "Config2"), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects2to1) + XCTAssertEqual(objects2to1?.count, 2) + XCTAssertEqual( + (objects2to1 ?? []).map { $0.testString ?? "" }, + [ + "nil:TestEntity1:2", + "nil:TestEntity1:1" + ] + ) + + let emptyObjects = transaction.fetchAll( + From(nil, "Config2"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyObjects) + XCTAssertEqual(emptyObjects?.count, 0) + } + stack.beginSynchronous { (transaction) in + + let objects4to5 = transaction.fetchAll( + From("Config1", "Config2"), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects4to5) + XCTAssertEqual(objects4to5?.count, 2) + XCTAssertEqual( + (objects4to5 ?? []).map { $0.testString ?? "" }, + [ + "Config1:TestEntity1:4", + "Config1:TestEntity1:5" + ] + ) + + let objects2to1 = transaction.fetchAll( + From("Config1", "Config2"), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(objects2to1) + XCTAssertEqual(objects2to1?.count, 2) + XCTAssertEqual( + (objects2to1 ?? []).map { $0.testString ?? "" }, + [ + "Config1:TestEntity1:2", + "Config1:TestEntity1:1" + ] + ) + + let emptyObjects = transaction.fetchAll( + From("Config1", "Config2"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyObjects) + XCTAssertEqual(emptyObjects?.count, 0) + } + } + } + + @objc + dynamic func test_ThatTransactions_CanFetchCountFromDefaultConfiguration() { + + let configurations: [String?] = [nil] + self.prepareStack(configurations: configurations) { (stack) in + + self.prepareStubsForStack(stack, configurations: configurations) + + stack.beginSynchronous { (transaction) in + + let count2to4 = transaction.fetchCount( + From(TestEntity1), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count2to4) + XCTAssertEqual(count2to4, 3) + + let count4to2 = transaction.fetchCount( + From(TestEntity1), + Where("%K < %@", "testNumber", 5), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count4to2) + XCTAssertEqual(count4to2, 3) + + let emptyCount = transaction.fetchCount( + From(TestEntity1), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyCount) + XCTAssertEqual(emptyCount, 0) + } + stack.beginSynchronous { (transaction) in + + let count2to4 = transaction.fetchCount( + From(nil), + Where("%K > %@", "testNumber", 1), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count2to4) + XCTAssertEqual(count2to4, 3) + + let count4to2 = transaction.fetchCount( + From(nil), + Where("%K < %@", "testNumber", 5), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count4to2) + XCTAssertEqual(count4to2, 3) + + let emptyCount = transaction.fetchCount( + From(nil), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyCount) + XCTAssertEqual(emptyCount, 0) + } + stack.beginSynchronous { (transaction) in + + let nilCount1 = transaction.fetchCount( + From("Config1"), + Where("%K < %@", "testNumber", 4), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilCount1) + + let nilCount2 = transaction.fetchCount( + From("Config1"), + Where("testNumber", isEqualTo: 0), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNil(nilCount2) + } + } + } + + @objc + dynamic func test_ThatTransactions_CanFetchCountFromSingleConfiguration() { + + let configurations: [String?] = [nil, "Config1", "Config2"] + self.prepareStack(configurations: configurations) { (stack) in + + self.prepareStubsForStack(stack, configurations: configurations) + + stack.beginSynchronous { (transaction) in + + let count4to5 = transaction.fetchCount( + From(TestEntity1), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count4to5) + XCTAssertEqual(count4to5, 3) + + let count2to1 = transaction.fetchCount( + From(TestEntity1), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count2to1) + XCTAssertEqual(count2to1, 3) + + let emptyCount = transaction.fetchCount( + From(TestEntity1), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyCount) + XCTAssertEqual(emptyCount, 0) + } + stack.beginSynchronous { (transaction) in + + let count4to5 = transaction.fetchCount( + From(nil), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count4to5) + XCTAssertEqual(count4to5, 2) + + let count2to1 = transaction.fetchCount( + From(nil), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count2to1) + XCTAssertEqual(count2to1, 2) + + let emptyCount = transaction.fetchCount( + From(nil), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyCount) + XCTAssertEqual(emptyCount, 0) + } + stack.beginSynchronous { (transaction) in + + let count4to5 = transaction.fetchCount( + From("Config1"), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count4to5) + XCTAssertEqual(count4to5, 2) + + let count2to1 = transaction.fetchCount( + From("Config1"), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count2to1) + XCTAssertEqual(count2to1, 2) + + let emptyCount = transaction.fetchCount( + From("Config1"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyCount) + XCTAssertEqual(emptyCount, 0) + } + stack.beginSynchronous { (transaction) in + + let nilCount1 = transaction.fetchCount( + From("Config2"), + Where("%K < %@", "testNumber", 4), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNil(nilCount1) + + let nilCount2 = transaction.fetchCount( + From("Config2"), + Where("testNumber", isEqualTo: 0), + OrderBy(.Descending("testEntityID")) + ) + XCTAssertNil(nilCount2) + } + } + } + + @objc + dynamic func test_ThatTransactions_CanFetchCountFromMultipleConfigurations() { + + let configurations: [String?] = [nil, "Config1", "Config2"] + self.prepareStack(configurations: configurations) { (stack) in + + self.prepareStubsForStack(stack, configurations: configurations) + + stack.beginSynchronous { (transaction) in + + let count4to5 = transaction.fetchCount( + From(nil, "Config1"), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count4to5) + XCTAssertEqual(count4to5, 3) + + let count2to1 = transaction.fetchCount( + From(nil, "Config1"), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count2to1) + XCTAssertEqual(count2to1, 3) + + let emptyCount = transaction.fetchCount( + From(nil, "Config1"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyCount) + XCTAssertEqual(emptyCount, 0) + } + stack.beginSynchronous { (transaction) in + + let count4to5 = transaction.fetchCount( + From(nil, "Config2"), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count4to5) + XCTAssertEqual(count4to5, 2) + + let count2to1 = transaction.fetchCount( + From(nil, "Config2"), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count2to1) + XCTAssertEqual(count2to1, 2) + + let emptyCount = transaction.fetchCount( + From(nil, "Config2"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyCount) + XCTAssertEqual(emptyCount, 0) + } + stack.beginSynchronous { (transaction) in + + let count4to5 = transaction.fetchCount( + From("Config1", "Config2"), + Where("%K > %@", "testNumber", 3), + OrderBy(.Ascending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count4to5) + XCTAssertEqual(count4to5, 2) + + let count2to1 = transaction.fetchCount( + From("Config1", "Config2"), + Where("%K < %@", "testNumber", 3), + OrderBy(.Descending("testEntityID")), + Tweak { $0.fetchLimit = 3 } + ) + XCTAssertNotNil(count2to1) + XCTAssertEqual(count2to1, 2) + + let emptyCount = transaction.fetchCount( + From("Config1", "Config2"), + Where("%K > %@", "testNumber", 5), + OrderBy(.Ascending("testEntityID")) + ) + XCTAssertNotNil(emptyCount) + XCTAssertEqual(emptyCount, 0) + } + } + } + // MARK: Private diff --git a/CoreStoreTests/GroupByTests.swift b/CoreStoreTests/GroupByTests.swift index 7a92afb..5ed3aa2 100644 --- a/CoreStoreTests/GroupByTests.swift +++ b/CoreStoreTests/GroupByTests.swift @@ -69,7 +69,7 @@ final class GroupByTests: BaseTestCase { let groupBy = GroupBy("testString") let request = NSFetchRequest() - From(TestEntity1).applyToFetchRequest(request, context: dataStack.mainContext) + _ = From(TestEntity1).applyToFetchRequest(request, context: dataStack.mainContext) groupBy.applyToFetchRequest(request) XCTAssertNotNil(request.propertiesToGroupBy)