Skip to content
This repository
tree: 1f54b5da14
Fetching contributors…

Cannot retrieve contributors at this time

file 273 lines (228 sloc) 8.081 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/*
* Copyright 2010-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.liftweb
package squerylrecord

import org.specs.Specification
import record.{BaseField, Record}
import RecordTypeMode._
import MySchema.{TestData => td, _}


/**
* Systems under specification for SquerylRecord.
*/
object SquerylRecordSpec extends Specification("SquerylRecord Specification") {

  doBeforeSpec {
    DBHelper.initSquerylRecordWithInMemoryDB()
    DBHelper.createSchema()
  }

  "SquerylRecord" should {

    "load record by ID" >> {
      transaction {
        val company = companies.lookup(td.c2.id)
        checkCompaniesEqual(company.get, td.c2)

        val employee = employees.lookup(td.e1.id)
        checkEmployeesEqual(employee.get, td.e1)
      }
    }

    "load record by string field value" >> {
      transaction {
        val company = from(companies)(c =>
          where(c.name === td.c1.name.is) select (c))
        checkCompaniesEqual(company.single, td.c1)
      }
    }

    "support order by" >> {
      transaction {
        val orderedCompanies = from(companies)(c =>
          select(c) orderBy (c.name))
        val ids = orderedCompanies.map(_.id)
        ids must containInOrder(
          td.allCompanies.sortBy(_.name.is).map(_.id))
      }
    }

    "support normal joins" >> {
      transaction {
        val companiesWithEmployees = from(companies, employees)((c, e) =>
          where(c.id === e.id)
            select ((c, e)))
        val ids = companiesWithEmployees.map(entry => (entry._1.id,
          entry._2.id))
        ids must haveSize(2)
        ids must containAll(List((td.c1.id, td.e1.id),
          (td.c2.id, td.e2.id)))
      }
    }
    
    "support left outer joins" >> {
      transaction {
        val companiesWithEmployees = join(companies, employees.leftOuter)((c, e) =>
          select(c, e)
          on(c.id === e.map(_.companyId))
        )
        
        companiesWithEmployees must haveSize(3)
        // One company doesn't have an employee, two have
        companiesWithEmployees.filter(ce => ce._2.isEmpty) must haveSize(1)
        
        val companiesAndEmployeesWithSameName = join(companies, employees.leftOuter)((c, e) =>
          groupBy(c.id)
          compute(countDistinct(e.map(_.id)))
          on(c.name === e.map(_.name))
        )
        
        // There are three companies
        companiesAndEmployeesWithSameName must haveSize(3)
        // One company has the same name as an employee, two don't
        companiesAndEmployeesWithSameName.filter(ce => ce.measures == 0) must haveSize(2)
        
        val employeesWithSameAdminSetting = join(employees, employees.leftOuter)((e1, e2) =>
          select(e1, e2)
          on(e1.admin === e2.map(_.admin))
        )
        
        // two employees, both have distinct admin settings
        employeesWithSameAdminSetting must haveSize(2)
        employeesWithSameAdminSetting.foreach { ee =>
          ee._2 must not (beEmpty)
          ee._1.id must_== ee._2.get.id
        }
        
        val companiesWithSameCreationDate = join(companies, companies.leftOuter)((c1, c2) =>
          select(c1, c2)
          on(c1.created === c2.map(_.created))
        )
        companiesWithSameCreationDate must not (beEmpty)
        
        val employeesWithSameDepartmentNumber = join(employees, employees.leftOuter)((e1, e2) =>
          select(e1, e2)
          on(e1.departmentNumber === e2.map(_.departmentNumber))
        )
        employeesWithSameDepartmentNumber must not (beEmpty)

        val employeesWithSameRoles = join(employees, employees.leftOuter)((e1, e2) =>
          select(e1, e2)
          on(e1.role === e2.map(_.role))
        )
        employeesWithSameRoles must not (beEmpty)
      }
    }

    "support one to many relations" >> {
      transaction {
        val company = companies.lookup(td.c1.id)
        company must beSome[Company]
        val employees = company.get.employees
        employees must haveSize(1)
        checkEmployeesEqual(td.e1, employees.head)
      }
    }
    
    "support many to many relations" >> {
      transactionWithRollback {
        td.e1.rooms must haveSize(2)
        
        td.e2.rooms must beEmpty
        
        td.r1.employees must haveSize(1)
        td.r3.employees must beEmpty
        
        td.r3.employees.associate(td.e2)
        td.e2.rooms must haveSize(1)
      }
    }

    "support updates" >> {
      val id = td.c1.id

      transactionWithRollback {
        val company = companies.lookup(id).get
        company.name("New Name")
        company.postCode("11111")
        companies.update(company)

        val loaded = companies.lookup(id).get
        checkCompaniesEqual(company, loaded)

        update(companies)(c => where(c.id === id)
          set (c.name := "Name2"))
        val afterPartialUpdate = companies.lookup(id).get
        afterPartialUpdate.name.is must_== "Name2"
      }

      // After rollback, the company should still be the same:
      transaction {
        val company = companies.lookup(id).get
        checkCompaniesEqual(td.c1, company)
      }
    }

    "support delete" >> {
      transactionWithRollback {
        employees.delete(td.e2.id)
        employees.lookup(td.e2.id) must beNone
      }
    }
    
    "support select with properties of formerly fetched objects" >> {
      transaction {
        val company = companies.lookup(td.c2.id).head
        val employee = from(employees)(e =>
          where (e.companyId === company.idField) select(e)).head
        employee.id must_== td.e2.id
        
        val loadedCompanies = from(companies)(c =>
          where (c.created === company.created) select(c))
        loadedCompanies.size must beGreaterThanOrEqualTo(1)
      }
    }
    

  }

