mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-18 06:57:15 +01:00
WIP: Dynamic models (Goodbye xcdatamodel files!)
This commit is contained in:
81
CoreStoreTests/DynamicModelTests.swift
Normal file
81
CoreStoreTests/DynamicModelTests.swift
Normal file
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// DynamicModelTests.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Created by John Estropia on 2017/04/03.
|
||||
// Copyright © 2017 John Rommel Estropia. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
import CoreData
|
||||
import CoreStore
|
||||
|
||||
|
||||
class DynamicModelTests: 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 testDynamicModels_CanBeDeclaredCorrectly() {
|
||||
|
||||
class Bird: CoreStoreManagedObject {
|
||||
|
||||
let species = Attribute.Required<String>("species", default: "Swift")
|
||||
}
|
||||
class Mascot: Bird {
|
||||
|
||||
let nickname = Attribute.Optional<String>("nickname")
|
||||
let year = Attribute.Required<Int>("year", default: 2016)
|
||||
}
|
||||
|
||||
let k1 = Bird.keyPath({ $0.species })
|
||||
XCTAssertEqual(k1, "species")
|
||||
|
||||
let k2 = Mascot.keyPath({ $0.species })
|
||||
XCTAssertEqual(k2, "species")
|
||||
|
||||
let k3 = Mascot.keyPath({ $0.nickname })
|
||||
XCTAssertEqual(k3, "nickname")
|
||||
|
||||
let entities = [
|
||||
"Bird": Entity<Bird>("Bird").entityDescription,
|
||||
"Mascot": Entity<Mascot>("Mascot").entityDescription
|
||||
]
|
||||
enum Static {
|
||||
|
||||
static let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
|
||||
}
|
||||
let rawBird = NSManagedObject(entity: entities["Bird"]!, insertInto: Static.context)
|
||||
let rawMascot = NSManagedObject(entity: entities["Mascot"]!, insertInto: Static.context)
|
||||
|
||||
|
||||
let bird = Bird(rawBird)
|
||||
XCTAssertEqual(bird.species*, "Swift")
|
||||
XCTAssertTrue(type(of: bird.species*) == String.self)
|
||||
|
||||
bird.species .= "Sparrow"
|
||||
XCTAssertEqual(bird.species*, "Sparrow")
|
||||
|
||||
let mascot = Mascot(rawMascot)
|
||||
XCTAssertEqual(mascot.species*, "Swift")
|
||||
XCTAssertEqual(mascot.nickname*, nil)
|
||||
|
||||
mascot.nickname .= "Riko"
|
||||
XCTAssertEqual(mascot.nickname*, "Riko")
|
||||
|
||||
|
||||
let p1 = (Bird.meta.species == "Swift")
|
||||
XCTAssertEqual(p1, Where("%K == %@", "species", "Swift").predicate)
|
||||
|
||||
let p2 = (Mascot.meta.nickname == "Riko")
|
||||
XCTAssertEqual(p2, Where("%K == %@", "nickname", "Riko").predicate)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user