Slick Module¶
Recipes | Test |
- table
- column can have any of the primitive data type.
- selection gives display ready selected section of set of rows
table CalendarDateTest("calendar_date_test") {
column id("id"):Int PK AutoInc
column str("str"):String
column birthDay("birth_day"):CalendarDate
column optBirthDay("opt_birth_day"):CalendarDate?
}
- combination represents join of two tables, where columns comes from the reference tables
table Employee("Employee") {
column id("id"):Int PK AutoInc
column name("name"):String
column designation("designation"):String?
column deptId("dept_id"):Int?
FK empDeptRelation ("emp_dept_relation")(deptId) ForeignTable Department(id)
selection Summary(id,name, designation) {
let lcName:String = `name.toLowerCase`
}
table Department("Department") {
column id("id"):Int PK AutoInc
column name("name"):String
}
combination EmployeeDepartment(
Employee.id as empId,
Department.name as dname,
Employee.name as ename,
Employee.designation as desig
) {
let lcName:String = `ename.toLowerCase`
}
Recipes¶
Classes generated for the Employee table are CalendarDateTestRow, CalendarDateTestQuery, CalendarDateTestUpdate, CalendarDateTestWriter and they are used as follows,
- Api for the primitive mandatory data type
val date1 = CalendarDate.from(2005, 2, 11);
CalendarDateTestWriter().insert(str = "abc", birthDay = date1, optBirthDay = Some(date1))
CalendarDateTestQuery().str.is("abc").findOne.get.str
CalendarDateTestQuery().str.is("abc").findOne.get.birthDay
CalendarDateTestQuery().birthDay.is(date1).findOne.get.birthDay
CalendarDateTestQuery().birthDay.le(date3).find
CalendarDateTestQuery().birthDay.lt(date3).find
CalendarDateTestQuery().birthDay.ge(date3).find
CalendarDateTestQuery().birthDay.gt(date3).find
CalendarDateTestQuery().birthDay.in(List(date3)).find
CalendarDateTestQuery().birthDay.not(date3).find
CalendarDateTestQuery().birthDay.notIn(List(date1, date2)).find
CalendarDateTestQuery().birthDay.between(date1, date5).find
CalendarDateTestQuery().birthDay.notBetween(date1, date2).find
- Api for the primitive optional data type
CalendarDateTestQuery().optBirthDay.iss(date1).findOne.get.optBirthDay.get
CalendarDateTestQuery().optBirthDay.isNotNull.findOne.get.optBirthDay.get
CalendarDateTestQuery().optBirthDay.isNull.findOne
CalendarDateTestQuery().optBirthDay.lee(date3).find
CalendarDateTestQuery().optBirthDay.ltt(date3).find
CalendarDateTestQuery().optBirthDay.gee(date3).find
CalendarDateTestQuery().optBirthDay.gtt(date3).find
CalendarDateTestQuery().optBirthDay.inn(List(date3)).find
CalendarDateTestQuery().optBirthDay.nott(date3).find
CalendarDateTestQuery().optBirthDay.notInn(List(date1, date2)).find
CalendarDateTestQuery().optBirthDay.betweenn(date1, date5).find
CalendarDateTestQuery().optBirthDay.notBetweenn(date1, date2).find
- Combination table usage :
val emp = EmployeeRow.syntax("e");
val dept = DepartmentRow.syntax("d");
GlobalSettings.loggingSQLAndTime = new LoggingSQLAndTimeSettings(
enabled = true,
singleLineMode = true,
printUnprocessedStackTrace = true,
stackTraceDepth = 15,
logLevel = 'debug,
warningEnabled = true,
warningThresholdMillis = 3000L,
warningLogLevel = 'debug
)
val rows =
sql"""
select
${EmployeeDepartment.cols(emp, dept)}
from
${emp.table}, ${dept.table}
where
${emp.deptId} = ${dept.id}
""".map(EmployeeDepartment.row(emp, dept)).list.apply()
}
Test¶
For each slick module one test file gets generated under test dir. Write code there to test the module
class <moduleName>TestSuite extends FunSuite with SmilePerTest {
test("test1", Tag("tag1")) {
}
> test //This will run entire test cases
> test-only *.<ClassName>.scala -- -n <tagName>
Can add your own test files in this directory , Test scala file must extend FunSuite and SmilePerTest To clean up resources using the beforeEach method in test class
override def beforeEach(testData: TestData): Unit = {
super.beforeEach(testData);
}