tests and sql fixes
This commit is contained in:
parent
07b3c3821f
commit
6ad8e8f353
9 changed files with 53 additions and 19 deletions
|
@ -1,10 +1,7 @@
|
|||
package dev.thebread
|
||||
|
||||
import dev.thebread.Model.PreparedStatementCase
|
||||
import dev.thebread.Models.Stage
|
||||
import java.sql.PreparedStatement
|
||||
import java.sql.ResultSet
|
||||
import java.time.LocalDate
|
||||
import java.util.Date
|
||||
|
||||
|
||||
|
@ -28,9 +25,6 @@ abstract class Model<T : Model<T>> {
|
|||
fun assureStatements(): Map<PreparedStatementCase, PreparedStatement> {
|
||||
return stmts!!
|
||||
}
|
||||
fun setStatements(stmts: Map<PreparedStatementCase, PreparedStatement>) {
|
||||
Common.stmts = stmts
|
||||
}
|
||||
abstract fun get(id: Int): T?
|
||||
abstract fun getAll(): Set<T>
|
||||
abstract fun create(): T
|
||||
|
@ -72,19 +66,20 @@ class Models {
|
|||
override fun create(): User {
|
||||
val stmt = assureStatements()[PreparedStatementCase.ADD_USER]!!
|
||||
stmt.setString(1, this.name)
|
||||
if(this.local != null) stmt.setInt(2, this.local!!)
|
||||
if(this.discord != null) stmt.setInt(3, this.discord!!)
|
||||
if(this.google != null) stmt.setInt(4, this.google!!)
|
||||
if(this.local != -1) stmt.setInt(2, this.local)
|
||||
if(this.discord != -1) stmt.setInt(3, this.discord)
|
||||
if(this.google != -1) stmt.setInt(4, this.google)
|
||||
val result = stmt.executeQuery()
|
||||
val user = User(result)
|
||||
stmt.clearParameters()
|
||||
this.id = user.id
|
||||
return user
|
||||
}
|
||||
}
|
||||
|
||||
class List() : Model<List>() {
|
||||
lateinit var name: String
|
||||
lateinit var description: String
|
||||
var description: String? = null
|
||||
|
||||
constructor(result: ResultSet) : this() {
|
||||
this.id = result.getInt("id")
|
||||
|
@ -121,7 +116,7 @@ class Models {
|
|||
|
||||
class Stage() : Model<Stage>() {
|
||||
lateinit var name: String
|
||||
lateinit var description: String
|
||||
var description: String? = null
|
||||
var list: List? = null
|
||||
|
||||
constructor(result: ResultSet) : this() {
|
||||
|
@ -164,7 +159,7 @@ class Models {
|
|||
|
||||
class Task() : Model<Task>() {
|
||||
lateinit var name: String
|
||||
lateinit var description: String
|
||||
var description: String? = null
|
||||
var stage: Stage? = null
|
||||
var due: Date? = null
|
||||
var dueStage: Stage? = null
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
insert into task_stages
|
||||
values (?1, ?2, ?3)
|
||||
values (?1, ?2, ?3);
|
|
@ -0,0 +1 @@
|
|||
;
|
|
@ -0,0 +1 @@
|
|||
;
|
|
@ -1,3 +1,3 @@
|
|||
update tasks
|
||||
set stage = ?1
|
||||
where id = ?2
|
||||
where id = ?2;
|
|
@ -0,0 +1 @@
|
|||
;
|
|
@ -0,0 +1 @@
|
|||
;
|
3
src/test/kotlin/CliTests.kt
Normal file
3
src/test/kotlin/CliTests.kt
Normal file
|
@ -0,0 +1,3 @@
|
|||
class CliTests {
|
||||
|
||||
}
|
|
@ -1,10 +1,42 @@
|
|||
import org.junit.jupiter.api.*;
|
||||
|
||||
import dev.thebread.KlistApp;
|
||||
import dev.thebread.Model
|
||||
import dev.thebread.Model.PreparedStatementCase
|
||||
import dev.thebread.Models
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.sql.Connection;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import java.sql.DriverManager
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class DatabaseTests {
|
||||
private val app : KlistApp = KlistApp(null);
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
val conn = DriverManager.getConnection("jdbc:sqlite::memory:")
|
||||
|
||||
val initSql = javaClass.getResource("/init.sql")!!.readText()
|
||||
conn.createStatement().executeUpdate(initSql)
|
||||
|
||||
Model.Common.stmts = PreparedStatementCase.entries.associateWith { case ->
|
||||
conn.prepareStatement(
|
||||
javaClass.getResource("/prepare/${
|
||||
case.toString().lowercase()
|
||||
}.sql")!!.readText()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun createUser() {
|
||||
val user = Models.User()
|
||||
user.name = "test"
|
||||
user.create()
|
||||
assertTrue { user.id != -1 && user.id != null }
|
||||
val r = Models.User().get(user.id!!)
|
||||
assertTrue { r != null && r.id != null && r.name == user.name }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getUser() {
|
||||
val user = Models.User().getAll()
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue