Test Case Of Data Access Layer Function in Play:-


In this blog I am trying to show how to create test case of data access layer function.
Lets have following function of DAL (data access layer) as an example in models packegae of play.

All the necessary files must be imported to avoid errors.

package model.dals

import scala.slick.driver.PostgresDriver.simple._
import scala.slick.session.Session
import model.domains.Domain._
import play.api.Logger
import utils.Connection

trait MobileDALComponent {
def getMobileRecordByIMEID(imeid: String): List[Mobile]
}

class MobileDAL extends MobileDALComponent {
override def getMobileRecordByIMEID(imeid: String): List[Mobile] = {
Connection.databaseObject().withSession { implicit session: Session =>
Logger.info("Calling getMobileRecordByIMEID" + imeid)
(for { mobile <- Mobiles if ((mobile.imeiMeid === imeid) || (mobile.otherImeiMeid=== imeid)) } yield mobile).list
}
}

object MobileDAL extends MobileDAL

Now the test case of above function must be created in Test directory in play.
In this test case I am using FunSuite test library :- A suite of tests in which each test is represented as a function value. The “Fun” in FunSuite stands for “function.”

All the necessary files must be imported.

The values passed to the DAL function in a test is dummy data.Once the DAL function is processed the expected result is asserted.


package model.dals

import org.scalatest.FunSuite
import model.domains.Domain._
import model.dals.MobileDAL._
import play.api.test.FakeApplication
import play.api.test.Helpers._
import play.api.Logger
import org.scalatest.BeforeAndAfter

class mobileDALTest extends FunSuite{

val date = new java.sql.Date(new java.util.Date().getTime())
val brand=Brand("nokia",date)
val model=MobileModels("N72",1)
val mobileUser = Mobile(
"test", 1, 5, "12345678901234", "12345678902134", "12-05-2013", "+91 1111111111",
"test@gmail.com","stolen",Status.pending, "ddas  asd","10-10-2013","test.png","Sigma","Sigma454")

test("DBLayerTesting: Get User by IMEID number successfully ") {

running(FakeApplication()) {
val user = MobileDAL.getMobileRecordByIMEID("12345678901234")
assert(user.head.email === "test@gmail.com")
}
}

If the asserted value is correct the test passes.

reference*

http://www.artima.com/docs-scalatest-2.0/index.html#org.scalatest.FunSuite

Advertisements
This entry was posted in Scala. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s