Last active
January 25, 2017 22:20
-
-
Save ajjames/c00996bf16919021e49af25e775e9e65 to your computer and use it in GitHub Desktop.
Proof-of-Concept Tests
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 XCTest | |
import Realm | |
import RealmSwift | |
class ProofOfConceptTests: RealmTestCase { | |
// NOTE this is for objects with 1-to-1 relationships | |
func testUpdateAsset() { | |
// 1. make a full object | |
let city = City() | |
city.id = "123456" | |
city.name = "Seattle" | |
city.zoo = Zoo() | |
city.zoo?.id = "123456" | |
city.zoo?.name = "Woodland Park Zoo" | |
city.zoo?.animal = Animal() | |
city.zoo?.animal?.id = "123456" | |
city.zoo?.animal?.name = "Komodo Dragon" | |
// save it | |
let persistenceController = MolokiniKit.PersistenceController() | |
let cityRepository = CityRepository() | |
cityRepository.save(city) | |
// get it out again. Assert it was saved | |
guard let savedCity = cityRepository.retrieveAll().first | |
else { | |
XCTFail() | |
return | |
} | |
// Assert saved object is correct | |
XCTAssertEqual(savedCity.name, "Seattle") | |
XCTAssertEqual(savedCity.zoo?.name, "Woodland Park Zoo") | |
XCTAssertEqual(savedCity.zoo?.animal?.name, "Komodo Dragon") | |
var allAnimals = persistenceController.objects(of: Animal.self) | |
XCTAssertEqual(allAnimals.count, 1) | |
// 2. make a new object (same ID), but with an updated animal object | |
let city2 = City() | |
city2.id = "123456" | |
city2.name = "Seattle" | |
city2.zoo = Zoo() | |
city2.zoo?.name = "Woodland Park Zoo" | |
city2.zoo?.animal = Animal() | |
city2.zoo?.animal?.name = "Llama" | |
// save it | |
cityRepository.save(city2) | |
// Assert that a new animal object was not created, but updated instead | |
allAnimals = persistenceController.objects(of: Animal.self) | |
XCTAssertEqual(allAnimals.count, 1) | |
XCTAssertEqual(savedCity.zoo?.animal?.name, "Llama") | |
// 3. make yet another new object that has | |
let city3 = City() | |
city3.id = "123456" | |
city3.name = "Seattle" | |
// save it | |
cityRepository.save(city3) | |
// Assert that zoo is nil | |
XCTAssertNil(savedCity.zoo) | |
// Assert that all zoo and animal objects have been cleaned up | |
let allZoos = persistenceController.objects(of: Zoo.self) | |
XCTAssertEqual(allZoos.count, 0) | |
XCTAssertNil(savedCity.zoo) | |
allAnimals = persistenceController.objects(of: Animal.self) | |
XCTAssertEqual(allAnimals.count, 0) | |
XCTAssertNil(savedCity.zoo?.animal) | |
} | |
func testCascadeDeleteAsset() { | |
// 1. make a full object | |
let city = City() | |
city.id = "123456" | |
city.name = "Seattle" | |
city.zoo = Zoo() | |
city.zoo?.id = "123456" | |
city.zoo?.name = "Woodland Park Zoo" | |
city.zoo?.animal = Animal() | |
city.zoo?.animal?.id = "123456" | |
city.zoo?.animal?.name = "Komodo Dragon" | |
// save it | |
let cityRepository = CityRepository() | |
cityRepository.save(city) | |
// get it out again. Assert it was saved | |
guard let savedCity = cityRepository.retrieveAll().first | |
else { | |
XCTFail() | |
return | |
} | |
// Assert saved object is correct | |
XCTAssertEqual(savedCity.name, "Seattle") | |
XCTAssertEqual(savedCity.zoo?.name, "Woodland Park Zoo") | |
XCTAssertEqual(savedCity.zoo?.animal?.name, "Komodo Dragon") | |
cityRepository.delete(savedCity) | |
let persistenceController = MolokiniKit.PersistenceController() | |
let allCities = persistenceController.objects(of: City.self) | |
let allZoos = persistenceController.objects(of: Zoo.self) | |
let allAnimals = persistenceController.objects(of: Animal.self) | |
XCTAssertEqual(allCities.count, 0) | |
XCTAssertEqual(allZoos.count, 0) | |
XCTAssertEqual(allAnimals.count, 0) | |
} | |
func testAutoCleanupAsset() { | |
// 1. make a full object | |
let city = City() | |
city.id = "123456" | |
city.name = "Seattle" | |
city.zoo = Zoo() | |
city.zoo?.id = "123456" | |
city.zoo?.name = "Woodland Park Zoo" | |
city.zoo?.animal = Animal() | |
city.zoo?.animal?.id = "123456" | |
city.zoo?.animal?.name = "Komodo Dragon" | |
// save it | |
let cityRepository = CityRepository() | |
cityRepository.save(city) | |
// get it out again. Assert it was saved | |
guard let savedCity = cityRepository.retrieveAll().first | |
else { | |
XCTFail() | |
return | |
} | |
// Assert saved object is correct | |
XCTAssertEqual(savedCity.name, "Seattle") | |
XCTAssertEqual(savedCity.zoo?.name, "Woodland Park Zoo") | |
XCTAssertEqual(savedCity.zoo?.animal?.name, "Komodo Dragon") | |
let city1 = City() | |
city1.id = "123456" | |
city1.name = "Seattle" | |
city1.zoo = Zoo() | |
city1.zoo?.id = "123456" | |
city1.zoo?.name = "Woodland Park Zoo" | |
city1.zoo?.animal = Animal() | |
city1.zoo?.animal?.id = "123456" | |
city1.zoo?.animal?.name = "Komodo Dragon" | |
let realm = try? Realm() | |
_ = try? realm?.write { | |
realm?.add(city1, update: true) | |
realm?.delete(city1) | |
} | |
let persistenceController = MolokiniKit.PersistenceController() | |
let savedCity1 = persistenceController.object(of: City.self) | |
XCTAssertNil(savedCity1) | |
XCTAssertNil(city1.realm) | |
} | |
} | |
import Realm | |
import RealmSwift | |
class CityRepository: Repositable { | |
typealias ObjectType = City | |
typealias IdentifierType = String | |
let persistenceController = PersistenceController() | |
func save(_ city: City) { | |
if let existingCity = persistenceController.object(of: City.self, withID: city.id) { | |
update(existingCity: existingCity, with: city) | |
return | |
} | |
do { | |
updatePropertyPrimaryKeys(on: city, primaryKey: city.id) | |
try persistenceController.add(city) | |
} catch { | |
XCTFail() | |
} | |
} | |
private func update(existingCity: City, with newCity: City) { | |
updatePropertyPrimaryKeys(on: newCity, primaryKey: existingCity.id) | |
if newCity.zoo?.animal == nil, | |
let animal = existingCity.zoo?.animal { | |
delete(animal) | |
} | |
if newCity.zoo == nil, | |
let zoo = existingCity.zoo { | |
delete(zoo) | |
} | |
do { | |
try persistenceController.add(newCity) | |
} catch { | |
XCTFail() | |
} | |
} | |
private func updatePropertyPrimaryKeys(on city: City, primaryKey: String) { | |
city.zoo?.id = primaryKey | |
city.zoo?.animal?.id = primaryKey | |
} | |
func delete(_ object: City) { | |
if let animal = object.zoo?.animal { | |
delete(animal) | |
} | |
if let zoo = object.zoo { | |
delete(zoo) | |
} | |
_ = try? persistenceController.delete(object) | |
} | |
private func delete(_ object: Zoo) { | |
if let animal = object.animal { | |
delete(animal) | |
} | |
_ = try? persistenceController.delete(object) | |
} | |
private func delete(_ object: Animal) { | |
_ = try? persistenceController.delete(object) | |
} | |
} | |
class City: StringKeyedObject { | |
dynamic var name: String? | |
dynamic var zoo: Zoo? | |
} | |
class Zoo: StringKeyedObject { | |
dynamic var name: String? | |
dynamic var animal: Animal? | |
} | |
class Animal: StringKeyedObject { | |
dynamic var name: String? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment