Created
November 26, 2015 20:40
-
-
Save tapi/490f581fb9223d8832b6 to your computer and use it in GitHub Desktop.
Test case demonstrating relationship clobbering in ObjectMapper 1.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import ObjectMapper | |
class Person: Mappable { | |
var name: String? | |
var spouse: Person? | |
required init?(_ map: Map) { | |
} | |
func mapping(map: Map) { | |
name <- map["name"] | |
spouse <- map["spouse"] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import XCTest | |
import ObjectMapper | |
class ReferenceTypesTestsFromJSON: XCTestCase { | |
override func setUp() { | |
super.setUp() | |
// Put setup code here. This method is called before the invocation of each test method in the class. | |
} | |
override func tearDown() { | |
// Put teardown code here. This method is called after the invocation of each test method in the class. | |
super.tearDown() | |
} | |
func testMappingPersonFromJSON(){ | |
let name = "ASDF" | |
let spouseName = "HJKL" | |
let JSONString = "{\"name\" : \"\(name)\", \"spouse\" : {\"name\" : \"\(spouseName)\"}}" | |
let mappedObject = Mapper<Person>().map(JSONString) | |
XCTAssertNotNil(mappedObject) | |
XCTAssertEqual(mappedObject?.name, name) | |
XCTAssertEqual(mappedObject?.spouse?.name, spouseName) | |
} | |
func testUpdatingPersonFromJSON(){ | |
let name = "ASDF" | |
let initialSpouseName = "HJKL" | |
let updatedSpouseName = "QWERTY" | |
let initialJSONString = "{\"name\" : \"\(name)\", \"spouse\" : {\"name\" : \"\(initialSpouseName)\"}}" | |
let updatedJSONString = "{\"name\" : \"\(name)\", \"spouse\" : {\"name\" : \"\(updatedSpouseName)\"}}" | |
let mappedObject = Mapper<Person>().map(initialJSONString) | |
let initialSpouse = mappedObject?.spouse | |
XCTAssertNotNil(mappedObject) | |
let updatedObject = Mapper<Person>().map(updatedJSONString, toObject: mappedObject!) | |
XCTAssert(initialSpouse === updatedObject.spouse, "Expected mapping to update the existing object not create a new one") | |
XCTAssertEqual(updatedObject.spouse?.name, updatedSpouseName) | |
XCTAssertEqual(initialSpouse?.name, upda) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment