forked from jessesquires/JSQCoreDataKit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResetStackTests.swift
More file actions
115 lines (94 loc) · 3.73 KB
/
Copy pathResetStackTests.swift
File metadata and controls
115 lines (94 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// Created by Jesse Squires
// http://www.jessesquires.com
//
//
// Documentation
// http://www.jessesquires.com/JSQCoreDataKit
//
//
// GitHub
// https://github.com/jessesquires/JSQCoreDataKit
//
//
// License
// Copyright © 2015 Jesse Squires
// Released under an MIT license: http://opensource.org/licenses/MIT
//
import XCTest
import CoreData
import ExampleModel
@testable
import JSQCoreDataKit
final class ResetStackTests: TestCase {
func test_ThatMainContext_WithChanges_DoesNotHaveObjects_AfterReset() {
// GIVEN: a stack and context with changes
let context = inMemoryStack.mainContext
context.performAndWait {
self.generateCompaniesInContext(context, count: 3)
}
let expectation = self.expectation(description: #function)
// WHEN: we attempt to reset the stack
inMemoryStack.reset() { (result: StackResult) in
if case .failure(let e) = result {
XCTFail("Error while resetting the stack: \(e)")
}
expectation.fulfill()
}
// THEN: the reset succeeds and the contexts contain no objects
waitForExpectations(timeout: defaultTimeout) { (error) -> Void in
XCTAssertNil(error, "Expectation should not error")
}
XCTAssertEqual(inMemoryStack.mainContext.registeredObjects.count, 0)
XCTAssertEqual(inMemoryStack.backgroundContext.registeredObjects.count, 0)
}
func test_ThatBackgroundContext_WithChanges_DoesNotHaveObjects_AfterReset() {
// GIVEN: a stack and context with changes
let context = inMemoryStack.backgroundContext
context.performAndWait {
self.generateCompaniesInContext(context, count: 3)
}
let expectation = self.expectation(description: #function)
// WHEN: we attempt to reset the stack
inMemoryStack.reset() { (result: StackResult) in
if case .failure(let e) = result {
XCTFail("Error while resetting the stack: \(e)")
}
expectation.fulfill()
}
// THEN: the reset succeeds and the contexts contain no objects
waitForExpectations(timeout: defaultTimeout) { (error) -> Void in
XCTAssertNil(error, "Expectation should not error")
}
XCTAssertEqual(inMemoryStack.mainContext.registeredObjects.count, 0)
XCTAssertEqual(inMemoryStack.backgroundContext.registeredObjects.count, 0)
}
func test_ThatPersistentStore_WithChanges_DoesNotHaveObjects_AfterReset() {
// GIVEN: a stack and persistent store with data
let model = CoreDataModel(name: modelName, bundle: modelBundle)
let factory = CoreDataStackFactory(model: model)
let stack = factory.createStack().stack()!
let context = stack.mainContext
context.performAndWait {
self.generateCompaniesInContext(context, count: 3)
}
saveContext(context)
let request = Company.fetchRequest
let objectsBefore = try? context.count(for: request)
XCTAssertEqual(objectsBefore, 3)
let expectation = self.expectation(description: #function)
// WHEN: we attempt to reset the stack
stack.reset() { (result: StackResult) in
if case .failure(let e) = result {
XCTFail("Error while resetting the stack: \(e)")
}
expectation.fulfill()
}
// THEN: the reset succeeds and the stack contains no managed objects
waitForExpectations(timeout: defaultTimeout) { (error) -> Void in
XCTAssertNil(error, "Expectation should not error")
}
let objectsAfter = try? stack.mainContext.count(for: request)
XCTAssertEqual(objectsAfter, 0)
}
}