Skip to content

Commit

Permalink
add simple test for files
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Nov 5, 2024
1 parent de6271e commit 51bd420
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ jobs:
- name: Run SQL kenerator
uses: gradle/gradle-build-action@ac2d340dc04d9e1113182899e983b5400c17cda1 # v3
with:
arguments: :modules:kenerator:sql:run
arguments: :modules:kenerator:sql:run
- name: Run test
uses: gradle/gradle-build-action@ac2d340dc04d9e1113182899e983b5400c17cda1 # v3
with:
arguments: :web-api:test
2 changes: 2 additions & 0 deletions web-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ dependencies {
// Exposed
implementation(libs.exposed.core)
implementation(libs.exposed.dao)
// test
testImplementation(kotlin("test"))
// Services
implementation(projects.modules.buildKonfig)
implementation(projects.modules.model)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.flipperdevices.ifrmvp.backend.route.signal.data

import com.flipperdevices.ifrmvp.backend.db.signal.di.SignalApiModule
import com.flipperdevices.ifrmvp.backend.db.signal.table.BrandTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.CategoryTable
import com.flipperdevices.ifrmvp.backend.envkonfig.EnvKonfig
import com.flipperdevices.ifrmvp.backend.model.CategoryConfiguration
import com.flipperdevices.ifrmvp.backend.model.CategoryType
import com.flipperdevices.ifrmvp.backend.model.DeviceKey
import com.flipperdevices.ifrmvp.backend.model.SignalRequestModel
import com.flipperdevices.ifrmvp.backend.route.signal.data.model.IncludedFile
import com.flipperdevices.ifrmvp.model.buttondata.UnknownButtonData
import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.andWhere
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class IncludedFilesRepositoryTest {
private var signalApiModule: SignalApiModule? = null
private val requireSignalApiModule: SignalApiModule
get() = signalApiModule ?: error("Forget to register module")

@BeforeTest
fun setup() {
signalApiModule = SignalApiModule.Default(
signalDbConnection = EnvKonfig.signalDatabaseConnection
)
}

@AfterTest
fun tearDown() {
signalApiModule?.database?.run(TransactionManager::closeAndUnregister)
signalApiModule = null
}

fun stubOrderModel(key: DeviceKey) = CategoryConfiguration.OrderModel(
message = "stub",
index = key.ordinal,
key = key,
data = UnknownButtonData
)

@Test
fun `test included files are correct`(): Unit = runBlocking {

val categoryId = transaction(requireSignalApiModule.database) {
CategoryTable.select(CategoryTable.id)
.where { CategoryTable.folderName eq CategoryType.TVS.folderName }
.limit(1)
.map { it[CategoryTable.id] }
.first()
.value
}
val brandId = transaction(requireSignalApiModule.database) {
BrandTable.select(BrandTable.id)
.where { BrandTable.categoryId eq categoryId }
.andWhere { BrandTable.folderName eq "Skyworth" }
.limit(1)
.map { it[BrandTable.id] }
.first()
.value
}
val includedFilesRepository = IncludedFilesRepository(requireSignalApiModule.database)
val signalRepository = SignalRepository(requireSignalApiModule.database)

SignalRequestModel(brandId = brandId).let { request1 ->
val includedFiles1 = includedFilesRepository.findIncludedFiles(signalRequestModel = request1)
assertEquals(31, includedFiles1.size)
val signal1 = signalRepository.getSignalModel(
signalRequestModel = request1,
order = stubOrderModel(DeviceKey.PWR),
includedFiles = includedFiles1.map(IncludedFile::fileId)
)
assertNotNull(signal1)
assertEquals("parsed", signal1.remote.type)
assertEquals("00 00 00 00", signal1.remote.address)
assertEquals("0C 00 00 00", signal1.remote.command)
assertEquals("RC5", signal1.remote.protocol)
SignalRequestModel(
brandId = brandId,
successResults = listOf(SignalRequestModel.SignalResultData(signal1.id))
).let { request2 ->
val includedFiles2 = includedFilesRepository.findIncludedFiles(signalRequestModel = request2)
assertEquals(4, includedFiles2.size)
val signal2 = signalRepository.getSignalModel(
signalRequestModel = request1,
order = stubOrderModel(DeviceKey.VOL_UP),
includedFiles = includedFiles1.map(IncludedFile::fileId)
)
assertNotNull(signal2)
assertEquals("parsed", signal2.remote.type)
assertEquals("00 00 00 00", signal2.remote.address)
assertEquals("10 00 00 00", signal2.remote.command)
assertEquals("RC5", signal2.remote.protocol)
SignalRequestModel(
brandId = brandId,
successResults = listOf(
SignalRequestModel.SignalResultData(signal1.id),
SignalRequestModel.SignalResultData(signal2.id)
)
).let { request3 ->
val includedFiles3 = includedFilesRepository.findIncludedFiles(signalRequestModel = request3)
assertEquals(3, includedFiles3.size)
val signal3 = signalRepository.getSignalModel(
signalRequestModel = request1,
order = stubOrderModel(DeviceKey.VOL_DOWN),
includedFiles = includedFiles1.map(IncludedFile::fileId)
)
assertNotNull(signal3)
}
}
}
}
}

0 comments on commit 51bd420

Please sign in to comment.