Skip to content

Refactored the testcode and added zerodate-test for mysql #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions src/test/scala/io/vertx/asyncsql/test/BaseSqlTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ trait BaseSqlTests {

private val timeout: Int = 15000

protected def isMysql: Boolean = false

protected def failedTest: PartialFunction[Throwable, Unit] = {
case ex: Throwable =>
logger.warn("failed in test", ex)
fail("test failed. see warning above")
}

private def sendWithTimeout(json: JsonObject): Future[(Message[JsonObject], JsonObject)] = {
protected def sendWithTimeout(json: JsonObject): Future[(Message[JsonObject], JsonObject)] = {
val p = Promise[(Message[JsonObject], JsonObject)]()
vertx.eventBus.sendWithTimeout(address, json, timeout, {
case Success(reply) => p.success(reply, reply.body())
Expand All @@ -30,7 +28,7 @@ trait BaseSqlTests {
p.future
}

private def replyWithTimeout(msg: Message[JsonObject], json: JsonObject): Future[(Message[JsonObject], JsonObject)] = {
protected def replyWithTimeout(msg: Message[JsonObject], json: JsonObject): Future[(Message[JsonObject], JsonObject)] = {
val p = Promise[(Message[JsonObject], JsonObject)]()
msg.replyWithTimeout(json, timeout, {
case Success(reply) => p.success(reply, reply.body())
Expand All @@ -39,13 +37,13 @@ trait BaseSqlTests {
p.future
}

private def checkOkay(json: JsonObject)(msg: (Message[JsonObject], JsonObject)): (Message[JsonObject], JsonObject) = {
protected def checkOkay(json: JsonObject)(msg: (Message[JsonObject], JsonObject)): (Message[JsonObject], JsonObject) = {
assertEquals(s"should get 'ok' back when sending ${json.encode()}, but got ${msg._2.encode()}",
"ok", msg._2.getString("status"))
(msg._1, msg._2)
}

private def checkError(json: JsonObject)(msg: (Message[JsonObject], JsonObject)): (Message[JsonObject], JsonObject) = {
protected def checkError(json: JsonObject)(msg: (Message[JsonObject], JsonObject)): (Message[JsonObject], JsonObject) = {
assertEquals(s"should get an 'error' back when sending ${json.encode()}, but got ${msg._2.encode()}",
"error", msg._2.getString("status"))
(msg._1, msg._2)
Expand All @@ -57,19 +55,19 @@ trait BaseSqlTests {
protected def sendFail(json: JsonObject): Future[(Message[JsonObject], JsonObject)] =
sendWithTimeout(json) map checkError(json)

private def replyOk(msg: Message[JsonObject], json: JsonObject): Future[(Message[JsonObject], JsonObject)] =
protected def replyOk(msg: Message[JsonObject], json: JsonObject): Future[(Message[JsonObject], JsonObject)] =
replyWithTimeout(msg, json) map checkOkay(json)

private def replyFail(msg: Message[JsonObject], json: JsonObject): Future[(Message[JsonObject], JsonObject)] =
protected def replyFail(msg: Message[JsonObject], json: JsonObject): Future[(Message[JsonObject], JsonObject)] =
replyWithTimeout(msg, json) map checkError(json)

private def setupTableTest(): Future[_] = for {
protected def setupTableTest(): Future[_] = for {
(msg, reply) <- sendOk(raw(createTableStatement("some_test")))
} yield {
assertEquals(0, reply.getInteger("rows"))
}

private def setupTypeTest(): Future[_] = for {
protected def setupTypeTest(): Future[_] = for {
_ <- setupTableTest()
(msg, reply) <- sendOk(insert("some_test",
Json.fromArrayString( """["name","email","is_male","age","money","wedding_date"]"""),
Expand Down Expand Up @@ -389,13 +387,7 @@ trait BaseSqlTests {
| name VARCHAR(255),
| PRIMARY KEY (id)
|);""".stripMargin))
(msg, _) <- replyOk(msg, raw(
s"""CREATE TABLE test_two (
| id SERIAL,
| name VARCHAR(255),
| one_id BIGINT ${if (isMysql) "UNSIGNED" else ""} NOT NULL,
| PRIMARY KEY (id)
|);""".stripMargin))
(msg, _) <- replyOk(msg, raw(createTableTestTwo))
(msg, _) <- replyOk(msg, raw(
"""ALTER TABLE test_two ADD CONSTRAINT test_two_one_id_fk
|FOREIGN KEY (one_id)
Expand Down
45 changes: 25 additions & 20 deletions src/test/scala/io/vertx/asyncsql/test/SqlTestVerticle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ abstract class SqlTestVerticle extends TestVerticle with BaseVertxIntegrationTes

val address = "campudus.asyncdb"

protected val baseConf = Json.obj("address" -> address, "maxPoolSize" -> 3, "transactionTimeout" -> 5000L)
protected val baseConf = Json.obj("address" -> address, "maxPoolSize" -> 3, "transactionTimeout" -> 5000L)

override final def before() {}

override def asyncBefore(): Future[Unit] = {
val p = Promise[Unit]
container.deployModule(System.getProperty("vertx.modulename"), getConfig(), 1, { deploymentID: AsyncResult[String] =>
Expand Down Expand Up @@ -47,6 +48,7 @@ abstract class SqlTestVerticle extends TestVerticle with BaseVertxIntegrationTes
Json.obj("action" -> "insert", "table" -> table, "fields" -> fields, "values" -> values)

protected def select(table: String, fields: JsonArray): JsonObject = select(table, Some(fields))

protected def select(table: String, fields: JsonArray, conditions: JsonObject): JsonObject = select(table, Some(fields), Some(conditions))

protected def select(table: String, fields: Option[JsonArray] = None, conditions: Option[JsonObject] = None): JsonObject = {
Expand All @@ -71,23 +73,26 @@ abstract class SqlTestVerticle extends TestVerticle with BaseVertxIntegrationTes
reply
}

protected def createDateTable(dateDataType :String) = s"""
| CREATE TABLE date_test (
| id SERIAL,
| test_date $dateDataType
| );
""".stripMargin

protected def createTableStatement(tableName: String) = """
DROP TABLE IF EXISTS """ + tableName + """;
CREATE TABLE """ + tableName + """ (
id SERIAL,
name VARCHAR(255),
email VARCHAR(255) UNIQUE,
is_male BOOLEAN,
age INT,
money DOUBLE PRECISION,
wedding_date DATE
);
"""
protected def createDateTable(dateDataType: String) = s"""CREATE TABLE date_test (
| id SERIAL,
| test_date $dateDataType
|);""".stripMargin

protected def createTableStatement(tableName: String) = s"""DROP TABLE IF EXISTS $tableName;
CREATE TABLE $tableName (
| id SERIAL,
| name VARCHAR(255),
| email VARCHAR(255) UNIQUE,
| is_male BOOLEAN,
| age INT,
| money DOUBLE PRECISION,
| wedding_date DATE
|);""".stripMargin

protected def createTableTestTwo: String = s"""CREATE TABLE test_two (
| id SERIAL,
| name VARCHAR(255),
| one_id BIGINT NOT NULL,
| PRIMARY KEY (id)
|);""".stripMargin
}
57 changes: 36 additions & 21 deletions src/test/scala/io/vertx/asyncsql/test/mysql/MySqlTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@ import org.vertx.testtools.VertxAssert._

class MySqlTest extends SqlTestVerticle with BaseSqlTests {

override def isMysql = true

override def doBefore() = expectOk(raw("DROP TABLE IF EXISTS `some_test`"))

override def getConfig() = baseConf.putString("connection", "MySQL")

override def createDateTable(dateDataType: String) = s"""
| CREATE TABLE date_test (
| id INT NOT NULL AUTO_INCREMENT,
| test_date $dateDataType,
| PRIMARY KEY(id)
| );
""".stripMargin

override def createTableStatement(tableName: String) = """
CREATE TABLE """ + tableName + """ (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
email VARCHAR(255) UNIQUE,
is_male BOOLEAN,
age INT,
money FLOAT,
wedding_date DATE,
PRIMARY KEY (id)
);"""
override def createDateTable(dateDataType: String) = s"""CREATE TABLE date_test (
| id INT NOT NULL AUTO_INCREMENT,
| test_date $dateDataType,
| PRIMARY KEY(id)
|);""".stripMargin

override def createTableStatement(tableName: String) = s"""CREATE TABLE $tableName (
| id INT NOT NULL AUTO_INCREMENT,
| name VARCHAR(255),
| email VARCHAR(255) UNIQUE,
| is_male BOOLEAN,
| age INT,
| money FLOAT,
| wedding_date DATE,
| PRIMARY KEY (id)
|);""".stripMargin

override def createTableTestTwo: String = """CREATE TABLE test_two (
| id SERIAL,
| name VARCHAR(255),
| one_id BIGINT UNSIGNED NOT NULL,
| PRIMARY KEY (id)
|);""".stripMargin

@Test
def datetimeTest(): Unit =
Expand All @@ -48,4 +50,17 @@ CREATE TABLE """ + tableName + """ (
testComplete()
}) recover failedTest

@Test
def zeroDateTest(): Unit = (for {
_ <- setupTableTest()
(msg, insertReply) <- sendOk(raw("INSERT INTO some_test (name, wedding_date) VALUES ('tester', '0000-00-00');"))
(msg, reply) <- sendOk(prepared("SELECT wedding_date FROM some_test WHERE name=?", Json.arr("tester")))
} yield {
val receivedFields = reply.getArray("fields")
logger.info(reply.getArray("results").get[JsonArray](0).get[String](0))
assertEquals(Json.arr("wedding_date"), receivedFields)
assertEquals(null, reply.getArray("results").get[JsonArray](0).get[String](0))
testComplete()
}) recover failedTest

}