Skip to content
This repository
tree: f2410f6a95
Fetching contributors…

Cannot retrieve contributors at this time

file 321 lines (247 sloc) 10.277 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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/*
* 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 couchdb

import java.net.ConnectException

import dispatch.{Http, StatusCode}

import org.specs.Specification

import common._
import json._
import JsonDSL._
import record.field.{IntField, StringField}
import DocumentHelpers.stripIdAndRev


package couchtestrecords {
  class Person private () extends CouchRecord[Person] {
    def meta = Person
  
    object name extends StringField(this, 200)
    object age extends IntField(this)
  }
  
  object Person extends Person with CouchMetaRecord[Person]
  
  class Company private () extends CouchRecord[Company] {
    def meta = Company
  
    object name extends StringField(this, 200)
  }
  
  object Company extends Company with CouchMetaRecord[Company]
}

/**
* Systems under specification for CouchRecord.
*/
object CouchRecordSpec extends Specification("CouchRecord Specification") {
  import CouchDB.defaultDatabase
  import couchtestrecords._
  
  val design: JObject =
    ("language" -> "javascript") ~
    ("views" -> (("people_by_age" -> ("map" -> "function(doc) { if (doc.type == 'Person') { emit(doc.age, doc); } }")) ~
                 ("oldest" -> (("map" -> "function(doc) { if (doc.type == 'Person') { emit(doc.name, doc.age); } }") ~
                                      ("reduce" -> "function(keys, values) { return Math.max.apply(null, values); }")))))

