mirror of
https://github.com/smyalygames/checklist-tester.git
synced 2025-11-30 01:39:38 +01:00
feat(connector): add database transaction for testing
This commit is contained in:
@@ -0,0 +1,89 @@
|
|||||||
|
package io.anthonyberg.connector.shared
|
||||||
|
|
||||||
|
import io.anthonyberg.connector.shared.database.ActionResultDatabase
|
||||||
|
import io.anthonyberg.connector.shared.database.DriverFactory
|
||||||
|
import io.anthonyberg.connector.shared.database.TestDatabase
|
||||||
|
import kotlinx.datetime.Clock
|
||||||
|
import kotlinx.datetime.Instant
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Database transactions for testing procedures
|
||||||
|
*/
|
||||||
|
class TestTransaction (driverFactory: DriverFactory) {
|
||||||
|
private val testDb = TestDatabase(driverFactory)
|
||||||
|
private val actionResultDb = ActionResultDatabase(driverFactory)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the test on the database
|
||||||
|
*
|
||||||
|
* @param procedureId ID of the Procedure that is being tested
|
||||||
|
*
|
||||||
|
* @return ID of the Test that was started
|
||||||
|
*/
|
||||||
|
fun startTest(procedureId: Int): Int {
|
||||||
|
val currentTime = Clock.System.now().toString()
|
||||||
|
|
||||||
|
val id = testDb.startTest(
|
||||||
|
procedureId = procedureId.toLong(),
|
||||||
|
startUTC = currentTime,
|
||||||
|
)
|
||||||
|
|
||||||
|
return id.toInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells the database that the test has ended
|
||||||
|
*
|
||||||
|
* @param id ID of the Test
|
||||||
|
*
|
||||||
|
* @return The final time for when the test ended
|
||||||
|
*/
|
||||||
|
fun endTest(id: Int): Instant {
|
||||||
|
val currentTime = Clock.System.now()
|
||||||
|
|
||||||
|
testDb.endTest(testId = id.toLong(), endUTC = currentTime.toString())
|
||||||
|
|
||||||
|
return currentTime
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the test for a specific action on the database
|
||||||
|
*
|
||||||
|
* @param testId ID of the current test that is running
|
||||||
|
* @param actionId ID of the current action that is being tested
|
||||||
|
* @param initState The initial state of the action in the simulator
|
||||||
|
*
|
||||||
|
* @return ID of the Action created on the database
|
||||||
|
*/
|
||||||
|
fun startAction(testId: Int, actionId: Int, initState: String): Int {
|
||||||
|
val currentTime = Clock.System.now().toString()
|
||||||
|
|
||||||
|
val id = actionResultDb.startResult(
|
||||||
|
testId = testId.toLong(),
|
||||||
|
actionId = actionId.toLong(),
|
||||||
|
initState = initState,
|
||||||
|
startUTC = currentTime,
|
||||||
|
)
|
||||||
|
|
||||||
|
return id.toInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells the database that the test has ended and adds the final state
|
||||||
|
*
|
||||||
|
* @param id ID of the ActionResult
|
||||||
|
*
|
||||||
|
* @return The time for when the test for the specific action finished
|
||||||
|
*/
|
||||||
|
fun finishAction(id: Int, endState: String): Instant {
|
||||||
|
val currentTime = Clock.System.now()
|
||||||
|
|
||||||
|
actionResultDb.finishResult(
|
||||||
|
id = id.toLong(),
|
||||||
|
endState = endState,
|
||||||
|
endUTC = currentTime.toString()
|
||||||
|
)
|
||||||
|
|
||||||
|
return currentTime
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package io.anthonyberg.connector.shared.database
|
||||||
|
|
||||||
|
internal class ActionResultDatabase (driverFactory: DriverFactory) {
|
||||||
|
private val database = Database(driverFactory.createDriver())
|
||||||
|
private val dbQuery = database.actionResultQueries
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the start of a test for an action to the database
|
||||||
|
*
|
||||||
|
* @return ID of the ActionResult created
|
||||||
|
*/
|
||||||
|
internal fun startResult(
|
||||||
|
testId: Long,
|
||||||
|
actionId: Long,
|
||||||
|
initState: String,
|
||||||
|
startUTC: String
|
||||||
|
): Long {
|
||||||
|
dbQuery.startResult(
|
||||||
|
testId = testId,
|
||||||
|
actionId = actionId,
|
||||||
|
initState = initState,
|
||||||
|
startUTC = startUTC
|
||||||
|
)
|
||||||
|
|
||||||
|
return dbQuery.lastInsertedRowId().executeAsOne()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the final results of the test for the action in the database
|
||||||
|
*/
|
||||||
|
internal fun finishResult(id: Long, endState: String, endUTC: String) {
|
||||||
|
dbQuery.finishResult(
|
||||||
|
id = id,
|
||||||
|
endState = endState,
|
||||||
|
endUTC = endUTC,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package io.anthonyberg.connector.shared.database
|
||||||
|
|
||||||
|
internal class TestDatabase (driverFactory: DriverFactory) {
|
||||||
|
private val database = Database(driverFactory.createDriver())
|
||||||
|
private val dbQuery = database.testQueries
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Test record on the database
|
||||||
|
*
|
||||||
|
* @return ID of the Test created
|
||||||
|
*/
|
||||||
|
internal fun startTest(procedureId: Long, startUTC: String) : Long {
|
||||||
|
dbQuery.startTest(
|
||||||
|
procedureId = procedureId,
|
||||||
|
startUTC = startUTC,
|
||||||
|
)
|
||||||
|
|
||||||
|
return dbQuery.lastInsertedRowId().executeAsOne()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates Test record to include the end time
|
||||||
|
*/
|
||||||
|
internal fun endTest(testId: Long, endUTC: String) {
|
||||||
|
dbQuery.endTest(
|
||||||
|
id = testId,
|
||||||
|
endUTC = endUTC,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package io.anthonyberg.connector.shared.entity
|
||||||
|
|
||||||
|
import kotlinx.datetime.TimeZone
|
||||||
|
import kotlinx.datetime.toInstant
|
||||||
|
import kotlinx.datetime.toLocalDateTime
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class ActionResult(
|
||||||
|
@SerialName("id")
|
||||||
|
val id: Int,
|
||||||
|
@SerialName("test_id")
|
||||||
|
val testId: Int,
|
||||||
|
@SerialName("action_id")
|
||||||
|
val actionId: Int,
|
||||||
|
@SerialName("init_state")
|
||||||
|
val initState: String,
|
||||||
|
@SerialName("end_state")
|
||||||
|
val endState: String?,
|
||||||
|
@SerialName("description")
|
||||||
|
val description: String,
|
||||||
|
@SerialName("start_utc")
|
||||||
|
val startUTC: String,
|
||||||
|
@SerialName("end_utc")
|
||||||
|
val endUTC: String?,
|
||||||
|
) {
|
||||||
|
// Convert String time to LocalDateTime variable
|
||||||
|
var start = startUTC.toInstant().toLocalDateTime(TimeZone.UTC)
|
||||||
|
var end = endUTC?.toInstant()?.toLocalDateTime(TimeZone.UTC)
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package io.anthonyberg.connector.shared.entity
|
||||||
|
|
||||||
|
import kotlinx.datetime.TimeZone
|
||||||
|
import kotlinx.datetime.toInstant
|
||||||
|
import kotlinx.datetime.toLocalDateTime
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class Test(
|
||||||
|
@SerialName("id")
|
||||||
|
val id: Int,
|
||||||
|
@SerialName("procedure_id")
|
||||||
|
val procedureId: Int,
|
||||||
|
@SerialName("actions")
|
||||||
|
val actions: List<ActionResult>?,
|
||||||
|
@SerialName("start_utc")
|
||||||
|
val startUTC: String,
|
||||||
|
@SerialName("end_utc")
|
||||||
|
val endUTC: String?,
|
||||||
|
) {
|
||||||
|
// Convert String time to LocalDateTime variable
|
||||||
|
var start = startUTC.toInstant().toLocalDateTime(TimeZone.UTC)
|
||||||
|
var end = endUTC?.toInstant()?.toLocalDateTime(TimeZone.UTC)
|
||||||
|
}
|
||||||
@@ -18,3 +18,6 @@ finishResult:
|
|||||||
UPDATE ActionResult
|
UPDATE ActionResult
|
||||||
SET endState = ?, endUTC = ?
|
SET endState = ?, endUTC = ?
|
||||||
WHERE id = ?;
|
WHERE id = ?;
|
||||||
|
|
||||||
|
lastInsertedRowId:
|
||||||
|
SELECT last_insert_rowid();
|
||||||
|
|||||||
@@ -14,3 +14,6 @@ endTest:
|
|||||||
UPDATE Test
|
UPDATE Test
|
||||||
SET endUTC = ?
|
SET endUTC = ?
|
||||||
WHERE id = ?;
|
WHERE id = ?;
|
||||||
|
|
||||||
|
lastInsertedRowId:
|
||||||
|
SELECT last_insert_rowid();
|
||||||
|
|||||||
Reference in New Issue
Block a user