Skip to content

Instantly share code, notes, and snippets.

@tapi
Created November 26, 2015 20:40
Show Gist options
  • Save tapi/490f581fb9223d8832b6 to your computer and use it in GitHub Desktop.
Save tapi/490f581fb9223d8832b6 to your computer and use it in GitHub Desktop.
Test case demonstrating relationship clobbering in ObjectMapper 1.0
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"]
}
}
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