SwiftArrayTests.swift 25 KB

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