Skip to content

Commit ff29ea1

Browse files
committed
refactored tests
1 parent 39f4a47 commit ff29ea1

File tree

2 files changed

+62
-47
lines changed

2 files changed

+62
-47
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.vertx.asyncsql.test
2+
3+
import org.vertx.java.core.json.JsonObject
4+
import scala.concurrent.Future
5+
import scala.concurrent.Promise
6+
import org.vertx.scala.core.Vertx
7+
import org.vertx.testtools.TestVerticle
8+
import org.vertx.testtools.VertxAssert._
9+
import io.vertx.helpers.VertxExecutionContext
10+
import org.vertx.scala.core.eventbus.Message
11+
import java.util.concurrent.atomic.AtomicInteger
12+
13+
trait BaseVertxIntegrationTest extends VertxExecutionContext { this: TestVerticle =>
14+
lazy val log = getContainer().logger()
15+
16+
val address: String
17+
18+
protected def asyncTest[X](fn: => Future[X]): Future[Unit] = {
19+
fn recover {
20+
case x =>
21+
log.error("async fail in test code!", x)
22+
fail("Something failed asynchronously: " + x.getClass() + x.getMessage())
23+
} map { _ =>
24+
testComplete
25+
}
26+
}
27+
28+
protected def ebSend(q: JsonObject): Future[JsonObject] = {
29+
val p = Promise[JsonObject]
30+
log.info("sending " + q.encode() + " to " + address)
31+
Vertx(getVertx()).eventBus.send(address, q) { reply: Message[JsonObject] =>
32+
log.info("got a reply: " + reply.body.encode())
33+
p.success(reply.body)
34+
}
35+
p.future
36+
}
37+
38+
protected def expectOk(q: JsonObject): Future[JsonObject] = ebSend(q) map { reply =>
39+
assertEquals("ok", reply.getString("status"))
40+
reply
41+
}
42+
43+
protected def expectError(q: JsonObject, errorId: Option[String] = None, errorMessage: Option[String] = None): Future[JsonObject] = ebSend(q) map { reply =>
44+
assertEquals("error", reply.getString("status"))
45+
errorId.map(assertEquals(_, reply.getString("id")))
46+
errorMessage.map(assertEquals(_, reply.getString("message")))
47+
reply
48+
}
49+
50+
}

src/test/scala/io/vertx/asyncsql/test/SqlTestVerticle.scala

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import org.vertx.scala.core.Vertx
1616
import io.vertx.helpers.VertxScalaHelpers
1717
import org.vertx.scala.core.json._
1818

19-
abstract class SqlTestVerticle extends org.vertx.testtools.TestVerticle with VertxExecutionContext with VertxScalaHelpers {
20-
21-
lazy val log = container.logger()
19+
abstract class SqlTestVerticle extends org.vertx.testtools.TestVerticle with BaseVertxIntegrationTest with VertxScalaHelpers {
2220

2321
override def start() = {
2422
initialize()
@@ -44,40 +42,6 @@ abstract class SqlTestVerticle extends org.vertx.testtools.TestVerticle with Ver
4442

4543
def getConfig(): JsonObject = Json.emptyObj()
4644

47-
val address: String
48-
49-
protected def asyncTest[X](fn: => Future[X]) = {
50-
fn recover {
51-
case x =>
52-
log.error("async fail in test code!", x)
53-
fail("Something failed asynchronously: " + x.getClass() + x.getMessage())
54-
} map { _ =>
55-
testComplete()
56-
}
57-
}
58-
59-
protected def ebSend(q: JsonObject): Future[JsonObject] = {
60-
val p = Promise[JsonObject]
61-
log.info("sending " + q.encode() + " to " + address)
62-
Vertx(vertx).eventBus.send(address, q) { reply: Message[JsonObject] =>
63-
log.info("got a reply: " + reply.body.encode())
64-
p.success(reply.body)
65-
}
66-
p.future
67-
}
68-
69-
protected def expectOk(q: JsonObject): Future[JsonObject] = ebSend(q) map { reply =>
70-
assertEquals("ok", reply.getString("status"))
71-
reply
72-
}
73-
74-
protected def expectError(q: JsonObject, errorId: Option[String] = None, errorMessage: Option[String] = None): Future[JsonObject] = ebSend(q) map { reply =>
75-
assertEquals("error", reply.getString("status"))
76-
errorId.map(assertEquals(_, reply.getString("id")))
77-
errorMessage.map(assertEquals(_, reply.getString("message")))
78-
reply
79-
}
80-
8145
protected def raw(q: String) = Json.obj("action" -> "raw", "command" -> q)
8246

8347
protected def insert(table: String, fields: JsonArray, values: JsonArray) =
@@ -87,7 +51,16 @@ abstract class SqlTestVerticle extends org.vertx.testtools.TestVerticle with Ver
8751

8852
protected def prepared(statement: String, values: JsonArray) = Json.obj("action" -> "prepared", "statement" -> statement, "values" -> values)
8953

90-
protected def createTable(tableName: String) = expectOk(raw("""
54+
protected def createTable(tableName: String) = expectOk(raw(createTableStatement(tableName))) map { reply =>
55+
assertEquals(0, reply.getNumber("rows"))
56+
reply
57+
}
58+
59+
protected def dropTable(tableName: String) = expectOk(raw("DROP TABLE " + tableName + ";")) map { reply =>
60+
reply
61+
}
62+
63+
protected def createTableStatement(tableName: String) = """
9164
CREATE TABLE """ + tableName + """ (
9265
id SERIAL,
9366
name VARCHAR(255),
@@ -97,13 +70,5 @@ CREATE TABLE """ + tableName + """ (
9770
money FLOAT,
9871
wedding_date DATE
9972
);
100-
""")) map { reply =>
101-
assertEquals(0, reply.getNumber("rows"))
102-
reply
103-
}
104-
105-
protected def dropTable(tableName: String) = expectOk(raw("DROP TABLE " + tableName + ";")) map { reply =>
106-
reply
107-
}
108-
73+
"""
10974
}

0 commit comments

Comments
 (0)