⚗️ Hjelpefunksjoner for å skrive tester til databasen
Co-authored-by: Markus A. R. Johansen <markus.aleksander.rakil.johansen@nav.no> Co-authored-by: Amalie Erdal Mansåker <amalie.erdal.mansaker@nav.no> Co-authored-by: Hege Haavaldsen <hege.haavaldsen@nav.no> Co-authored-by: Helene Arnesen <helene.arnesen@nav.no>
This commit is contained in:
		
							parent
							
								
									77abcef166
								
							
						
					
					
						commit
						9c5613c707
					
				
					 1 changed files with 43 additions and 0 deletions
				
			
		
							
								
								
									
										43
									
								
								backend/src/test/kotlin/no/nav/helse/sprik/TestUtils.kt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								backend/src/test/kotlin/no/nav/helse/sprik/TestUtils.kt
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,43 @@
 | 
			
		|||
package no.nav.helse.sprik
 | 
			
		||||
 | 
			
		||||
import com.zaxxer.hikari.HikariConfig
 | 
			
		||||
import no.nav.helse.sprik.db.Database
 | 
			
		||||
import org.flywaydb.core.Flyway
 | 
			
		||||
import org.junit.jupiter.api.Test
 | 
			
		||||
import org.testcontainers.containers.PostgreSQLContainer
 | 
			
		||||
import kotlin.test.assertEquals
 | 
			
		||||
 | 
			
		||||
// Lager Docker-container som databasen kan kjør på, og setter opp env-variabler for oss
 | 
			
		||||
fun postgres() = PostgreSQLContainer<Nothing>("postgres:15").apply {
 | 
			
		||||
    withReuse(true)
 | 
			
		||||
    withLabel("App", "db")
 | 
			
		||||
    start()
 | 
			
		||||
    println("Databasen har startet opp på port $firstMappedPort")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Lager en testconfig for oss for å connecte til databasen
 | 
			
		||||
fun dbconfig(): HikariConfig {
 | 
			
		||||
    val postgres = postgres()
 | 
			
		||||
    return HikariConfig().apply {
 | 
			
		||||
        jdbcUrl = postgres.jdbcUrl
 | 
			
		||||
        username = postgres.username
 | 
			
		||||
        password = postgres.password
 | 
			
		||||
        maximumPoolSize = 2
 | 
			
		||||
        minimumIdle = 1
 | 
			
		||||
        idleTimeout = 500000
 | 
			
		||||
        connectionTimeout = 10000
 | 
			
		||||
        maxLifetime = 600000
 | 
			
		||||
        initializationFailTimeout = 5000
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Migrerer databasen
 | 
			
		||||
fun Database.configureFlyway(): Database = this.also { database ->
 | 
			
		||||
    Flyway.configure()
 | 
			
		||||
        .dataSource(database.dataSource)
 | 
			
		||||
        .failOnMissingLocations(true)
 | 
			
		||||
        .cleanDisabled(false)
 | 
			
		||||
        .load()
 | 
			
		||||
        .also(Flyway::clean)
 | 
			
		||||
        .migrate()
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue