NSManagedObject features are now fully supported for CoreStoreObject types. MacOSX 10.12 onwards now support ListMonitors and ObjectMonitors

This commit is contained in:
John Estropia
2017-04-07 20:14:13 +09:00
parent 4aa1a63f9a
commit c0ae129b22
58 changed files with 1172 additions and 803 deletions

View File

@@ -27,10 +27,9 @@
import CoreStore
#if os(iOS) || os(watchOS) || os(tvOS)
// MARK: - ConvenienceTests
@available(OSX 10.12, *)
class ConvenienceTests: BaseTestCase {
@objc
@@ -90,5 +89,3 @@ class ConvenienceTests: BaseTestCase {
}
}
}
#endif

View File

@@ -2,42 +2,62 @@
// DynamicModelTests.swift
// CoreStore
//
// Created by John Estropia on 2017/04/03.
// Copyright © 2017 John Rommel Estropia. All rights reserved.
// Copyright © 2017 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
import CoreData
@testable import CoreStore
@testable
import CoreStore
class Animal: ManagedObject {
class Animal: CoreStoreObject {
let species = Attribute.Required<String>("species", default: "Swift")
let master = Relationship.ToOne<Person>("master", inverse: { $0.pet })
let species = Value.Required<String>("species", default: "Swift")
let master = Relationship.ToOne<Person>("master")
}
class Dog: Animal {
let nickname = Attribute.Optional<String>("nickname")
let age = Attribute.Required<Int>("age", default: 1)
let nickname = Value.Optional<String>("nickname")
let age = Value.Required<Int>("age", default: 1)
let friends = Relationship.ToManyUnordered<Dog>("friends")
let friends2 = Relationship.ToManyUnordered<Dog>("friends2", inverse: { $0.friends })
}
class Person: ManagedObject {
class Person: CoreStoreObject {
let name = Attribute.Required<String>("name")
let pet = Relationship.ToOne<Animal>("pet")
let name = Value.Required<String>("name")
let pet = Relationship.ToOne<Animal>("pet", inverse: { $0.master })
}
// MARK: - DynamicModelTests
class DynamicModelTests: BaseTestDataTestCase {
func testDynamicModels_CanBeDeclaredCorrectly() {
let dataStack = DataStack(
dynamicModel: ObjectModel(
dynamicModel: DynamicModel(
version: "V1",
entities: [
Entity<Animal>("Animal"),
@@ -57,26 +77,28 @@ class DynamicModelTests: BaseTestDataTestCase {
let k3 = Dog.keyPath({ $0.nickname })
XCTAssertEqual(k3, "nickname")
let expectation = self.expectation(description: "done")
let updateDone = self.expectation(description: "update-done")
let fetchDone = self.expectation(description: "fetch-done")
stack.perform(
asynchronous: { (transaction) in
let animal = transaction.create(Into<Animal>())
XCTAssertEqual(animal.species*, "Swift")
XCTAssertTrue(type(of: animal.species*) == String.self)
XCTAssertEqual(animal.species.value, "Swift")
XCTAssertTrue(type(of: animal.species.value) == String.self)
animal.species .= "Sparrow"
XCTAssertEqual(animal.species*, "Sparrow")
XCTAssertEqual(animal.species.value, "Sparrow")
let dog = transaction.create(Into<Dog>())
XCTAssertEqual(dog.species*, "Swift")
XCTAssertEqual(dog.nickname*, nil)
XCTAssertEqual(dog.species.value, "Swift")
XCTAssertEqual(dog.nickname.value, nil)
XCTAssertEqual(dog.age.value, 1)
dog.species .= "Dog"
XCTAssertEqual(dog.species*, "Dog")
XCTAssertEqual(dog.species.value, "Dog")
dog.nickname .= "Spot"
XCTAssertEqual(dog.nickname*, "Spot")
XCTAssertEqual(dog.nickname.value, "Spot")
let person = transaction.create(Into<Person>())
XCTAssertNil(person.pet.value)
@@ -89,7 +111,7 @@ class DynamicModelTests: BaseTestDataTestCase {
},
success: {
print("done")
updateDone.fulfill()
},
failure: { _ in
@@ -104,15 +126,15 @@ class DynamicModelTests: BaseTestDataTestCase {
let bird = transaction.fetchOne(From<Animal>(), p1)
XCTAssertNotNil(bird)
XCTAssertEqual(bird!.species*, "Sparrow")
XCTAssertEqual(bird!.species.value, "Sparrow")
let p2 = Dog.where({ $0.nickname == "Spot" })
XCTAssertEqual(p2.predicate, NSPredicate(format: "%K == %@", "nickname", "Spot"))
let dog = transaction.fetchOne(From<Dog>(), p2)
XCTAssertNotNil(dog)
XCTAssertEqual(dog!.nickname*, "Spot")
XCTAssertEqual(dog!.species*, "Dog")
XCTAssertEqual(dog!.nickname.value, "Spot")
XCTAssertEqual(dog!.species.value, "Dog")
let person = transaction.fetchOne(From<Person>())
XCTAssertNotNil(person)
@@ -123,7 +145,7 @@ class DynamicModelTests: BaseTestDataTestCase {
},
success: {
expectation.fulfill()
fetchDone.fulfill()
withExtendedLifetime(stack, {})
},
failure: { _ in

View File

@@ -29,27 +29,11 @@ import XCTest
import CoreStore
#if os(iOS) || os(watchOS) || os(tvOS)
// MARK: - ListObserverTests
@available(OSX 10.12, *)
class ListObserverTests: BaseTestDataTestCase {
@objc
dynamic func test_ThatListObservers_CanDowncast() {
self.prepareStack { (stack) in
let monitor = stack.monitorSectionedList(
From<TestEntity1>(),
SectionBy(#keyPath(TestEntity1.testBoolean)),
OrderBy(.ascending(#keyPath(TestEntity1.testBoolean)), .ascending(#keyPath(TestEntity1.testEntityID)))
)
let downcast = monitor.downcast()
XCTAssertTrue(monitor == downcast)
}
}
@objc
dynamic func test_ThatListObservers_CanReceiveInsertNotifications() {
@@ -118,8 +102,8 @@ class ListObserverTests: BaseTestDataTestCase {
)
let indexPath = userInfo?["indexPath"] as? NSIndexPath
XCTAssertEqual(indexPath?.section, 0)
XCTAssertEqual(indexPath?.row, 0)
XCTAssertEqual(indexPath?.index(atPosition: 0), 0)
XCTAssertEqual(indexPath?.index(atPosition: 1), 0)
let object = userInfo?["object"] as? TestEntity1
XCTAssertEqual(object?.testBoolean, NSNumber(value: true))
@@ -236,8 +220,8 @@ class ListObserverTests: BaseTestDataTestCase {
switch object?.testEntityID {
case NSNumber(value: 101)?:
XCTAssertEqual(indexPath?.section, 1)
XCTAssertEqual(indexPath?.row, 0)
XCTAssertEqual(indexPath?.index(atPosition: 0), 1)
XCTAssertEqual(indexPath?.index(atPosition: 1), 0)
XCTAssertEqual(object?.testBoolean, NSNumber(value: true))
XCTAssertEqual(object?.testNumber, NSNumber(value: 11))
@@ -247,8 +231,8 @@ class ListObserverTests: BaseTestDataTestCase {
XCTAssertEqual(object?.testDate, self.dateFormatter.date(from: "2000-01-11T00:00:00Z")!)
case NSNumber(value: 102)?:
XCTAssertEqual(indexPath?.section, 0)
XCTAssertEqual(indexPath?.row, 0)
XCTAssertEqual(indexPath?.index(atPosition: 0), 0)
XCTAssertEqual(indexPath?.index(atPosition: 1), 0)
XCTAssertEqual(object?.testBoolean, NSNumber(value: false))
XCTAssertEqual(object?.testNumber, NSNumber(value: 22))
@@ -376,12 +360,12 @@ class ListObserverTests: BaseTestDataTestCase {
)
let fromIndexPath = userInfo?["fromIndexPath"] as? NSIndexPath
XCTAssertEqual(fromIndexPath?.section, 0)
XCTAssertEqual(fromIndexPath?.row, 0)
XCTAssertEqual(fromIndexPath?.index(atPosition: 0), 0)
XCTAssertEqual(fromIndexPath?.index(atPosition: 1), 0)
let toIndexPath = userInfo?["toIndexPath"] as? NSIndexPath
XCTAssertEqual(toIndexPath?.section, 1)
XCTAssertEqual(toIndexPath?.row, 1)
XCTAssertEqual(toIndexPath?.index(atPosition: 0), 1)
XCTAssertEqual(toIndexPath?.index(atPosition: 1), 1)
let object = userInfo?["object"] as? TestEntity1
XCTAssertEqual(object?.testEntityID, NSNumber(value: 102))
@@ -488,7 +472,7 @@ class ListObserverTests: BaseTestDataTestCase {
let indexPath = userInfo?["indexPath"] as? NSIndexPath
XCTAssertEqual(indexPath?.section, 0)
XCTAssert(indexPath?.row == 0 || indexPath?.row == 1)
XCTAssert(indexPath?.index(atPosition: 1) == 0 || indexPath?.index(atPosition: 1) == 1)
let object = userInfo?["object"] as? TestEntity1
XCTAssertEqual(object?.isDeleted, true)
@@ -571,6 +555,7 @@ class ListObserverTests: BaseTestDataTestCase {
// MARK: TestListObserver
@available(OSX 10.12, *)
class TestListObserver: ListSectionObserver {
// MARK: ListObserver
@@ -693,5 +678,3 @@ class TestListObserver: ListSectionObserver {
)
}
}
#endif

View File

@@ -29,31 +29,10 @@ import XCTest
import CoreStore
#if os(iOS) || os(watchOS) || os(tvOS)
// MARK: - ObjectObserverTests
class ObjectObserverTests: BaseTestDataTestCase {
@objc
dynamic func test_ThatObjectObservers_CanDowncast() {
self.prepareStack { (stack) in
self.prepareTestDataForStack(stack)
guard let object = stack.fetchOne(
From<TestEntity1>(),
Where(#keyPath(TestEntity1.testEntityID), isEqualTo: 101)) else {
XCTFail()
return
}
let monitor = stack.monitorObject(object)
let downcast = monitor.downcast()
XCTAssertTrue(monitor == downcast)
}
}
@available(OSX 10.12, *)
class ObjectObserverTests: BaseTestDataTestCase {
@objc
dynamic func test_ThatObjectObservers_CanReceiveUpdateNotifications() {
@@ -224,6 +203,7 @@ import CoreStore
// MARK: TestObjectObserver
@available(OSX 10.12, *)
class TestObjectObserver: ObjectObserver {
typealias ObjectEntityType = TestEntity1
@@ -262,5 +242,3 @@ class TestObjectObserver: ObjectObserver {
)
}
}
#endif

View File

@@ -29,10 +29,9 @@ import XCTest
import CoreStore
#if os(iOS) || os(watchOS) || os(tvOS)
//MARK: - SectionByTests
@available(OSX 10.12, *)
final class SectionByTests: XCTestCase {
@objc
@@ -53,5 +52,3 @@ final class SectionByTests: XCTestCase {
}
}
}
#endif

View File

@@ -386,8 +386,8 @@ final class TransactionTests: BaseTestCase {
}
}
#if os(iOS) || os(watchOS) || os(tvOS)
@available(OSX 10.12, *)
@objc
dynamic func test_ThatSynchronousTransactions_CanCommitWithoutWaitingForMerges() {
@@ -432,8 +432,8 @@ final class TransactionTests: BaseTestCase {
)
let indexPath = userInfo?["indexPath"] as? NSIndexPath
XCTAssertEqual(indexPath?.section, 0)
XCTAssertEqual(indexPath?.row, 0)
XCTAssertEqual(indexPath?.index(atPosition: 0), 0)
XCTAssertEqual(indexPath?.index(atPosition: 1), 0)
let object = userInfo?["object"] as? TestEntity1
XCTAssertEqual(object?.testBoolean, NSNumber(value: true))
@@ -489,7 +489,6 @@ final class TransactionTests: BaseTestCase {
}
}
#endif
@objc
dynamic func test_ThatAsynchronousTransactions_CanPerformCRUDs() {