Add unit tests for List and Object Publishers

This commit is contained in:
John Estropia
2019-10-25 14:34:22 +09:00
parent 11a9e3991c
commit 98d860aff6
3 changed files with 385 additions and 602 deletions
+8
View File
@@ -522,6 +522,9 @@
B580857A1CDF808C004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; }; B580857A1CDF808C004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
B580857B1CDF808D004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; }; B580857B1CDF808D004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
B580857C1CDF808F004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; }; B580857C1CDF808F004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
B581B9322362BB8C002BDB2B /* ObjectPublisherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B581B9312362BB8C002BDB2B /* ObjectPublisherTests.swift */; };
B581B9332362BB8C002BDB2B /* ObjectPublisherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B581B9312362BB8C002BDB2B /* ObjectPublisherTests.swift */; };
B581B9342362BB8C002BDB2B /* ObjectPublisherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B581B9312362BB8C002BDB2B /* ObjectPublisherTests.swift */; };
B5831B701F34AC3400A9F647 /* AttributeProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5831B6F1F34AC3400A9F647 /* AttributeProtocol.swift */; }; B5831B701F34AC3400A9F647 /* AttributeProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5831B6F1F34AC3400A9F647 /* AttributeProtocol.swift */; };
B5831B711F34AC3400A9F647 /* AttributeProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5831B6F1F34AC3400A9F647 /* AttributeProtocol.swift */; }; B5831B711F34AC3400A9F647 /* AttributeProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5831B6F1F34AC3400A9F647 /* AttributeProtocol.swift */; };
B5831B721F34AC3400A9F647 /* AttributeProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5831B6F1F34AC3400A9F647 /* AttributeProtocol.swift */; }; B5831B721F34AC3400A9F647 /* AttributeProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5831B6F1F34AC3400A9F647 /* AttributeProtocol.swift */; };
@@ -994,6 +997,7 @@
B57D27BD1D0BBE8200539C58 /* BaseTestDataTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestDataTestCase.swift; sourceTree = "<group>"; }; B57D27BD1D0BBE8200539C58 /* BaseTestDataTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestDataTestCase.swift; sourceTree = "<group>"; };
B57D27C11D0BC20100539C58 /* QueryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryTests.swift; sourceTree = "<group>"; }; B57D27C11D0BC20100539C58 /* QueryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryTests.swift; sourceTree = "<group>"; };
B58085741CDF7F00004C2EEB /* SetupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetupTests.swift; sourceTree = "<group>"; }; B58085741CDF7F00004C2EEB /* SetupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetupTests.swift; sourceTree = "<group>"; };
B581B9312362BB8C002BDB2B /* ObjectPublisherTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectPublisherTests.swift; sourceTree = "<group>"; };
B5831B6F1F34AC3400A9F647 /* AttributeProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributeProtocol.swift; sourceTree = "<group>"; }; B5831B6F1F34AC3400A9F647 /* AttributeProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributeProtocol.swift; sourceTree = "<group>"; };
B5831B741F34AC7A00A9F647 /* RelationshipProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelationshipProtocol.swift; sourceTree = "<group>"; }; B5831B741F34AC7A00A9F647 /* RelationshipProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelationshipProtocol.swift; sourceTree = "<group>"; };
B5831B791F34ACBA00A9F647 /* Transformable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Transformable.swift; sourceTree = "<group>"; }; B5831B791F34ACBA00A9F647 /* Transformable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Transformable.swift; sourceTree = "<group>"; };
@@ -1262,6 +1266,7 @@
B5D8CA7A2346EC550055D7D1 /* ListPublisherTests.swift */, B5D8CA7A2346EC550055D7D1 /* ListPublisherTests.swift */,
B5DC47C51C93D22900FA3BF3 /* MigrationChainTests.swift */, B5DC47C51C93D22900FA3BF3 /* MigrationChainTests.swift */,
B5220E071D0C5F8D009BC71E /* ObjectObserverTests.swift */, B5220E071D0C5F8D009BC71E /* ObjectObserverTests.swift */,
B581B9312362BB8C002BDB2B /* ObjectPublisherTests.swift */,
B52557771D02826E00E51965 /* OrderByTests.swift */, B52557771D02826E00E51965 /* OrderByTests.swift */,
B57D27C11D0BC20100539C58 /* QueryTests.swift */, B57D27C11D0BC20100539C58 /* QueryTests.swift */,
B52557831D02A07400E51965 /* SectionByTests.swift */, B52557831D02A07400E51965 /* SectionByTests.swift */,
@@ -2285,6 +2290,7 @@
B5220E0C1D0D0D19009BC71E /* ImportTests.swift in Sources */, B5220E0C1D0D0D19009BC71E /* ImportTests.swift in Sources */,
B5D339B41E925C2B00C880DE /* DynamicModelTests.swift in Sources */, B5D339B41E925C2B00C880DE /* DynamicModelTests.swift in Sources */,
B5D372841A39CD6900F583D9 /* Model.xcdatamodeld in Sources */, B5D372841A39CD6900F583D9 /* Model.xcdatamodeld in Sources */,
B581B9322362BB8C002BDB2B /* ObjectPublisherTests.swift in Sources */,
B52557881D02DE8100E51965 /* FetchTests.swift in Sources */, B52557881D02DE8100E51965 /* FetchTests.swift in Sources */,
B5489F501CF603D5008B4978 /* FromTests.swift in Sources */, B5489F501CF603D5008B4978 /* FromTests.swift in Sources */,
B52557781D02826E00E51965 /* OrderByTests.swift in Sources */, B52557781D02826E00E51965 /* OrderByTests.swift in Sources */,
@@ -2508,6 +2514,7 @@
B5220E0D1D0D0D19009BC71E /* ImportTests.swift in Sources */, B5220E0D1D0D0D19009BC71E /* ImportTests.swift in Sources */,
B5D339B51E925C2B00C880DE /* DynamicModelTests.swift in Sources */, B5D339B51E925C2B00C880DE /* DynamicModelTests.swift in Sources */,
B525576D1CFAF18F00E51965 /* IntoTests.swift in Sources */, B525576D1CFAF18F00E51965 /* IntoTests.swift in Sources */,
B581B9332362BB8C002BDB2B /* ObjectPublisherTests.swift in Sources */,
B580857B1CDF808D004C2EEB /* SetupTests.swift in Sources */, B580857B1CDF808D004C2EEB /* SetupTests.swift in Sources */,
B52557891D02DE8100E51965 /* FetchTests.swift in Sources */, B52557891D02DE8100E51965 /* FetchTests.swift in Sources */,
B5489F511CF603D5008B4978 /* FromTests.swift in Sources */, B5489F511CF603D5008B4978 /* FromTests.swift in Sources */,
@@ -2731,6 +2738,7 @@
B525576E1CFAF18F00E51965 /* IntoTests.swift in Sources */, B525576E1CFAF18F00E51965 /* IntoTests.swift in Sources */,
B5D339B61E925C2B00C880DE /* DynamicModelTests.swift in Sources */, B5D339B61E925C2B00C880DE /* DynamicModelTests.swift in Sources */,
B580857C1CDF808F004C2EEB /* SetupTests.swift in Sources */, B580857C1CDF808F004C2EEB /* SetupTests.swift in Sources */,
B581B9342362BB8C002BDB2B /* ObjectPublisherTests.swift in Sources */,
B525578A1D02DE8100E51965 /* FetchTests.swift in Sources */, B525578A1D02DE8100E51965 /* FetchTests.swift in Sources */,
B5220E281D1308E5009BC71E /* SectionByTests.swift in Sources */, B5220E281D1308E5009BC71E /* SectionByTests.swift in Sources */,
B5489F521CF603D5008B4978 /* FromTests.swift in Sources */, B5489F521CF603D5008B4978 /* FromTests.swift in Sources */,
+223 -602
View File
@@ -41,99 +41,26 @@ class ListPublisherTests: BaseTestDataTestCase {
self.prepareStack { (stack) in self.prepareStack { (stack) in
// let observer = TestListObserver() let observer = NSObject()
let listPublisher = stack.listPublisher( let listPublisher = stack.publishList(
From<TestEntity1>(), From<TestEntity1>(),
SectionBy(#keyPath(TestEntity1.testBoolean)), SectionBy(#keyPath(TestEntity1.testBoolean)),
OrderBy<TestEntity1>(.ascending(#keyPath(TestEntity1.testBoolean)), .ascending(#keyPath(TestEntity1.testEntityID))) OrderBy<TestEntity1>(.ascending(#keyPath(TestEntity1.testBoolean)), .ascending(#keyPath(TestEntity1.testEntityID)))
) )
// monitor.addObserver(observer) XCTAssertFalse(listPublisher.snapshot.hasSections())
// XCTAssertFalse(listPublisher.snapshot.hasItems())
// XCTAssertFalse(monitor.hasSections()) XCTAssertTrue(listPublisher.snapshot.itemIDs.isEmpty)
// XCTAssertFalse(monitor.hasObjects())
// XCTAssertTrue(monitor.objectsInAllSections().isEmpty) let didChangeExpectation = self.expectation(description: "didChange")
// listPublisher.addObserver(observer) { listPublisher in
// var events = 0
// XCTAssertTrue(listPublisher.snapshot.hasSections())
// let willChangeExpectation = self.expectation( XCTAssertTrue(listPublisher.snapshot.hasItems())
// forNotification: NSNotification.Name(rawValue: "listMonitorWillChange:"), XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 0), 1)
// object: observer,
// handler: { (note) -> Bool in didChangeExpectation.fulfill()
// }
// XCTAssertEqual(events, 0)
// XCTAssertEqual((note.userInfo as NSDictionary?) ?? [:], NSDictionary())
// defer {
//
// events += 1
// }
// return events == 0
// }
// )
// let didInsertSectionExpectation = self.expectation(
// forNotification: NSNotification.Name(rawValue: "listMonitor:didInsertSection:toSectionIndex:"),
// object: observer,
// handler: { (note) -> Bool in
//
// XCTAssertEqual(events, 1)
// XCTAssertEqual(
// ((note.userInfo as NSDictionary?) ?? [:]),
// [
// "sectionInfo": monitor.sectionInfo(at: 0),
// "sectionIndex": 0
// ] as NSDictionary
// )
// defer {
//
// events += 1
// }
// return events == 1
// }
// )
// let didInsertObjectExpectation = self.expectation(
// forNotification: NSNotification.Name(rawValue: "listMonitor:didInsertObject:toIndexPath:"),
// object: observer,
// handler: { (note) -> Bool in
//
// XCTAssertEqual(events, 2)
//
// let userInfo = note.userInfo
// XCTAssertNotNil(userInfo)
// XCTAssertEqual(
// Set(userInfo?.keys.map({ $0 as! String }) ?? []),
// ["indexPath", "object"]
// )
//
// let indexPath = userInfo?["indexPath"] as? NSIndexPath
// XCTAssertEqual(indexPath?.index(atPosition: 0), 0)
// XCTAssertEqual(indexPath?.index(atPosition: 1), 0)
//
// let object = userInfo?["object"] as? TestEntity1
// XCTAssertEqual(object?.testBoolean, NSNumber(value: true))
// XCTAssertEqual(object?.testNumber, NSNumber(value: 1))
// XCTAssertEqual(object?.testDecimal, NSDecimalNumber(string: "1"))
// XCTAssertEqual(object?.testString, "nil:TestEntity1:1")
// XCTAssertEqual(object?.testData, ("nil:TestEntity1:1" as NSString).data(using: String.Encoding.utf8.rawValue)!)
// XCTAssertEqual(object?.testDate, self.dateFormatter.date(from: "2000-01-01T00:00:00Z")!)
// defer {
//
// events += 1
// }
// return events == 2
// }
// )
// let didChangeExpectation = self.expectation(
// forNotification: NSNotification.Name(rawValue: "listMonitorDidChange:"),
// object: observer,
// handler: { (note) -> Bool in
//
// XCTAssertEqual((note.userInfo as NSDictionary?) ?? [:], NSDictionary())
// defer {
//
// events += 1
// }
// return events == 3
// }
// )
let saveExpectation = self.expectation(description: "save") let saveExpectation = self.expectation(description: "save")
stack.perform( stack.perform(
asynchronous: { (transaction) -> Bool in asynchronous: { (transaction) -> Bool in
@@ -161,522 +88,216 @@ class ListPublisherTests: BaseTestDataTestCase {
self.waitAndCheckExpectations() self.waitAndCheckExpectations()
withExtendedLifetime(listPublisher, {}) withExtendedLifetime(listPublisher, {})
withExtendedLifetime(observer, {})
} }
} }
// @objc @objc
// dynamic func test_ThatListObservers_CanReceiveUpdateNotifications() { dynamic func test_ThatListPublishers_CanReceiveUpdateNotifications() {
//
// self.prepareStack { (stack) in self.prepareStack { (stack) in
//
// self.prepareTestDataForStack(stack) self.prepareTestDataForStack(stack)
//
// let observer = TestListObserver() let observer = NSObject()
// let monitor = stack.monitorSectionedList( let listPublisher = stack.publishList(
// From<TestEntity1>(), From<TestEntity1>(),
// SectionBy(#keyPath(TestEntity1.testBoolean)), SectionBy(#keyPath(TestEntity1.testBoolean)),
// OrderBy<TestEntity1>(.ascending(#keyPath(TestEntity1.testBoolean)), .ascending(#keyPath(TestEntity1.testEntityID))) OrderBy<TestEntity1>(.ascending(#keyPath(TestEntity1.testBoolean)), .ascending(#keyPath(TestEntity1.testEntityID)))
// ) )
// monitor.addObserver(observer) XCTAssertTrue(listPublisher.snapshot.hasSections())
// XCTAssertEqual(listPublisher.snapshot.numberOfSections, 2)
// XCTAssertTrue(monitor.hasSections()) XCTAssertTrue(listPublisher.snapshot.hasItems())
// XCTAssertEqual(monitor.numberOfSections(), 2) XCTAssertTrue(listPublisher.snapshot.hasItems(inSectionIndex: 0))
// XCTAssertTrue(monitor.hasObjects()) XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 0), 2)
// XCTAssertTrue(monitor.hasObjects(in: 0)) XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 1), 3)
// XCTAssertEqual(monitor.numberOfObjects(in: 0), 2)
// XCTAssertEqual(monitor.numberOfObjects(in: 1), 3) let didChangeExpectation = self.expectation(description: "didChange")
// listPublisher.addObserver(observer) { listPublisher in
// var events = 0
// XCTAssertTrue(listPublisher.snapshot.hasSections())
// let willChangeExpectation = self.expectation( XCTAssertEqual(listPublisher.snapshot.numberOfSections, 2)
// forNotification: NSNotification.Name(rawValue: "listMonitorWillChange:"), XCTAssertTrue(listPublisher.snapshot.hasItems())
// object: observer, XCTAssertTrue(listPublisher.snapshot.hasItems(inSectionIndex: 0))
// handler: { (note) -> Bool in XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 0), 2)
// XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 1), 3)
// XCTAssertEqual(events, 0)
// XCTAssertEqual((note.userInfo as NSDictionary?) ?? [:], NSDictionary()) didChangeExpectation.fulfill()
// defer { }
//
// events += 1 let saveExpectation = self.expectation(description: "save")
// } stack.perform(
// return events == 0 asynchronous: { (transaction) -> Bool in
// }
// ) if let object = try transaction.fetchOne(
// From<TestEntity1>(),
// let didUpdateObjectExpectation = self.expectation( Where<TestEntity1>(#keyPath(TestEntity1.testEntityID), isEqualTo: 101)) {
// forNotification: NSNotification.Name(rawValue: "listMonitor:didUpdateObject:atIndexPath:"),
// object: observer, object.testNumber = NSNumber(value: 11)
// handler: { (note) -> Bool in object.testDecimal = NSDecimalNumber(string: "11")
// object.testString = "nil:TestEntity1:11"
// XCTAssert(events == 1 || events == 2) object.testData = ("nil:TestEntity1:11" as NSString).data(using: String.Encoding.utf8.rawValue)!
// object.testDate = self.dateFormatter.date(from: "2000-01-11T00:00:00Z")!
// let userInfo = note.userInfo }
// XCTAssertNotNil(userInfo) else {
// XCTAssertEqual(
// Set(userInfo?.keys.map({ $0 as! String }) ?? []), XCTFail()
// ["indexPath", "object"] }
// ) if let object = try transaction.fetchOne(
// From<TestEntity1>(),
// let indexPath = userInfo?["indexPath"] as? NSIndexPath Where<TestEntity1>(#keyPath(TestEntity1.testEntityID), isEqualTo: 102)) {
// let object = userInfo?["object"] as? TestEntity1
// object.testNumber = NSNumber(value: 22)
// switch object?.testEntityID { object.testDecimal = NSDecimalNumber(string: "22")
// object.testString = "nil:TestEntity1:22"
// case NSNumber(value: 101)?: object.testData = ("nil:TestEntity1:22" as NSString).data(using: String.Encoding.utf8.rawValue)!
// XCTAssertEqual(indexPath?.index(atPosition: 0), 1) object.testDate = self.dateFormatter.date(from: "2000-01-22T00:00:00Z")!
// XCTAssertEqual(indexPath?.index(atPosition: 1), 0) }
// else {
// XCTAssertEqual(object?.testBoolean, NSNumber(value: true))
// XCTAssertEqual(object?.testNumber, NSNumber(value: 11)) XCTFail()
// XCTAssertEqual(object?.testDecimal, NSDecimalNumber(string: "11")) }
// XCTAssertEqual(object?.testString, "nil:TestEntity1:11") return transaction.hasChanges
// XCTAssertEqual(object?.testData, ("nil:TestEntity1:11" as NSString).data(using: String.Encoding.utf8.rawValue)!) },
// XCTAssertEqual(object?.testDate, self.dateFormatter.date(from: "2000-01-11T00:00:00Z")!) success: { (hasChanges) in
//
// case NSNumber(value: 102)?: XCTAssertTrue(hasChanges)
// XCTAssertEqual(indexPath?.index(atPosition: 0), 0) saveExpectation.fulfill()
// XCTAssertEqual(indexPath?.index(atPosition: 1), 0) },
// failure: { _ in
// XCTAssertEqual(object?.testBoolean, NSNumber(value: false))
// XCTAssertEqual(object?.testNumber, NSNumber(value: 22)) XCTFail()
// XCTAssertEqual(object?.testDecimal, NSDecimalNumber(string: "22")) }
// XCTAssertEqual(object?.testString, "nil:TestEntity1:22") )
// XCTAssertEqual(object?.testData, ("nil:TestEntity1:22" as NSString).data(using: String.Encoding.utf8.rawValue)!) self.waitAndCheckExpectations()
// XCTAssertEqual(object?.testDate, self.dateFormatter.date(from: "2000-01-22T00:00:00Z")!)
// withExtendedLifetime(listPublisher, {})
// default: withExtendedLifetime(observer, {})
// XCTFail() }
// } }
// defer {
// @objc
// events += 1 dynamic func test_ThatListPublishers_CanReceiveMoveNotifications() {
// }
// return events == 1 || events == 2 self.prepareStack { (stack) in
// }
// ) self.prepareTestDataForStack(stack)
// let didChangeExpectation = self.expectation(
// forNotification: NSNotification.Name(rawValue: "listMonitorDidChange:"), let observer = NSObject()
// object: observer, let listPublisher = stack.publishList(
// handler: { (note) -> Bool in From<TestEntity1>(),
// SectionBy(#keyPath(TestEntity1.testBoolean)),
// XCTAssertEqual(events, 3) OrderBy<TestEntity1>(.ascending(#keyPath(TestEntity1.testBoolean)), .ascending(#keyPath(TestEntity1.testEntityID)))
// XCTAssertEqual((note.userInfo as NSDictionary?) ?? [:], NSDictionary()) )
// defer { XCTAssertTrue(listPublisher.snapshot.hasSections())
// XCTAssertEqual(listPublisher.snapshot.numberOfSections, 2)
// events += 1 XCTAssertTrue(listPublisher.snapshot.hasItems())
// } XCTAssertTrue(listPublisher.snapshot.hasItems(inSectionIndex: 0))
// return events == 3 XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 0), 2)
// } XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 1), 3)
// )
// let saveExpectation = self.expectation(description: "save") let didChangeExpectation = self.expectation(description: "didChange")
// stack.perform( listPublisher.addObserver(observer) { listPublisher in
// asynchronous: { (transaction) -> Bool in
// XCTAssertTrue(listPublisher.snapshot.hasSections())
// if let object = try transaction.fetchOne( XCTAssertEqual(listPublisher.snapshot.numberOfSections, 2)
// From<TestEntity1>(), XCTAssertTrue(listPublisher.snapshot.hasItems())
// Where<TestEntity1>(#keyPath(TestEntity1.testEntityID), isEqualTo: 101)) { XCTAssertTrue(listPublisher.snapshot.hasItems(inSectionIndex: 0))
// XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 0), 2)
// object.testNumber = NSNumber(value: 11) XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 1), 3)
// object.testDecimal = NSDecimalNumber(string: "11")
// object.testString = "nil:TestEntity1:11" didChangeExpectation.fulfill()
// object.testData = ("nil:TestEntity1:11" as NSString).data(using: String.Encoding.utf8.rawValue)! }
// object.testDate = self.dateFormatter.date(from: "2000-01-11T00:00:00Z")!
// } let saveExpectation = self.expectation(description: "save")
// else { stack.perform(
// asynchronous: { (transaction) -> Bool in
// XCTFail()
// } if let object = try transaction.fetchOne(
// if let object = try transaction.fetchOne( From<TestEntity1>(),
// From<TestEntity1>(), Where<TestEntity1>(#keyPath(TestEntity1.testEntityID), isEqualTo: 102)) {
// Where<TestEntity1>(#keyPath(TestEntity1.testEntityID), isEqualTo: 102)) {
// object.testBoolean = NSNumber(value: true)
// object.testNumber = NSNumber(value: 22) }
// object.testDecimal = NSDecimalNumber(string: "22") else {
// object.testString = "nil:TestEntity1:22"
// object.testData = ("nil:TestEntity1:22" as NSString).data(using: String.Encoding.utf8.rawValue)! XCTFail()
// object.testDate = self.dateFormatter.date(from: "2000-01-22T00:00:00Z")! }
// } return transaction.hasChanges
// else { },
// success: { (hasChanges) in
// XCTFail()
// } XCTAssertTrue(hasChanges)
// return transaction.hasChanges saveExpectation.fulfill()
// }, },
// success: { (hasChanges) in failure: { _ in
//
// XCTAssertTrue(hasChanges) XCTFail()
// saveExpectation.fulfill() }
// }, )
// failure: { _ in self.waitAndCheckExpectations()
//
// XCTFail() withExtendedLifetime(listPublisher, {})
// } withExtendedLifetime(observer, {})
// ) }
// self.waitAndCheckExpectations() }
// }
// } @objc
// dynamic func test_ThatListPublishers_CanReceiveDeleteNotifications() {
// @objc
// dynamic func test_ThatListObservers_CanReceiveMoveNotifications() { self.prepareStack { (stack) in
//
// self.prepareStack { (stack) in self.prepareTestDataForStack(stack)
//
// self.prepareTestDataForStack(stack) let observer = NSObject()
// let listPublisher = stack.publishList(
// let observer = TestListObserver() From<TestEntity1>(),
// let monitor = stack.monitorSectionedList( SectionBy(#keyPath(TestEntity1.testBoolean)),
// From<TestEntity1>(), OrderBy<TestEntity1>(.ascending(#keyPath(TestEntity1.testBoolean)), .ascending(#keyPath(TestEntity1.testEntityID)))
// SectionBy(#keyPath(TestEntity1.testBoolean)), )
// OrderBy<TestEntity1>(.ascending(#keyPath(TestEntity1.testBoolean)), .ascending(#keyPath(TestEntity1.testEntityID))) XCTAssertTrue(listPublisher.snapshot.hasSections())
// ) XCTAssertEqual(listPublisher.snapshot.numberOfSections, 2)
// monitor.addObserver(observer) XCTAssertTrue(listPublisher.snapshot.hasItems())
// XCTAssertTrue(listPublisher.snapshot.hasItems(inSectionIndex: 0))
// var events = 0 XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 0), 2)
// XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 1), 3)
// let willChangeExpectation = self.expectation(
// forNotification: NSNotification.Name(rawValue: "listMonitorWillChange:"), let didChangeExpectation = self.expectation(description: "didChange")
// object: observer, listPublisher.addObserver(observer) { listPublisher in
// handler: { (note) -> Bool in
// XCTAssertTrue(listPublisher.snapshot.hasSections())
// XCTAssertEqual(events, 0) XCTAssertEqual(listPublisher.snapshot.numberOfSections, 1)
// XCTAssertEqual((note.userInfo as NSDictionary?) ?? [:], NSDictionary()) XCTAssertTrue(listPublisher.snapshot.hasItems())
// defer { XCTAssertTrue(listPublisher.snapshot.hasItems(inSectionIndex: 0))
// XCTAssertEqual(listPublisher.snapshot.numberOfItems(inSectionIndex: 0), 3)
// events += 1
// } didChangeExpectation.fulfill()
// return events == 0 }
// }
// ) let saveExpectation = self.expectation(description: "save")
// let didMoveObjectExpectation = self.expectation( stack.perform(
// forNotification: NSNotification.Name(rawValue: "listMonitor:didMoveObject:fromIndexPath:toIndexPath:"), asynchronous: { (transaction) -> Bool in
// object: observer,
// handler: { (note) -> Bool in let count = try transaction.deleteAll(
// From<TestEntity1>(),
// XCTAssertEqual(events, 1) Where<TestEntity1>(#keyPath(TestEntity1.testBoolean), isEqualTo: false)
// )
// let userInfo = note.userInfo XCTAssertEqual(count, 2)
// XCTAssertNotNil(userInfo) return transaction.hasChanges
// XCTAssertEqual( },
// Set(userInfo?.keys.map({ $0 as! String }) ?? []), success: { (hasChanges) in
// ["fromIndexPath", "toIndexPath", "object"]
// ) XCTAssertTrue(hasChanges)
// saveExpectation.fulfill()
// let fromIndexPath = userInfo?["fromIndexPath"] as? NSIndexPath },
// XCTAssertEqual(fromIndexPath?.index(atPosition: 0), 0) failure: { _ in
// XCTAssertEqual(fromIndexPath?.index(atPosition: 1), 0)
// XCTFail()
// let toIndexPath = userInfo?["toIndexPath"] as? NSIndexPath }
// XCTAssertEqual(toIndexPath?.index(atPosition: 0), 1) )
// XCTAssertEqual(toIndexPath?.index(atPosition: 1), 1) self.waitAndCheckExpectations()
// }
// let object = userInfo?["object"] as? TestEntity1 }
// XCTAssertEqual(object?.testEntityID, NSNumber(value: 102))
// XCTAssertEqual(object?.testBoolean, NSNumber(value: true))
//
// defer {
//
// events += 1
// }
// return events == 1
// }
// )
// let didChangeExpectation = self.expectation(
// forNotification: NSNotification.Name(rawValue: "listMonitorDidChange:"),
// object: observer,
// handler: { (note) -> Bool in
//
// XCTAssertEqual(events, 2)
// XCTAssertEqual((note.userInfo as NSDictionary?) ?? [:], NSDictionary())
// defer {
//
// events += 1
// }
// return events == 2
// }
// )
// let saveExpectation = self.expectation(description: "save")
// stack.perform(
// asynchronous: { (transaction) -> Bool in
//
// if let object = try transaction.fetchOne(
// From<TestEntity1>(),
// Where<TestEntity1>(#keyPath(TestEntity1.testEntityID), isEqualTo: 102)) {
//
// object.testBoolean = NSNumber(value: true)
// }
// else {
//
// XCTFail()
// }
// return transaction.hasChanges
// },
// success: { (hasChanges) in
//
// XCTAssertTrue(hasChanges)
// saveExpectation.fulfill()
// },
// failure: { _ in
//
// XCTFail()
// }
// )
// self.waitAndCheckExpectations()
// }
// }
//
// @objc
// dynamic func test_ThatListObservers_CanReceiveDeleteNotifications() {
//
// self.prepareStack { (stack) in
//
// self.prepareTestDataForStack(stack)
//
// let observer = TestListObserver()
// let monitor = stack.monitorSectionedList(
// From<TestEntity1>(),
// SectionBy(#keyPath(TestEntity1.testBoolean)),
// OrderBy<TestEntity1>(.ascending(#keyPath(TestEntity1.testBoolean)), .ascending(#keyPath(TestEntity1.testEntityID)))
// )
// monitor.addObserver(observer)
//
// var events = 0
//
// let willChangeExpectation = self.expectation(
// forNotification: NSNotification.Name(rawValue: "listMonitorWillChange:"),
// object: observer,
// handler: { (note) -> Bool in
//
// XCTAssertEqual(events, 0)
// XCTAssertEqual((note.userInfo as NSDictionary?) ?? [:], NSDictionary())
// defer {
//
// events += 1
// }
// return events == 0
// }
// )
// let didUpdateObjectExpectation = self.expectation(
// forNotification: NSNotification.Name(rawValue: "listMonitor:didDeleteObject:fromIndexPath:"),
// object: observer,
// handler: { (note) -> Bool in
//
// XCTAssert(events == 1 || events == 2)
//
// let userInfo = note.userInfo
// XCTAssertNotNil(userInfo)
// XCTAssertEqual(
// Set(userInfo?.keys.map({ $0 as! String }) ?? []),
// ["indexPath", "object"]
// )
//
// let indexPath = userInfo?["indexPath"] as? NSIndexPath
//
// XCTAssertEqual(indexPath?.section, 0)
// XCTAssert(indexPath?.index(atPosition: 1) == 0 || indexPath?.index(atPosition: 1) == 1)
//
// let object = userInfo?["object"] as? TestEntity1
// XCTAssertEqual(object?.isDeleted, true)
//
// defer {
//
// events += 1
// }
// return events == 1 || events == 2
// }
// )
// let didDeleteSectionExpectation = self.expectation(
// forNotification: NSNotification.Name(rawValue: "listMonitor:didDeleteSection:fromSectionIndex:"),
// object: observer,
// handler: { (note) -> Bool in
//
// XCTAssertEqual(events, 3)
//
// let userInfo = note.userInfo
// XCTAssertNotNil(userInfo)
// XCTAssertEqual(
// Set(userInfo?.keys.map({ $0 as! String }) ?? []),
// ["sectionInfo", "sectionIndex"]
// )
//
// let sectionInfo = userInfo?["sectionInfo"] as? NSFetchedResultsSectionInfo
// XCTAssertNotNil(sectionInfo)
// XCTAssertEqual(sectionInfo?.name, "0")
//
// let sectionIndex = userInfo?["sectionIndex"]
// XCTAssertEqual(sectionIndex as? NSNumber, NSNumber(value: 0))
//
// defer {
//
// events += 1
// }
// return events == 3
// }
// )
// let didChangeExpectation = self.expectation(
// forNotification: NSNotification.Name(rawValue: "listMonitorDidChange:"),
// object: observer,
// handler: { (note) -> Bool in
//
// XCTAssertEqual(events, 4)
// XCTAssertEqual((note.userInfo as NSDictionary?) ?? [:], NSDictionary())
// defer {
//
// events += 1
// }
// return events == 4
// }
// )
// let saveExpectation = self.expectation(description: "save")
// stack.perform(
// asynchronous: { (transaction) -> Bool in
//
// let count = try transaction.deleteAll(
// From<TestEntity1>(),
// Where<TestEntity1>(#keyPath(TestEntity1.testBoolean), isEqualTo: false)
// )
// XCTAssertEqual(count, 2)
// return transaction.hasChanges
// },
// success: { (hasChanges) in
//
// XCTAssertTrue(hasChanges)
// saveExpectation.fulfill()
// },
// failure: { _ in
//
// XCTFail()
// }
// )
// self.waitAndCheckExpectations()
// }
// }
//}
//
//
//// MARK: TestListObserver
//
//@available(macOS 10.12, *)
//class TestListObserver: ListSectionObserver {
//
// // MARK: ListObserver
//
// typealias ListEntityType = TestEntity1
//
// func listMonitorWillChange(_ monitor: ListMonitor<TestEntity1>) {
//
// NotificationCenter.default.post(
// name: Notification.Name(rawValue: "listMonitorWillChange:"),
// object: self,
// userInfo: [:]
// )
// }
//
// func listMonitorDidChange(_ monitor: ListMonitor<TestEntity1>) {
//
// NotificationCenter.default.post(
// name: Notification.Name(rawValue: "listMonitorDidChange:"),
// object: self,
// userInfo: [:]
// )
// }
//
// func listMonitorWillRefetch(_ monitor: ListMonitor<TestEntity1>) {
//
// NotificationCenter.default.post(
// name: Notification.Name(rawValue: "listMonitorWillRefetch:"),
// object: self,
// userInfo: [:]
// )
// }
//
// func listMonitorDidRefetch(_ monitor: ListMonitor<TestEntity1>) {
//
// NotificationCenter.default.post(
// name: Notification.Name(rawValue: "listMonitorDidRefetch:"),
// object: self,
// userInfo: [:]
// )
// }
//
//
// // MARK: ListObjectObserver
//
// func listMonitor(_ monitor: ListMonitor<TestEntity1>, didInsertObject object: TestEntity1, toIndexPath indexPath: IndexPath) {
//
// NotificationCenter.default.post(
// name: Notification.Name(rawValue: "listMonitor:didInsertObject:toIndexPath:"),
// object: self,
// userInfo: [
// "object": object,
// "indexPath": indexPath
// ]
// )
// }
//
// func listMonitor(_ monitor: ListMonitor<TestEntity1>, didDeleteObject object: TestEntity1, fromIndexPath indexPath: IndexPath) {
//
// NotificationCenter.default.post(
// name: Notification.Name(rawValue: "listMonitor:didDeleteObject:fromIndexPath:"),
// object: self,
// userInfo: [
// "object": object,
// "indexPath": indexPath
// ]
// )
// }
//
// func listMonitor(_ monitor: ListMonitor<TestEntity1>, didUpdateObject object: TestEntity1, atIndexPath indexPath: IndexPath) {
//
// NotificationCenter.default.post(
// name: Notification.Name(rawValue: "listMonitor:didUpdateObject:atIndexPath:"),
// object: self,
// userInfo: [
// "object": object,
// "indexPath": indexPath
// ]
// )
// }
//
//
// func listMonitor(_ monitor: ListMonitor<TestEntity1>, didMoveObject object: TestEntity1, fromIndexPath: IndexPath, toIndexPath: IndexPath) {
//
// NotificationCenter.default.post(
// name: Notification.Name(rawValue: "listMonitor:didMoveObject:fromIndexPath:toIndexPath:"),
// object: self,
// userInfo: [
// "object": object,
// "fromIndexPath": fromIndexPath,
// "toIndexPath": toIndexPath
// ]
// )
// }
//
//
// // MARK: ListSectionObserver
//
// func listMonitor(_ monitor: ListMonitor<TestEntity1>, didInsertSection sectionInfo: NSFetchedResultsSectionInfo, toSectionIndex sectionIndex: Int) {
//
// NotificationCenter.default.post(
// name: Notification.Name(rawValue: "listMonitor:didInsertSection:toSectionIndex:"),
// object: self,
// userInfo: [
// "sectionInfo": sectionInfo,
// "sectionIndex": sectionIndex
// ]
// )
// }
//
// func listMonitor(_ monitor: ListMonitor<TestEntity1>, didDeleteSection sectionInfo: NSFetchedResultsSectionInfo, fromSectionIndex sectionIndex: Int) {
//
// NotificationCenter.default.post(
// name: Notification.Name(rawValue: "listMonitor:didDeleteSection:fromSectionIndex:"),
// object: self,
// userInfo: [
// "sectionInfo": sectionInfo,
// "sectionIndex": sectionIndex
// ]
// )
// }
} }
#endif #endif
+154
View File
@@ -0,0 +1,154 @@
//
// ObjectPublisherTests.swift
// CoreStore
//
// Copyright © 2018 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: - ObjectPublisherTests
@available(macOS 10.12, *)
class ObjectPublisherTests: BaseTestDataTestCase {
@objc
dynamic func test_ThatObjectPublishers_CanReceiveUpdateNotifications() {
self.prepareStack { (stack) in
self.prepareTestDataForStack(stack)
guard let object = try stack.fetchOne(
From<TestEntity1>(),
Where<TestEntity1>(#keyPath(TestEntity1.testEntityID), isEqualTo: 101)) else {
XCTFail()
return
}
let observer = NSObject()
let objectPublisher = stack.publishObject(object)
XCTAssertEqual(objectPublisher.object, object)
XCTAssertNotNil(objectPublisher.snapshot)
let didChangeExpectation = self.expectation(description: "didChange")
objectPublisher.addObserver(observer) { objectPublisher in
XCTAssertEqual(objectPublisher.snapshot?.testNumber, NSNumber(value: 10))
XCTAssertEqual(objectPublisher.snapshot?.testString, "nil:TestEntity1:10")
didChangeExpectation.fulfill()
}
let saveExpectation = self.expectation(description: "save")
stack.perform(
asynchronous: { (transaction) -> Bool in
guard let object = transaction.edit(object) else {
XCTFail()
try transaction.cancel()
}
object.testNumber = NSNumber(value: 10)
object.testString = "nil:TestEntity1:10"
return transaction.hasChanges
},
success: { (hasChanges) in
XCTAssertTrue(hasChanges)
saveExpectation.fulfill()
},
failure: { _ in
XCTFail()
}
)
self.waitAndCheckExpectations()
withExtendedLifetime(objectPublisher, {})
withExtendedLifetime(observer, {})
}
}
@objc
dynamic func test_ThatObjectPublishers_CanReceiveDeleteNotifications() {
self.prepareStack { (stack) in
self.prepareTestDataForStack(stack)
guard let object = try stack.fetchOne(
From<TestEntity1>(),
Where<TestEntity1>(#keyPath(TestEntity1.testEntityID), isEqualTo: 101)) else {
XCTFail()
return
}
let observer = NSObject()
let objectPublisher = stack.publishObject(object)
XCTAssertEqual(objectPublisher.object, object)
XCTAssertNotNil(objectPublisher.snapshot)
let didChangeExpectation = self.expectation(description: "didChange")
objectPublisher.addObserver(observer) { objectPublisher in
XCTAssertNil(objectPublisher.object)
XCTAssertNil(objectPublisher.snapshot)
didChangeExpectation.fulfill()
}
let saveExpectation = self.expectation(description: "save")
stack.perform(
asynchronous: { (transaction) -> Bool in
guard let object = transaction.edit(object) else {
XCTFail()
try transaction.cancel()
}
transaction.delete(object)
return transaction.hasChanges
},
success: { (hasChanges) in
XCTAssertTrue(hasChanges)
saveExpectation.fulfill()
},
failure: { _ in
XCTFail()
}
)
self.waitAndCheckExpectations()
withExtendedLifetime(objectPublisher, {})
withExtendedLifetime(observer, {})
}
}
}