  def setup = {
    val database = new Database("test")
    (try { Http(database delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
    Http(database create)
    Http(database.design("test") put design)
    defaultDatabase = database
  }

  def assertEqualPerson(a: Person, b: Person) = {
    a.name.value must_== b.name.value
    a.age.value must_== b.age.value
  }

  def assertEqualRows(foundRows: Seq[Person], expectedRows: Seq[Person]) = {
    foundRows.length must_== expectedRows.length
    for ((found, expected) <- foundRows.toList zip expectedRows.toList) {
      found.id.valueBox must_== expected.id.valueBox
      found.rev.valueBox must_== expected.rev.valueBox
      assertEqualPerson(found, expected)
    }
  }
    
  "A couch record" should {
    def testRec1: Person = Person.createRecord.name("Alice").age(25)
    val testDoc1: JObject = ("age" -> 25) ~ ("name" -> "Alice") ~ ("type" -> "Person")
    def testRec2: Person = Person.createRecord.name("Bob").age(30)
    val testDoc2: JObject = ("age" -> 30) ~ ("extra1" -> "value1") ~ ("extra2" -> "value2") ~ ("name" -> "Bob") ~ ("type" -> "Person")
    def testRec3: Company = Company.createRecord.name("Acme")

    "give emtpy box on get when nonexistant" in {
      setup must_== ()
 
      Person.fetch("testdoc") must verify (!_.isDefined)
    }

    "be insertable" in {
      setup
      
      val newRec = testRec1
      newRec save

      assertEqualPerson(newRec, testRec1)
      newRec.saved_? must_== true
      newRec.id.valueBox must verify (_.isDefined)
      newRec.rev.valueBox must verify (_.isDefined)

      val Full(foundRec) = Person.fetch(newRec.id.valueBox.open_!)
      assertEqualPerson(foundRec, testRec1)
      foundRec.id.valueBox must_== newRec.id.valueBox
      foundRec.rev.valueBox must_== newRec.rev.valueBox
      
    }

    "generate the right JSON" in {
      setup
      val newRec = testRec1
      newRec save

      val foundDoc = Http(defaultDatabase(newRec.id.valueBox.open_!) fetch)
      compact(render(stripIdAndRev(foundDoc))) must_== compact(render(testDoc1))
    }

    "be deletable" in {
      setup
      val newRec = testRec1
      newRec.save

      newRec.id.valueBox must verify(_.isDefined)
      val id = newRec.id.valueBox.open_!

      Person.fetch(id) must verify(_.isDefined)
      newRec.delete_! must verify(_.isDefined)
      Person.fetch(id) must not(verify(_.isDefined))
      newRec.delete_! must not(verify(_.isDefined))

      newRec.save
      Http(defaultDatabase(newRec.id.valueBox.open_!) @@ newRec.rev.valueBox.open_! delete)
      newRec.delete_! must not(verify(_.isDefined))
    }

    "be fetchable in bulk" in {
      setup
      val newRec1, newRec2, newRec3 = testRec1

      newRec1.save
      newRec2.save
      newRec3.save

      newRec1.saved_? must_== true
      newRec2.saved_? must_== true
      newRec3.saved_? must_== true

      val expectedRows = newRec1::newRec3::Nil

      Person.fetchMany(newRec1.id.valueBox.open_!, newRec3.id.valueBox.open_!).map(_.toList) must beLike {
        case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
      }
    }

    "support queries" in {
      setup

      val newRec1, newRec3 = testRec1
      val newRec2 = testRec2
      newRec1.save
      newRec2.save
      newRec3.save

      newRec1.saved_? must_== true
      newRec2.saved_? must_== true
      newRec3.saved_? must_== true

      val expectedRows = newRec2::Nil

      Person.queryView("test", "people_by_age", _.key(JInt(30))) must beLike {
        case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
      }
    }

    "support queries returning documents" in {
      setup

      val newRec1 = testRec1
      val newRec2 = testRec2
      newRec1.save
      newRec2.save

      newRec1.saved_? must_== true
      newRec2.saved_? must_== true

      val expectedRows = newRec1::newRec2::Nil

      Person.queryViewDocs("test", "oldest", _.dontReduce) must beLike {
        case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
      }
    }

    "support queries returning documents for a non-reducing view" in {
      setup

      val newRec1 = testRec1
      val newRec2 = testRec2
      newRec1.save
      newRec2.save

      newRec1.saved_? must_== true
      newRec2.saved_? must_== true

      val expectedRows = newRec1::newRec2::Nil

      Person.queryViewDocs("test", "people_by_age", identity) must beLike {
        case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
      }
    }

    "support multiple databases for fetching" in {
      setup
      val database2 = new Database("test2")
      (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
      Http(database2 create)

      val newRec = testRec1
      newRec.database = database2
      newRec.save

      newRec.saved_? must_== true

      val foundRecBox = Person.fetchFrom(database2, newRec.id.valueBox.open_!)
      foundRecBox must verify(_.isDefined)
      val Full(foundRec) = foundRecBox
      assertEqualPerson(foundRec, testRec1)
      foundRec.id.valueBox must_== newRec.id.valueBox
      foundRec.rev.valueBox must_== newRec.rev.valueBox
      
      Person.fetch(newRec.id.valueBox.open_!) must not(verify(_.isDefined))
    }

    "support multiple databases for fetching in bulk" in {
      setup
      val database2 = new Database("test2")
      (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
      Http(database2 create)

      val newRec1, newRec2, newRec3 = testRec1
      newRec1.database = database2
      newRec2.database = database2
      newRec3.database = database2
      newRec1.save
      newRec2.save
      newRec3.save

      newRec1.saved_? must_== true
      newRec2.saved_? must_== true
      newRec3.saved_? must_== true

      val expectedRows = newRec1::newRec3::Nil

      Person.fetchManyFrom(database2, newRec1.id.valueBox.open_!, newRec3.id.valueBox.open_!).map(_.toList) must beLike {
        case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
      }

      Person.fetchMany(newRec1.id.valueBox.open_!, newRec3.id.valueBox.open_!) must beLike { case Full(seq) if seq.isEmpty => true }
    }

    "support multiple databases for queries" in {
      setup
      val database2 = new Database("test2")
      (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
      Http(database2 create)
      Http(database2.design("test") put design)

      val newRec1, newRec3 = testRec1
      val newRec2 = testRec2
      newRec1.database = database2
      newRec2.database = database2
      newRec3.database = database2
      newRec1.save
      newRec2.save
      newRec3.save

      newRec1.saved_? must_== true
      newRec2.saved_? must_== true
      newRec3.saved_? must_== true

      val expectedRows = newRec2::Nil

      Person.queryViewFrom(database2, "test", "people_by_age", _.key(JInt(30))) must beLike {
        case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
      }

      Person.queryView("test", "people_by_age", _.key(JInt(30))) must beLike { case Full(seq) if seq.isEmpty => true }
    }

    "support multiple databases for queries returning documents" in {
      setup
      val database2 = new Database("test2")
      (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
      Http(database2 create)
      Http(database2.design("test") put design)

      val newRec1 = testRec1
      val newRec2 = testRec2
      newRec1.database = database2
      newRec2.database = database2
      newRec1.save
      newRec2.save

      newRec1.saved_? must_== true
      newRec2.saved_? must_== true

      val expectedRows = newRec1::newRec2::Nil

      Person.queryViewDocsFrom(database2, "test", "oldest", _.dontReduce) must beLike {
        case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
      }

      Person.queryViewDocs("test", "oldest", _.dontReduce) must beLike { case Full(seq) if seq.isEmpty => true }
    }
  }
}
Something went wrong with that request. Please try again.