-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add usecase testing to backend (#131)
Co-authored-by: Jose Miguel Lozano <[email protected]> Co-authored-by: Arpon <[email protected]>
- Loading branch information
1 parent
54538b0
commit 1be22fe
Showing
39 changed files
with
1,619 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
application/src/jvmTest/kotlin/replace/datastore/InMemoryFileStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package replace.datastore | ||
|
||
import java.io.InputStream | ||
|
||
class InMemoryFileStorage : FileStorage { | ||
|
||
private val backing: MutableMap<String, ByteArray> = mutableMapOf() | ||
|
||
override suspend fun exists(path: String): Boolean { | ||
return backing.containsKey(path) | ||
} | ||
|
||
override suspend fun saveFile(path: String, input: InputStream): Boolean { | ||
backing[path] = input.readBytes() | ||
return true | ||
} | ||
|
||
override suspend fun deleteFile(path: String): Boolean { | ||
backing.remove(path) | ||
return true | ||
} | ||
|
||
override suspend fun copyFile(from: String, to: String): Boolean { | ||
backing[to] = backing[from] ?: return false | ||
return true | ||
} | ||
|
||
override suspend fun readFile(path: String): InputStream { | ||
return backing[path]?.inputStream() ?: InputStream.nullInputStream() | ||
} | ||
|
||
override suspend fun getFileSize(path: String): Long { | ||
return backing[path]?.size?.toLong() ?: 0L | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
application/src/jvmTest/kotlin/replace/usecase/DatabaseTesting.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package replace.usecase | ||
|
||
import org.jetbrains.exposed.sql.Database | ||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.jetbrains.exposed.sql.Table | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import org.testcontainers.containers.PostgreSQLContainer | ||
import replace.model.BookableEntities | ||
import replace.model.BookableEntityTypes | ||
import replace.model.BookedEntities | ||
import replace.model.Bookings | ||
import replace.model.Floors | ||
import replace.model.TemporaryFiles | ||
import replace.model.Users | ||
|
||
val tables: Array<out Table> = arrayOf( | ||
BookedEntities, | ||
BookableEntities, | ||
BookableEntityTypes, | ||
Bookings, | ||
Floors, | ||
TemporaryFiles, | ||
Users, | ||
) | ||
|
||
inline fun useDatabase(block: () -> Unit) { | ||
PostgreSQLContainer("postgres").use { container -> | ||
container.start() | ||
println("Postgres container started @ ${container.jdbcUrl}") | ||
Database.connect( | ||
url = container.jdbcUrl, | ||
driver = "org.postgresql.Driver", | ||
user = "test", | ||
password = "test", | ||
) | ||
println("Successfully connected to postgres") | ||
|
||
transaction { | ||
SchemaUtils.create(*tables) | ||
} | ||
println("Successfully created tables ${tables.joinToString { it.tableName }}") | ||
block() | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...tion/src/jvmTest/kotlin/replace/usecase/bookableentity/CreateBookableEntityUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package replace.usecase.bookableentity | ||
|
||
import io.kotest.assertions.throwables.shouldNotThrowAny | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.shouldNotBe | ||
import io.kotest.property.checkAll | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import replace.model.BookableEntity | ||
import replace.usecase.generator.ReplaceArb | ||
import replace.usecase.generator.bookableEntityCreateDto | ||
import replace.usecase.useDatabase | ||
import java.util.UUID | ||
|
||
class CreateBookableEntityUseCaseTest : FunSpec( | ||
{ | ||
context("happy path") { | ||
test("create a simple bookable entity") { | ||
useDatabase { | ||
checkAll(20, ReplaceArb.bookableEntityCreateDto()) { dto -> | ||
val fromUseCase = CreateBookableEntityUseCase.execute(dto) | ||
|
||
fromUseCase.id shouldNotBe null | ||
shouldNotThrowAny { UUID.fromString(fromUseCase.id) } | ||
fromUseCase.name shouldBe dto.name | ||
fromUseCase.posX shouldBe dto.posX | ||
fromUseCase.posY shouldBe dto.posY | ||
fromUseCase.floorId shouldBe dto.floorId | ||
fromUseCase.typeId shouldBe dto.typeId | ||
fromUseCase.index shouldBe dto.index | ||
|
||
val fromDb = transaction { | ||
BookableEntity.findById(fromUseCase.id) | ||
} | ||
|
||
fromDb shouldNotBe null | ||
fromDb!! | ||
|
||
fromDb.id shouldNotBe null | ||
fromDb.name shouldBe dto.name | ||
fromDb.posX shouldBe dto.posX | ||
fromDb.posY shouldBe dto.posY | ||
fromDb.floorId.toString() shouldBe dto.floorId | ||
fromDb.typeId.toString() shouldBe dto.typeId | ||
fromDb.index shouldBe dto.index | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
) |
51 changes: 51 additions & 0 deletions
51
...src/jvmTest/kotlin/replace/usecase/bookableentity/UpdateBookableEntityOrderUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package replace.usecase.bookableentity | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.constant | ||
import io.kotest.property.arbitrary.list | ||
import io.kotest.property.arbitrary.next | ||
import io.kotest.property.checkAll | ||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction | ||
import replace.dto.UpdateBookableEntityOrderDto | ||
import replace.model.BookableEntities | ||
import replace.model.BookableEntity | ||
import replace.usecase.generator.ReplaceArb | ||
import replace.usecase.generator.bookableEntity | ||
import replace.usecase.generator.floor | ||
import replace.usecase.useDatabase | ||
|
||
class UpdateBookableEntityOrderUseCaseTest : FunSpec( | ||
{ | ||
context("happy path") { | ||
test("Update a simple floor") { | ||
useDatabase { | ||
checkAll(5, ReplaceArb.floor()) { floor -> | ||
val entities = Arb.list(ReplaceArb.bookableEntity(floorArb = Arb.constant(floor)), 1..10).next() | ||
val shuffled = entities.shuffled() | ||
|
||
newSuspendedTransaction { | ||
BookableEntity.find { BookableEntities.floor_id eq floor.id } | ||
.map { it.id.toString() }.sorted() shouldBe shuffled.map { it.id.toString() }.sorted() | ||
} | ||
|
||
UpdateBookableEntityOrderUseCase.execute( | ||
UpdateBookableEntityOrderDto( | ||
floorId = floor.id.toString(), | ||
bookableEntityIds = shuffled.map { it.id.toString() }, | ||
), | ||
) | ||
|
||
newSuspendedTransaction { | ||
BookableEntity.find { BookableEntities.floor_id eq floor.id } | ||
.sortedBy { it.index } | ||
.map { it.id.toString() } | ||
.shouldBe(shuffled.map { it.id.toString() }) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
) |
52 changes: 52 additions & 0 deletions
52
...tion/src/jvmTest/kotlin/replace/usecase/bookableentity/UpdateBookableEntityUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package replace.usecase.bookableentity | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.shouldNotBe | ||
import io.kotest.property.checkAll | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import replace.model.BookableEntity | ||
import replace.usecase.generator.ReplaceArb | ||
import replace.usecase.generator.bookableEntity | ||
import replace.usecase.generator.bookableEntityUpdateDto | ||
import replace.usecase.useDatabase | ||
|
||
class UpdateBookableEntityUseCaseTest : FunSpec( | ||
{ | ||
context("happy path") { | ||
test("Update a simple bookable entity") { | ||
useDatabase { | ||
checkAll(5, ReplaceArb.bookableEntity()) { original -> | ||
checkAll(5, ReplaceArb.bookableEntityUpdateDto(original.id.toString())) { update -> | ||
val fromUseCase = UpdateBookableEntityUseCase.execute(update) | ||
|
||
fromUseCase.id shouldBe update.id | ||
fromUseCase.name shouldBe update.name | ||
fromUseCase.floorId shouldBe update.floorId | ||
fromUseCase.parentId shouldBe update.parentId | ||
fromUseCase.typeId shouldBe update.typeId | ||
fromUseCase.posX shouldBe update.posX | ||
fromUseCase.posY shouldBe update.posY | ||
fromUseCase.index shouldBe update.index | ||
|
||
val fromDB = transaction { | ||
BookableEntity.findById(update.id) | ||
} | ||
fromDB shouldNotBe null | ||
fromDB!! | ||
|
||
fromDB.id.toString() shouldBe update.id | ||
fromDB.name shouldBe update.name | ||
fromDB.floorId.toString() shouldBe update.floorId | ||
fromDB.parentId?.toString() shouldBe update.parentId | ||
fromDB.typeId?.toString() shouldBe update.typeId | ||
fromDB.posX shouldBe update.posX | ||
fromDB.posY shouldBe update.posY | ||
fromDB.index shouldBe update.index | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
) |
41 changes: 41 additions & 0 deletions
41
.../jvmTest/kotlin/replace/usecase/bookableentitytype/CreateBookableEntityTypeUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package replace.usecase.bookableentitytype | ||
|
||
import io.kotest.assertions.throwables.shouldNotThrowAny | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.shouldNotBe | ||
import io.kotest.property.checkAll | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import replace.model.BookableEntityType | ||
import replace.usecase.generator.ReplaceArb | ||
import replace.usecase.generator.bookableEntityTypeCreateDto | ||
import replace.usecase.useDatabase | ||
import java.util.UUID | ||
|
||
class CreateBookableEntityTypeUseCaseTest : FunSpec( | ||
{ | ||
context("happy path") { | ||
test("create a simple bookable entity type") { | ||
useDatabase { | ||
checkAll(20, ReplaceArb.bookableEntityTypeCreateDto()) { dto -> | ||
val fromUseCase = CreateBookableEntityTypeUseCase.execute(dto) | ||
|
||
fromUseCase.id shouldNotBe null | ||
shouldNotThrowAny { UUID.fromString(fromUseCase.id) } | ||
fromUseCase.name shouldBe dto.name | ||
|
||
val fromDb = transaction { | ||
BookableEntityType.findById(fromUseCase.id) | ||
} | ||
|
||
fromDb shouldNotBe null | ||
fromDb!! | ||
|
||
fromDb.id.toString() shouldBe fromUseCase.id | ||
fromDb.name shouldBe fromUseCase.name | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
) |
Oops, something went wrong.