|
|
@@ -109,54 +109,18 @@ import Testing
|
|
|
// Given
|
|
|
let date = Date()
|
|
|
let forecastTypes = ["iob", "cob", "zt", "uam"]
|
|
|
- var determinationId: NSManagedObjectID?
|
|
|
let expectedValuesPerForecast = 5
|
|
|
|
|
|
// STEP 1: Create test data
|
|
|
- await testContext.perform {
|
|
|
- let determination = OrefDetermination(context: testContext)
|
|
|
- determination.id = UUID()
|
|
|
- determination.deliverAt = date
|
|
|
- determination.timestamp = date
|
|
|
- determination.enacted = true
|
|
|
-
|
|
|
- // Create all forecast types with values
|
|
|
- for type in forecastTypes {
|
|
|
- let forecast = Forecast(context: testContext)
|
|
|
- forecast.id = UUID()
|
|
|
- forecast.date = date
|
|
|
- forecast.type = type
|
|
|
- forecast.orefDetermination = determination
|
|
|
-
|
|
|
- // Add test values with different patterns per type
|
|
|
- for i in 0 ..< expectedValuesPerForecast {
|
|
|
- let value = ForecastValue(context: testContext)
|
|
|
- value.index = Int32(i)
|
|
|
-
|
|
|
- // Different value patterns for each type
|
|
|
- switch type {
|
|
|
- case "iob": value.value = Int32(100 + i * 10) // 100, 110, 120...
|
|
|
- case "cob": value.value = Int32(50 + i * 5) // 50, 55, 60...
|
|
|
- case "zt": value.value = Int32(80 + i * 8) // 80, 88, 96...
|
|
|
- case "uam": value.value = Int32(120 - i * 15) // 120, 105, 90...
|
|
|
- default: value.value = 0
|
|
|
- }
|
|
|
-
|
|
|
- value.forecast = forecast
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- try? testContext.save()
|
|
|
- determinationId = determination.objectID
|
|
|
- }
|
|
|
-
|
|
|
- guard let determinationId = determinationId else {
|
|
|
- throw TestError("Failed to create test data")
|
|
|
- }
|
|
|
+ let id = try await createTestData(
|
|
|
+ date: date,
|
|
|
+ forecastTypes: forecastTypes,
|
|
|
+ expectedValuesPerForecast: expectedValuesPerForecast
|
|
|
+ )
|
|
|
|
|
|
// STEP 2: Test hierarchy fetching
|
|
|
let hierarchy = try await storage.fetchForecastHierarchy(
|
|
|
- for: determinationId,
|
|
|
+ for: id,
|
|
|
in: testContext
|
|
|
)
|
|
|
|
|
|
@@ -171,7 +135,6 @@ import Testing
|
|
|
)
|
|
|
|
|
|
// Test basic structure
|
|
|
- #expect(id != UUID(), "Should have valid UUID")
|
|
|
#expect(forecast != nil, "Forecast should exist")
|
|
|
#expect(values.count == expectedValuesPerForecast, "Should have correct number of values")
|
|
|
|
|
|
@@ -203,7 +166,7 @@ import Testing
|
|
|
// STEP 4: Test relationship integrity
|
|
|
try await testContext.perform {
|
|
|
do {
|
|
|
- let determination = try testContext.existingObject(with: determinationId) as? OrefDetermination
|
|
|
+ let determination = try testContext.existingObject(with: id) as? OrefDetermination
|
|
|
let forecasts = Array(determination?.forecasts ?? [])
|
|
|
|
|
|
#expect(forecasts.count == forecastTypes.count, "Determination should have all forecasts")
|
|
|
@@ -216,4 +179,52 @@ import Testing
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private func createTestData(
|
|
|
+ date: Date,
|
|
|
+ forecastTypes: [String],
|
|
|
+ expectedValuesPerForecast: Int
|
|
|
+ ) async throws -> NSManagedObjectID {
|
|
|
+ try await testContext.perform {
|
|
|
+ let determination = OrefDetermination(context: testContext)
|
|
|
+ determination.id = UUID()
|
|
|
+ determination.deliverAt = date
|
|
|
+ determination.timestamp = date
|
|
|
+ determination.enacted = true
|
|
|
+
|
|
|
+ // Create all forecast types with values
|
|
|
+ for type in forecastTypes {
|
|
|
+ let forecast = Forecast(context: testContext)
|
|
|
+ forecast.id = UUID()
|
|
|
+ forecast.date = date
|
|
|
+ forecast.type = type
|
|
|
+ forecast.orefDetermination = determination
|
|
|
+
|
|
|
+ // Add test values with different patterns per type
|
|
|
+ for i in 0 ..< expectedValuesPerForecast {
|
|
|
+ let value = ForecastValue(context: testContext)
|
|
|
+ value.index = Int32(i)
|
|
|
+
|
|
|
+ // Different value patterns for each type
|
|
|
+ switch type {
|
|
|
+ case "iob": value.value = Int32(100 + i * 10) // 100, 110, 120...
|
|
|
+ case "cob": value.value = Int32(50 + i * 5) // 50, 55, 60...
|
|
|
+ case "zt": value.value = Int32(80 + i * 8) // 80, 88, 96...
|
|
|
+ case "uam": value.value = Int32(120 - i * 15) // 120, 105, 90...
|
|
|
+ default: value.value = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ value.forecast = forecast
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ do {
|
|
|
+ try testContext.save()
|
|
|
+
|
|
|
+ return determination.objectID
|
|
|
+ } catch {
|
|
|
+ throw TestError("Failed to create test data: \(error)")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|