       [#keyPath(Car.maker): "FIAT", #keyPath(Car.numberPlate): "4", #keyPath(Car.model): "Panda"],
       [#keyPath(Car.maker): "FIAT", #keyPath(Car.numberPlate): "5", #keyPath(Car.model): "Panda"],
     ]
-
+    
     // Provide one object at a time when the block is called.
     var index = 0
     let total = dictionaries.count

      XCTAssertFalse(car.objectID.isTemporaryID)
    }
  }

  func testFindFirstMaterializedObject() throws {
    let context = container.viewContext

    context.performAndWait {
      context.fillWithSampleData()
      try! context.save()
    }

    // materialize all expensive sport cars
    let request = ExpensiveSportCar.newFetchRequest()
    request.returnsObjectsAsFaults = false
    request.predicate = NSPredicate(value: true)

    _ = try context.fetch(request)

    XCTAssertNotNil(Car.materializedObject(in: context, where: NSPredicate(value: true)))

    let predicate = NSPredicate(format: "\(#keyPath(Car.numberPlate)) == %@", "304")
    XCTAssertNotNil(Car.materializedObject(in: context, where: predicate))

    // de-materialize all objects
    context.refreshAllObjects()

    XCTAssertNil(Car.materializedObject(in: context, where: predicate))
  }

  // MARK: Materialized Object

  func testfindMaterializedObjects() throws {
    let context = container.viewContext

    context.performAndWait {
      context.fillWithSampleData()
      try! context.save()
    }

    // materialize all expensive sport cars
    let request = ExpensiveSportCar.newFetchRequest()
    request.returnsObjectsAsFaults = false
    request.predicate = NSPredicate(value: true)
    _ = try context.fetch(request)

    XCTAssertTrue(Car.materializedObjects(in: context, where: NSPredicate(value: true)).count > 1)
    // with the previous fetch we have materialized only one Lamborghini (the expensive one)
    XCTAssertTrue(SportCar.materializedObjects(in: context, where: NSPredicate(format: "\(#keyPath(Car.maker)) == %@", "Lamborghini")).count == 1)

    // de-materialize all objects
    context.refreshAllObjects()

    XCTAssertTrue(Car.materializedObjects(in: context, where: NSPredicate(value: true)).isEmpty)
  }

  // MARK: Batch Delete

  func testbatchDeleteWithResultTypeStatusOnly() throws {
    // Given
    let context = container.viewContext
    context.fillWithSampleData()
    try context.save()

    let fiatPredicate = NSPredicate(format: "%K == %@", #keyPath(Car.maker), "FIAT")
    let result = try Car.batchDelete(using: context, resultType: .resultTypeStatusOnly) { $0.predicate = fiatPredicate }

    XCTAssertNotNil(result.status)
    XCTAssertTrue(result.status! == true)
    XCTAssertNil(result.deletes)
    XCTAssertEqual(result.changes?[NSDeletedObjectsKey]?.count, nil) // wrong result type
  }

  func testbatchDeleteWithResultTypeCount() throws {
    // Given
    let context = container.viewContext
    context.fillWithSampleData()
    try context.save()

    let fiatPredicate = NSPredicate(format: "%K == %@", #keyPath(Car.maker), "FIAT")
    let result = try Car.batchDelete(using: context, resultType: .resultTypeCount) { $0.predicate = fiatPredicate }

    XCTAssertNotNil(result.count)
    XCTAssertTrue(result.count! > 1)
    XCTAssertEqual(result.changes?[NSDeletedObjectsKey]?.count, nil) // wrong result type
  }

  func testbatchDeleteWithResultTypeObjectIDs() throws {
    // Given
    let context = container.viewContext
    context.fillWithSampleData()
    try context.save()


    let fiatPredicate = NSPredicate(format: "%K == %@", #keyPath(Car.maker), "FIAT")
    let fiatCount = try Car.count(in: context) { request in request.predicate = fiatPredicate }
    XCTAssertTrue(fiatCount > 0)

    let backgroundContext = context.newBackgroundContext()
    let result = try backgroundContext.performAndWaitResult {
      try Car.batchDelete(using: $0, resultType: .resultTypeObjectIDs) { $0.predicate = fiatPredicate }
    }

    XCTAssertNotNil(result.deletes)
    let deletedValues = result.deletes?.count ?? 0
    XCTAssertTrue(deletedValues > 1)
    XCTAssertTrue(result.changes?[NSDeletedObjectsKey]?.count ?? 0 > 1)

    // the var `changes`  is usefull when used while merging changes
    NSManagedObjectContext.mergeChanges(fromRemoteContextSave: result.changes!, into: [context])

    let fiatCountAfterMerge = try Car.count(in: context) { request in request.predicate = fiatPredicate }
    XCTAssertEqual(fiatCountAfterMerge, 0)
  }

  func testBatchdeleteEntities() throws {
    // Given
    let context = container.viewContext
    context.fillWithSampleData()
    try context.save()

    // When, Then
    let result = try SportCar.batchDelete(using: context, resultType: .resultTypeStatusOnly)
    XCTAssertTrue(result.status!)

    context.reset()
    let sportCarCount = try ExpensiveSportCar.count(in: context)
    let expensiveSportCarCount = try ExpensiveSportCar.count(in: context)
    XCTAssertEqual(sportCarCount, 0)
    XCTAssertEqual(expensiveSportCarCount, 0)
  }

  func testBatchDeleteEntitiesExcludingSubEntities() throws {
    // Given
    let context = container.viewContext
    context.fillWithSampleData()
    try context.save()

    let preDeleteSportCarCount = try SportCar.count(in: context) // This count include all the subentities
    let preDeleteExpensiveSportCarCount = try ExpensiveSportCar.count(in: context)
    let expectedRemovedCarCount = preDeleteSportCarCount - preDeleteExpensiveSportCarCount
    let expectedRemainingSportCartCount = preDeleteSportCarCount - expectedRemovedCarCount

    // When, Then
    let result = try SportCar.batchDelete(using: context, resultType: .resultTypeStatusOnly) { request in
      request.includesSubentities = false
    }
    XCTAssertTrue(result.status!)

    context.reset()
    let sportCarCount = try SportCar.count(in: context)
    let expensiveSportCarCount = try ExpensiveSportCar.count(in: context)
    XCTAssertEqual(sportCarCount, expectedRemainingSportCartCount)
    XCTAssertEqual(expensiveSportCarCount, preDeleteExpensiveSportCarCount)
  }

  func testbatchDeleteMarkedForDeletion() throws {
    // Given
    let context = container.viewContext
    context.fillWithSampleData()
    try context.save()

    let kias = [9, 10, 11]
    let kiasCars = try Car.fetch(in: context, with: { $0.predicate = NSPredicate(format: "%K IN %@", #keyPath(Car.numberPlate), kias) })
    XCTAssertEqual(kiasCars.count, 3)

      XCTAssertTrue(car.maker == "Lamborghini")
      XCTAssertFalse(car.objectID.isTemporaryID)
    }
    
    /// new object
    do {
      let car = try Car.findOneOrCreate(in: context, where: NSPredicate(format: "\(#keyPath(Car.numberPlate)) == %@", "304-new"), with: { car in
