forked from mongodb/casbah
/
MapperSpec.scala
385 lines (311 loc) · 10.4 KB
/
MapperSpec.scala
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
* Copyright (c) 2010, Novus Partners, Inc. <http://novus.com>
*
* 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.
*
* NOTICE: Portions of this work are derived from the Apache License 2.0 "mongo-scala-driver" work
* by Alexander Azarov <azarov@osinka.ru>, available from http://github.com/alaz/mongo-scala-driver
*/
package com.novus.casbah
package test
import java.util.Date
import net.lag.configgy.Configgy
import org.specs._
import org.specs.specification.PendingUntilFixed
import _root_.scala.collection.Map
import _root_.scala.collection.mutable.{Buffer, ArrayBuffer, Map => MMap}
import _root_.scala.collection.JavaConversions._
import _root_.scala.reflect.BeanInfo
import java.math.BigInteger
import Imports._
import mapper.Mapper
import mapper.annotations._
import commons.util.Logging
import org.apache.commons.lang.RandomStringUtils.{randomAscii => rs}
import org.apache.commons.lang.math.RandomUtils.{nextInt => rn}
@BeanInfo @UseTypeHints
class Widget(@ID var name: String, @Key var price: Int) {
def this() = this(null, 0)
override def toString() = "Widget(" + name + ", " + price + ")"
}
@BeanInfo
class Piggy(@Key val giggity: String) {
@ID(auto = true)
var id: ObjectId = _
@Key
var favorite_foods: List[String] = Nil
@Key
var this_field_is_always_null: String = null
@Key @UseTypeHints
var badges: List[Badge] = Nil
@Key
var political_views: Map[String, String] = MMap.empty[String, String]
@Key
var family: Map[String, Piggy] = MMap.empty[String, Piggy]
@Key
var balance: Option[BigDecimal] = None
@Key
var freshness: Freshness.Value = Freshness.Fresh
}
object Freshness extends Enumeration("fresh", "stale") {
val Fresh, Stale = Value
}
@BeanInfo
class Chair {
@ID(auto = true)
var id: ObjectId = _
@Key @UseTypeHints
var optional_piggy: Option[Piggy] = None
@Key var always_here: Option[String] = Some("foo")
@Key var never_here: Option[String] = None
@Key
lazy val timestamp: Date = new Date
@Key
lazy val argh: String = { "boo" }
@Key
var things: Set[String] = Set.empty[String]
}
trait Badge {
@Key var name: String = _
}
@BeanInfo
case class WorldOutsideOfManhattan(@ID scrum: String) extends Badge {
name = "world outside of Manhattan"
}
@BeanInfo
case class OnABoat(@ID water: String) extends Badge {
name = "I'm on a boat"
}
object ChairMapper extends Mapper[Chair] {
conn = MongoConnection()
db = "mapper_test"
coll = "chairs"
}
object WidgetMapper extends Mapper[Widget] {
conn = MongoConnection()
db = "mapper_test"
coll = "widgets"
}
object PiggyMapper extends Mapper[Piggy] {
conn = MongoConnection()
db = "mapper_test"
coll = "piggies"
"freshness".enum(Freshness)
}
object WOOM_Mapper extends Mapper[WorldOutsideOfManhattan]
object OAB_Mapper extends Mapper[OnABoat]
@BeanInfo
case class One(@Key one: Option[String] = Some("one"))
@BeanInfo
case class Two(@Key two: String = "two") {
@Key val three: Option[String] = None
}
object OneMapper extends Mapper[One]
object TwoMapper extends Mapper[Two]
@BeanInfo
trait Node {
val name: String
val cheat: String
}
@BeanInfo
case class ListNode(@Key name: String, @Key @UseTypeHints children: List[Node] = Nil, @Key cheat: String = "list") extends Node
@BeanInfo
case class MapNode(@Key name: String, @Key @UseTypeHints children: Map[String, Node] = Map.empty[String, Node], @Key cheat: String = "map") extends Node
object ListNodeMapper extends Mapper[ListNode]
object MapNodeMapper extends Mapper[MapNode]
object NodeMapper {
def asDBObject(n: Node): DBObject =
n match {
case l: ListNode => ListNodeMapper.asDBObject(l)
case m: MapNode => MapNodeMapper.asDBObject(m)
}
def asObject(o: DBObject): Node =
o.get("cheat") match {
case "list" => ListNodeMapper.asObject(o)
case "map" => MapNodeMapper.asObject(o)
}
}
object NodeCounter {
var n: Int = _
}
class MapperSpec extends Specification with PendingUntilFixed with Logging {
private implicit def pimpTimes(n: Int) = new {
def times[T](f: => T) = (1 to n).toList.map(_ => f.apply)
}
detailedDiffs()
doBeforeSpec {
Configgy.configure("casbah-commons/src/test/resources/casbah.config")
com.novus.casbah.conversions.scala.RegisterConversionHelpers()
ChairMapper
WidgetMapper
PiggyMapper
WOOM_Mapper
OAB_Mapper
OneMapper
TwoMapper
ListNodeMapper
MapNodeMapper
NodeCounter.n = 0
}
"a mapper" should {
shareVariables
val widget = new Widget("something", 7824)
Mapper[Widget].upsert(widget)
"discover mapper for a class" in {
Mapper[Piggy] must_== PiggyMapper
}
"compute id" in {
Mapper[Widget].idProp.get.name must_== "name"
}
"cull non-ids" in {
Mapper[Widget].nonIdProps must exist(pd => pd.name == "price")
}
"convert object to MongoDBObject" in {
val dbo: MongoDBObject = Mapper[Widget].asDBObject(widget)
dbo must havePair("_id", "something")
}
"retrieve an object" in {
Mapper[Widget].findOne(widget.name) must beSome[Widget].which {
loaded =>
loaded.name must_== widget.name
loaded.price must_== widget.price
}
}
"preserve collection-level type hints" in {
Mapper[Widget].coll.findOne(Map("_id" -> widget.name).asDBObject) must beSome[DBObject].which {
dbo =>
dbo("_typeHint") must_== classOf[Widget].getName
}
}
"automatically assign (& retrieve objects by) MongoDB OID-s" in {
val piggy = new Piggy("oy vey")
Some(Mapper[Piggy].upsert(piggy)) must beSome[Piggy].which {
saved =>
saved.id must notBeNull
Mapper[Piggy].findOne(saved.id) must beSome[Piggy].which {
retrieved =>
retrieved.id must_== saved.id
retrieved.giggity must_== piggy.giggity
}
}
}
"exclude null non-@ID fields from output" in {
val dbo: MongoDBObject = Mapper[Piggy].asDBObject(new Piggy("has a null field"))
dbo must notHaveKey("this_field_is_always_null")
}
"save & de-serialize nested documents" in {
val FOODS = "bacon" :: "steak" :: "eggs" :: "club mate" :: Nil
val BADGES: List[Badge] = List(WorldOutsideOfManhattan("mile high"), OnABoat("swine"))
val POLITICAL_VIEWS = Map("wikileaks" -> "is my favorite site", "democrats" -> "have ruined the economy")
val FAMILY = Map("father" -> new Piggy("father"), "mother" -> new Piggy("mother"))
val BALANCE = BigDecimal("0.12345678901234")
val before = new Chair
val piggy = new Piggy("foo")
piggy.favorite_foods = FOODS
piggy.badges = BADGES
piggy.political_views = POLITICAL_VIEWS
piggy.family = FAMILY
piggy.balance = Some(BALANCE)
piggy.freshness = Freshness.Stale
before.optional_piggy = Some(piggy)
before.things = Set("foo", "bar", "baz", "quux", "foo", "baz")
val id = Mapper[Chair].upsert(before).id
val dbo = Mapper[Chair].coll.findOne(id).get
dbo("argh") = "ya"
Some(ChairMapper.asObject(dbo)) must beSome[Chair].which {
after =>
after.optional_piggy must beSome[Piggy].which {
piggy =>
piggy.giggity == before.optional_piggy.get.giggity
piggy.favorite_foods must containAll(FOODS)
piggy.political_views must havePairs(POLITICAL_VIEWS.toList : _*)
piggy.family.get("father") must beSome[Piggy].which {
father => father.giggity must_== "father"
}
piggy.balance must beSome[BigDecimal].which {
b => b must_== BALANCE
}
piggy.freshness must_== Freshness.Stale
}
after.always_here must beSome[String]
after.never_here must beNone
after.things.size must_== before.things.size
after.things must containAll(before.things)
//after.argh must_== "ya"
}
}
"preserve default values" in {
val one = One()
val _one = OneMapper.asObject(OneMapper.asDBObject(one))
_one must_== one
_one.one must beSome[String].which { _ == "one" }
val two = Two()
val _two = TwoMapper.asObject(TwoMapper.asDBObject(two))
_two must_== two
_two.two must_== "two"
_two.three must beNone
}
}
"not take too much time doing stuff" in {
var tree: Option[Node] = None
val generateTree = measure { tree = node(2) }
log.info("generated %d nodes in %d ms", NodeCounter.n, generateTree)
tree must beSome[Node]
val outTimes = 10.times {
val readTree = measure { Some(NodeMapper.asDBObject(tree.get)) }
log.info("serialized tree in %d ms", readTree)
readTree
}
log.info("serialization times: min( %d ms ) <<< avg( %d ms ) <<< max( %d ms )",
outTimes.min, outTimes.sum / outTimes.size, outTimes.max)
val dbo = NodeMapper.asDBObject(tree.get)
val inTimes = 10.times {
val writeTree = measure { NodeMapper.asObject(dbo) }
log.info("de-serialized tree in %d ms ", writeTree)
writeTree
}
log.info("de-serialization times: min( %d ms ) <<< avg( %d ms ) <<< max( %d ms )",
inTimes.min, inTimes.sum / inTimes.size, inTimes.max)
val tree2 = NodeMapper.asObject(dbo)
tree2.name must_== tree.get.name
}
private def measure(f: => Unit): Long = {
val start = System.currentTimeMillis
f.apply
val stop = System.currentTimeMillis
stop - start
}
private def node(level: Int): Option[Node] =
if (level == 0) None
else {
NodeCounter.n += 1
if (rn(10) % 2 == 0) {
Some(ListNode(crap, (1 to many).toList.map(_ => node(level - 1)).filter(_.isDefined).map(_.get)))
} else {
Some(MapNode(crap, Map.empty[String, Node] ++ (1 to many).toList.map {
_ =>
node(level - 1) match {
case Some(n) => Some(n.name -> n)
case _ => None
}
}.filter(_.isDefined).map(_.get)))
}
}
private val many = 3
private def _many: Int =
rn(10) match {
case n if n < 3 => many
case n => n
}
private def crap: String = rs(many)
}