vendredi 1 janvier 2016

Saving an example picture in the database in Scala Tests

I have started to develop in BDD by using Scala test. In my program I'd like to upload a file and save it in the Postgres database. So now I came accross the problem, that I don't know how I can simulate this uploaded file in the Scala Test file.

My Business Model looks like following:

case class Product(id: Option[Int],
               name: String,
               category: String,
               picture: Option[Array[Byte]],
               amount: Int,
               criticalAmount: Int
                ) {
}

object Product {
 implicit val byteArrayWrites = new Writes[Array[Byte]]{
  def writes(o: Array[Byte]) = JsString(new String((Base64.encodeBase64(o))))
 }
 implicit val byteArrayReads = new Reads[Array[Byte]]{
   def reads(json: JsValue) = json match {
    case JsString(value) => JsSuccess(Base64.decodeBase64(value.getBytes))
    case _ => JsError(Seq(JsPath()->Seq(ValidationError("validate.error.expected.JsString"))))
   }
 }


 def tupled(t: (Option[Int], String, String, Option[Array[Byte]], Int, Int)) =
 Product(t._1, t._2, t._3, t._4, t._5, t._6)

 def toTuple(p: Product) = Some((p.id, p.name, p.category, p.picture, p.amount, p.criticalAmount))

}

My Table Model looks like this:

class Products(tag: Tag) extends Table[Product](tag, "PRODUCTS"){
  def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
  def name = column[String]("NAME")
  def category = column[String]("CATEGORY")
  def picture = column[Array[Byte]]("PICTURE")
  def amount = column[Int]("AMOUNT")
  def criticalAmount = column[Int]("CRITICALAMOUNT")
  def * = (id.?, name, category, picture.?, amount, criticalAmount) <>(Product.tupled, Product.toTuple)
}

I hope anyone is able to help me :)

Kind regards

Aucun commentaire:

Enregistrer un commentaire