SwiftArrayTests.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import XCTest
  19. import Realm
  20. import Foundation
  21. class SwiftArrayTests: RLMTestCase {
  22. // Swift models
  23. func testFastEnumeration() {
  24. let realm = realmWithTestPath()
  25. realm.beginWriteTransaction()
  26. let dateMinInput = Date()
  27. let dateMaxInput = dateMinInput.addingTimeInterval(1000)
  28. _ = SwiftAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  29. _ = SwiftAggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
  30. _ = SwiftAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  31. _ = SwiftAggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
  32. _ = SwiftAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  33. _ = SwiftAggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
  34. _ = SwiftAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  35. _ = SwiftAggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
  36. _ = SwiftAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  37. _ = SwiftAggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  38. try! realm.commitWriteTransaction()
  39. let result = SwiftAggregateObject.objects(in: realm, where: "intCol < %d", 100)
  40. XCTAssertEqual(result.count, UInt(10), "10 objects added")
  41. var totalSum = 0
  42. for obj in result {
  43. if let ao = obj as? SwiftAggregateObject {
  44. totalSum += ao.intCol
  45. }
  46. }
  47. XCTAssertEqual(totalSum, 100, "total sum should be 100")
  48. }
  49. func testObjectAggregate() {
  50. let realm = realmWithTestPath()
  51. realm.beginWriteTransaction()
  52. let dateMinInput = Date()
  53. let dateMaxInput = dateMinInput.addingTimeInterval(1000)
  54. _ = SwiftAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  55. _ = SwiftAggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
  56. _ = SwiftAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  57. _ = SwiftAggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
  58. _ = SwiftAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  59. _ = SwiftAggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
  60. _ = SwiftAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  61. _ = SwiftAggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
  62. _ = SwiftAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  63. _ = SwiftAggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  64. try! realm.commitWriteTransaction()
  65. let noArray = SwiftAggregateObject.objects(in: realm, where: "boolCol == NO")
  66. let yesArray = SwiftAggregateObject.objects(in: realm, where: "boolCol == YES")
  67. // SUM ::::::::::::::::::::::::::::::::::::::::::::::
  68. // Test int sum
  69. XCTAssertEqual(noArray.sum(ofProperty: "intCol").intValue, 4, "Sum should be 4")
  70. XCTAssertEqual(yesArray.sum(ofProperty: "intCol").intValue, 0, "Sum should be 0")
  71. // Test float sum
  72. XCTAssertEqual(noArray.sum(ofProperty: "floatCol").floatValue, Float(0), accuracy: 0.1, "Sum should be 0.0")
  73. XCTAssertEqual(yesArray.sum(ofProperty: "floatCol").floatValue, Float(7.2), accuracy: 0.1, "Sum should be 7.2")
  74. // Test double sum
  75. XCTAssertEqual(noArray.sum(ofProperty: "doubleCol").doubleValue, Double(10), accuracy: 0.1, "Sum should be 10.0")
  76. XCTAssertEqual(yesArray.sum(ofProperty: "doubleCol").doubleValue, Double(0), accuracy: 0.1, "Sum should be 0.0")
  77. // Average ::::::::::::::::::::::::::::::::::::::::::::::
  78. // Test int average
  79. XCTAssertEqual(noArray.average(ofProperty: "intCol")!.doubleValue, Double(1), accuracy: 0.1, "Average should be 1.0")
  80. XCTAssertEqual(yesArray.average(ofProperty: "intCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
  81. // Test float average
  82. XCTAssertEqual(noArray.average(ofProperty: "floatCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
  83. XCTAssertEqual(yesArray.average(ofProperty: "floatCol")!.doubleValue, Double(1.2), accuracy: 0.1, "Average should be 1.2")
  84. // Test double average
  85. XCTAssertEqual(noArray.average(ofProperty: "doubleCol")!.doubleValue, Double(2.5), accuracy: 0.1, "Average should be 2.5")
  86. XCTAssertEqual(yesArray.average(ofProperty: "doubleCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
  87. // MIN ::::::::::::::::::::::::::::::::::::::::::::::
  88. // Test int min
  89. var min = noArray.min(ofProperty: "intCol") as! NSNumber
  90. XCTAssertEqual(min.int32Value, Int32(1), "Minimum should be 1")
  91. min = yesArray.min(ofProperty: "intCol") as! NSNumber
  92. XCTAssertEqual(min.int32Value, Int32(0), "Minimum should be 0")
  93. // Test float min
  94. min = noArray.min(ofProperty: "floatCol") as! NSNumber
  95. XCTAssertEqual(min.floatValue, Float(0), accuracy: 0.1, "Minimum should be 0.0f")
  96. min = yesArray.min(ofProperty: "floatCol") as! NSNumber
  97. XCTAssertEqual(min.floatValue, Float(1.2), accuracy: 0.1, "Minimum should be 1.2f")
  98. // Test double min
  99. min = noArray.min(ofProperty: "doubleCol") as! NSNumber
  100. XCTAssertEqual(min.doubleValue, Double(2.5), accuracy: 0.1, "Minimum should be 1.5")
  101. min = yesArray.min(ofProperty: "doubleCol") as! NSNumber
  102. XCTAssertEqual(min.doubleValue, Double(0), accuracy: 0.1, "Minimum should be 0.0")
  103. // Test date min
  104. var dateMinOutput = noArray.min(ofProperty: "dateCol") as! Date
  105. XCTAssertEqual(dateMinOutput, dateMaxInput, "Minimum should be dateMaxInput")
  106. dateMinOutput = yesArray.min(ofProperty: "dateCol") as! Date
  107. XCTAssertEqual(dateMinOutput, dateMinInput, "Minimum should be dateMinInput")
  108. // MAX ::::::::::::::::::::::::::::::::::::::::::::::
  109. // Test int max
  110. var max = noArray.max(ofProperty: "intCol") as! NSNumber
  111. XCTAssertEqual(max.intValue, 1, "Maximum should be 8")
  112. max = yesArray.max(ofProperty: "intCol") as! NSNumber
  113. XCTAssertEqual(max.intValue, 0, "Maximum should be 10")
  114. // Test float max
  115. max = noArray.max(ofProperty: "floatCol") as! NSNumber
  116. XCTAssertEqual(max.floatValue, Float(0), accuracy: 0.1, "Maximum should be 0.0f")
  117. max = yesArray.max(ofProperty: "floatCol") as! NSNumber
  118. XCTAssertEqual(max.floatValue, Float(1.2), accuracy: 0.1, "Maximum should be 1.2f")
  119. // Test double max
  120. max = noArray.max(ofProperty: "doubleCol") as! NSNumber
  121. XCTAssertEqual(max.doubleValue, Double(2.5), accuracy: 0.1, "Maximum should be 3.5")
  122. max = yesArray.max(ofProperty: "doubleCol") as! NSNumber
  123. XCTAssertEqual(max.doubleValue, Double(0), accuracy: 0.1, "Maximum should be 0.0")
  124. // Test date max
  125. var dateMaxOutput = noArray.max(ofProperty: "dateCol") as! Date
  126. XCTAssertEqual(dateMaxOutput, dateMaxInput, "Maximum should be dateMaxInput")
  127. dateMaxOutput = yesArray.max(ofProperty: "dateCol") as! Date
  128. XCTAssertEqual(dateMaxOutput, dateMinInput, "Maximum should be dateMinInput")
  129. }
  130. func testArrayDescription() {
  131. let realm = realmWithTestPath()
  132. realm.beginWriteTransaction()
  133. for _ in 0..<1012 {
  134. let person = SwiftEmployeeObject()
  135. person.name = "Mary"
  136. person.age = 24
  137. person.hired = true
  138. realm.add(person)
  139. }
  140. try! realm.commitWriteTransaction()
  141. let description = SwiftEmployeeObject.allObjects(in: realm).description
  142. XCTAssertTrue((description as NSString).range(of: "name").location != Foundation.NSNotFound, "property names should be displayed when calling \"description\" on RLMArray")
  143. XCTAssertTrue((description as NSString).range(of: "Mary").location != Foundation.NSNotFound, "property values should be displayed when calling \"description\" on RLMArray")
  144. XCTAssertTrue((description as NSString).range(of: "age").location != Foundation.NSNotFound, "property names should be displayed when calling \"description\" on RLMArray")
  145. XCTAssertTrue((description as NSString).range(of: "24").location != Foundation.NSNotFound, "property values should be displayed when calling \"description\" on RLMArray")
  146. XCTAssertTrue((description as NSString).range(of: "12 objects skipped").location != Foundation.NSNotFound, "'12 objects skipped' should be displayed when calling \"description\" on RLMArray")
  147. }
  148. func testDeleteLinksAndObjectsInArray() {
  149. let realm = realmWithTestPath()
  150. realm.beginWriteTransaction()
  151. let po1 = SwiftEmployeeObject()
  152. po1.age = 40
  153. po1.name = "Joe"
  154. po1.hired = true
  155. let po2 = SwiftEmployeeObject()
  156. po2.age = 30
  157. po2.name = "John"
  158. po2.hired = false
  159. let po3 = SwiftEmployeeObject()
  160. po3.age = 25
  161. po3.name = "Jill"
  162. po3.hired = true
  163. realm.add(po1)
  164. realm.add(po2)
  165. realm.add(po3)
  166. let company = SwiftCompanyObject()
  167. realm.add(company)
  168. company.employees.addObjects(SwiftEmployeeObject.allObjects(in: realm))
  169. try! realm.commitWriteTransaction()
  170. let peopleInCompany = company.employees
  171. XCTAssertEqual(peopleInCompany.count, UInt(3), "No links should have been deleted")
  172. realm.beginWriteTransaction()
  173. peopleInCompany.removeObject(at: 1) // Should delete link to employee
  174. try! realm.commitWriteTransaction()
  175. XCTAssertEqual(peopleInCompany.count, UInt(2), "link deleted when accessing via links")
  176. var test = peopleInCompany[0]
  177. XCTAssertEqual(test.age, po1.age, "Should be equal")
  178. XCTAssertEqual(test.name, po1.name, "Should be equal")
  179. XCTAssertEqual(test.hired, po1.hired, "Should be equal")
  180. // XCTAssertEqual(test, po1, "Should be equal") //FIXME, should work. Asana : https://app.asana.com/0/861870036984/13123030433568
  181. test = peopleInCompany[1]
  182. XCTAssertEqual(test.age, po3.age, "Should be equal")
  183. XCTAssertEqual(test.name, po3.name, "Should be equal")
  184. XCTAssertEqual(test.hired, po3.hired, "Should be equal")
  185. // XCTAssertEqual(test, po3, "Should be equal") //FIXME, should work. Asana : https://app.asana.com/0/861870036984/13123030433568
  186. realm.beginWriteTransaction()
  187. peopleInCompany.removeLastObject()
  188. XCTAssertEqual(peopleInCompany.count, UInt(1), "1 remaining link")
  189. peopleInCompany.replaceObject(at: 0, with: po2)
  190. XCTAssertEqual(peopleInCompany.count, UInt(1), "1 link replaced")
  191. peopleInCompany.insert(po1, at: 0)
  192. XCTAssertEqual(peopleInCompany.count, UInt(2), "2 links")
  193. peopleInCompany.removeAllObjects()
  194. XCTAssertEqual(peopleInCompany.count, UInt(0), "0 remaining links")
  195. try! realm.commitWriteTransaction()
  196. }
  197. // Objective-C models
  198. func testFastEnumeration_objc() {
  199. let realm = realmWithTestPath()
  200. realm.beginWriteTransaction()
  201. let dateMinInput = Date()
  202. let dateMaxInput = dateMinInput.addingTimeInterval(1000)
  203. _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  204. _ = AggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
  205. _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  206. _ = AggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
  207. _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  208. _ = AggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
  209. _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  210. _ = AggregateObject.create(in: realm, withValue: [10, 0 as Float, 2.5 as Double, false, dateMaxInput])
  211. _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  212. _ = AggregateObject.create(in: realm, withValue: [10, 1.2 as Float, 0 as Double, true, dateMinInput])
  213. try! realm.commitWriteTransaction()
  214. let result = AggregateObject.objects(in: realm, where: "intCol < %d", 100)
  215. XCTAssertEqual(result.count, UInt(10), "10 objects added")
  216. var totalSum: CInt = 0
  217. for obj in result {
  218. if let ao = obj as? AggregateObject {
  219. totalSum += ao.intCol
  220. }
  221. }
  222. XCTAssertEqual(totalSum, CInt(100), "total sum should be 100")
  223. }
  224. func testObjectAggregate_objc() {
  225. let realm = realmWithTestPath()
  226. realm.beginWriteTransaction()
  227. let dateMinInput = Date()
  228. let dateMaxInput = dateMinInput.addingTimeInterval(1000)
  229. _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  230. _ = AggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
  231. _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  232. _ = AggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
  233. _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  234. _ = AggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
  235. _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  236. _ = AggregateObject.create(in: realm, withValue: [1, 0 as Float, 2.5 as Double, false, dateMaxInput])
  237. _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  238. _ = AggregateObject.create(in: realm, withValue: [0, 1.2 as Float, 0 as Double, true, dateMinInput])
  239. try! realm.commitWriteTransaction()
  240. let noArray = AggregateObject.objects(in: realm, where: "boolCol == NO")
  241. let yesArray = AggregateObject.objects(in: realm, where: "boolCol == YES")
  242. // SUM ::::::::::::::::::::::::::::::::::::::::::::::
  243. // Test int sum
  244. XCTAssertEqual(noArray.sum(ofProperty: "intCol").intValue, 4, "Sum should be 4")
  245. XCTAssertEqual(yesArray.sum(ofProperty: "intCol").intValue, 0, "Sum should be 0")
  246. // Test float sum
  247. XCTAssertEqual(noArray.sum(ofProperty: "floatCol").floatValue, Float(0), accuracy: 0.1, "Sum should be 0.0")
  248. XCTAssertEqual(yesArray.sum(ofProperty: "floatCol").floatValue, Float(7.2), accuracy: 0.1, "Sum should be 7.2")
  249. // Test double sum
  250. XCTAssertEqual(noArray.sum(ofProperty: "doubleCol").doubleValue, Double(10), accuracy: 0.1, "Sum should be 10.0")
  251. XCTAssertEqual(yesArray.sum(ofProperty: "doubleCol").doubleValue, Double(0), accuracy: 0.1, "Sum should be 0.0")
  252. // Average ::::::::::::::::::::::::::::::::::::::::::::::
  253. // Test int average
  254. XCTAssertEqual(noArray.average(ofProperty: "intCol")!.doubleValue, Double(1), accuracy: 0.1, "Average should be 1.0")
  255. XCTAssertEqual(yesArray.average(ofProperty: "intCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
  256. // Test float average
  257. XCTAssertEqual(noArray.average(ofProperty: "floatCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
  258. XCTAssertEqual(yesArray.average(ofProperty: "floatCol")!.doubleValue, Double(1.2), accuracy: 0.1, "Average should be 1.2")
  259. // Test double average
  260. XCTAssertEqual(noArray.average(ofProperty: "doubleCol")!.doubleValue, Double(2.5), accuracy: 0.1, "Average should be 2.5")
  261. XCTAssertEqual(yesArray.average(ofProperty: "doubleCol")!.doubleValue, Double(0), accuracy: 0.1, "Average should be 0.0")
  262. // MIN ::::::::::::::::::::::::::::::::::::::::::::::
  263. // Test int min
  264. var min = noArray.min(ofProperty: "intCol") as! NSNumber
  265. XCTAssertEqual(min.int32Value, Int32(1), "Minimum should be 1")
  266. min = yesArray.min(ofProperty: "intCol") as! NSNumber
  267. XCTAssertEqual(min.int32Value, Int32(0), "Minimum should be 0")
  268. // Test float min
  269. min = noArray.min(ofProperty: "floatCol") as! NSNumber
  270. XCTAssertEqual(min.floatValue, Float(0), accuracy: 0.1, "Minimum should be 0.0f")
  271. min = yesArray.min(ofProperty: "floatCol") as! NSNumber
  272. XCTAssertEqual(min.floatValue, Float(1.2), accuracy: 0.1, "Minimum should be 1.2f")
  273. // Test double min
  274. min = noArray.min(ofProperty: "doubleCol") as! NSNumber
  275. XCTAssertEqual(min.doubleValue, Double(2.5), accuracy: 0.1, "Minimum should be 1.5")
  276. min = yesArray.min(ofProperty: "doubleCol") as! NSNumber
  277. XCTAssertEqual(min.doubleValue, Double(0), accuracy: 0.1, "Minimum should be 0.0")
  278. // Test date min
  279. var dateMinOutput = noArray.min(ofProperty: "dateCol") as! Date
  280. XCTAssertEqual(dateMinOutput, dateMaxInput, "Minimum should be dateMaxInput")
  281. dateMinOutput = yesArray.min(ofProperty: "dateCol") as! Date
  282. XCTAssertEqual(dateMinOutput, dateMinInput, "Minimum should be dateMinInput")
  283. // MAX ::::::::::::::::::::::::::::::::::::::::::::::
  284. // Test int max
  285. var max = noArray.max(ofProperty: "intCol") as! NSNumber
  286. XCTAssertEqual(max.intValue, 1, "Maximum should be 8")
  287. max = yesArray.max(ofProperty: "intCol") as! NSNumber
  288. XCTAssertEqual(max.intValue, 0, "Maximum should be 10")
  289. // Test float max
  290. max = noArray.max(ofProperty: "floatCol") as! NSNumber
  291. XCTAssertEqual(max.floatValue, Float(0), accuracy: 0.1, "Maximum should be 0.0f")
  292. max = yesArray.max(ofProperty: "floatCol") as! NSNumber
  293. XCTAssertEqual(max.floatValue, Float(1.2), accuracy: 0.1, "Maximum should be 1.2f")
  294. // Test double max
  295. max = noArray.max(ofProperty: "doubleCol") as! NSNumber
  296. XCTAssertEqual(max.doubleValue, Double(2.5), accuracy: 0.1, "Maximum should be 3.5")
  297. max = yesArray.max(ofProperty: "doubleCol") as! NSNumber
  298. XCTAssertEqual(max.doubleValue, Double(0), accuracy: 0.1, "Maximum should be 0.0")
  299. // Test date max
  300. var dateMaxOutput = noArray.max(ofProperty: "dateCol") as! Date
  301. XCTAssertEqual(dateMaxOutput, dateMaxInput, "Maximum should be dateMaxInput")
  302. dateMaxOutput = yesArray.max(ofProperty: "dateCol") as! Date
  303. XCTAssertEqual(dateMaxOutput, dateMinInput, "Maximum should be dateMinInput")
  304. }
  305. func testArrayDescription_objc() {
  306. let realm = realmWithTestPath()
  307. realm.beginWriteTransaction()
  308. for _ in 0..<1012 {
  309. let person = EmployeeObject()
  310. person.name = "Mary"
  311. person.age = 24
  312. person.hired = true
  313. realm.add(person)
  314. }
  315. try! realm.commitWriteTransaction()
  316. let description = EmployeeObject.allObjects(in: realm).description
  317. XCTAssertTrue((description as NSString).range(of: "name").location != Foundation.NSNotFound, "property names should be displayed when calling \"description\" on RLMArray")
  318. XCTAssertTrue((description as NSString).range(of: "Mary").location != Foundation.NSNotFound, "property values should be displayed when calling \"description\" on RLMArray")
  319. XCTAssertTrue((description as NSString).range(of: "age").location != Foundation.NSNotFound, "property names should be displayed when calling \"description\" on RLMArray")
  320. XCTAssertTrue((description as NSString).range(of: "24").location != Foundation.NSNotFound, "property values should be displayed when calling \"description\" on RLMArray")
  321. XCTAssertTrue((description as NSString).range(of: "912 objects skipped").location != Foundation.NSNotFound, "'912 objects skipped' should be displayed when calling \"description\" on RLMArray")
  322. }
  323. func makeEmployee(_ realm: RLMRealm, _ age: Int32, _ name: String, _ hired: Bool) -> EmployeeObject {
  324. let employee = EmployeeObject()
  325. employee.age = age
  326. employee.name = name
  327. employee.hired = hired
  328. realm.add(employee)
  329. return employee
  330. }
  331. func testDeleteLinksAndObjectsInArray_objc() {
  332. let realm = realmWithTestPath()
  333. realm.beginWriteTransaction()
  334. let po1 = makeEmployee(realm, 40, "Joe", true)
  335. _ = makeEmployee(realm, 30, "John", false)
  336. let po3 = makeEmployee(realm, 25, "Jill", true)
  337. let company = CompanyObject()
  338. company.name = "name"
  339. realm.add(company)
  340. company.employees.addObjects(EmployeeObject.allObjects(in: realm))
  341. try! realm.commitWriteTransaction()
  342. let peopleInCompany: RLMArray<EmployeeObject> = company.employees!
  343. XCTAssertEqual(peopleInCompany.count, UInt(3), "No links should have been deleted")
  344. realm.beginWriteTransaction()
  345. peopleInCompany.removeObject(at: 1) // Should delete link to employee
  346. try! realm.commitWriteTransaction()
  347. XCTAssertEqual(peopleInCompany.count, UInt(2), "link deleted when accessing via links")
  348. var test = peopleInCompany[0]
  349. XCTAssertEqual(test.age, po1.age, "Should be equal")
  350. XCTAssertEqual(test.name!, po1.name!, "Should be equal")
  351. XCTAssertEqual(test.hired, po1.hired, "Should be equal")
  352. // XCTAssertEqual(test, po1, "Should be equal") //FIXME, should work. Asana : https://app.asana.com/0/861870036984/13123030433568
  353. test = peopleInCompany[1]
  354. XCTAssertEqual(test.age, po3.age, "Should be equal")
  355. XCTAssertEqual(test.name!, po3.name!, "Should be equal")
  356. XCTAssertEqual(test.hired, po3.hired, "Should be equal")
  357. // XCTAssertEqual(test, po3, "Should be equal") //FIXME, should work. Asana : https://app.asana.com/0/861870036984/13123030433568
  358. let allPeople = EmployeeObject.allObjects(in: realm)
  359. XCTAssertEqual(allPeople.count, UInt(3), "Only links should have been deleted, not the employees")
  360. }
  361. func testIndexOfObject_objc() {
  362. let realm = realmWithTestPath()
  363. realm.beginWriteTransaction()
  364. let po1 = makeEmployee(realm, 40, "Joe", true)
  365. let po2 = makeEmployee(realm, 30, "John", false)
  366. let po3 = makeEmployee(realm, 25, "Jill", true)
  367. try! realm.commitWriteTransaction()
  368. let results = EmployeeObject.objects(in: realm, where: "hired = YES")
  369. XCTAssertEqual(UInt(2), results.count)
  370. XCTAssertEqual(UInt(0), results.index(of: po1));
  371. XCTAssertEqual(UInt(1), results.index(of: po3));
  372. XCTAssertEqual(NSNotFound, Int(results.index(of: po2)));
  373. }
  374. func testIndexOfObjectWhere_objc() {
  375. let realm = realmWithTestPath()
  376. realm.beginWriteTransaction()
  377. _ = makeEmployee(realm, 40, "Joe", true)
  378. _ = makeEmployee(realm, 30, "John", false)
  379. _ = makeEmployee(realm, 25, "Jill", true)
  380. try! realm.commitWriteTransaction()
  381. let results = EmployeeObject.objects(in: realm, where: "hired = YES")
  382. XCTAssertEqual(UInt(2), results.count)
  383. XCTAssertEqual(UInt(0), results.indexOfObject(where: "age = %d", 40))
  384. XCTAssertEqual(UInt(1), results.indexOfObject(where: "age = %d", 25))
  385. XCTAssertEqual(NSNotFound, Int(results.indexOfObject(where: "age = %d", 30)))
  386. }
  387. func testSortingExistingQuery_objc() {
  388. let realm = realmWithTestPath()
  389. realm.beginWriteTransaction()
  390. _ = makeEmployee(realm, 20, "A", true)
  391. _ = makeEmployee(realm, 30, "B", false)
  392. _ = makeEmployee(realm, 40, "C", true)
  393. try! realm.commitWriteTransaction()
  394. let sortedByAge = EmployeeObject.allObjects(in: realm).sortedResults(usingKeyPath: "age", ascending: true)
  395. let sortedByName = sortedByAge.sortedResults(usingKeyPath: "name", ascending: false)
  396. XCTAssertEqual(Int32(20), (sortedByAge[0] as! EmployeeObject).age)
  397. XCTAssertEqual(Int32(40), (sortedByName[0] as! EmployeeObject).age)
  398. }
  399. }