@sschmaljohann commented on GitHub (Aug 13, 2021):
Ok, I solved the issue(s) I had.
First I needed to do the migration asynchronously, so instead of try v1_0_1.addStorageAndWait(testStore), I needed to do this:
let expectation = XCTestExpectation(description: "Asyncronous adding of storeage")
v1_0_1.addStorage(testStore) { result in
switch result {
case let .failure(error):
XCTFail("An error occured: " + error.localizedDescription)
default:
break
}
expectation.fulfill()
}
wait(for: [expectation], timeout: 15)
Secondly, I realised, I needed to run this part of the test on the main queue, so the final working test was this:
class MigrationTests: XCTestCase {
private let testStore = SQLiteStore(fileName: "migrationTestStore",
localStorageOptions: .allowSynchronousLightweightMigration)
override func setUp() {
try? FileManager.default.removeItem(at: testStore.fileURL)
}
func testMigrationFrom1_0_0() {
testMigration(from: V1_0_0.self)
}
private func testMigration(from: TestableVersion.Type) {
do {
let start = DataStack(from.schema)
try start.addStorageAndWait(testStore)
try from.addTestData(to: start)
let end = DataStack(CurrentVersion.schema,
from.schema,
migrationChain: CurrentVersion.migrationChain)
let expectation = XCTestExpectation(description: "Asyncronous adding of storeage")
var progress: Progress?
DispatchQueue.main.async {
progress = end.addStorage(self.testStore) { result in
switch result {
case let .failure(error):
XCTFail("An error occured: " + error.localizedDescription)
default:
break
}
expectation.fulfill()
}
}
if progress != nil {
wait(for: [expectation], timeout: 15)
} else {
print("no migration needed from \(from.schema.modelVersion) to \(CurrentVersion.schema.modelVersion)")
}
} catch {
XCTFail("An error occured: " + error.localizedDescription)
}
}
}
@sschmaljohann commented on GitHub (Aug 13, 2021):
Ok, I solved the issue(s) I had.
First I needed to do the migration asynchronously, so instead of `try v1_0_1.addStorageAndWait(testStore)`, I needed to do this:
```
let expectation = XCTestExpectation(description: "Asyncronous adding of storeage")
v1_0_1.addStorage(testStore) { result in
switch result {
case let .failure(error):
XCTFail("An error occured: " + error.localizedDescription)
default:
break
}
expectation.fulfill()
}
wait(for: [expectation], timeout: 15)
```
Secondly, I realised, I needed to run this part of the test on the main queue, so the final working test was this:
```
class MigrationTests: XCTestCase {
private let testStore = SQLiteStore(fileName: "migrationTestStore",
localStorageOptions: .allowSynchronousLightweightMigration)
override func setUp() {
try? FileManager.default.removeItem(at: testStore.fileURL)
}
func testMigrationFrom1_0_0() {
testMigration(from: V1_0_0.self)
}
private func testMigration(from: TestableVersion.Type) {
do {
let start = DataStack(from.schema)
try start.addStorageAndWait(testStore)
try from.addTestData(to: start)
let end = DataStack(CurrentVersion.schema,
from.schema,
migrationChain: CurrentVersion.migrationChain)
let expectation = XCTestExpectation(description: "Asyncronous adding of storeage")
var progress: Progress?
DispatchQueue.main.async {
progress = end.addStorage(self.testStore) { result in
switch result {
case let .failure(error):
XCTFail("An error occured: " + error.localizedDescription)
default:
break
}
expectation.fulfill()
}
}
if progress != nil {
wait(for: [expectation], timeout: 15)
} else {
print("no migration needed from \(from.schema.modelVersion) to \(CurrentVersion.schema.modelVersion)")
}
} catch {
XCTFail("An error occured: " + error.localizedDescription)
}
}
}
```
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @sschmaljohann on GitHub (Aug 9, 2021).
Hi,
I am trying to test my migration chain, but I keep getting an error.
This is my test:
And this is the error I'm getting:
Is there anything obvious I'm doing wrong?
@sschmaljohann commented on GitHub (Aug 13, 2021):
Ok, I solved the issue(s) I had.
First I needed to do the migration asynchronously, so instead of
try v1_0_1.addStorageAndWait(testStore), I needed to do this:Secondly, I realised, I needed to run this part of the test on the main queue, so the final working test was this: