@@ -1272,6 +1272,35 @@ final class MigrationTests: XCTestCase {
12721272 // Check instructions are correctly set.
12731273 assertEqual ( " payment gateway instructions " , migratedPaymentGateway. value ( forKey: " instructions " ) as? String )
12741274 }
1275+
1276+ func test_migrating_from_72_to_73_adds_filterKey_attribute_to_ProductSearchResults( ) throws {
1277+ // Given
1278+ let sourceContainer = try startPersistentContainer ( " Model 72 " )
1279+ let sourceContext = sourceContainer. viewContext
1280+
1281+ let productSearchResults = insertProductSearchResults ( to: sourceContext)
1282+ try sourceContext. save ( )
1283+
1284+ // `filterKey` should not be present before migration.
1285+ XCTAssertNil ( productSearchResults. entity. attributesByName [ " filterKey " ] )
1286+
1287+ // When
1288+ let targetContainer = try migrate ( sourceContainer, to: " Model 73 " )
1289+ let targetContext = targetContainer. viewContext
1290+
1291+ XCTAssertEqual ( try targetContext. count ( entityName: " ProductSearchResults " ) , 1 )
1292+ let migratedProductSearchResults = try XCTUnwrap ( targetContext. first ( entityName: " ProductSearchResults " ) )
1293+
1294+ // Checks for nil URL value.
1295+ XCTAssertNil ( migratedProductSearchResults. value ( forKey: " filterKey " ) )
1296+
1297+ // Sets a random `filterKey`.
1298+ migratedProductSearchResults. setValue ( " sku " , forKey: " filterKey " )
1299+ targetContext. saveIfNeeded ( )
1300+
1301+ // Check `filterKey` is correctly set.
1302+ XCTAssertEqual ( migratedProductSearchResults. value ( forKey: " filterKey " ) as? String , " sku " )
1303+ }
12751304}
12761305
12771306// MARK: - Persistent Store Setup and Migrations
@@ -1801,4 +1830,11 @@ private extension MigrationTests {
18011830 " value " : " USD "
18021831 ] )
18031832 }
1833+
1834+ @discardableResult
1835+ func insertProductSearchResults( to context: NSManagedObjectContext ) -> NSManagedObject {
1836+ context. insert ( entityName: " ProductSearchResults " , properties: [
1837+ " keyword " : " soul "
1838+ ] )
1839+ }
18041840}
0 commit comments