@@ -1524,6 +1524,34 @@ final class MigrationTests: XCTestCase {
15241524 let newAverageOrderValue = try XCTUnwrap ( migratedOrderStatsV4Totals. value ( forKey: " averageOrderValue " ) as? Double )
15251525 XCTAssertEqual ( newAverageOrderValue, averageOrderValue)
15261526 }
1527+
1528+ func test_migrating_from_78_to_79_adds_views_attribute( ) throws {
1529+ // Given
1530+ let sourceContainer = try startPersistentContainer ( " Model 78 " )
1531+ let sourceContext = sourceContainer. viewContext
1532+
1533+ let siteVisitStatsItem = insertSiteVisitStatsItem ( to: sourceContainer. viewContext)
1534+ try sourceContext. save ( )
1535+
1536+ XCTAssertNil ( siteVisitStatsItem. entity. attributesByName [ " views " ] )
1537+
1538+ // When
1539+ let targetContainer = try migrate ( sourceContainer, to: " Model 79 " )
1540+ let targetContext = targetContainer. viewContext
1541+
1542+ let migratedSiteVisitStatsItem = try XCTUnwrap ( targetContext. first ( entityName: " SiteVisitStatsItem " ) )
1543+ let defaultViewsCount = try XCTUnwrap ( migratedSiteVisitStatsItem. value ( forKey: " views " ) as? Int )
1544+
1545+ let viewsCount = 12
1546+ migratedSiteVisitStatsItem. setValue ( viewsCount, forKey: " views " )
1547+
1548+ // Then
1549+ // Default value is 0.
1550+ XCTAssertEqual ( defaultViewsCount, 0 )
1551+
1552+ let newViewsCount = try XCTUnwrap ( migratedSiteVisitStatsItem. value ( forKey: " views " ) as? Int )
1553+ XCTAssertEqual ( newViewsCount, viewsCount)
1554+ }
15271555}
15281556
15291557// MARK: - Persistent Store Setup and Migrations
@@ -1943,6 +1971,14 @@ private extension MigrationTests {
19431971 ] )
19441972 }
19451973
1974+ @discardableResult
1975+ func insertSiteVisitStatsItem( to context: NSManagedObjectContext ) -> NSManagedObject {
1976+ context. insert ( entityName: " SiteVisitStatsItem " , properties: [
1977+ " period " : " day " ,
1978+ " visitors " : 3
1979+ ] )
1980+ }
1981+
19461982 @discardableResult
19471983 func insertTopEarnerStats( to context: NSManagedObjectContext ) -> NSManagedObject {
19481984 context. insert ( entityName: " TopEarnerStats " , properties: [
0 commit comments