diff --git a/CoreStore.xcodeproj/project.pbxproj b/CoreStore.xcodeproj/project.pbxproj index 32723ed..edb54bd 100644 --- a/CoreStore.xcodeproj/project.pbxproj +++ b/CoreStore.xcodeproj/project.pbxproj @@ -99,6 +99,24 @@ B51FE5AF1CD4D00300E54258 /* CoreStore+CustomDebugStringConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = B51FE5AA1CD4D00300E54258 /* CoreStore+CustomDebugStringConvertible.swift */; }; B5202CFA1C04688100DED140 /* NSFetchedResultsController+Convenience.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5202CF91C04688100DED140 /* NSFetchedResultsController+Convenience.swift */; }; B5202CFD1C046E8400DED140 /* NSFetchedResultsController+Convenience.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5202CF91C04688100DED140 /* NSFetchedResultsController+Convenience.swift */; }; + B525576C1CFAF18F00E51965 /* IntoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525576B1CFAF18F00E51965 /* IntoTests.swift */; }; + B525576D1CFAF18F00E51965 /* IntoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525576B1CFAF18F00E51965 /* IntoTests.swift */; }; + B525576E1CFAF18F00E51965 /* IntoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525576B1CFAF18F00E51965 /* IntoTests.swift */; }; + B52557701D02561A00E51965 /* SelectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525576F1D02561A00E51965 /* SelectTests.swift */; }; + B52557711D02561A00E51965 /* SelectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525576F1D02561A00E51965 /* SelectTests.swift */; }; + B52557721D02561A00E51965 /* SelectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525576F1D02561A00E51965 /* SelectTests.swift */; }; + B52557741D02791400E51965 /* WhereTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B52557731D02791400E51965 /* WhereTests.swift */; }; + B52557751D02791400E51965 /* WhereTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B52557731D02791400E51965 /* WhereTests.swift */; }; + B52557761D02791400E51965 /* WhereTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B52557731D02791400E51965 /* WhereTests.swift */; }; + B52557781D02826E00E51965 /* OrderByTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B52557771D02826E00E51965 /* OrderByTests.swift */; }; + B52557791D02826E00E51965 /* OrderByTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B52557771D02826E00E51965 /* OrderByTests.swift */; }; + B525577A1D02826E00E51965 /* OrderByTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B52557771D02826E00E51965 /* OrderByTests.swift */; }; + B525577C1D0291FE00E51965 /* GroupByTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525577B1D0291FE00E51965 /* GroupByTests.swift */; }; + B525577D1D0291FE00E51965 /* GroupByTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525577B1D0291FE00E51965 /* GroupByTests.swift */; }; + B525577E1D0291FE00E51965 /* GroupByTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525577B1D0291FE00E51965 /* GroupByTests.swift */; }; + B52557801D029D2500E51965 /* TweakTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525577F1D029D2500E51965 /* TweakTests.swift */; }; + B52557811D029D2500E51965 /* TweakTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525577F1D029D2500E51965 /* TweakTests.swift */; }; + B52557821D029D2500E51965 /* TweakTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B525577F1D029D2500E51965 /* TweakTests.swift */; }; B52661401CADD585007B85D9 /* CoreStoreFetchRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B526613F1CADD585007B85D9 /* CoreStoreFetchRequest.swift */; }; B52661411CADD585007B85D9 /* CoreStoreFetchRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B526613F1CADD585007B85D9 /* CoreStoreFetchRequest.swift */; }; B52661421CADD585007B85D9 /* CoreStoreFetchRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B526613F1CADD585007B85D9 /* CoreStoreFetchRequest.swift */; }; @@ -215,9 +233,9 @@ B5489F4C1CF5F743008B4978 /* BaseTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5489F4B1CF5F743008B4978 /* BaseTestCase.swift */; }; B5489F4D1CF5F743008B4978 /* BaseTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5489F4B1CF5F743008B4978 /* BaseTestCase.swift */; }; B5489F4E1CF5F743008B4978 /* BaseTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5489F4B1CF5F743008B4978 /* BaseTestCase.swift */; }; - B5489F501CF603D5008B4978 /* ClauseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5489F4F1CF603D5008B4978 /* ClauseTests.swift */; }; - B5489F511CF603D5008B4978 /* ClauseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5489F4F1CF603D5008B4978 /* ClauseTests.swift */; }; - B5489F521CF603D5008B4978 /* ClauseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5489F4F1CF603D5008B4978 /* ClauseTests.swift */; }; + B5489F501CF603D5008B4978 /* FromTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5489F4F1CF603D5008B4978 /* FromTests.swift */; }; + B5489F511CF603D5008B4978 /* FromTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5489F4F1CF603D5008B4978 /* FromTests.swift */; }; + B5489F521CF603D5008B4978 /* FromTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5489F4F1CF603D5008B4978 /* FromTests.swift */; }; B54A6A551BA15F2A007870FD /* FetchedResultsControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B54A6A541BA15F2A007870FD /* FetchedResultsControllerDelegate.swift */; }; B5519A401CA1B17B002BEF78 /* ErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5519A3F1CA1B17B002BEF78 /* ErrorTests.swift */; }; B5519A411CA1B17B002BEF78 /* ErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5519A3F1CA1B17B002BEF78 /* ErrorTests.swift */; }; @@ -646,6 +664,12 @@ B51BE0691B47FC4B0069F532 /* NSManagedObjectModel+Setup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectModel+Setup.swift"; sourceTree = ""; }; B51FE5AA1CD4D00300E54258 /* CoreStore+CustomDebugStringConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CoreStore+CustomDebugStringConvertible.swift"; sourceTree = ""; }; B5202CF91C04688100DED140 /* NSFetchedResultsController+Convenience.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSFetchedResultsController+Convenience.swift"; sourceTree = ""; }; + B525576B1CFAF18F00E51965 /* IntoTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntoTests.swift; sourceTree = ""; }; + B525576F1D02561A00E51965 /* SelectTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectTests.swift; sourceTree = ""; }; + B52557731D02791400E51965 /* WhereTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WhereTests.swift; sourceTree = ""; }; + B52557771D02826E00E51965 /* OrderByTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrderByTests.swift; sourceTree = ""; }; + B525577B1D0291FE00E51965 /* GroupByTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GroupByTests.swift; sourceTree = ""; }; + B525577F1D029D2500E51965 /* TweakTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TweakTests.swift; sourceTree = ""; }; B526613F1CADD585007B85D9 /* CoreStoreFetchRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreStoreFetchRequest.swift; sourceTree = ""; }; B529C2031CA4A2DB007E7EBD /* CSSaveResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSSaveResult.swift; sourceTree = ""; }; B52DD1741BE1F8CC00949AFE /* CoreStore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CoreStore.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -664,7 +688,7 @@ B5489F3E1CF5EEBC008B4978 /* TestEntity2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestEntity2.swift; sourceTree = ""; }; B5489F451CF5F017008B4978 /* TransactionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionTests.swift; sourceTree = ""; }; B5489F4B1CF5F743008B4978 /* BaseTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestCase.swift; sourceTree = ""; }; - B5489F4F1CF603D5008B4978 /* ClauseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClauseTests.swift; sourceTree = ""; }; + B5489F4F1CF603D5008B4978 /* FromTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FromTests.swift; sourceTree = ""; }; B54A6A541BA15F2A007870FD /* FetchedResultsControllerDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchedResultsControllerDelegate.swift; sourceTree = ""; }; B5519A3F1CA1B17B002BEF78 /* ErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorTests.swift; sourceTree = ""; }; B5519A491CA1F4FB002BEF78 /* CSError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSError.swift; sourceTree = ""; }; @@ -911,18 +935,24 @@ 2F03A53C19C5C6DA005002A5 /* CoreStoreTests */ = { isa = PBXGroup; children = ( - B5519A3F1CA1B17B002BEF78 /* ErrorTests.swift */, - B5DC47C51C93D22900FA3BF3 /* MigrationChainTests.swift */, - B58085741CDF7F00004C2EEB /* SetupTests.swift */, - B5DC47C91C93D9C800FA3BF3 /* StorageInterfaceTests.swift */, - B5489F4F1CF603D5008B4978 /* ClauseTests.swift */, - B5489F451CF5F017008B4978 /* TransactionTests.swift */, - 2F03A53F19C5C6DA005002A5 /* CoreStoreTests.swift */, + B5489F4A1CF5F743008B4978 /* BaseTests */, B5DBE2DD1C9939E100B5CEFA /* BridgingTests.h */, B5DBE2DE1C9939E100B5CEFA /* BridgingTests.m */, - B5489F4A1CF5F743008B4978 /* BaseTests */, - B5489F3C1CF5EEBC008B4978 /* TestEntities */, + 2F03A53F19C5C6DA005002A5 /* CoreStoreTests.swift */, + B5519A3F1CA1B17B002BEF78 /* ErrorTests.swift */, + B5489F4F1CF603D5008B4978 /* FromTests.swift */, + B525577B1D0291FE00E51965 /* GroupByTests.swift */, + B525576B1CFAF18F00E51965 /* IntoTests.swift */, + B5DC47C51C93D22900FA3BF3 /* MigrationChainTests.swift */, + B52557771D02826E00E51965 /* OrderByTests.swift */, + B525576F1D02561A00E51965 /* SelectTests.swift */, + B58085741CDF7F00004C2EEB /* SetupTests.swift */, + B5DC47C91C93D9C800FA3BF3 /* StorageInterfaceTests.swift */, 2F03A53D19C5C6DA005002A5 /* Supporting Files */, + B5489F3C1CF5EEBC008B4978 /* TestEntities */, + B5489F451CF5F017008B4978 /* TransactionTests.swift */, + B525577F1D029D2500E51965 /* TweakTests.swift */, + B52557731D02791400E51965 /* WhereTests.swift */, ); path = CoreStoreTests; sourceTree = ""; @@ -1711,12 +1741,18 @@ buildActionMask = 2147483647; files = ( B5519A401CA1B17B002BEF78 /* ErrorTests.swift in Sources */, + B525577C1D0291FE00E51965 /* GroupByTests.swift in Sources */, + B52557741D02791400E51965 /* WhereTests.swift in Sources */, 2F03A54019C5C6DA005002A5 /* CoreStoreTests.swift in Sources */, B5DC47C61C93D22900FA3BF3 /* MigrationChainTests.swift in Sources */, + B525576C1CFAF18F00E51965 /* IntoTests.swift in Sources */, B5D372841A39CD6900F583D9 /* Model.xcdatamodeld in Sources */, - B5489F501CF603D5008B4978 /* ClauseTests.swift in Sources */, + B5489F501CF603D5008B4978 /* FromTests.swift in Sources */, + B52557781D02826E00E51965 /* OrderByTests.swift in Sources */, B5489F421CF5EEBC008B4978 /* TestEntity2.swift in Sources */, + B52557701D02561A00E51965 /* SelectTests.swift in Sources */, B5489F461CF5F017008B4978 /* TransactionTests.swift in Sources */, + B52557801D029D2500E51965 /* TweakTests.swift in Sources */, B580857A1CDF808C004C2EEB /* SetupTests.swift in Sources */, B5489F4C1CF5F743008B4978 /* BaseTestCase.swift in Sources */, B5DBE2DF1C9939E100B5CEFA /* BridgingTests.m in Sources */, @@ -1846,12 +1882,18 @@ buildActionMask = 2147483647; files = ( B5519A411CA1B17B002BEF78 /* ErrorTests.swift in Sources */, + B525577D1D0291FE00E51965 /* GroupByTests.swift in Sources */, + B52557751D02791400E51965 /* WhereTests.swift in Sources */, B5DC47C71C93D22900FA3BF3 /* MigrationChainTests.swift in Sources */, B5DBE2E01C9939E100B5CEFA /* BridgingTests.m in Sources */, + B525576D1CFAF18F00E51965 /* IntoTests.swift in Sources */, B580857B1CDF808D004C2EEB /* SetupTests.swift in Sources */, - B5489F511CF603D5008B4978 /* ClauseTests.swift in Sources */, + B5489F511CF603D5008B4978 /* FromTests.swift in Sources */, + B52557791D02826E00E51965 /* OrderByTests.swift in Sources */, B5489F431CF5EEBC008B4978 /* TestEntity2.swift in Sources */, + B52557711D02561A00E51965 /* SelectTests.swift in Sources */, B5489F471CF5F017008B4978 /* TransactionTests.swift in Sources */, + B52557811D029D2500E51965 /* TweakTests.swift in Sources */, 82BA18D91C4BBD9700A0916E /* CoreStoreTests.swift in Sources */, B5489F4D1CF5F743008B4978 /* BaseTestCase.swift in Sources */, 82BA18DC1C4BBD9C00A0916E /* Model.xcdatamodeld in Sources */, @@ -1962,12 +2004,18 @@ buildActionMask = 2147483647; files = ( B5519A421CA1B17B002BEF78 /* ErrorTests.swift in Sources */, + B525577E1D0291FE00E51965 /* GroupByTests.swift in Sources */, + B52557761D02791400E51965 /* WhereTests.swift in Sources */, B5DC47C81C93D22900FA3BF3 /* MigrationChainTests.swift in Sources */, B5DBE2E11C9939E100B5CEFA /* BridgingTests.m in Sources */, + B525576E1CFAF18F00E51965 /* IntoTests.swift in Sources */, B580857C1CDF808F004C2EEB /* SetupTests.swift in Sources */, - B5489F521CF603D5008B4978 /* ClauseTests.swift in Sources */, + B5489F521CF603D5008B4978 /* FromTests.swift in Sources */, + B525577A1D02826E00E51965 /* OrderByTests.swift in Sources */, B5489F441CF5EEBC008B4978 /* TestEntity2.swift in Sources */, + B52557721D02561A00E51965 /* SelectTests.swift in Sources */, B5489F481CF5F017008B4978 /* TransactionTests.swift in Sources */, + B52557821D029D2500E51965 /* TweakTests.swift in Sources */, B52DD1CC1BE1F94D00949AFE /* CoreStoreTests.swift in Sources */, B5489F4E1CF5F743008B4978 /* BaseTestCase.swift in Sources */, B5598BCC1BE2093D0092EFCE /* Model.xcdatamodeld in Sources */, diff --git a/CoreStoreTests/BaseTests/BaseTestCase.swift b/CoreStoreTests/BaseTests/BaseTestCase.swift index b309415..38f1537 100644 --- a/CoreStoreTests/BaseTests/BaseTestCase.swift +++ b/CoreStoreTests/BaseTests/BaseTestCase.swift @@ -36,7 +36,7 @@ class BaseTestCase: XCTestCase { // MARK: Internal @nonobjc - func prepareStack(configuration: String = "Config1", @noescape _ closure: (dataStack: DataStack) -> Void) { + func prepareStack(configurations configurations: [String?] = [nil], @noescape _ closure: (dataStack: DataStack) -> Void) { let stack = DataStack( modelName: "Model", @@ -44,13 +44,16 @@ class BaseTestCase: XCTestCase { ) do { - try stack.addStorageAndWait( - SQLiteStore( - fileName: "\(self.dynamicType).sqlite", - configuration: configuration, - localStorageOptions: .RecreateStoreOnModelMismatch + try configurations.forEach { + + try stack.addStorageAndWait( + SQLiteStore( + fileName: "\(self.dynamicType)_\($0).sqlite", + configuration: $0, + localStorageOptions: .RecreateStoreOnModelMismatch + ) ) - ) + } } catch let error as NSError { diff --git a/CoreStoreTests/FromTests.swift b/CoreStoreTests/FromTests.swift new file mode 100644 index 0000000..f62e43b --- /dev/null +++ b/CoreStoreTests/FromTests.swift @@ -0,0 +1,389 @@ +// +// FromTests.swift +// CoreStore +// +// Copyright © 2016 John Rommel Estropia +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import XCTest + +@testable +import CoreStore + + +//MARK: - FromTests + +final class FromTests: BaseTestCase { + + @objc + dynamic func test_ThatFromClauses_ConfigureCorrectly() { + + do { + + let from = From() + XCTAssert(from.entityClass === NSManagedObject.self) + XCTAssertNil(from.dumpInfo) + } + do { + + let from = From() + XCTAssert(from.entityClass === TestEntity1.self) + XCTAssertNil(from.dumpInfo) + } + do { + + let from = From("Config1") + XCTAssert(from.entityClass === TestEntity1.self) + + let dumpInfo = from.dumpInfo + XCTAssertEqual(dumpInfo?.key, "configurations") + + let configurations = dumpInfo?.value as! [String?] + XCTAssertEqual(configurations.count, 1) + XCTAssertEqual(configurations[0], "Config1") + } + do { + + let from = From(nil, "Config1") + XCTAssert(from.entityClass === TestEntity1.self) + + let dumpInfo = from.dumpInfo + XCTAssertEqual(dumpInfo?.key, "configurations") + + let configurations = dumpInfo?.value as! [String?] + XCTAssertEqual(configurations.count, 2) + XCTAssertEqual(configurations[0], nil) + XCTAssertEqual(configurations[1], "Config1") + } + } + + @objc + dynamic func test_ThatFromClauses_ApplyToFetchRequestsCorrectlyForDefaultConfigurations() { + + self.prepareStack(configurations: [nil]) { (dataStack) in + + do { + + let from = From() + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertEqual(affectedConfigurations, ["PF_DEFAULT_CONFIGURATION_NAME"]) + } + do { + + let from = From("Config1") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertTrue(affectedConfigurations.isEmpty) + } + } + } + + @objc + dynamic func test_ThatFromClauses_ApplyToFetchRequestsCorrectlyForSingleConfigurations() { + + self.prepareStack(configurations: ["Config1"]) { (dataStack) in + + do { + + let from = From() + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertEqual(affectedConfigurations, ["Config1"]) + } + do { + + let from = From("Config1") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertEqual(affectedConfigurations, ["Config1"]) + } + do { + + let from = From("Config2") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertTrue(affectedConfigurations.isEmpty) + } + do { + + let from = From() + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertTrue(affectedConfigurations.isEmpty) + } + do { + + let from = From("Config1") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertTrue(affectedConfigurations.isEmpty) + } + do { + + let from = From("Config2") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertTrue(affectedConfigurations.isEmpty) + } + } + } + + @objc + dynamic func test_ThatFromClauses_ApplyToFetchRequestsCorrectlyForDefaultAndCustomConfigurations() { + + self.prepareStack(configurations: [nil, "Config1"]) { (dataStack) in + + do { + + let from = From() + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertEqual(Set(affectedConfigurations), ["PF_DEFAULT_CONFIGURATION_NAME", "Config1"] as Set) + } + do { + + let from = From("Config1") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertEqual(affectedConfigurations, ["Config1"]) + } + do { + + let from = From("Config2") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertTrue(affectedConfigurations.isEmpty) + } + do { + + let from = From() + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertEqual(affectedConfigurations, ["PF_DEFAULT_CONFIGURATION_NAME"]) + } + do { + + let from = From("Config1") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertTrue(affectedConfigurations.isEmpty) + } + do { + + let from = From("Config2") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertTrue(affectedConfigurations.isEmpty) + } + } + } + + @objc + dynamic func test_ThatFromClauses_ApplyToFetchRequestsCorrectlyForMultipleConfigurations() { + + self.prepareStack(configurations: ["Config1", "Config2"]) { (dataStack) in + + do { + + let from = From() + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertEqual(affectedConfigurations, ["Config1"]) + } + do { + + let from = From("Config1") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertEqual(affectedConfigurations, ["Config1"]) + } + do { + + let from = From("Config2") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertTrue(affectedConfigurations.isEmpty) + } + do { + + let from = From() + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertEqual(affectedConfigurations, ["Config2"]) + } + do { + + let from = From("Config1") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertTrue(affectedConfigurations.isEmpty) + } + do { + + let from = From("Config2") + + let request = NSFetchRequest() + from.applyToFetchRequest(request, context: dataStack.mainContext) + XCTAssertNotNil(request.entity) + XCTAssertNotNil(request.affectedStores) + + XCTAssert(from.entityClass == NSClassFromString(request.entity!.managedObjectClassName)) + + let affectedConfigurations = request.affectedStores!.map { $0.configurationName } + XCTAssertEqual(affectedConfigurations, ["Config2"]) + } + } + } +} diff --git a/CoreStoreTests/GroupByTests.swift b/CoreStoreTests/GroupByTests.swift new file mode 100644 index 0000000..7a92afb --- /dev/null +++ b/CoreStoreTests/GroupByTests.swift @@ -0,0 +1,81 @@ +// +// GroupByTests.swift +// CoreStore +// +// Copyright © 2016 John Rommel Estropia +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import XCTest + +@testable +import CoreStore + + +//MARK: - GroupByTests + +final class GroupByTests: BaseTestCase { + + @objc + dynamic func test_ThatGroupByClauses_ConfigureCorrectly() { + + do { + + let groupBy = GroupBy() + XCTAssertEqual(groupBy, GroupBy([] as [String])) + XCTAssertNotEqual(groupBy, GroupBy("key")) + XCTAssertTrue(groupBy.keyPaths.isEmpty) + } + do { + + let groupBy = GroupBy("key1") + XCTAssertEqual(groupBy, GroupBy("key1")) + XCTAssertEqual(groupBy, GroupBy(["key1"])) + XCTAssertNotEqual(groupBy, GroupBy("key2")) + XCTAssertEqual(groupBy.keyPaths, ["key1"]) + } + do { + + let groupBy = GroupBy("key1", "key2") + XCTAssertEqual(groupBy, GroupBy("key1", "key2")) + XCTAssertEqual(groupBy, GroupBy(["key1", "key2"])) + XCTAssertNotEqual(groupBy, GroupBy("key2", "key1")) + XCTAssertEqual(groupBy.keyPaths, ["key1", "key2"]) + } + } + + @objc + dynamic func test_ThatGroupByClauses_ApplyToFetchRequestsCorrectly() { + + self.prepareStack { (dataStack) in + + let groupBy = GroupBy("testString") + + let request = NSFetchRequest() + From(TestEntity1).applyToFetchRequest(request, context: dataStack.mainContext) + groupBy.applyToFetchRequest(request) + + XCTAssertNotNil(request.propertiesToGroupBy) + + let attributes = (request.propertiesToGroupBy ?? []) as! [NSAttributeDescription] + XCTAssertEqual(attributes.map { $0.name }, groupBy.keyPaths) + } + } +} diff --git a/CoreStoreTests/IntoTests.swift b/CoreStoreTests/IntoTests.swift new file mode 100644 index 0000000..ffa7481 --- /dev/null +++ b/CoreStoreTests/IntoTests.swift @@ -0,0 +1,204 @@ +// +// IntoTests.swift +// CoreStore +// +// Copyright © 2016 John Rommel Estropia +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import XCTest + +@testable +import CoreStore + + +//MARK: - IntoTests + +final class IntoTests: XCTestCase { + + @objc + dynamic func test_ThatIntoClauseConstants_AreCorrect() { + + XCTAssertEqual(Into.defaultConfigurationName, "PF_DEFAULT_CONFIGURATION_NAME") + } + + @objc + dynamic func test_ThatIntoClauses_ConfigureCorrectly() { + + do { + + let into = Into() + XCTAssert(into.entityClass === NSManagedObject.self) + XCTAssertNil(into.configuration) + XCTAssertTrue(into.inferStoreIfPossible) + } + do { + + let into = Into() + XCTAssert(into.entityClass === TestEntity1.self) + XCTAssertNil(into.configuration) + XCTAssertTrue(into.inferStoreIfPossible) + } + do { + + let into = Into(TestEntity1) + XCTAssert(into.entityClass === TestEntity1.self) + XCTAssertNil(into.configuration) + XCTAssertTrue(into.inferStoreIfPossible) + } + do { + + let into = Into(TestEntity1.self as AnyClass) + XCTAssert(into.entityClass === TestEntity1.self) + XCTAssertNil(into.configuration) + XCTAssertTrue(into.inferStoreIfPossible) + } + do { + + let into = Into("Config1") + XCTAssert(into.entityClass === TestEntity1.self) + XCTAssertEqual(into.configuration, "Config1") + XCTAssertFalse(into.inferStoreIfPossible) + } + do { + + let into = Into(TestEntity1.self, "Config1") + XCTAssert(into.entityClass === TestEntity1.self) + XCTAssertEqual(into.configuration, "Config1") + XCTAssertFalse(into.inferStoreIfPossible) + } + do { + + let into = Into(TestEntity1.self as AnyClass, "Config1") + XCTAssert(into.entityClass === TestEntity1.self) + XCTAssertEqual(into.configuration, "Config1") + XCTAssertFalse(into.inferStoreIfPossible) + } + } + + @objc + dynamic func test_ThatIntoClauses_AreEquatable() { + + do { + + let into = Into() + XCTAssertEqual(into, Into()) + XCTAssertEqual(into, Into()) + XCTAssertEqual(into, Into(NSManagedObject.self as AnyClass)) + XCTAssertFalse(into == Into()) + XCTAssertNotEqual(into, Into("Config1")) + } + do { + + let into = Into() + XCTAssertEqual(into, Into(TestEntity1)) + XCTAssertEqual(into, Into(TestEntity1.self as AnyClass)) + XCTAssertFalse(into == Into()) + XCTAssertNotEqual(into, Into("Config1")) + } + do { + + let into = Into(TestEntity1) + XCTAssertEqual(into, Into()) + XCTAssertEqual(into, Into(TestEntity1.self as AnyClass)) + XCTAssertFalse(into == Into()) + XCTAssertNotEqual(into, Into("Config1")) + } + do { + + let into = Into(TestEntity1.self as AnyClass) + XCTAssert(into == Into()) + XCTAssertEqual(into, Into(TestEntity1)) + XCTAssertFalse(into == Into()) + XCTAssertFalse(into == Into("Config1")) + } + do { + + let into = Into("Config1") + XCTAssertEqual(into, Into(TestEntity1.self, "Config1")) + XCTAssertEqual(into, Into(TestEntity1.self as AnyClass, "Config1")) + XCTAssertFalse(into == Into("Config1")) + XCTAssertNotEqual(into, Into("Config2")) + } + do { + + let into = Into(TestEntity1.self, "Config1") + XCTAssertEqual(into, Into(TestEntity1.self, "Config1")) + XCTAssertEqual(into, Into("Config1")) + XCTAssertFalse(into == Into("Config1")) + XCTAssertNotEqual(into, Into("Config2")) + } + do { + + let into = Into(TestEntity1.self as AnyClass, "Config1") + XCTAssert(into == Into("Config1")) + XCTAssertEqual(into, Into(TestEntity1.self, "Config1")) + XCTAssertFalse(into == Into("Config1")) + XCTAssertFalse(into == Into("Config2")) + } + } + + @objc + dynamic func test_ThatIntoClauses_BridgeCorrectly() { + + do { + + let into = Into() + let objcInto = into.bridgeToObjectiveC + XCTAssertEqual(into, objcInto.bridgeToSwift) + } + do { + + let into = Into() + let objcInto = into.bridgeToObjectiveC + XCTAssertTrue(into == objcInto.bridgeToSwift) + } + do { + + let into = Into(TestEntity1.self as AnyClass) + let objcInto = into.bridgeToObjectiveC + XCTAssertEqual(into, objcInto.bridgeToSwift) + } + do { + + let into = Into(TestEntity1.self as AnyClass) + let objcInto = into.bridgeToObjectiveC + XCTAssertEqual(into, objcInto.bridgeToSwift) + } + do { + + let into = Into("Config1") + let objcInto = into.bridgeToObjectiveC + XCTAssertTrue(into == objcInto.bridgeToSwift) + } + do { + + let into = Into(TestEntity1.self, "Config1") + let objcInto = into.bridgeToObjectiveC + XCTAssertTrue(into == objcInto.bridgeToSwift) + } + do { + + let into = Into(TestEntity1.self as AnyClass, "Config1") + let objcInto = into.bridgeToObjectiveC + XCTAssertTrue(into == objcInto.bridgeToSwift) + } + } +} diff --git a/CoreStoreTests/OrderByTests.swift b/CoreStoreTests/OrderByTests.swift new file mode 100644 index 0000000..483f508 --- /dev/null +++ b/CoreStoreTests/OrderByTests.swift @@ -0,0 +1,187 @@ +// +// OrderByTests.swift +// CoreStore +// +// Copyright © 2016 John Rommel Estropia +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import XCTest + +@testable +import CoreStore + + +//MARK: - OrderByTests + +final class OrderByTests: XCTestCase { + + @objc + dynamic func test_ThatOrderByClauses_ConfigureCorrectly() { + + do { + + let orderBy = OrderBy() + XCTAssertEqual(orderBy, OrderBy([] as [NSSortDescriptor])) + XCTAssertNotEqual(orderBy, OrderBy(NSSortDescriptor(key: "key", ascending: false))) + XCTAssertTrue(orderBy.sortDescriptors.isEmpty) + } + do { + + let sortDescriptor = NSSortDescriptor(key: "key1", ascending: true) + let orderBy = OrderBy(sortDescriptor) + XCTAssertEqual(orderBy, OrderBy(sortDescriptor)) + XCTAssertEqual(orderBy, OrderBy(.Ascending("key1"))) + XCTAssertNotEqual(orderBy, OrderBy(.Ascending("key2"))) + XCTAssertNotEqual(orderBy, OrderBy(.Descending("key1"))) + XCTAssertNotEqual(orderBy, OrderBy(NSSortDescriptor(key: "key1", ascending: false))) + XCTAssertEqual(orderBy, OrderBy([sortDescriptor])) + XCTAssertEqual(orderBy.sortDescriptors, [sortDescriptor]) + } + do { + + let sortDescriptors = [ + NSSortDescriptor(key: "key1", ascending: true), + NSSortDescriptor(key: "key2", ascending: false) + ] + let orderBy = OrderBy(sortDescriptors) + XCTAssertEqual(orderBy, OrderBy(sortDescriptors)) + XCTAssertEqual(orderBy, OrderBy(.Ascending("key1"), .Descending("key2"))) + XCTAssertNotEqual( + orderBy, + OrderBy( + [ + NSSortDescriptor(key: "key1", ascending: false), + NSSortDescriptor(key: "key2", ascending: false) + ] + ) + ) + XCTAssertNotEqual(orderBy, OrderBy(.Ascending("key1"), .Ascending("key2"))) + XCTAssertNotEqual(orderBy, OrderBy(.Ascending("key1"), .Descending("key3"))) + XCTAssertEqual(orderBy.sortDescriptors, sortDescriptors) + } + do { + + let orderBy = OrderBy(.Ascending("key1")) + let sortDescriptor = NSSortDescriptor(key: "key1", ascending: true) + XCTAssertEqual(orderBy, OrderBy(sortDescriptor)) + XCTAssertEqual(orderBy, OrderBy(.Ascending("key1"))) + XCTAssertNotEqual(orderBy, OrderBy(.Descending("key1"))) + XCTAssertNotEqual(orderBy, OrderBy(.Ascending("key2"))) + XCTAssertEqual(orderBy, OrderBy([sortDescriptor])) + XCTAssertEqual(orderBy.sortDescriptors, [sortDescriptor]) + } + do { + + let orderBy = OrderBy(.Ascending("key1"), .Descending("key2")) + let sortDescriptors = [ + NSSortDescriptor(key: "key1", ascending: true), + NSSortDescriptor(key: "key2", ascending: false) + ] + XCTAssertEqual(orderBy, OrderBy(sortDescriptors)) + XCTAssertEqual(orderBy, OrderBy(.Ascending("key1"), .Descending("key2"))) + XCTAssertNotEqual( + orderBy, + OrderBy( + [ + NSSortDescriptor(key: "key1", ascending: false), + NSSortDescriptor(key: "key2", ascending: false) + ] + ) + ) + XCTAssertNotEqual(orderBy, OrderBy(.Ascending("key1"), .Ascending("key2"))) + XCTAssertNotEqual(orderBy, OrderBy(.Ascending("key1"), .Descending("key3"))) + XCTAssertEqual(orderBy.sortDescriptors, sortDescriptors) + } + do { + + let sortKeys: [SortKey] = [.Ascending("key1"), .Descending("key2")] + let orderBy = OrderBy(sortKeys) + let sortDescriptors = [ + NSSortDescriptor(key: "key1", ascending: true), + NSSortDescriptor(key: "key2", ascending: false) + ] + XCTAssertEqual(orderBy, OrderBy(sortDescriptors)) + XCTAssertEqual(orderBy, OrderBy(.Ascending("key1"), .Descending("key2"))) + XCTAssertNotEqual( + orderBy, + OrderBy( + [ + NSSortDescriptor(key: "key1", ascending: false), + NSSortDescriptor(key: "key2", ascending: false) + ] + ) + ) + XCTAssertNotEqual(orderBy, OrderBy(.Ascending("key1"), .Ascending("key2"))) + XCTAssertNotEqual(orderBy, OrderBy(.Ascending("key1"), .Descending("key3"))) + XCTAssertEqual(orderBy.sortDescriptors, sortDescriptors) + } + } + + @objc + dynamic func test_ThatOrderByClauseOperations_ComputeCorrectly() { + + let orderBy1 = OrderBy(.Ascending("key1")) + let orderBy2 = OrderBy(.Descending("key2")) + let orderBy3 = OrderBy(.Ascending("key3")) + + do { + + let plusOrderBy = orderBy1 + orderBy2 + orderBy3 + XCTAssertEqual(plusOrderBy, OrderBy(.Ascending("key1"), .Descending("key2"), .Ascending("key3"))) + XCTAssertEqual(plusOrderBy, OrderBy(.Ascending("key1")) + OrderBy(.Descending("key2"), .Ascending("key3"))) + XCTAssertNotEqual(plusOrderBy, orderBy1 + orderBy3 + orderBy2) + XCTAssertNotEqual(plusOrderBy, orderBy2 + orderBy1 + orderBy3) + XCTAssertNotEqual(plusOrderBy, orderBy2 + orderBy3 + orderBy1) + XCTAssertNotEqual(plusOrderBy, orderBy3 + orderBy1 + orderBy2) + XCTAssertNotEqual(plusOrderBy, orderBy3 + orderBy2 + orderBy1) + XCTAssertEqual(plusOrderBy.sortDescriptors, orderBy1.sortDescriptors + orderBy2.sortDescriptors + orderBy3.sortDescriptors) + } + do { + + var plusOrderBy = orderBy1 + plusOrderBy += orderBy2 + XCTAssertEqual(plusOrderBy, OrderBy(.Ascending("key1"), .Descending("key2"))) + XCTAssertEqual(plusOrderBy, OrderBy(.Ascending("key1")) + OrderBy(.Descending("key2"))) + XCTAssertNotEqual(plusOrderBy, orderBy2 + orderBy1) + XCTAssertEqual(plusOrderBy.sortDescriptors, orderBy1.sortDescriptors + orderBy2.sortDescriptors) + + plusOrderBy += orderBy3 + XCTAssertEqual(plusOrderBy, OrderBy(.Ascending("key1"), .Descending("key2"), .Ascending("key3"))) + XCTAssertEqual(plusOrderBy, OrderBy(.Ascending("key1"), .Descending("key2")) + OrderBy(.Ascending("key3"))) + XCTAssertNotEqual(plusOrderBy, orderBy1 + orderBy3 + orderBy2) + XCTAssertNotEqual(plusOrderBy, orderBy2 + orderBy1 + orderBy3) + XCTAssertNotEqual(plusOrderBy, orderBy2 + orderBy3 + orderBy1) + XCTAssertNotEqual(plusOrderBy, orderBy3 + orderBy1 + orderBy2) + XCTAssertNotEqual(plusOrderBy, orderBy3 + orderBy2 + orderBy1) + XCTAssertEqual(plusOrderBy.sortDescriptors, orderBy1.sortDescriptors + orderBy2.sortDescriptors + orderBy3.sortDescriptors) + } + } + + @objc + dynamic func test_ThatOrderByClauses_ApplyToFetchRequestsCorrectly() { + + let orderBy = OrderBy(.Ascending("key")) + let request = NSFetchRequest() + orderBy.applyToFetchRequest(request) + XCTAssertNotNil(request.sortDescriptors) + XCTAssertEqual(request.sortDescriptors ?? [], orderBy.sortDescriptors) + } +} diff --git a/CoreStoreTests/SelectTests.swift b/CoreStoreTests/SelectTests.swift new file mode 100644 index 0000000..210125c --- /dev/null +++ b/CoreStoreTests/SelectTests.swift @@ -0,0 +1,420 @@ +// +// SelectTests.swift +// CoreStore +// +// Copyright © 2016 John Rommel Estropia +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import XCTest + +@testable +import CoreStore + + +//MARK: - SelectTests + +final class SelectTests: XCTestCase { + + @objc + dynamic func test_ThatAttributeSelectTerms_ConfigureCorrectly() { + + do { + + let term: SelectTerm = "attribute" + XCTAssertEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Attribute(let key): + XCTAssertEqual(key, "attribute") + + default: + XCTFail() + } + } + do { + + let term = SelectTerm.Attribute("attribute") + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Attribute(let key): + XCTAssertEqual(key, "attribute") + + default: + XCTFail() + } + } + } + + @objc + dynamic func test_ThatAverageSelectTerms_ConfigureCorrectly() { + + do { + + let term = SelectTerm.Average("attribute") + XCTAssertEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute", As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Aggregate(let function, let keyPath, let alias, let nativeType): + XCTAssertEqual(function, "average:") + XCTAssertEqual(keyPath, "attribute") + XCTAssertEqual(alias, "average(attribute)") + XCTAssertTrue(nativeType == .DecimalAttributeType) + + default: + XCTFail() + } + } + do { + + let term = SelectTerm.Average("attribute", As: "alias") + XCTAssertEqual(term, SelectTerm.Average("attribute", As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute", As: "alias2")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Aggregate(let function, let keyPath, let alias, let nativeType): + XCTAssertEqual(function, "average:") + XCTAssertEqual(keyPath, "attribute") + XCTAssertEqual(alias, "alias") + XCTAssertTrue(nativeType == .DecimalAttributeType) + + default: + XCTFail() + } + } + } + + @objc + dynamic func test_ThatCountSelectTerms_ConfigureCorrectly() { + + do { + + let term = SelectTerm.Count("attribute") + XCTAssertEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute", As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Aggregate(let function, let keyPath, let alias, let nativeType): + XCTAssertEqual(function, "count:") + XCTAssertEqual(keyPath, "attribute") + XCTAssertEqual(alias, "count(attribute)") + XCTAssertTrue(nativeType == .Integer64AttributeType) + + default: + XCTFail() + } + } + do { + + let term = SelectTerm.Count("attribute", As: "alias") + XCTAssertEqual(term, SelectTerm.Count("attribute", As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute", As: "alias2")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Aggregate(let function, let keyPath, let alias, let nativeType): + XCTAssertEqual(function, "count:") + XCTAssertEqual(keyPath, "attribute") + XCTAssertEqual(alias, "alias") + XCTAssertTrue(nativeType == .Integer64AttributeType) + + default: + XCTFail() + } + } + } + + @objc + dynamic func test_ThatMaximumSelectTerms_ConfigureCorrectly() { + + do { + + let term = SelectTerm.Maximum("attribute") + XCTAssertEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute", As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Aggregate(let function, let keyPath, let alias, let nativeType): + XCTAssertEqual(function, "max:") + XCTAssertEqual(keyPath, "attribute") + XCTAssertEqual(alias, "max(attribute)") + XCTAssertTrue(nativeType == .UndefinedAttributeType) + + default: + XCTFail() + } + } + do { + + let term = SelectTerm.Maximum("attribute", As: "alias") + XCTAssertEqual(term, SelectTerm.Maximum("attribute", As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute", As: "alias2")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Aggregate(let function, let keyPath, let alias, let nativeType): + XCTAssertEqual(function, "max:") + XCTAssertEqual(keyPath, "attribute") + XCTAssertEqual(alias, "alias") + XCTAssertTrue(nativeType == .UndefinedAttributeType) + + default: + XCTFail() + } + } + } + + @objc + dynamic func test_ThatMinimumSelectTerms_ConfigureCorrectly() { + + do { + + let term = SelectTerm.Minimum("attribute") + XCTAssertEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute", As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Aggregate(let function, let keyPath, let alias, let nativeType): + XCTAssertEqual(function, "min:") + XCTAssertEqual(keyPath, "attribute") + XCTAssertEqual(alias, "min(attribute)") + XCTAssertTrue(nativeType == .UndefinedAttributeType) + + default: + XCTFail() + } + } + do { + + let term = SelectTerm.Minimum("attribute", As: "alias") + XCTAssertEqual(term, SelectTerm.Minimum("attribute", As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute", As: "alias2")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Aggregate(let function, let keyPath, let alias, let nativeType): + XCTAssertEqual(function, "min:") + XCTAssertEqual(keyPath, "attribute") + XCTAssertEqual(alias, "alias") + XCTAssertTrue(nativeType == .UndefinedAttributeType) + + default: + XCTFail() + } + } + } + + @objc + dynamic func test_ThatSumSelectTerms_ConfigureCorrectly() { + + do { + + let term = SelectTerm.Sum("attribute") + XCTAssertEqual(term, SelectTerm.Sum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute", As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Aggregate(let function, let keyPath, let alias, let nativeType): + XCTAssertEqual(function, "sum:") + XCTAssertEqual(keyPath, "attribute") + XCTAssertEqual(alias, "sum(attribute)") + XCTAssertTrue(nativeType == .DecimalAttributeType) + + default: + XCTFail() + } + } + do { + + let term = SelectTerm.Sum("attribute", As: "alias") + XCTAssertEqual(term, SelectTerm.Sum("attribute", As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute", As: "alias2")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute2")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + switch term { + + case ._Aggregate(let function, let keyPath, let alias, let nativeType): + XCTAssertEqual(function, "sum:") + XCTAssertEqual(keyPath, "attribute") + XCTAssertEqual(alias, "alias") + XCTAssertTrue(nativeType == .DecimalAttributeType) + + default: + XCTFail() + } + } + } + + @objc + dynamic func test_ThatObjectIDSelectTerms_ConfigureCorrectly() { + + do { + + let term = SelectTerm.ObjectID() + XCTAssertEqual(term, SelectTerm.ObjectID()) + XCTAssertNotEqual(term, SelectTerm.ObjectID(As: "alias")) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + switch term { + + case ._Identity(let alias, let nativeType): + XCTAssertEqual(alias, "objectID") + XCTAssertTrue(nativeType == .ObjectIDAttributeType) + + default: + XCTFail() + } + } + do { + + let term = SelectTerm.ObjectID(As: "alias") + XCTAssertEqual(term, SelectTerm.ObjectID(As: "alias")) + XCTAssertNotEqual(term, SelectTerm.ObjectID(As: "alias2")) + XCTAssertNotEqual(term, SelectTerm.ObjectID()) + XCTAssertNotEqual(term, SelectTerm.Attribute("attribute")) + XCTAssertNotEqual(term, SelectTerm.Average("attribute")) + XCTAssertNotEqual(term, SelectTerm.Count("attribute")) + XCTAssertNotEqual(term, SelectTerm.Maximum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Minimum("attribute")) + XCTAssertNotEqual(term, SelectTerm.Sum("attribute")) + switch term { + + case ._Identity(let alias, let nativeType): + XCTAssertEqual(alias, "alias") + XCTAssertTrue(nativeType == .ObjectIDAttributeType) + + default: + XCTFail() + } + } + } + + @objc + dynamic func test_ThatSelectClauses_ConfigureCorrectly() { + + let term1 = SelectTerm.Attribute("attribute1") + let term2 = SelectTerm.Attribute("attribute2") + let term3 = SelectTerm.Attribute("attribute3") + do { + + let select = Select(term1, term2, term3) + XCTAssertEqual(select.selectTerms, [term1, term2, term3]) + XCTAssertNotEqual(select.selectTerms, [term1, term3, term2]) + XCTAssertNotEqual(select.selectTerms, [term2, term1, term3]) + XCTAssertNotEqual(select.selectTerms, [term2, term3, term1]) + XCTAssertNotEqual(select.selectTerms, [term3, term1, term2]) + XCTAssertNotEqual(select.selectTerms, [term3, term2, term1]) + } + do { + + let select = Select([term1, term2, term3]) + XCTAssertEqual(select.selectTerms, [term1, term2, term3]) + XCTAssertNotEqual(select.selectTerms, [term1, term3, term2]) + XCTAssertNotEqual(select.selectTerms, [term2, term1, term3]) + XCTAssertNotEqual(select.selectTerms, [term2, term3, term1]) + XCTAssertNotEqual(select.selectTerms, [term3, term1, term2]) + XCTAssertNotEqual(select.selectTerms, [term3, term2, term1]) + } + } +} diff --git a/CoreStoreTests/ClauseTests.swift b/CoreStoreTests/TweakTests.swift similarity index 65% rename from CoreStoreTests/ClauseTests.swift rename to CoreStoreTests/TweakTests.swift index 6c032da..18416d4 100644 --- a/CoreStoreTests/ClauseTests.swift +++ b/CoreStoreTests/TweakTests.swift @@ -1,5 +1,5 @@ // -// ClauseTests.swift +// TweakTests.swift // CoreStore // // Copyright © 2016 John Rommel Estropia @@ -29,15 +29,25 @@ import XCTest import CoreStore -//MARK: - ClauseTests +//MARK: - TweakTests -final class ClauseTests: XCTestCase { - - // MARK: Into +final class TweakTests: XCTestCase { @objc - dynamic func test_IntoClauses_ConfigureCorrectly() { + dynamic func test_ThatTweakClauses_ApplyToFetchRequestsCorrectly() { - // TODO: + let predicate = NSPredicate(format: "%K == %@", "key", "value") + let tweak = Tweak { + + $0.fetchOffset = 100 + $0.fetchLimit = 200 + $0.predicate = predicate + } + let request = NSFetchRequest() + tweak.applyToFetchRequest(request) + XCTAssertEqual(request.fetchOffset, 100) + XCTAssertEqual(request.fetchLimit, 200) + XCTAssertNotNil(request.predicate) + XCTAssertEqual(request.predicate, predicate) } } diff --git a/CoreStoreTests/WhereTests.swift b/CoreStoreTests/WhereTests.swift new file mode 100644 index 0000000..053e657 --- /dev/null +++ b/CoreStoreTests/WhereTests.swift @@ -0,0 +1,150 @@ +// +// WhereTests.swift +// CoreStore +// +// Copyright © 2016 John Rommel Estropia +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import XCTest + +@testable +import CoreStore + + +//MARK: - WhereTests + +final class WhereTests: XCTestCase { + + @objc + dynamic func test_ThatWhereClauses_ConfigureCorrectly() { + + do { + + let whereClause = Where() + XCTAssertEqual(whereClause, Where(true)) + XCTAssertNotEqual(whereClause, Where(false)) + XCTAssertEqual(whereClause.predicate, NSPredicate(value: true)) + } + do { + + let whereClause = Where(true) + XCTAssertEqual(whereClause, Where()) + XCTAssertNotEqual(whereClause, Where(false)) + XCTAssertEqual(whereClause.predicate, NSPredicate(value: true)) + } + do { + + let predicate = NSPredicate(format: "%K == %@", "key", "value") + let whereClause = Where(predicate) + XCTAssertEqual(whereClause, Where(predicate)) + XCTAssertEqual(whereClause.predicate, predicate) + } + do { + + let whereClause = Where("%K == %@", "key", "value") + let predicate = NSPredicate(format: "%K == %@", "key", "value") + XCTAssertEqual(whereClause, Where(predicate)) + XCTAssertEqual(whereClause.predicate, predicate) + } + do { + + let whereClause = Where("%K == %@", argumentArray: ["key", "value"]) + let predicate = NSPredicate(format: "%K == %@", "key", "value") + XCTAssertEqual(whereClause, Where(predicate)) + XCTAssertEqual(whereClause.predicate, predicate) + } + do { + + let whereClause = Where("key", isEqualTo: "value") + let predicate = NSPredicate(format: "%K == %@", "key", "value") + XCTAssertEqual(whereClause, Where(predicate)) + XCTAssertEqual(whereClause.predicate, predicate) + } + do { + + let whereClause = Where("key", isMemberOf: ["value1", "value2", "value3"]) + let predicate = NSPredicate(format: "%K IN %@", "key", ["value1", "value2", "value3"]) + XCTAssertEqual(whereClause, Where(predicate)) + XCTAssertEqual(whereClause.predicate, predicate) + } + } + + @objc + dynamic func test_ThatWhereClauseOperations_ComputeCorrectly() { + + let whereClause1 = Where("key1", isEqualTo: "value1") + let whereClause2 = Where("key2", isEqualTo: "value2") + let whereClause3 = Where("key3", isEqualTo: "value3") + + do { + + let notWhere = !whereClause1 + let notPredicate = NSCompoundPredicate( + type: .NotPredicateType, + subpredicates: [whereClause1.predicate] + ) + XCTAssertEqual(notWhere.predicate, notPredicate) + XCTAssertEqual(notWhere, !whereClause1) + } + do { + + let andWhere = whereClause1 && whereClause2 && whereClause3 + let andPredicate = NSCompoundPredicate( + type: .AndPredicateType, + subpredicates: [ + NSCompoundPredicate( + type: .AndPredicateType, + subpredicates: [whereClause1.predicate, whereClause2.predicate] + ), + whereClause3.predicate + ] + ) + XCTAssertEqual(andWhere.predicate, andPredicate) + XCTAssertEqual(andWhere, whereClause1 && whereClause2 && whereClause3) + } + do { + + let orWhere = whereClause1 || whereClause2 || whereClause3 + let orPredicate = NSCompoundPredicate( + type: .OrPredicateType, + subpredicates: [ + NSCompoundPredicate( + type: .OrPredicateType, + subpredicates: [whereClause1.predicate, whereClause2.predicate] + ), + whereClause3.predicate + ] + ) + XCTAssertEqual(orWhere.predicate, orPredicate) + XCTAssertEqual(orWhere, whereClause1 || whereClause2 || whereClause3) + } + } + + @objc + dynamic func test_ThatWhereClauses_ApplyToFetchRequestsCorrectly() { + + let whereClause = Where("key", isEqualTo: "value") + let request = NSFetchRequest() + whereClause.applyToFetchRequest(request) + XCTAssertNotNil(request.predicate) + XCTAssertEqual(request.predicate, whereClause.predicate) + } +} diff --git a/Sources/Fetching and Querying/Concrete Clauses/From.swift b/Sources/Fetching and Querying/Concrete Clauses/From.swift index da41746..11aaac4 100644 --- a/Sources/Fetching and Querying/Concrete Clauses/From.swift +++ b/Sources/Fetching and Querying/Concrete Clauses/From.swift @@ -75,6 +75,10 @@ public struct From { */ public init(_ entityClass: AnyClass) { + CoreStore.assert( + entityClass is T.Type, + "Attempted to create generic type \(cs_typeName(From)) with entity class \(cs_typeName(entityClass))" + ) self.init(entityClass: entityClass) } @@ -146,6 +150,10 @@ public struct From { */ public init(_ entityClass: AnyClass, _ configuration: String?, _ otherConfigurations: String?...) { + CoreStore.assert( + entityClass is T.Type, + "Attempted to create generic type \(cs_typeName(From)) with entity class \(cs_typeName(entityClass))" + ) self.init(entityClass: entityClass, configurations: [configuration] + otherConfigurations) } @@ -160,143 +168,13 @@ public struct From { */ public init(_ entityClass: AnyClass, _ configurations: [String?]) { + CoreStore.assert( + entityClass is T.Type, + "Attempted to create generic type \(cs_typeName(From)) with entity class \(cs_typeName(entityClass))" + ) self.init(entityClass: entityClass, configurations: configurations) } - /** - Initializes a `From` clause with the specified store URLs. - - - parameter storeURL: the persistent store URL to associate objects from. - - parameter otherStoreURLs: an optional list of other persistent store URLs to associate objects from (see `storeURL` parameter) - */ - public init(_ storeURL: NSURL, _ otherStoreURLs: NSURL...) { - - self.init(entityClass: T.self, storeURLs: [storeURL] + otherStoreURLs) - } - - /** - Initializes a `From` clause with the specified store URLs. - - - parameter storeURLs: the persistent store URLs to associate objects from. - */ - public init(_ storeURLs: [NSURL]) { - - self.init(entityClass: T.self, storeURLs: storeURLs) - } - - /** - Initializes a `From` clause with the specified store URLs. - - - parameter entity: the associated `NSManagedObject` type - - parameter storeURL: the persistent store URL to associate objects from. - - parameter otherStoreURLs: an optional list of other persistent store URLs to associate objects from (see `storeURL` parameter) - */ - public init(_ entity: T.Type, _ storeURL: NSURL, _ otherStoreURLs: NSURL...) { - - self.init(entityClass: entity, storeURLs: [storeURL] + otherStoreURLs) - } - - /** - Initializes a `From` clause with the specified store URLs. - - - parameter entity: the associated `NSManagedObject` type - - parameter storeURLs: the persistent store URLs to associate objects from. - */ - public init(_ entity: T.Type, _ storeURLs: [NSURL]) { - - self.init(entityClass: entity, storeURLs: storeURLs) - } - - /** - Initializes a `From` clause with the specified store URLs. - - - parameter entity: the associated `NSManagedObject` entity class - - parameter storeURL: the persistent store URL to associate objects from. - - parameter otherStoreURLs: an optional list of other persistent store URLs to associate objects from (see `storeURL` parameter) - */ - public init(_ entityClass: AnyClass, _ storeURL: NSURL, _ otherStoreURLs: NSURL...) { - - self.init(entityClass: entityClass, storeURLs: [storeURL] + otherStoreURLs) - } - - /** - Initializes a `From` clause with the specified store URLs. - - - parameter entity: the associated `NSManagedObject` entity class - - parameter storeURLs: the persistent store URLs to associate objects from. - */ - public init(_ entityClass: AnyClass, _ storeURLs: [NSURL]) { - - self.init(entityClass: entityClass, storeURLs: storeURLs) - } - - /** - Initializes a `From` clause with the specified `NSPersistentStore`s. - - - parameter persistentStore: the `NSPersistentStore` to associate objects from. - - parameter otherPersistentStores: an optional list of other `NSPersistentStore`s to associate objects from (see `persistentStore` parameter) - */ - public init(_ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) { - - self.init(entityClass: T.self, persistentStores: [persistentStore] + otherPersistentStores) - } - - /** - Initializes a `From` clause with the specified `NSPersistentStore`s. - - - parameter persistentStores: the `NSPersistentStore`s to associate objects from. - */ - public init(_ persistentStores: [NSPersistentStore]) { - - self.init(entityClass: T.self, persistentStores: persistentStores) - } - - /** - Initializes a `From` clause with the specified `NSPersistentStore`s. - - - parameter entity: the associated `NSManagedObject` type - - parameter persistentStore: the `NSPersistentStore` to associate objects from. - - parameter otherPersistentStores: an optional list of other `NSPersistentStore`s to associate objects from (see `persistentStore` parameter) - */ - public init(_ entity: T.Type, _ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) { - - self.init(entityClass: entity, persistentStores: [persistentStore] + otherPersistentStores) - } - - /** - Initializes a `From` clause with the specified `NSPersistentStore`s. - - - parameter entity: the associated `NSManagedObject` type - - parameter persistentStores: the `NSPersistentStore`s to associate objects from. - */ - public init(_ entity: T.Type, _ persistentStores: [NSPersistentStore]) { - - self.init(entityClass: entity, persistentStores: persistentStores) - } - - /** - Initializes a `From` clause with the specified `NSPersistentStore`s. - - - parameter entity: the associated `NSManagedObject` entity class - - parameter persistentStore: the `NSPersistentStore` to associate objects from. - - parameter otherPersistentStores: an optional list of other `NSPersistentStore`s to associate objects from (see `persistentStore` parameter) - */ - public init(_ entityClass: AnyClass, _ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) { - - self.init(entityClass: entityClass, persistentStores: [persistentStore] + otherPersistentStores) - } - - /** - Initializes a `From` clause with the specified `NSPersistentStore`s. - - - parameter entity: the associated `NSManagedObject` entity class - - parameter persistentStores: the `NSPersistentStore`s to associate objects from. - */ - public init(_ entityClass: AnyClass, _ persistentStores: [NSPersistentStore]) { - - self.init(entityClass: entityClass, persistentStores: persistentStores) - } - // MARK: Internal @@ -399,4 +277,131 @@ public struct From { self.dumpInfo = dumpInfo self.findPersistentStores = findPersistentStores } + + + // MARK: Deprecated + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ storeURL: NSURL, _ otherStoreURLs: NSURL...) { + + self.init(entityClass: T.self, storeURLs: [storeURL] + otherStoreURLs) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ storeURLs: [NSURL]) { + + self.init(entityClass: T.self, storeURLs: storeURLs) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ entity: T.Type, _ storeURL: NSURL, _ otherStoreURLs: NSURL...) { + + self.init(entityClass: entity, storeURLs: [storeURL] + otherStoreURLs) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ entity: T.Type, _ storeURLs: [NSURL]) { + + self.init(entityClass: entity, storeURLs: storeURLs) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ entityClass: AnyClass, _ storeURL: NSURL, _ otherStoreURLs: NSURL...) { + + CoreStore.assert( + entityClass is T.Type, + "Attempted to create generic type \(cs_typeName(From)) with entity class \(cs_typeName(entityClass))" + ) + self.init(entityClass: entityClass, storeURLs: [storeURL] + otherStoreURLs) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ entityClass: AnyClass, _ storeURLs: [NSURL]) { + + CoreStore.assert( + entityClass is T.Type, + "Attempted to create generic type \(cs_typeName(From)) with entity class \(cs_typeName(entityClass))" + ) + self.init(entityClass: entityClass, storeURLs: storeURLs) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) { + + self.init(entityClass: T.self, persistentStores: [persistentStore] + otherPersistentStores) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ persistentStores: [NSPersistentStore]) { + + self.init(entityClass: T.self, persistentStores: persistentStores) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ entity: T.Type, _ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) { + + self.init(entityClass: entity, persistentStores: [persistentStore] + otherPersistentStores) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ entity: T.Type, _ persistentStores: [NSPersistentStore]) { + + self.init(entityClass: entity, persistentStores: persistentStores) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ entityClass: AnyClass, _ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) { + + CoreStore.assert( + entityClass is T.Type, + "Attempted to create generic type \(cs_typeName(From)) with entity class \(cs_typeName(entityClass))" + ) + self.init(entityClass: entityClass, persistentStores: [persistentStore] + otherPersistentStores) + } + + /** + Deprecated. Use initializers that accept configuration names. + */ + @available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.") + public init(_ entityClass: AnyClass, _ persistentStores: [NSPersistentStore]) { + + CoreStore.assert( + entityClass is T.Type, + "Attempted to create generic type \(cs_typeName(From)) with entity class \(cs_typeName(entityClass))" + ) + self.init(entityClass: entityClass, persistentStores: persistentStores) + } } diff --git a/Sources/Internal/CoreStoreFetchedResultsController.swift b/Sources/Internal/CoreStoreFetchedResultsController.swift index bc35a1d..6db8322 100644 --- a/Sources/Internal/CoreStoreFetchedResultsController.swift +++ b/Sources/Internal/CoreStoreFetchedResultsController.swift @@ -52,6 +52,10 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll from?.applyToFetchRequest(fetchRequest, context: context, applyAffectedStores: false) applyFetchClauses(fetchRequest: fetchRequest) + CoreStore.assert( + fetchRequest.sortDescriptors?.isEmpty == false, + "An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor." + ) if let from = from { self.reapplyAffectedStores = { fetchRequest, context in @@ -63,7 +67,7 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll guard let from = (fetchRequest.entity.flatMap { $0.managedObjectClassName }).flatMap(NSClassFromString).flatMap(From.init) else { - fatalError("Attempted to create an \(cs_typeName(NSFetchedResultsController)) without a From clause or an NSEntityDescription.") + fatalError("Attempted to create an \(cs_typeName(NSFetchedResultsController)) without a \(cs_typeName(From)) clause or an \(cs_typeName(NSEntityDescription)).") } self.reapplyAffectedStores = { fetchRequest, context in diff --git a/Sources/ObjectiveC/CSUnsafeDataTransaction.swift b/Sources/ObjectiveC/CSUnsafeDataTransaction.swift index ff2dae5..53d7ee8 100644 --- a/Sources/ObjectiveC/CSUnsafeDataTransaction.swift +++ b/Sources/ObjectiveC/CSUnsafeDataTransaction.swift @@ -96,31 +96,25 @@ public final class CSUnsafeDataTransaction: CSBaseDataTransaction { Immediately flushes all pending changes to the transaction's observers. This is useful in conjunction with `ListMonitor`s and `ObjectMonitor`s created from `UnsafeDataTransaction`s used to manage temporary "scratch" data. - Important: Note that unlike `commit()`, `flush()` does not propagate/save updates to the `DataStack` and the persistent store. However, the flushed changes will be seen by children transactions created further from the current transaction (i.e. through `transaction.beginUnsafe()`) - - parameter error: the `NSError` pointer that indicates the reason in case of an failure */ @objc - public func flush(error error: NSErrorPointer) { + public func flush() { - bridge(error) { - - try self.bridgeToSwift.flush() - } + self.bridgeToSwift.flush() } /** Flushes all pending changes to the transaction's observers at the end of the `closure`'s execution. This is useful in conjunction with `ListMonitor`s and `ObjectMonitor`s created from `UnsafeDataTransaction`s used to manage temporary "scratch" data. - Important: Note that unlike `commit()`, `flush()` does not propagate/save updates to the `DataStack` and the persistent store. However, the flushed changes will be seen by children transactions created further from the current transaction (i.e. through `transaction.beginUnsafe()`) - - parameter error: the `NSError` pointer that indicates the reason in case of an failure - parameter closure: the closure where changes can be made prior to the flush */ @objc - public func flush(error error: NSErrorPointer, block: () -> Void) throws { + public func flush(block: () -> Void) { - bridge(error) { + self.bridgeToSwift.flush { block() - try self.bridgeToSwift.context.save() } } diff --git a/Sources/Observing/DataStack+Observing.swift b/Sources/Observing/DataStack+Observing.swift index 6517715..9795dbe 100644 --- a/Sources/Observing/DataStack+Observing.swift +++ b/Sources/Observing/DataStack+Observing.swift @@ -78,10 +78,6 @@ public extension DataStack { NSThread.isMainThread(), "Attempted to observe objects from \(cs_typeName(self)) outside the main thread." ) - CoreStore.assert( - fetchClauses.contains { $0 is OrderBy }, - "A ListMonitor requires an OrderBy clause." - ) return ListMonitor( dataStack: self, from: from, @@ -118,10 +114,6 @@ public extension DataStack { NSThread.isMainThread(), "Attempted to observe objects from \(cs_typeName(self)) outside the main thread." ) - CoreStore.assert( - fetchClauses.contains { $0 is OrderBy }, - "A ListMonitor requires an OrderBy clause." - ) _ = ListMonitor( dataStack: self, from: from, @@ -163,10 +155,6 @@ public extension DataStack { NSThread.isMainThread(), "Attempted to observe objects from \(cs_typeName(self)) outside the main thread." ) - CoreStore.assert( - fetchClauses.contains { $0 is OrderBy }, - "A ListMonitor requires an OrderBy clause." - ) return ListMonitor( dataStack: self, @@ -206,10 +194,6 @@ public extension DataStack { NSThread.isMainThread(), "Attempted to observe objects from \(cs_typeName(self)) outside the main thread." ) - CoreStore.assert( - fetchClauses.contains { $0 is OrderBy }, - "A ListMonitor requires an OrderBy clause." - ) _ = ListMonitor( dataStack: self, diff --git a/Sources/Transactions/Into.swift b/Sources/Transactions/Into.swift index 6384fac..18ba958 100644 --- a/Sources/Transactions/Into.swift +++ b/Sources/Transactions/Into.swift @@ -75,6 +75,10 @@ public struct Into: Hashable { */ public init(_ entityClass: AnyClass) { + CoreStore.assert( + entityClass is T.Type, + "Attempted to create generic type \(cs_typeName(Into)) with entity class \(cs_typeName(entityClass))" + ) self.init(entityClass: entityClass, configuration: nil, inferStoreIfPossible: true) } @@ -116,6 +120,10 @@ public struct Into: Hashable { */ public init(_ entityClass: AnyClass, _ configuration: String?) { + CoreStore.assert( + entityClass is T.Type, + "Attempted to create generic type \(cs_typeName(Into)) with entity class \(cs_typeName(entityClass))" + ) self.init(entityClass: entityClass, configuration: configuration, inferStoreIfPossible: false) } @@ -171,3 +179,11 @@ public func == (lhs: Into, rhs: Into< && lhs.configuration == rhs.configuration && lhs.inferStoreIfPossible == rhs.inferStoreIfPossible } + +@warn_unused_result +public func != (lhs: Into, rhs: Into) -> Bool { + + return lhs.entityClass == rhs.entityClass + && lhs.configuration == rhs.configuration + && lhs.inferStoreIfPossible == rhs.inferStoreIfPossible +}