4

Ich habe Probleme mit Entwicklungen beim Ausführen von Tests im Rahmen SpielWie man Spielevolutionen anwendet, wenn man Tests im Spiel-Rahmen durchführt?

  • playframework V2.6.6 für scala
  • Play-Slick v3.0.2
  • Play-Slick-Evolutionen v3.0.2
mit

Der Test sieht wie folgt aus:

class TestFooController extends PlaySpec with GuiceOneServerPerSuite { 
    "foo endpoint should store some data" in { 
    val wsClient = app.injector.instanceOf[WSClient] 
    val url = s"http://localhost:$port/foo" 
    val requestData = Json.obj("foo" -> "bar") 
    val response = await(wsClient.url(url).post(requestData)) 
    response.status mustBe OK 
    } 
} 

Die Datenbank configuratio n sieht wie folgt aus:

slick.dbs.default.driver="slick.driver.H2Driver$" 
slick.dbs.default.db.driver="org.h2.Driver" 
slick.dbs.default.db.url="jdbc:h2:mem:play" 

asume gibt es eine Evolution-Skript, das die Tabelle foos und dieses Skript funktioniert gut in dev-Modus erstellt.

Beim Ausführen des Tests der folgende Fehler ausgelöst:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[JdbcSQLException: Table "foos" not found;

Die Tabelle foos nicht so gehe ich davon gefunden werden konnte, dass Datenbank Entwicklungen nicht angewendet wurden.

Dann änderte ich die Datenbankkonfiguration zu postgresql, die im Dev-Modus verwendet wird.

Mit dieser Konfiguration funktionieren die Tests einwandfrei und die Daten werden in der Datenbank gespeichert, so dass Datenbankentwicklungen problemlos verlaufen.

Jetzt ist das Problem, dass die Datenbank nach Tests nicht bereinigt wird. Ich möchte jede Testsuite mit einer sauberen Datenbank ausführen.

Zusammenfassend. Mit H2Db Entwicklungen werden nicht angewendet, mit Postgresql Entwicklungen werden angewendet, aber nicht aufgeräumt.

Auch wenn dies explizit definiert in application.test.conf

play.evolutions.autoApply=true 
play.evolutions.autoApplyDowns=true 

Ich habe auch versucht

play.evolutions.db.default.autoApply=true 
play.evolutions.db.default.autoApplyDowns=true 

keine Wirkung.

Dann habe ich versucht, dies zu tun manuell über:

def withManagedDatabase[T](block: Database => T): Unit = { 
    val dbapi = app.injector.instanceOf[DBApi] 
    val database = dbapi.database("default") 
    Evolutions.applyEvolutions(database) 
    block(database) 
    Evolutions.cleanupEvolutions(database) 
    } 

und dann den Test zu ändern:

"foo endpoint should store some data" in withManagedDatabase { _ => 
    ... 
    } 

Für die H2-Datenbankkonfiguration sie keinen Effekt hat, der gleiche Fehler, dass Tabelle foos kann nicht gefunden werden geworfen. Für die PostgreSQL-Datenbank-Konfiguration ist eine Weiterentwicklung Ausnahmen

play.api.db.evolutions.InconsistentDatabase: Database 'default' is in an inconsistent state![An evolution has not been applied properly. Please check the problem and resolve it manually before marking it as resolved.]

geworfen ich Evolution ups laufen soll, bevor und Entwicklung downs nach jeder Testsuite ausgeführt wird. Wie kann dies erreicht werden?

Antwort

0

Dies ist für mich arbeiten:

class DAOSpec extends PlaySpec with GuiceOneAppPerSuite { 

    val dbUrl = sys.env.getOrElse("DATABASE_URL", "postgres://foo:[email protected]:5432/foo") 

    val testConfig = Map("db.default.url" -> dbUrl) 

    implicit override def fakeApplication() = new GuiceApplicationBuilder().configure(testConfig).build() 

    lazy val database = app.injector.instanceOf[Database] 
    lazy val dao = app.injector.instanceOf[DAO] 

    "create" must { 
    "work" in Evolutions.withEvolutions(database) { 
     val foo = await(dao.create("foo")) 
     foo.id must not be null 
    } 
    } 

} 
Verwandte Themen