Skip to content

Commit 34b1481

Browse files
committed
Add tests for queryset operations
1 parent e372b2e commit 34b1481

File tree

2 files changed

+92
-33
lines changed

2 files changed

+92
-33
lines changed

RxQueryKit/RxQueryKit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ extension QuerySet {
3232

3333
if let predicate = self.predicate {
3434
count += (insertedObjects as NSArray).filteredArrayUsingPredicate(predicate).count
35-
count += (deletedObjects as NSArray).filteredArrayUsingPredicate(predicate).count
35+
count -= (deletedObjects as NSArray).filteredArrayUsingPredicate(predicate).count
3636
} else {
37-
count -= insertedObjects.count
37+
count += insertedObjects.count
3838
count -= deletedObjects.count
3939
}
4040

Lines changed: 90 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,95 @@
1-
//
2-
// RxQueryKitTests.swift
3-
// RxQueryKitTests
4-
//
5-
// Created by Kyle Fuller on 05/09/2015.
6-
// Copyright © 2015 Cocode. All rights reserved.
7-
//
8-
91
import XCTest
10-
@testable import RxQueryKit
2+
import CoreData
3+
import QueryKit
4+
import RxQueryKit
5+
116

127
class RxQueryKitTests: XCTestCase {
13-
14-
override func setUp() {
15-
super.setUp()
16-
// Put setup code here. This method is called before the invocation of each test method in the class.
17-
}
18-
19-
override func tearDown() {
20-
// Put teardown code here. This method is called after the invocation of each test method in the class.
21-
super.tearDown()
22-
}
23-
24-
func testExample() {
25-
// This is an example of a functional test case.
26-
// Use XCTAssert and related functions to verify your tests produce the correct results.
27-
}
28-
29-
func testPerformanceExample() {
30-
// This is an example of a performance test case.
31-
self.measureBlock {
32-
// Put the code you want to measure the time of here.
33-
}
8+
var context: NSManagedObjectContext!
9+
10+
override func setUp() {
11+
let model = NSManagedObjectModel()
12+
model.entities = [Person.createEntityDescription(), Comment.createEntityDescription()]
13+
let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
14+
try! persistentStoreCoordinator.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil)
15+
context = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
16+
context.persistentStoreCoordinator = persistentStoreCoordinator
17+
}
18+
19+
// Make sure we've configure our store and model correctly
20+
func testCoreData() {
21+
let person = Person.create(context, name: "kyle")
22+
XCTAssertEqual(person.name, "kyle")
23+
}
24+
25+
func testCount() {
26+
var counts: [Int] = []
27+
let queryset = Person.queryset(context)
28+
let disposable = try! queryset.count().subscribeNext {
29+
counts.append($0)
3430
}
35-
31+
32+
// Initial value
33+
XCTAssertEqual(counts, [0])
34+
35+
// Created
36+
let p1 = Person.create(context, name: "kyle1")
37+
Person.create(context, name: "kyle2")
38+
let p3 = Person.create(context, name: "kyle3")
39+
try! context.save()
40+
XCTAssertEqual(counts, [0, 3])
41+
42+
// Deleted
43+
context.deleteObject(p1)
44+
context.deleteObject(p3)
45+
try! context.save()
46+
XCTAssertEqual(counts, [0, 3, 1])
47+
48+
disposable.dispose()
49+
}
3650
}
51+
52+
53+
@objc(Person) class Person : NSManagedObject {
54+
class func createEntityDescription() -> NSEntityDescription {
55+
let name = NSAttributeDescription()
56+
name.name = "name"
57+
name.attributeType = .StringAttributeType
58+
name.optional = false
59+
60+
let entity = NSEntityDescription()
61+
entity.name = "Person"
62+
entity.managedObjectClassName = "Person"
63+
entity.properties = [name]
64+
return entity
65+
}
66+
67+
class func queryset(context: NSManagedObjectContext) -> QuerySet<Person> {
68+
return QuerySet(context, "Person")
69+
}
70+
71+
class func create(context: NSManagedObjectContext, name: String) -> Person {
72+
let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: context)!
73+
let person = Person(entity: entity, insertIntoManagedObjectContext: context)
74+
person.name = name
75+
return person
76+
}
77+
78+
@NSManaged var name: String
79+
}
80+
81+
@objc(Comment) class Comment : NSManagedObject {
82+
class func createEntityDescription() -> NSEntityDescription {
83+
let text = NSAttributeDescription()
84+
text.name = "text"
85+
text.attributeType = .StringAttributeType
86+
text.optional = false
87+
88+
let entity = NSEntityDescription()
89+
entity.name = "Comment"
90+
entity.managedObjectClassName = "Comment"
91+
return entity
92+
}
93+
94+
@NSManaged var text: String
95+
}

0 commit comments

Comments
 (0)