  class TransactionRollbackException extends RuntimeException

  /**
* Runs the given code in a transaction and rolls
* back the transaction afterwards.
*/
  private def transactionWithRollback[T](code: => T): T = {
    var result: T = null.asInstanceOf[T]
    try {
      transaction {
        result = code
        throw new TransactionRollbackException()
      }
    } catch {
      case e: TransactionRollbackException => // OK, was rolled back
    }

    result
  }

  private def checkCompaniesEqual(c1: Company, c2: Company) {
    val cmp = new RecordComparer[Company](c1, c2)
    cmp.check(_.idField)
    cmp.check(_.description)
    cmp.check(_.country)
    cmp.check(_.postCode)
    cmp.check(_.created)

    cmp.checkXHtml()
  }

  private def checkEmployeesEqual(e1: Employee, e2: Employee) {
    val cmp = new RecordComparer[Employee](e1, e2)
    cmp.check(_.name)
    cmp.check(_.companyId)
    cmp.check(_.email)
    cmp.check(_.salary)
    cmp.check(_.locale)
    cmp.check(_.timeZone)
    //cmp.check(_.password)
    cmp.check(_.admin)
    cmp.check(_.departmentNumber)
    cmp.check(_.role)

    // Photo must be checked separately
    e1.photo.is match {
      case Some(p) => {
        val p2 = e2.photo.is
        p2 must beSome[Array[Byte]]
        p2.get.size must_== p.size

        (0 until p.size).foreach { i =>
          p2.get(i) must_== p(i)
        }
      }
      case None => e2.photo.is must beNone
    }
  }

  class RecordComparer[T <: Record[T]](val r1: T, val r2: T) {
    def check(fieldExtractor: (T) => BaseField) {
      val f1 = fieldExtractor(r1)
      val f2 = fieldExtractor(r2)
      f1.get must_== f2.get
      f1.name must_== f2.name
    }

    def checkXHtml() {
      r1.toXHtml must_== r2.toXHtml
    }
  }
}
Something went wrong with that request. Please try again.