Skip to content

Commit

Permalink
Updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Apr 30, 2024
1 parent eadc4f6 commit 925670f
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions Tests/CoreModelMongoDBTests/MongoDBModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class MongoDBModelTests: XCTestCase {
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
let client = try MongoClient("mongodb://localhost:27017", using: elg)
let database = client.db("test")
try await database.drop()

defer {
// clean up driver resources
Expand All @@ -31,6 +32,11 @@ final class MongoDBModelTests: XCTestCase {
name: "John Appleseed",
age: 22
)

var person2 = Person(
name: "Jane Doe",
age: 19
)

var event1 = Event(
name: "WWDC",
Expand All @@ -43,6 +49,7 @@ final class MongoDBModelTests: XCTestCase {

try await store.insert(event1)
try await store.insert(person1)
try await store.insert(person2)
person1 = try await store.fetch(Person.self, for: person1.id)!
XCTAssertEqual(person1.events, [event1.id])
event1 = try await store.fetch(Event.self, for: event1.id)!
Expand All @@ -61,34 +68,55 @@ final class MongoDBModelTests: XCTestCase {
officeHours: Campground.Schedule(start: 60 * 8, end: 60 * 18)
)

let rentalUnit = Campground.RentalUnit(
let rentalUnit1 = Campground.RentalUnit(
campground: campground.id,
name: "A1",
amenities: [.amp50, .water, .mail, .river, .laundry],
checkout: campground.officeHours
)

let rentalUnit2 = Campground.RentalUnit(
campground: campground.id,
name: "A2",
amenities: [.amp30, .water, .mail, .laundry],
checkout: campground.officeHours
)

var campgroundData = try campground.encode(log: { print("Encoder:", $0) })
try await store.insert(campgroundData)
let rentalUnitData = try rentalUnit.encode(log: { print("Encoder:", $0) })
XCTAssertEqual(rentalUnitData.relationships[PropertyKey(Campground.RentalUnit.CodingKeys.campground)], .toOne(ObjectID(campground.id)))
try await store.insert(rentalUnitData)
let rentalUnitData1 = try rentalUnit1.encode(log: { print("Encoder:", $0) })
XCTAssertEqual(rentalUnitData1.relationships[PropertyKey(Campground.RentalUnit.CodingKeys.campground)], .toOne(ObjectID(campground.id)))
try await store.insert(rentalUnitData1)
try await store.insert(rentalUnit2)

// update value
campground.units = [rentalUnit.id]
// update relationships
campground.units = [rentalUnit1.id, rentalUnit2.id]
try await store.insert(campground)

campgroundData = try await store.fetch(Campground.entityName, for: ObjectID(campground.id))!
campground = try .init(from: campgroundData, log: { print("Decoder:", $0) })
XCTAssertEqual(campground.units, [rentalUnit.id])
XCTAssertEqual(campgroundData.relationships[PropertyKey(Campground.CodingKeys.units)], .toMany([ObjectID(rentalUnit.id)]))
let fetchedRentalUnit = try await store.fetch(Campground.RentalUnit.self, for: rentalUnit.id)
XCTAssertEqual(fetchedRentalUnit, rentalUnit)
XCTAssertEqual(campground.units, [rentalUnit1.id, rentalUnit2.id])
XCTAssertEqual(campgroundData.relationships[PropertyKey(Campground.CodingKeys.units)], .toMany([ObjectID(rentalUnit1.id), ObjectID(rentalUnit2.id)]))
let fetchedRentalUnit1 = try await store.fetch(Campground.RentalUnit.self, for: rentalUnit1.id)
XCTAssertEqual(fetchedRentalUnit1, rentalUnit1)
let rentalUnitFetchRequest = FetchRequest(
entity: Campground.RentalUnit.entityName,
predicate: Campground.RentalUnit.CodingKeys.campground.stringValue.compare(.equalTo, .relationship(.toOne(ObjectID(campground.id))))
)
let rentalUnitIDs = try await store.fetchID(rentalUnitFetchRequest)
XCTAssertEqual(rentalUnitIDs, campground.units.map { ObjectID($0) })

// fetch person 2
let personFetchResult = try await store.fetch(Person.self, predicate: Person.CodingKeys.name.stringValue == "Jane Doe")
XCTAssertEqual(personFetchResult.count, 1)
XCTAssertEqual(personFetchResult.first?.id, person2.id)

do {
_ = try await store.fetch(Person.self, predicate: Person.CodingKeys.name.stringValue.compare(.contains, [.caseInsensitive], .attribute(.string("jane"))))
XCTFail()
}
catch {
// error expected
}
}
}

0 comments on commit 925670f

Please sign in to comment.