Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
Report an error when the schema load fails
Browse files Browse the repository at this point in the history
[#135665631]
  • Loading branch information
Konstantin Semenov committed Feb 11, 2017
1 parent a7be34a commit 8736a57
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ class DatabaseTestProjectRunner(val testCaseRunner: TestCaseRunner, val scriptEx
}

private fun TrilogyTestProject.runTests(): TestProjectResult {
applySchema()
tryToApplySchema()
runSourceScripts()
return runTestCases()
}

private fun TrilogyTestProject.tryToApplySchema() {
try {
applySchema()
} catch(e: RuntimeException) {
val errorObjects = listOf(e.localizedMessage.prependIndent(" "))
val message = createErrorMessage("testProjectRunner.errors.schema.invalid", errorObjects)
throw SchemaLoadFailedException(message, e)
}
}

private fun TrilogyTestProject.runSourceScripts() {
sourceScripts.forEach { script ->
sourceScripts.forEach { (name, content) ->
try {
scriptExecuter.execute(script.content)
scriptExecuter.execute(content)
} catch(e: BadSqlGrammarException) {
val errorObjects = listOf(script.name, e.localizedMessage.prependIndent(" "))
val errorObjects = listOf(name, e.localizedMessage.prependIndent(" "))
val message = createErrorMessage("testProjectRunner.errors.scripts.invalid", errorObjects)
throw SourceScriptLoadException(message, e)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.pivotal.trilogy.testrunner

class SchemaLoadFailedException(message: String, exception: RuntimeException) : UnrecoverableException(message, exception)
1 change: 1 addition & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ testCaseRunner.errors.missingFixture = Unable to find fixture ''{0}''
testCaseRunner.errors.fixtureRun = Unable to load the ''{0}'' {1} fixture\n{2}
assertionExecuter.error = Assertion failure: {0}\n{1}
testProjectRunner.errors.scripts.invalid = Unable to load script ''{0}'':\n{1}
testProjectRunner.errors.schema.invalid = Unable to load schema:\n{0}
connectionFailure = Unable to connect to the database:\n{0}
applicationUsage = Usage: trilogy [<filePath>|--project=<path to trilogy test project>] --db_url=<jdbc url> --db_user=<db user name> --db_password=<db user password>\nThe db_url, db_user and db_password can be replaced by setting environment variables with the same name in upper case
fatalFailure = [STOP] Execution aborted - the database may be in an inconsistent state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.pivotal.trilogy.testcase.ProcedureTrilogyTestCase
import io.pivotal.trilogy.testproject.TestProjectBuilder
import io.pivotal.trilogy.testproject.TrilogyTestProject
import io.pivotal.trilogy.testproject.UrlTestProjectResourceLocator
import org.amshove.kluent.AnyException
import org.jetbrains.spek.api.Spek
import org.springframework.jdbc.BadSqlGrammarException
import java.io.File
Expand Down Expand Up @@ -76,7 +77,7 @@ class DatabaseTestProjectRunnerTests : Spek({
}
}

describe("test cases with schema definition") {
describe("test project with schema definition") {
val project = projectNamed("schema")

it("excludes fixtures from test case file list") {
Expand All @@ -90,6 +91,27 @@ class DatabaseTestProjectRunnerTests : Spek({
DatabaseTestProjectRunner(TestCaseRunnerMock(), scriptExecuterMock).run(project)
scriptExecuterMock.executeArgList.first() shouldStartWith "CREATE TABLE CLIENTS"
}

it("stops on failed schema load") {
val scriptExecuterMock = ScriptExecuterMock()
scriptExecuterMock.shouldFailExecution = true
{ DatabaseTestProjectRunner(TestCaseRunnerMock(), scriptExecuterMock).run(project) } shouldThrow SchemaLoadFailedException::class
}

it("describes the schema load error") {
val scriptExecutorMock = ScriptExecuterMock()
scriptExecutorMock.shouldFailExecution = true
scriptExecutorMock.failureException = RuntimeException("The transformator\nis quickly seismic.")
val exception = try {
DatabaseTestProjectRunner(TestCaseRunnerMock(), scriptExecutorMock).run(project)
null
} catch (e: SchemaLoadFailedException) {
e
}

val error = "Unable to load schema:\n The transformator\n is quickly seismic."
expect(error) { exception!!.localizedMessage }
}
}

describe("test case with broken source scripts") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
THE SPACE HARVESTS COLLISION COURSE LIKE A HUMAN GREEN PEOPLE.
GREEN PEOPLE
THE TRANSFORMATOR DIES RUMOUR LIKE AN EVASIVE MERMAID.
CAPTAIN'S QUARTERS;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Everyone loves the asperity of pickles pudding coverd with ground corn syrup.
Ho-ho-ho! horror of love. Fly proudly like a final green people. Era secundus caesium est.
11 changes: 11 additions & 0 deletions src/test/resources/projects/broken_schema/tests/testcase.stt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TEST CASE COUNT_CLIENTS
Example
## TEST
We should be able to count the clients
### DATA
|CLIENT_COUNT$|=ERROR=|
|------------:|-------|
| 0 | |
| 0 | |
| 0 | |
| 0 | |

0 comments on commit 8736a57

Please sign in to comment.