123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- ////////////////////////////////////////////////////////////////////////////
- //
- // Copyright 2014 Realm Inc.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- ////////////////////////////////////////////////////////////////////////////
- import XCTest
- import Realm
- import Foundation
- import RealmTestSupport
- class SwiftRLMArrayTests: RLMTestCase {
- // Swift models
- func testFastEnumeration() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- let dateMinInput = Date()
- let dateMaxInput = dateMinInput.addingTimeInterval(1000)
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- try! realm.commitWriteTransaction()
- let result = SwiftRLMAggregateObject.objects(in: realm, where: "intCol < %d", 100)
- XCTAssertEqual(result.count, UInt(10), "10 objects added")
- var totalSum = 0
- for obj in result {
- if let ao = obj as? SwiftRLMAggregateObject {
- totalSum += ao.intCol
- }
- }
- XCTAssertEqual(totalSum, 100, "total sum should be 100")
- }
- func testObjectAggregate() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- let dateMinInput = Date()
- let dateMaxInput = dateMinInput.addingTimeInterval(1000)
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = SwiftRLMAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- try! realm.commitWriteTransaction()
- let noArray = SwiftRLMAggregateObject.objects(in: realm, where: "boolCol == NO")
- let yesArray = SwiftRLMAggregateObject.objects(in: realm, where: "boolCol == YES")
- // SUM ::::::::::::::::::::::::::::::::::::::::::::::
- // Test int sum
- XCTAssertEqual(noArray.sum(ofProperty: "intCol").intValue, 4, "Sum should be 4")
- XCTAssertEqual(yesArray.sum(ofProperty: "intCol").intValue, 0, "Sum should be 0")
- // Test float sum
- XCTAssertEqual(noArray.sum(ofProperty: "floatCol").floatValue, Float(0), accuracy: 0.1, "Sum should be 0.0")
- XCTAssertEqual(yesArray.sum(ofProperty: "floatCol").floatValue, Float(7.2), accuracy: 0.1, "Sum should be 7.2")
- // Test double sum
- XCTAssertEqual(noArray.sum(ofProperty: "doubleCol").doubleValue, Double(10), accuracy: 0.1, "Sum should be 10.0")
- XCTAssertEqual(yesArray.sum(ofProperty: "doubleCol").doubleValue, Double(0), accuracy: 0.1, "Sum should be 0.0")
- // Average ::::::::::::::::::::::::::::::::::::::::::::::
- // Test int average
- XCTAssertEqual(noArray.average(ofProperty: "intCol")!.doubleValue, Double(1), accuracy: 0.1, "Average should be 1.0")
- XCTAssertEqual(yesArray.average(ofProperty: "intCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
- // Test float average
- XCTAssertEqual(noArray.average(ofProperty: "floatCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
- XCTAssertEqual(yesArray.average(ofProperty: "floatCol")!.doubleValue, Double(1.2), accuracy: 0.1, "Average should be 1.2")
- // Test double average
- XCTAssertEqual(noArray.average(ofProperty: "doubleCol")!.doubleValue, Double(2.5), accuracy: 0.1, "Average should be 2.5")
- XCTAssertEqual(yesArray.average(ofProperty: "doubleCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
- // MIN ::::::::::::::::::::::::::::::::::::::::::::::
- // Test int min
- var min = noArray.min(ofProperty: "intCol") as! NSNumber
- XCTAssertEqual(min.int32Value, Int32(1), "Minimum should be 1")
- min = yesArray.min(ofProperty: "intCol") as! NSNumber
- XCTAssertEqual(min.int32Value, Int32(0), "Minimum should be 0")
- // Test float min
- min = noArray.min(ofProperty: "floatCol") as! NSNumber
- XCTAssertEqual(min.floatValue, Float(0), accuracy: 0.1, "Minimum should be 0.0f")
- min = yesArray.min(ofProperty: "floatCol") as! NSNumber
- XCTAssertEqual(min.floatValue, Float(1.2), accuracy: 0.1, "Minimum should be 1.2f")
- // Test double min
- min = noArray.min(ofProperty: "doubleCol") as! NSNumber
- XCTAssertEqual(min.doubleValue, Double(2.5), accuracy: 0.1, "Minimum should be 1.5")
- min = yesArray.min(ofProperty: "doubleCol") as! NSNumber
- XCTAssertEqual(min.doubleValue, Double(0), accuracy: 0.1, "Minimum should be 0.0")
- // Test date min
- var dateMinOutput = noArray.min(ofProperty: "dateCol") as! Date
- XCTAssertEqual(dateMinOutput, dateMaxInput, "Minimum should be dateMaxInput")
- dateMinOutput = yesArray.min(ofProperty: "dateCol") as! Date
- XCTAssertEqual(dateMinOutput, dateMinInput, "Minimum should be dateMinInput")
- // MAX ::::::::::::::::::::::::::::::::::::::::::::::
- // Test int max
- var max = noArray.max(ofProperty: "intCol") as! NSNumber
- XCTAssertEqual(max.intValue, 1, "Maximum should be 8")
- max = yesArray.max(ofProperty: "intCol") as! NSNumber
- XCTAssertEqual(max.intValue, 0, "Maximum should be 10")
- // Test float max
- max = noArray.max(ofProperty: "floatCol") as! NSNumber
- XCTAssertEqual(max.floatValue, Float(0), accuracy: 0.1, "Maximum should be 0.0f")
- max = yesArray.max(ofProperty: "floatCol") as! NSNumber
- XCTAssertEqual(max.floatValue, Float(1.2), accuracy: 0.1, "Maximum should be 1.2f")
- // Test double max
- max = noArray.max(ofProperty: "doubleCol") as! NSNumber
- XCTAssertEqual(max.doubleValue, Double(2.5), accuracy: 0.1, "Maximum should be 3.5")
- max = yesArray.max(ofProperty: "doubleCol") as! NSNumber
- XCTAssertEqual(max.doubleValue, Double(0), accuracy: 0.1, "Maximum should be 0.0")
- // Test date max
- var dateMaxOutput = noArray.max(ofProperty: "dateCol") as! Date
- XCTAssertEqual(dateMaxOutput, dateMaxInput, "Maximum should be dateMaxInput")
- dateMaxOutput = yesArray.max(ofProperty: "dateCol") as! Date
- XCTAssertEqual(dateMaxOutput, dateMinInput, "Maximum should be dateMinInput")
- }
- func testArrayDescription() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- for _ in 0..<1012 {
- let person = SwiftRLMEmployeeObject()
- person.name = "Mary"
- person.age = 24
- person.hired = true
- realm.add(person)
- }
- try! realm.commitWriteTransaction()
- let description = SwiftRLMEmployeeObject.allObjects(in: realm).description
- XCTAssertTrue((description as NSString).range(of: "name").location != Foundation.NSNotFound, "property names should be displayed when calling \"description\" on RLMArray")
- XCTAssertTrue((description as NSString).range(of: "Mary").location != Foundation.NSNotFound, "property values should be displayed when calling \"description\" on RLMArray")
- XCTAssertTrue((description as NSString).range(of: "age").location != Foundation.NSNotFound, "property names should be displayed when calling \"description\" on RLMArray")
- XCTAssertTrue((description as NSString).range(of: "24").location != Foundation.NSNotFound, "property values should be displayed when calling \"description\" on RLMArray")
- XCTAssertTrue((description as NSString).range(of: "12 objects skipped").location != Foundation.NSNotFound, "'12 objects skipped' should be displayed when calling \"description\" on RLMArray")
- }
- func testDeleteLinksAndObjectsInArray() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- let po1 = SwiftRLMEmployeeObject()
- po1.age = 40
- po1.name = "Joe"
- po1.hired = true
- let po2 = SwiftRLMEmployeeObject()
- po2.age = 30
- po2.name = "John"
- po2.hired = false
- let po3 = SwiftRLMEmployeeObject()
- po3.age = 25
- po3.name = "Jill"
- po3.hired = true
- realm.add(po1)
- realm.add(po2)
- realm.add(po3)
- let company = SwiftRLMCompanyObject()
- realm.add(company)
- company.employees.addObjects(SwiftRLMEmployeeObject.allObjects(in: realm))
- try! realm.commitWriteTransaction()
- let peopleInCompany = company.employees
- XCTAssertEqual(peopleInCompany.count, UInt(3), "No links should have been deleted")
- realm.beginWriteTransaction()
- peopleInCompany.removeObject(at: 1) // Should delete link to employee
- try! realm.commitWriteTransaction()
- XCTAssertEqual(peopleInCompany.count, UInt(2), "link deleted when accessing via links")
- var test = peopleInCompany[0]
- XCTAssertEqual(test.age, po1.age, "Should be equal")
- XCTAssertEqual(test.name, po1.name, "Should be equal")
- XCTAssertEqual(test.hired, po1.hired, "Should be equal")
- // XCTAssertEqual(test, po1, "Should be equal") //FIXME, should work. Asana : https://app.asana.com/0/861870036984/13123030433568
- test = peopleInCompany[1]
- XCTAssertEqual(test.age, po3.age, "Should be equal")
- XCTAssertEqual(test.name, po3.name, "Should be equal")
- XCTAssertEqual(test.hired, po3.hired, "Should be equal")
- // XCTAssertEqual(test, po3, "Should be equal") //FIXME, should work. Asana : https://app.asana.com/0/861870036984/13123030433568
- realm.beginWriteTransaction()
- peopleInCompany.removeLastObject()
- XCTAssertEqual(peopleInCompany.count, UInt(1), "1 remaining link")
- peopleInCompany.replaceObject(at: 0, with: po2)
- XCTAssertEqual(peopleInCompany.count, UInt(1), "1 link replaced")
- peopleInCompany.insert(po1, at: 0)
- XCTAssertEqual(peopleInCompany.count, UInt(2), "2 links")
- peopleInCompany.removeAllObjects()
- XCTAssertEqual(peopleInCompany.count, UInt(0), "0 remaining links")
- try! realm.commitWriteTransaction()
- }
- // Objective-C models
- func testFastEnumeration_objc() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- let dateMinInput = Date()
- let dateMaxInput = dateMinInput.addingTimeInterval(1000)
- _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = AggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = AggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = AggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = AggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
- try! realm.commitWriteTransaction()
- let result = AggregateObject.objects(in: realm, where: "intCol < %d", 100)
- XCTAssertEqual(result.count, UInt(10), "10 objects added")
- var totalSum: CInt = 0
- for obj in result {
- if let ao = obj as? AggregateObject {
- totalSum += ao.intCol
- }
- }
- XCTAssertEqual(totalSum, CInt(100), "total sum should be 100")
- }
- func testObjectAggregate_objc() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- let dateMinInput = Date()
- let dateMaxInput = dateMinInput.addingTimeInterval(1000)
- _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = AggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = AggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = AggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = AggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
- _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
- try! realm.commitWriteTransaction()
- let noArray = AggregateObject.objects(in: realm, where: "boolCol == NO")
- let yesArray = AggregateObject.objects(in: realm, where: "boolCol == YES")
- // SUM ::::::::::::::::::::::::::::::::::::::::::::::
- // Test int sum
- XCTAssertEqual(noArray.sum(ofProperty: "intCol").intValue, 4, "Sum should be 4")
- XCTAssertEqual(yesArray.sum(ofProperty: "intCol").intValue, 0, "Sum should be 0")
- // Test float sum
- XCTAssertEqual(noArray.sum(ofProperty: "floatCol").floatValue, Float(0), accuracy: 0.1, "Sum should be 0.0")
- XCTAssertEqual(yesArray.sum(ofProperty: "floatCol").floatValue, Float(7.2), accuracy: 0.1, "Sum should be 7.2")
- // Test double sum
- XCTAssertEqual(noArray.sum(ofProperty: "doubleCol").doubleValue, Double(10), accuracy: 0.1, "Sum should be 10.0")
- XCTAssertEqual(yesArray.sum(ofProperty: "doubleCol").doubleValue, Double(0), accuracy: 0.1, "Sum should be 0.0")
- // Average ::::::::::::::::::::::::::::::::::::::::::::::
- // Test int average
- XCTAssertEqual(noArray.average(ofProperty: "intCol")!.doubleValue, Double(1), accuracy: 0.1, "Average should be 1.0")
- XCTAssertEqual(yesArray.average(ofProperty: "intCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
- // Test float average
- XCTAssertEqual(noArray.average(ofProperty: "floatCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
- XCTAssertEqual(yesArray.average(ofProperty: "floatCol")!.doubleValue, Double(1.2), accuracy: 0.1, "Average should be 1.2")
- // Test double average
- XCTAssertEqual(noArray.average(ofProperty: "doubleCol")!.doubleValue, Double(2.5), accuracy: 0.1, "Average should be 2.5")
- XCTAssertEqual(yesArray.average(ofProperty: "doubleCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
- // MIN ::::::::::::::::::::::::::::::::::::::::::::::
- // Test int min
- var min = noArray.min(ofProperty: "intCol") as! NSNumber
- XCTAssertEqual(min.int32Value, Int32(1), "Minimum should be 1")
- min = yesArray.min(ofProperty: "intCol") as! NSNumber
- XCTAssertEqual(min.int32Value, Int32(0), "Minimum should be 0")
- // Test float min
- min = noArray.min(ofProperty: "floatCol") as! NSNumber
- XCTAssertEqual(min.floatValue, Float(0), accuracy: 0.1, "Minimum should be 0.0f")
- min = yesArray.min(ofProperty: "floatCol") as! NSNumber
- XCTAssertEqual(min.floatValue, Float(1.2), accuracy: 0.1, "Minimum should be 1.2f")
- // Test double min
- min = noArray.min(ofProperty: "doubleCol") as! NSNumber
- XCTAssertEqual(min.doubleValue, Double(2.5), accuracy: 0.1, "Minimum should be 1.5")
- min = yesArray.min(ofProperty: "doubleCol") as! NSNumber
- XCTAssertEqual(min.doubleValue, Double(0), accuracy: 0.1, "Minimum should be 0.0")
- // Test date min
- var dateMinOutput = noArray.min(ofProperty: "dateCol") as! Date
- XCTAssertEqual(dateMinOutput, dateMaxInput, "Minimum should be dateMaxInput")
- dateMinOutput = yesArray.min(ofProperty: "dateCol") as! Date
- XCTAssertEqual(dateMinOutput, dateMinInput, "Minimum should be dateMinInput")
- // MAX ::::::::::::::::::::::::::::::::::::::::::::::
- // Test int max
- var max = noArray.max(ofProperty: "intCol") as! NSNumber
- XCTAssertEqual(max.intValue, 1, "Maximum should be 8")
- max = yesArray.max(ofProperty: "intCol") as! NSNumber
- XCTAssertEqual(max.intValue, 0, "Maximum should be 10")
- // Test float max
- max = noArray.max(ofProperty: "floatCol") as! NSNumber
- XCTAssertEqual(max.floatValue, Float(0), accuracy: 0.1, "Maximum should be 0.0f")
- max = yesArray.max(ofProperty: "floatCol") as! NSNumber
- XCTAssertEqual(max.floatValue, Float(1.2), accuracy: 0.1, "Maximum should be 1.2f")
- // Test double max
- max = noArray.max(ofProperty: "doubleCol") as! NSNumber
- XCTAssertEqual(max.doubleValue, Double(2.5), accuracy: 0.1, "Maximum should be 3.5")
- max = yesArray.max(ofProperty: "doubleCol") as! NSNumber
- XCTAssertEqual(max.doubleValue, Double(0), accuracy: 0.1, "Maximum should be 0.0")
- // Test date max
- var dateMaxOutput = noArray.max(ofProperty: "dateCol") as! Date
- XCTAssertEqual(dateMaxOutput, dateMaxInput, "Maximum should be dateMaxInput")
- dateMaxOutput = yesArray.max(ofProperty: "dateCol") as! Date
- XCTAssertEqual(dateMaxOutput, dateMinInput, "Maximum should be dateMinInput")
- }
- func testArrayDescription_objc() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- for _ in 0..<1012 {
- let person = EmployeeObject()
- person.name = "Mary"
- person.age = 24
- person.hired = true
- realm.add(person)
- }
- try! realm.commitWriteTransaction()
- let description = EmployeeObject.allObjects(in: realm).description
- XCTAssertTrue((description as NSString).range(of: "name").location != Foundation.NSNotFound, "property names should be displayed when calling \"description\" on RLMArray")
- XCTAssertTrue((description as NSString).range(of: "Mary").location != Foundation.NSNotFound, "property values should be displayed when calling \"description\" on RLMArray")
- XCTAssertTrue((description as NSString).range(of: "age").location != Foundation.NSNotFound, "property names should be displayed when calling \"description\" on RLMArray")
- XCTAssertTrue((description as NSString).range(of: "24").location != Foundation.NSNotFound, "property values should be displayed when calling \"description\" on RLMArray")
- XCTAssertTrue((description as NSString).range(of: "912 objects skipped").location != Foundation.NSNotFound, "'912 objects skipped' should be displayed when calling \"description\" on RLMArray")
- }
- func makeEmployee(_ realm: RLMRealm, _ age: Int32, _ name: String, _ hired: Bool) -> EmployeeObject {
- let employee = EmployeeObject()
- employee.age = age
- employee.name = name
- employee.hired = hired
- realm.add(employee)
- return employee
- }
- func testDeleteLinksAndObjectsInArray_objc() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- let po1 = makeEmployee(realm, 40, "Joe", true)
- _ = makeEmployee(realm, 30, "John", false)
- let po3 = makeEmployee(realm, 25, "Jill", true)
- let company = CompanyObject()
- company.name = "name"
- realm.add(company)
- company.employees.addObjects(EmployeeObject.allObjects(in: realm))
- try! realm.commitWriteTransaction()
- let peopleInCompany: RLMArray<EmployeeObject> = company.employees!
- XCTAssertEqual(peopleInCompany.count, UInt(3), "No links should have been deleted")
- realm.beginWriteTransaction()
- peopleInCompany.removeObject(at: 1) // Should delete link to employee
- try! realm.commitWriteTransaction()
- XCTAssertEqual(peopleInCompany.count, UInt(2), "link deleted when accessing via links")
- var test = peopleInCompany[0]
- XCTAssertEqual(test.age, po1.age, "Should be equal")
- XCTAssertEqual(test.name!, po1.name!, "Should be equal")
- XCTAssertEqual(test.hired, po1.hired, "Should be equal")
- // XCTAssertEqual(test, po1, "Should be equal") //FIXME, should work. Asana : https://app.asana.com/0/861870036984/13123030433568
- test = peopleInCompany[1]
- XCTAssertEqual(test.age, po3.age, "Should be equal")
- XCTAssertEqual(test.name!, po3.name!, "Should be equal")
- XCTAssertEqual(test.hired, po3.hired, "Should be equal")
- // XCTAssertEqual(test, po3, "Should be equal") //FIXME, should work. Asana : https://app.asana.com/0/861870036984/13123030433568
- let allPeople = EmployeeObject.allObjects(in: realm)
- XCTAssertEqual(allPeople.count, UInt(3), "Only links should have been deleted, not the employees")
- }
- func testIndexOfObject_objc() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- let po1 = makeEmployee(realm, 40, "Joe", true)
- let po2 = makeEmployee(realm, 30, "John", false)
- let po3 = makeEmployee(realm, 25, "Jill", true)
- try! realm.commitWriteTransaction()
- let results = EmployeeObject.objects(in: realm, where: "hired = YES")
- XCTAssertEqual(UInt(2), results.count)
- XCTAssertEqual(UInt(0), results.index(of: po1));
- XCTAssertEqual(UInt(1), results.index(of: po3));
- XCTAssertEqual(NSNotFound, Int(results.index(of: po2)));
- }
- func testIndexOfObjectWhere_objc() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- _ = makeEmployee(realm, 40, "Joe", true)
- _ = makeEmployee(realm, 30, "John", false)
- _ = makeEmployee(realm, 25, "Jill", true)
- try! realm.commitWriteTransaction()
- let results = EmployeeObject.objects(in: realm, where: "hired = YES")
- XCTAssertEqual(UInt(2), results.count)
- XCTAssertEqual(UInt(0), results.indexOfObject(where: "age = %d", 40))
- XCTAssertEqual(UInt(1), results.indexOfObject(where: "age = %d", 25))
- XCTAssertEqual(NSNotFound, Int(results.indexOfObject(where: "age = %d", 30)))
- }
- func testSortingExistingQuery_objc() {
- let realm = realmWithTestPath()
- realm.beginWriteTransaction()
- _ = makeEmployee(realm, 20, "A", true)
- _ = makeEmployee(realm, 30, "B", false)
- _ = makeEmployee(realm, 40, "C", true)
- try! realm.commitWriteTransaction()
- let sortedByAge = EmployeeObject.allObjects(in: realm).sortedResults(usingKeyPath: "age", ascending: true)
- let sortedByName = sortedByAge.sortedResults(usingKeyPath: "name", ascending: false)
- XCTAssertEqual(Int32(20), (sortedByAge[0] as! EmployeeObject).age)
- XCTAssertEqual(Int32(40), (sortedByName[0] as! EmployeeObject).age)
- }
- }
|