Skip to content

Add BLOB test #57

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 1 commit into from
Dec 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
Binary file added src/test/resources/example.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 37 additions & 2 deletions src/test/scala/io/vertx/asyncsql/test/BaseSqlTests.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package io.vertx.asyncsql.test

import java.nio.charset.StandardCharsets

import org.vertx.java.core.Handler
import org.vertx.java.core.json.impl.Base64
import org.vertx.scala.core.buffer.Buffer

import scala.concurrent.{Future, Promise}
import org.vertx.scala.core.json.{JsonObject, Json, JsonArray}
import org.vertx.testtools.VertxAssert._
Expand Down Expand Up @@ -455,10 +461,39 @@ trait BaseSqlTests {
} yield {
val receivedFields = reply.getArray("fields")
assertEquals(Json.arr("test_date"), receivedFields)
logger.info("date is: " + reply.getArray("results").get[JsonArray](0).get[String](0))
assertEquals("2015-04-04T10:04:00.000", reply.getArray("results").get[JsonArray](0).get[String](0))
val date = reply.getArray("results").get[JsonArray](0).get[String](0)
logger.info(s"date is: $date")
assertEquals("2015-04-04T10:04:00.000", date)
testComplete()
}) recover failedTest

@Test
def blobUpload(): Unit = (for {
image <- readFile("example.jpg")
(msg, r0) <- sendOk(raw("DROP TABLE IF EXISTS blob_test"))
(msg, r1) <- sendOk(raw(createBlobTable))
(msg, r2) <- sendOk(prepared("INSERT INTO blob_test (test_blob) VALUES (?)", Json.emptyArr().addBinary(image)))
(msg, r3) <- sendOk(raw("SELECT test_blob FROM blob_test"))
} yield {
val receivedFields = r3.getArray("fields")
assertEquals(Json.arr("test_blob"), receivedFields)
logger.info(s"blob is: ${r3.getArray("results").get[JsonArray](0).get[Array[Byte]](0)}")
val blob = r3.getArray("results").get[JsonArray](0).get[JsonArray](0).toArray.map(_.asInstanceOf[Byte])
val str = new String(Base64.decode(new String(blob)))
logger.info(s"blob is2: ${blob.getClass}")
assertEquals(new String(image), str)
testComplete()
}) recover failedTest

private def readFile(file: String): Future[Array[Byte]] = {
val p = Promise[Array[Byte]]()
vertx.fileSystem.readFile(file, {
case Success(buffer) =>
logger.info(s"read file buffer in ${StandardCharsets.UTF_8.name()} encoding")
p.success(buffer.toString(StandardCharsets.UTF_8.name()).getBytes)
case Failure(ex) => p.failure(ex)
}: Try[Buffer] => Unit)
p.future
}
}

7 changes: 7 additions & 0 deletions src/test/scala/io/vertx/asyncsql/test/SqlTestVerticle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ abstract class SqlTestVerticle extends TestVerticle with BaseVertxIntegrationTes

val address = "campudus.asyncdb"

protected def blobDataType: String = "BLOB"

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

override final def before() {}
Expand Down Expand Up @@ -78,6 +80,11 @@ abstract class SqlTestVerticle extends TestVerticle with BaseVertxIntegrationTes
| test_date $dateDataType
|);""".stripMargin

protected def createBlobTable: String = s"""CREATE TABLE blob_test (
| id SERIAL,
| test_blob $blobDataType
|);""".stripMargin

protected def createTableStatement(tableName: String) = s"""DROP TABLE IF EXISTS $tableName;
CREATE TABLE $tableName (
| id SERIAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ class PostgreSqlTest extends SqlTestVerticle with BaseSqlTests {
override def getConfig() =
baseConf.putString("username", "vertx").putString("password", "test").putString("database", "testdb")

override protected def blobDataType: String = "BYTEA"

}