@@ -2333,6 +2333,36 @@ final class MigrationTests: XCTestCase {
23332333 // `note` should be present in `migratedBooking`
23342334 XCTAssertNotNil ( migratedBooking. entity. attributesByName [ " note " ] )
23352335 }
2336+
2337+ func test_migrating_from_130_to_131_adds_note_attribute_to_bookingCustomerInfo( ) throws {
2338+ // Given
2339+ let sourceContainer = try startPersistentContainer ( " Model 130 " )
2340+ let sourceContext = sourceContainer. viewContext
2341+
2342+ let customerInfo = insertBookingCustomerInfo ( to: sourceContext)
2343+ try sourceContext. save ( )
2344+
2345+ XCTAssertNil ( customerInfo. entity. attributesByName [ " note " ] , " Precondition. Attribute does not exist. " )
2346+
2347+ // When
2348+ let targetContainer = try migrate ( sourceContainer, to: " Model 131 " )
2349+
2350+ // Then
2351+ let targetContext = targetContainer. viewContext
2352+ let migratedCustomerInfo = try XCTUnwrap ( targetContext. first ( entityName: " BookingCustomerInfo " ) )
2353+
2354+ // `note` should be present in `migratedCustomerInfo`
2355+ XCTAssertNotNil ( migratedCustomerInfo. entity. attributesByName [ " note " ] )
2356+
2357+ let noteValue = migratedCustomerInfo. value ( forKey: " note " ) as? String
2358+ XCTAssertNil ( noteValue)
2359+
2360+ let updatedNote = " Customer note "
2361+ migratedCustomerInfo. setValue ( updatedNote, forKey: " note " )
2362+ try targetContext. save ( )
2363+
2364+ XCTAssertEqual ( migratedCustomerInfo. value ( forKey: " note " ) as? String , updatedNote)
2365+ }
23362366}
23372367
23382368// MARK: - Persistent Store Setup and Migrations
@@ -3275,7 +3305,7 @@ private extension MigrationTests {
32753305
32763306 @discardableResult
32773307 func insertBookingCustomerInfo( to context: NSManagedObjectContext ) -> NSManagedObject {
3278- context . insert ( entityName : " BookingCustomerInfo " , properties: [
3308+ var properties : [ String : Any ] = [
32793309 " billingFirstName " : " John " ,
32803310 " billingLastName " : " Doe " ,
32813311 " billingEmail " : " [email protected] " , @@ -3284,7 +3314,12 @@ private extension MigrationTests {
32843314 " billingState " : " CA " ,
32853315 " billingPostcode " : " 94102 " ,
32863316 " billingCountry " : " US "
3287- ] )
3317+ ]
3318+ if let entity = NSEntityDescription . entity ( forEntityName: " BookingCustomerInfo " , in: context) ,
3319+ entity. attributesByName. keys. contains ( " note " ) {
3320+ properties [ " note " ] = " Sample note "
3321+ }
3322+ return context. insert ( entityName: " BookingCustomerInfo " , properties: properties)
32883323 }
32893324
32903325 @discardableResult
0 commit comments