mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-04-21 16:31:21 +02:00
updated unit tests with easier to understand examples
This commit is contained in:
@@ -13,20 +13,22 @@ import CoreData
|
|||||||
@testable import CoreStore
|
@testable import CoreStore
|
||||||
|
|
||||||
|
|
||||||
class Bird: ManagedObject {
|
class Animal: ManagedObject {
|
||||||
|
|
||||||
let species = Attribute.Required<String>("species", default: "Swift")
|
let species = Attribute.Required<String>("species", default: "Swift")
|
||||||
let enemy = Relationship.ToOne<Cat>("enemy", inverse: { $0.toy })
|
let master = Relationship.ToOne<Person>("master", inverse: { $0.pet })
|
||||||
}
|
}
|
||||||
class Mascot: Bird {
|
|
||||||
|
class Dog: Animal {
|
||||||
|
|
||||||
let nickname = Attribute.Optional<String>("nickname")
|
let nickname = Attribute.Optional<String>("nickname")
|
||||||
let year = Attribute.Required<Int>("year", default: 2016)
|
let age = Attribute.Required<Int>("age", default: 1)
|
||||||
}
|
}
|
||||||
class Cat: ManagedObject {
|
|
||||||
|
class Person: ManagedObject {
|
||||||
|
|
||||||
let name = Attribute.Required<String>("name")
|
let name = Attribute.Required<String>("name")
|
||||||
let toy = Relationship.ToOne<Bird>("toy")
|
let pet = Relationship.ToOne<Animal>("pet")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -38,51 +40,52 @@ class DynamicModelTests: BaseTestDataTestCase {
|
|||||||
dynamicModel: ObjectModel(
|
dynamicModel: ObjectModel(
|
||||||
version: "V1",
|
version: "V1",
|
||||||
entities: [
|
entities: [
|
||||||
Entity<Bird>("Bird"),
|
Entity<Animal>("Animal"),
|
||||||
Entity<Mascot>("Mascot"),
|
Entity<Dog>("Dog"),
|
||||||
Entity<Cat>("Cat")
|
Entity<Person>("Person")
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.prepareStack(dataStack, configurations: [nil]) { (stack) in
|
self.prepareStack(dataStack, configurations: [nil]) { (stack) in
|
||||||
|
|
||||||
let k1 = Bird.keyPath({ $0.species })
|
let k1 = Animal.keyPath({ $0.species })
|
||||||
XCTAssertEqual(k1, "species")
|
XCTAssertEqual(k1, "species")
|
||||||
|
|
||||||
let k2 = Mascot.keyPath({ $0.species })
|
let k2 = Dog.keyPath({ $0.species })
|
||||||
XCTAssertEqual(k2, "species")
|
XCTAssertEqual(k2, "species")
|
||||||
|
|
||||||
let k3 = Mascot.keyPath({ $0.nickname })
|
let k3 = Dog.keyPath({ $0.nickname })
|
||||||
XCTAssertEqual(k3, "nickname")
|
XCTAssertEqual(k3, "nickname")
|
||||||
|
|
||||||
let expectation = self.expectation(description: "done")
|
let expectation = self.expectation(description: "done")
|
||||||
stack.perform(
|
stack.perform(
|
||||||
asynchronous: { (transaction) in
|
asynchronous: { (transaction) in
|
||||||
|
|
||||||
let bird = transaction.create(Into<Bird>())
|
let animal = transaction.create(Into<Animal>())
|
||||||
XCTAssertEqual(bird.species*, "Swift")
|
XCTAssertEqual(animal.species*, "Swift")
|
||||||
XCTAssertTrue(type(of: bird.species*) == String.self)
|
XCTAssertTrue(type(of: animal.species*) == String.self)
|
||||||
|
|
||||||
bird.species .= "Sparrow"
|
animal.species .= "Sparrow"
|
||||||
XCTAssertEqual(bird.species*, "Sparrow")
|
XCTAssertEqual(animal.species*, "Sparrow")
|
||||||
|
|
||||||
let mascot = transaction.create(Into<Mascot>())
|
let dog = transaction.create(Into<Dog>())
|
||||||
XCTAssertEqual(mascot.species*, "Swift")
|
XCTAssertEqual(dog.species*, "Swift")
|
||||||
XCTAssertEqual(mascot.nickname*, nil)
|
XCTAssertEqual(dog.nickname*, nil)
|
||||||
|
|
||||||
mascot.species .= "Swift3"
|
dog.species .= "Dog"
|
||||||
XCTAssertEqual(mascot.species*, "Swift3")
|
XCTAssertEqual(dog.species*, "Dog")
|
||||||
|
|
||||||
mascot.nickname .= "Riko"
|
dog.nickname .= "Spot"
|
||||||
XCTAssertEqual(mascot.nickname*, "Riko")
|
XCTAssertEqual(dog.nickname*, "Spot")
|
||||||
|
|
||||||
let cat = transaction.create(Into<Cat>())
|
let person = transaction.create(Into<Person>())
|
||||||
XCTAssertNil(cat.toy.value)
|
XCTAssertNil(person.pet.value)
|
||||||
|
|
||||||
cat.toy .= mascot
|
person.pet .= dog
|
||||||
XCTAssertEqual(cat.toy.value, mascot)
|
XCTAssertEqual(person.pet.value, dog)
|
||||||
XCTAssertEqual(cat.toy.value?.enemy.value, cat)
|
XCTAssertEqual(person.pet.value?.master.value, person)
|
||||||
XCTAssertEqual(mascot.enemy.value, cat)
|
XCTAssertEqual(dog.master.value, person)
|
||||||
|
XCTAssertEqual(dog.master.value?.pet.value, dog)
|
||||||
},
|
},
|
||||||
success: {
|
success: {
|
||||||
|
|
||||||
@@ -96,29 +99,27 @@ class DynamicModelTests: BaseTestDataTestCase {
|
|||||||
stack.perform(
|
stack.perform(
|
||||||
asynchronous: { (transaction) in
|
asynchronous: { (transaction) in
|
||||||
|
|
||||||
let p1 = Bird.where({ $0.species == "Sparrow" })
|
let p1 = Animal.where({ $0.species == "Sparrow" })
|
||||||
XCTAssertEqual(p1.predicate, Where("%K == %@", "species", "Sparrow").predicate)
|
XCTAssertEqual(p1.predicate, NSPredicate(format: "%K == %@", "species", "Sparrow"))
|
||||||
|
|
||||||
let bird = transaction.fetchOne(
|
let bird = transaction.fetchOne(From<Animal>(), p1)
|
||||||
From<Bird>(),
|
|
||||||
Tweak({ $0.includesSubentities = false })
|
|
||||||
)
|
|
||||||
XCTAssertNotNil(bird)
|
XCTAssertNotNil(bird)
|
||||||
XCTAssertEqual(bird!.species*, "Sparrow")
|
XCTAssertEqual(bird!.species*, "Sparrow")
|
||||||
|
|
||||||
let p2 = Mascot.where({ $0.nickname == "Riko" })
|
let p2 = Dog.where({ $0.nickname == "Spot" })
|
||||||
XCTAssertEqual(p2.predicate, Where("%K == %@", "nickname", "Riko").predicate)
|
XCTAssertEqual(p2.predicate, NSPredicate(format: "%K == %@", "nickname", "Spot"))
|
||||||
|
|
||||||
let mascot = transaction.fetchOne(From<Mascot>())
|
let dog = transaction.fetchOne(From<Dog>(), p2)
|
||||||
XCTAssertNotNil(mascot)
|
XCTAssertNotNil(dog)
|
||||||
XCTAssertEqual(mascot!.nickname*, "Riko")
|
XCTAssertEqual(dog!.nickname*, "Spot")
|
||||||
XCTAssertEqual(mascot!.species*, "Swift3")
|
XCTAssertEqual(dog!.species*, "Dog")
|
||||||
|
|
||||||
let cat = transaction.fetchOne(From<Cat>())
|
let person = transaction.fetchOne(From<Person>())
|
||||||
XCTAssertEqual(cat?.toy.value, mascot)
|
XCTAssertNotNil(person)
|
||||||
|
XCTAssertEqual(person!.pet.value, dog)
|
||||||
|
|
||||||
let p3 = Mascot.where({ $0.year == 2016 })
|
let p3 = Dog.where({ $0.age == 10 })
|
||||||
XCTAssertEqual(p3.predicate, Where("%K == %@", "year", 2016).predicate)
|
XCTAssertEqual(p3.predicate, NSPredicate(format: "%K == %d", "age", 10))
|
||||||
},
|
},
|
||||||
success: {
|
success: {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user