Skip to content

Commit

Permalink
WIP: Add data factory
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkleiven committed Oct 9, 2023
1 parent 09da4a7 commit 8f18116
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/test/AppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,19 @@ class ApplicationTest {
val num = body.split("},{").size
assertEquals(2, num)
}

@TestFactory
fun `test 200 response for all valid sensitivity inputs`() = allSensitivityAnalysisConfigs().map { config ->
DynamicTest.dynamicTest("Test 200 for config $config") {
testApplication {
val response = client.submitFormWithBinaryData(
url = "/sensitivity-analysis",
formData = sensitivityAnalysisFormData(config)
)
assertEquals(HttpStatusCode.OK, response.status)
}
}
}
}


Expand Down
131 changes: 131 additions & 0 deletions src/test/testDataFactory/SensitivityRunFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package testDataFactory

import com.github.statnett.loadflowservice.AutoSerializableSensitivityFactor
import io.ktor.client.request.forms.*
import io.ktor.http.content.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class Contingencies(
val type: String,
val version: String,
val name: String,
val contingencies: List<Contingency>
)

@Serializable
data class Contingency(
val id: String,
val elements: List<ContingencyElement>
)

@Serializable
data class ContingencyElement(
val id: String,
val type: String,
)

fun ieee14BusContingencies(): Contingencies {
val generator = ContingencyElement(id = "B1-G", type = "GENERATOR")
val br1 = ContingencyElement(id = "L7-8-0", type = "BRANCH")
val br2 = ContingencyElement(id = "L9-8-0", type = "BRANCH")

return Contingencies(
type = "default",
version = "1.0",
name = "list",
contingencies = listOf(
Contingency(id = "generatorContingency", listOf(generator)),
Contingency(id = "branchContingency", listOf(br1, br2))
)
)
}

fun ieee14SensitivityFactor(): AutoSerializableSensitivityFactor {
return AutoSerializableSensitivityFactor(
functionType = "BRANCH_ACTIVE_POWER_2",
functionId = "l",
variableType = "INJECTION_ACTIVE_POWER",
variableId = "g",
variableSet = false,
contingencyContextType = "ALL"
)
}

fun ieee14SensitivityLoadParams(): String {
return "{\"dc\": true}"
}

fun ieee14SensitivityParams(): String {
return "{\"voltage-voltage-sensitivity-threshold\": 0.001}"
}

data class SensitivityAnalysisConfig(
val withContingencies: Boolean,
val withSensitivityFactors: Boolean,
val withLoadParameters: Boolean,
val withSensitivityParameters: Boolean
)

fun allSensitivityAnalysisConfigs(): List<SensitivityAnalysisConfig> {
val options = listOf(true, false)
return options.map { withCtg ->
options.map { withSf ->
options.map { withLp ->
options.map { withSensParam ->
SensitivityAnalysisConfig(withCtg, withSf, withLp, withSensParam)
}
}.flatten()
}.flatten()
}.flatten()
}

fun sensitivityAnalysisFormData(config: SensitivityAnalysisConfig): List<PartData> {
val cgmesFile = formDataFromFile(ieeeCdfNetwork14CgmesFile()).toMutableList()
val sensFactors = formData {
append(
"sensitivity-factors",
Json.encodeToString(ieee14SensitivityFactor())
)
}

val loadParams = formData {
append(
"load-parameters",
ieee14SensitivityLoadParams()
)
}

val contingencies = formData {
append(
"contingencies",
Json.encodeToString(ieee14BusContingencies())
)
}

val sensParams = formData {
append(
"sensitivity-analysis-parameters",
ieee14SensitivityParams()
)
}

if (config.withContingencies) {
cgmesFile += contingencies
}

if (config.withSensitivityFactors) {
cgmesFile += sensFactors
}

if (config.withLoadParameters) {
cgmesFile += loadParams
}

if (config.withSensitivityParameters) {
cgmesFile += sensParams
}
return cgmesFile
}

0 comments on commit 8f18116

Please sign in to comment.