-
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.
Merge pull request #11 from espoon-voltti/integration-test-setup
integration test setup
- Loading branch information
Showing
6 changed files
with
119 additions
and
23 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
50 changes: 50 additions & 0 deletions
50
service/src/test/kotlin/fi/espoo/oppivelvollisuus/FullApplicationTest.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,50 @@ | ||
package fi.espoo.oppivelvollisuus | ||
|
||
import org.jdbi.v3.core.Jdbi | ||
import org.jdbi.v3.core.kotlin.withHandleUnchecked | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.TestInstance | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
@SpringBootTest( | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT | ||
) | ||
abstract class FullApplicationTest { | ||
@Autowired | ||
protected lateinit var jdbi: Jdbi | ||
|
||
@BeforeAll | ||
fun beforeAll() { | ||
jdbi.withHandleUnchecked { tx -> | ||
tx.execute( | ||
""" | ||
CREATE OR REPLACE FUNCTION reset_database() RETURNS void AS ${'$'}${'$'} | ||
BEGIN | ||
EXECUTE ( | ||
SELECT 'TRUNCATE TABLE ' || string_agg(quote_ident(table_name), ', ') || ' CASCADE' | ||
FROM information_schema.tables | ||
WHERE table_schema = 'public' | ||
AND table_type = 'BASE TABLE' | ||
AND table_name <> 'flyway_schema_history' | ||
); | ||
EXECUTE ( | ||
SELECT 'SELECT ' || coalesce(string_agg(format('setval(%L, %L, false)', sequence_name, start_value), ', '), '') | ||
FROM information_schema.sequences | ||
WHERE sequence_schema = 'public' | ||
); | ||
END ${'$'}${'$'} LANGUAGE plpgsql; | ||
""".trimIndent() | ||
) | ||
} | ||
} | ||
|
||
@BeforeEach | ||
fun beforeEach() { | ||
jdbi.withHandleUnchecked { tx -> | ||
tx.execute("SELECT reset_database()") | ||
} | ||
} | ||
} |
11 changes: 7 additions & 4 deletions
11
service/src/test/kotlin/fi/espoo/oppivelvollisuus/ServiceApplicationTests.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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package fi.espoo.oppivelvollisuus | ||
|
||
import org.junit.jupiter.api.Test | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import kotlin.test.assertEquals | ||
|
||
@SpringBootTest | ||
class ServiceApplicationTests { | ||
class ServiceApplicationTests : FullApplicationTest() { | ||
@Autowired | ||
lateinit var controller: MainController | ||
|
||
@Test | ||
fun contextLoads() { | ||
fun `get empty list of students`() { | ||
assertEquals(emptyList(), controller.getStudents()) | ||
} | ||
} |
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,5 @@ | ||
spring: | ||
datasource: | ||
url: "jdbc:postgresql://localhost:6432/oppivelvollisuus_it" | ||
username: "oppivelvollisuus" | ||
password: "postgres" |