Skip to content

Commit

Permalink
More unit tests.
Browse files Browse the repository at this point in the history
Added support of optional arrays!
  • Loading branch information
arturdev committed Mar 27, 2020
1 parent c4f5856 commit 413bd0f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Example/Tests/ExampleModels/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import Unrealm

struct Location: Realmable {
struct Location: Realmable, Equatable {
var lat: Double = 0
var lng: Double = 0
}
35 changes: 35 additions & 0 deletions Example/Tests/ExampleModels/NestedStruct.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// NestedStruct.swift
// Unrealm_Example
//
// Created by Artur Mkrtchyan on 3/20/20.
// Copyright © 2020 CocoaPods. All rights reserved.
//

import Foundation
import Unrealm

struct ParentStruct: Realmable {
struct ChildStruct: Realmable {
enum ChildEnum: Int, RealmableEnum {
case case1
case case2
case case3
}

var id: String = ""
var name: String = ""
var childEnum: ChildEnum = .case1
static func primaryKey() -> String? {
return "id"
}
}

var id: String = ""
var name: String = ""
var child: ChildStruct = ChildStruct()

static func primaryKey() -> String? {
return "id"
}
}
1 change: 1 addition & 0 deletions Example/Tests/ExampleModels/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct User: Realmable {
var floatOptional: Float? = nil
var doubleOptional: Double? = nil
var boolOptional: Bool? = nil
var arrayOptional: [Location]? = nil

static func primaryKey() -> String? {
return "id"
Expand Down
21 changes: 19 additions & 2 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Tests: XCTestCase {
}()

private static func configRealm() -> Realm.Configuration {
let realmableTypes: [RealmableBase.Type] = [Dog.self, User.self, Person.self, SubPerson.self, Location.self, Passenger.self, Driver.self]
let realmableTypes: [RealmableBase.Type] = [Dog.self, User.self, Person.self, SubPerson.self, Location.self, Passenger.self, Driver.self, ParentStruct.self, ParentStruct.ChildStruct.self]
Realm.registerRealmables(realmableTypes)

var objectTypes = realmableTypes.compactMap({$0.objectType()})
Expand All @@ -35,6 +35,21 @@ class Tests: XCTestCase {
super.tearDown()
}

func testNested() {
let child = ParentStruct.ChildStruct(id: UUID().uuidString, name: "Child", childEnum: .case2)
let parent = ParentStruct(id: UUID().uuidString, name: "Parent", child: child)
try! self.realm.write {
self.realm.add(parent, update: .all)
}

let savedParent = self.realm.objects(ParentStruct.self).last
XCTAssertEqual(parent.id, savedParent?.id)
XCTAssertEqual(parent.name, savedParent?.name)
XCTAssertEqual(parent.child.id, savedParent?.child.id)
XCTAssertEqual(parent.child.name, savedParent?.child.name)
XCTAssertEqual(parent.child.childEnum, savedParent?.child.childEnum)
}

func testPassenger() {
let url = Bundle(for: type(of: self)).url(forResource: "passenger", withExtension: "json")!
let jsonData = try! Data(contentsOf: url)
Expand Down Expand Up @@ -117,7 +132,8 @@ class Tests: XCTestCase {
intOptional: 3,
floatOptional: 3.4,
doubleOptional: 1.3,
boolOptional: true)
boolOptional: true,
arrayOptional: [loc2, loc3])

try! self.realm.write {
self.realm.add(user)
Expand All @@ -138,6 +154,7 @@ class Tests: XCTestCase {
XCTAssertEqual(NSDictionary(dictionary: user.dic), NSDictionary(dictionary: savedUser!.dic))
XCTAssertEqual(NSDictionary(dictionary: user.dicInt), NSDictionary(dictionary: savedUser!.dicInt))
XCTAssertEqual(user.intOptional, savedUser!.intOptional)
XCTAssertEqual(user.arrayOptional, savedUser!.arrayOptional)

XCTAssertEqual(user.list.count, savedUser!.list.count)
for i in 0..<user.list.count {
Expand Down
4 changes: 4 additions & 0 deletions Example/Unrealm.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
759706882424183E002BD2EE /* ToDoItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D4AEE2229B19550038C8C4 /* ToDoItem.swift */; };
7597068A24241E35002BD2EE /* TableCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7597068924241E35002BD2EE /* TableCellViewModel.swift */; };
7597068C2424218A002BD2EE /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7597068B2424218A002BD2EE /* WindowController.swift */; };
7597068F242537F8002BD2EE /* NestedStruct.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7597068D24253791002BD2EE /* NestedStruct.swift */; };
75D4AEE3229B19550038C8C4 /* ToDoItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D4AEE2229B19550038C8C4 /* ToDoItem.swift */; };
75D4AEE6229B1B3B0038C8C4 /* ToDoTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D4AEE5229B1B3B0038C8C4 /* ToDoTableViewCell.swift */; };
75D4AEEA229B1BDD0038C8C4 /* ToDoListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D4AEE9229B1BDD0038C8C4 /* ToDoListController.swift */; };
Expand Down Expand Up @@ -139,6 +140,7 @@
7597068424241216002BD2EE /* Example_with_Mac.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Example_with_Mac.entitlements; sourceTree = "<group>"; };
7597068924241E35002BD2EE /* TableCellViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableCellViewModel.swift; sourceTree = "<group>"; };
7597068B2424218A002BD2EE /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = "<group>"; };
7597068D24253791002BD2EE /* NestedStruct.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NestedStruct.swift; sourceTree = "<group>"; };
75D4AEE2229B19550038C8C4 /* ToDoItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToDoItem.swift; sourceTree = "<group>"; };
75D4AEE5229B1B3B0038C8C4 /* ToDoTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToDoTableViewCell.swift; sourceTree = "<group>"; };
75D4AEE9229B1BDD0038C8C4 /* ToDoListController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToDoListController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -322,6 +324,7 @@
757C87FF2292BF1600BF03A7 /* Dog.swift */,
750EC15422CAA44B005CBAE8 /* Person.swift */,
AFA449A123CDE69E003845CA /* RLMTestClass.swift */,
7597068D24253791002BD2EE /* NestedStruct.swift */,
);
path = ExampleModels;
sourceTree = "<group>";
Expand Down Expand Up @@ -950,6 +953,7 @@
607FACEC1AFB9204008FA782 /* Tests.swift in Sources */,
75D4AEF0229B2B160038C8C4 /* Dog.swift in Sources */,
AFA449A323CDE6A2003845CA /* RLMTestClass.swift in Sources */,
7597068F242537F8002BD2EE /* NestedStruct.swift in Sources */,
757C87F622905A2E00BF03A7 /* User.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
3 changes: 2 additions & 1 deletion Unrealm/Classes/Swift/Realm+Unrealm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ fileprivate func addProperties(of value: RealmableBase, to className: AnyClass,

addPropertyToClassIfNeeded(className: className, name: name, typeStr: typeStr)
}
addPropertyToClassIfNeeded(className: className, name: "__nilProperties", typeStr: "Array<String>")
}

fileprivate func addPropertyToClassIfNeeded(className: AnyObject.Type, name: String, typeStr: String) {
Expand Down Expand Up @@ -391,7 +392,7 @@ internal func getRealmArrayType(from type: String) -> String {
if typeStr.contains("Array<") {
typeStr = typeStr.replacingOccurrences(of: "Array", with: "RLMArray")
let generic = getGeneric(from: typeStr)
typeStr = typeStr.replacingOccurrences(of: generic, with: "RLM" + generic)
typeStr = typeStr.replacingOccurrences(of: generic, with: "RLM" + generic)
}

return typeStr
Expand Down
29 changes: 27 additions & 2 deletions Unrealm/Classes/Swift/Unrealm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ public extension Realmable {
let mirror = Mirror(reflecting: self)
let children = mirror.childrenIncludingSuperclass(subject: self)
guard let info = try? typeInfo(of: type(of: self)) else {return}

let nilProperties = obj.value(forKey: "__nilProperties") as? RLMArray<NSString>

for child in children {
guard let propertyName = child.label else {continue}
Expand All @@ -288,6 +290,10 @@ public extension Realmable {
}
} else {
do {
if let index = nilProperties?.index(of: propertyName as NSString), index != NSNotFound {
continue
}

if value is NSFastEnumeration {
let realmArray = value as! RLMArray<AnyObject>
if realmArray.firstObject() is Object {
Expand Down Expand Up @@ -367,10 +373,24 @@ fileprivate func convert<T: NSObject>(val: Any, to objectType: T.Type) -> AnyObj
let children = mirror.childrenIncludingSuperclass(subject: val)

let obj = objectType.init()
let nilProperties = (obj.value(forKey: "__nilProperties") as! RLMArray<NSString>)

children.filter({$0.label != nil}).forEach({
let label = $0.label!
guard obj.responds(to: NSSelectorFromString(label)) else {return}
let value = $0.value

if let nilable = value as? OptionalPrtc {
if !(nilable.val is NSNull) {
let index = nilProperties.index(of: label as NSString)
if index != NSNotFound {
nilProperties.removeObject(at: index)
}
} else {
nilProperties.add(label as NSString)
}
}

if value is RealmableBase {
guard let className = propertyClassName(label, objectType) else {return}
let fullClassName = objectsAndRealmables.keys.first(where: {$0 == className}) ?? className
Expand Down Expand Up @@ -424,11 +444,16 @@ fileprivate func convert<T: NSObject>(val: Any, to objectType: T.Type) -> AnyObj
}
#endif
} else {
//TODO: check if optional primitives
if let number = NSNumber(value: value) {
obj.setValue(number, forKey: label)
} else {
obj.setValue(value, forKey: label)
if let nilable = value as? OptionalPrtc {
if !(nilable.val is NSNull) {
obj.setValue(value, forKey: label)
}
} else {
obj.setValue(value, forKey: label)
}
}
}
}
Expand Down

0 comments on commit 413bd0f

Please sign in to comment.