Skip to content

Commit

Permalink
Merge pull request #69 from penland365/integration-tests
Browse files Browse the repository at this point in the history
Integration Test setup and QueryIntegrationSpec completed.
  • Loading branch information
penland365 committed May 12, 2016
2 parents 6ab971e + f2bfd2b commit 3f24413
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 9 deletions.
11 changes: 8 additions & 3 deletions build.sbt
Expand Up @@ -2,6 +2,8 @@ import com.typesafe.sbt.SbtGhPages.GhPagesKeys._
import sbtunidoc.Plugin.UnidocKeys._
import ScoverageSbtPlugin._

Defaults.itSettings

lazy val buildSettings = Seq(
organization := "com.github.finagle",
scalaVersion := "2.11.8",
Expand Down Expand Up @@ -42,18 +44,18 @@ lazy val baseSettings = Seq(
scalacOptions ++= compilerOptions,
scalacOptions in (Compile, console) := compilerOptions,
scalacOptions in (Compile, doc) ++= Seq(
"-doc-title", "Roc",
"-doc-title", "roc",
"-doc-version", version.value,
"-groups"
),
libraryDependencies ++= testDependencies.map(_ % "test"),
libraryDependencies ++= testDependencies.map(_ % "it,test"),
resolvers += Resolver.sonatypeRepo("snapshots"),
coverageEnabled := true,
autoAPIMappings := true,
resolvers += "Twitter Maven repo" at "http://maven.twttr.com/"
)

lazy val allSettings = buildSettings ++ baseSettings
lazy val allSettings = buildSettings ++ baseSettings ++ Defaults.itSettings

lazy val coreVersion = "0.0.4-SNAPSHOT"

Expand All @@ -69,13 +71,15 @@ lazy val jawnVersion = "0.8.4"

lazy val roc = project.in(file("."))
.settings(moduleName := "root")
.configs( IntegrationTest )
.settings(allSettings)
.settings(docSettings)
.aggregate(core, types)
.dependsOn(core, types)

lazy val core = project
.settings(moduleName := "roc-core")
.configs( IntegrationTest )
.settings(version := coreVersion)
.settings(allSettings:_*)
.settings(docSettings)
Expand All @@ -91,6 +95,7 @@ lazy val core = project
lazy val types = project
.settings(moduleName := "roc-types")
.settings(version := coreVersion)
.configs( IntegrationTest )
.settings(allSettings:_*)
.settings(docSettings)
.settings(sharedPublishSettings)
Expand Down
4 changes: 4 additions & 0 deletions circle.yml
Expand Up @@ -11,6 +11,10 @@ dependencies:
- which sbt
- sbt sbt-version
test:
pre:
- sbt it:compile
override:
- sbt test:test it:test
post:
- mkdir -p $CIRCLE_TEST_REPORTS/junit/
- find . -type f -regex ".*/core/target/test-reports/.*xml" -exec cp {} $CIRCLE_TEST_REPORTS/junit/ \;
Expand Down
5 changes: 5 additions & 0 deletions core/src/it/resources/sql/query/create.sql
@@ -0,0 +1,5 @@
CREATE TABLE roc_tests(
id SERIAL PRIMARY KEY,
name CHARACTER VARYING(100) NOT NULL
)
WITH(OIDS=FALSE);
1 change: 1 addition & 0 deletions core/src/it/resources/sql/query/insert.sql
@@ -0,0 +1 @@
INSERT INTO roc_tests (name) VALUES ('finagle');
1 change: 1 addition & 0 deletions core/src/it/resources/sql/query/update.sql
@@ -0,0 +1 @@
UPDATE roc_tests SET name = 'updated-finagle-name' WHERE id = 1;
33 changes: 33 additions & 0 deletions core/src/it/scala/roc/Client.scala
@@ -0,0 +1,33 @@
package roc
package integrations

import com.twitter.finagle.{Addr, Address, Name, Service}
import com.twitter.util.Var
import scala.io.Source

trait Client {
private val db = "circle_test"
private val user = "ubuntu"
private val passwd = ""
private val host = "127.0.0.1"
private val port = 5432

private lazy val address = Address(host, port)
protected lazy val Postgres = Postgresql.client
.withUserAndPasswd(user, passwd)
.withDatabase(db)
.newRichClient(
Name.Bound(Var[Addr](Addr.Bound(address)), "roc"),
"roc"
)
}

trait SqlReader {

def readSql(filename: String): String = {
val path = s"core/src/it/resources/sql/$filename"
Source.fromFile(path)
.getLines
.foldLeft("")(_ + _)
}
}
63 changes: 63 additions & 0 deletions core/src/it/scala/roc/QuerySpec.scala
@@ -0,0 +1,63 @@
package roc
package integrations

import com.twitter.finagle.{Addr, Address, Name, Service}
import com.twitter.util.Var
import com.twitter.util.Await
import org.specs2.Specification
import roc.postgresql.Request
import scala.io.Source

final class QuerySpec extends Specification
with SqlReader
with Client { def is = sequential ^ s2"""

Query
must execute a CREATE command $testCreate
must execute an INSERT command $testInsert
must execute an UPDATE command $testUpdate
must execute a SELECT command $testSelect
must execute a DELETE command $testDelete
must execute a DROP command $testDrop

"""

def testCreate() = {
val sql = readSql("query/create.sql")
val request = new Request(sql)
val result = Await.result(Postgres.query(request))
result.completedCommand must_== "CREATE TABLE"
}

def testInsert() = {
val sql = readSql("query/insert.sql")
val request = new Request(sql)
val result = Await.result(Postgres.query(request))
result.completedCommand must_== "INSERT 0 1"
}

def testUpdate() = {
val sql = readSql("query/update.sql")
val request = new Request(sql)
val result = Await.result(Postgres.query(request))
result.completedCommand must_== "UPDATE 1"
}

def testSelect() = {
val request = new Request("SELECT name FROM roc_tests WHERE id = 1")
val result = Await.result(Postgres.query(request))
result.toList.length must_== 1
}

def testDelete() = {
val request = new Request("DELETE FROM roc_tests WHERE id = 1;")
val result = Await.result(Postgres.query(request))
result.completedCommand must_== "DELETE 1"
}

def testDrop() = {
val request = new Request("DROP TABLE roc_tests;")
val result = Await.result(Postgres.query(request))
result.completedCommand must_== "DROP TABLE"
}
}
10 changes: 4 additions & 6 deletions core/src/main/scala/roc/postgresql/ClientDispatcher.scala
Expand Up @@ -139,9 +139,7 @@ private[roc] final class ClientDispatcher(trans: Transport[Packet, Packet],
case AuthenticationGSS =>
Future.exception(new UnsupportedAuthenticationFailure("AuthenticationGSS"))
case ErrorResponse(m) => Future.exception(new PostgresqlServerFailure(m))
case u => println(u); Future.exception(
new PostgresqlStateMachineFailure("StartupMessage", u.toString)
)
case u => Future.exception(new PostgresqlStateMachineFailure("StartupMessage", u.toString))
})
}

Expand Down Expand Up @@ -177,7 +175,7 @@ private[roc] final class ClientDispatcher(trans: Transport[Packet, Packet],
val pm = new PasswordMessage(startup.password)
exchange(pm).flatMap(response => response match {
case AuthenticationOk => Future.Done
case ErrorResponse(_) => Future.exception(new Exception())
case ErrorResponse(e) => Future.exception(new PostgresqlServerFailure(e))
case u => Future.exception(
new PostgresqlStateMachineFailure("PasswordMessage", u.toString)
)
Expand All @@ -192,8 +190,8 @@ private[roc] final class ClientDispatcher(trans: Transport[Packet, Packet],
salt)
val pm = new PasswordMessage(encryptedPasswd)
exchange(pm).flatMap(response => response match {
case AuthenticationOk => Future.Done
case er: ErrorResponse => Future.exception(new Exception())
case AuthenticationOk => Future.Done
case ErrorResponse(e) => Future.exception(new PostgresqlServerFailure(e))
case u => Future.exception(new PostgresqlStateMachineFailure("PasswordMessage", u.toString))
})
}
Expand Down

0 comments on commit 3f24413

Please sign in to comment.