question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to test repository?

See original GitHub issue

Hi!

I need to know how can I test my repository. For example:

class AdminRepositoryImpl : AdminRepository {
    override fun create(builder: AdminParams): Int {
        return transaction {
            Admins.insert {
                it[login] = builder.login
                it[password] = builder.password
                it[createdAt] = DateTime.now()
            }[Admins.id].value
        }
    }
}

How can I test it? I use ktor + exposed, so I thought that “withTestApplication” will be work but it doesn’t .

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
hfhbdcommented, Apr 8, 2021

Depending on your test/use case I use this test function:

@OptIn(ExperimentalContracts::class)
fun dbTest(
    name: String? = null,
    setup: suspend (Database) -> Unit = { },
    test: suspend (Database) -> Unit
) {
    contract {
        callsInPlace(setup, InvocationKind.EXACTLY_ONCE)
        callsInPlace(test, InvocationKind.EXACTLY_ONCE)
    }
    val dbName = name ?: "test${SecureRandom.nextUUID()}"
    val db = Database.connect("jdbc:h2:mem:$dbName;DB_CLOSE_DELAY=-1;", "org.h2.Driver")
    transaction(db) {
        runBlocking {
            setup(db)
        }
    }
    runBlocking {
        test(db)
    }
    TransactionManager.closeAndUnregister(db)
}

Remove the “features” you don’t use, eg contracts, suspend, name etc. Usage

@Test
fun createTest() = dbTest(setup = {
    SchemaUtils.create(Places)
}) { db ->
    val controller = PlaceController(db = db)
    controller.create(place1)
    controller.create(place2)
    assertEquals(SimilarResult(null, listOf(place1)), controller.findSimilar(place1.copy(name = "Real")))
}
0reactions
TheRepratorcommented, Apr 1, 2021

i want to test the above mentioned insert operation, so i can i make sure, whether database is correctly inserted or not with test with the command, i gave, @Tapac @LookBad

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test Spring Data repositories? - Stack Overflow
To make it short - there's no way to unit test Spring Data JPA repositories reasonably for a simple reason: it's way to...
Read more >
How to test services, endpoints, and repositories in Spring Boot
It is quite simple to test. We mock the repository and inject our mocks into UserService. Now when we run the test we'll...
Read more >
Spring Boot JPA - Unit Test Repository - Tutorialspoint
Right Click on the file in eclipse and select Run a JUnit Test and verify the result. Repository Test Result. Previous Page Print...
Read more >
How to Unit Test a Repository Implementation
I like to unit test every layer within my applications. Unit testing data access code can be a little tricky, firstly because they...
Read more >
Controller, Service, and Repository Layer Unit Testing using ...
4. Tips for writing testable code · Strictly follow the SOLID principal · Use dependency injection properly. · Separate object creation and application...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found