-
Notifications
You must be signed in to change notification settings - Fork 275
/
MongoFieldSpec.scala
470 lines (423 loc) · 15.3 KB
/
MongoFieldSpec.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* Copyright 2006-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 mongodb
package record
import java.util.{Date, UUID}
import java.util.regex.Pattern
import org.bson.types.ObjectId
import org.specs.Specification
import common._
import json._
import BsonDSL._
import util.Helpers.randomString
import http.{LiftSession, S}
import http.js.JE._
import http.js.JsExp
import net.liftweb.record._
import common.Box._
import xml.{Elem, NodeSeq, Text}
import util.{Helpers, FieldError}
import Helpers._
/**
* Systems under specification for MongoField.
*/
object MongoFieldSpec extends Specification("MongoField Specification") with MongoTestKit {
import fixtures._
def passBasicTests[A](
example: A,
mandatory: MandatoryTypedField[A],
legacyOptional: MandatoryTypedField[A],
canCheckDefaultValues: Boolean = true
)(implicit m: scala.reflect.Manifest[A]): Unit = {
def commonBehaviorsForAllFlavors(field: MandatoryTypedField[A]) = {
"which have the correct initial value" in {
field.value must beEqual(field.defaultValue).when(canCheckDefaultValues)
field.valueBox must beEqual(field.defaultValueBox).when(canCheckDefaultValues)
}
"which are readable and writable" in {
field.set(example)
field.value must_== example
field.valueBox must_== Full(example)
field.clear
field.value must_!= example
field.valueBox must_!= Full(example)
field.setBox(Full(example))
field.value must_== example
field.valueBox must_== Full(example)
}
"which correctly clear back to the default" in {
{ field.clear; field.valueBox } must beEqual(field.defaultValueBox).when(canCheckDefaultValues)
}
"which capture error conditions set in" in {
// FIXME: This needs to be rearranged just so that it doesn't foul with subsequent examples
// field.setBox(Failure("my failure"))
// Failure("my failure") must_== Failure("my failure")
}
}
"support mandatory fields" in {
setSequential()
"which are configured correctly" in {
mandatory.optional_? must_== false
}
"which initialize to some value" in {
mandatory.valueBox must verify(_.isDefined)
}
"common behaviors for all flavors" in {
commonBehaviorsForAllFlavors(mandatory)
}
"which correctly fail to be set to Empty" in {
mandatory.valueBox must verify(_.isDefined)
mandatory.setBox(Empty)
mandatory.valueBox must beLike { case Failure(s, _, _) if s == mandatory.notOptionalErrorMessage => true }
}
}
"support 'legacy' optional fields (override optional_?)" in {
setSequential()
"which are configured correctly" in {
legacyOptional.optional_? must_== true
}
"which initialize to Empty" in {
legacyOptional.valueBox must_== Empty
}
"common behaviors for all flavors" in {
commonBehaviorsForAllFlavors(legacyOptional)
}
"which do not fail when set to Empty" in {
legacyOptional.set(example)
legacyOptional.value must_== example
legacyOptional.valueBox must_== Full(example)
legacyOptional.clear
if (canCheckDefaultValues) {
legacyOptional.value must_== legacyOptional.defaultValue
legacyOptional.valueBox must_== legacyOptional.defaultValueBox
}
legacyOptional.set(example)
legacyOptional.value must_== example
legacyOptional.valueBox must_== Full(example)
legacyOptional.setBox(Empty)
if (canCheckDefaultValues) {
legacyOptional.value must_== legacyOptional.defaultValue
legacyOptional.valueBox must_== legacyOptional.defaultValueBox
}
}
}
}
def passConversionTests[A](example: A, mandatory: MandatoryTypedField[A], jsexp: JsExp, jvalue: JValue, formPattern: Box[NodeSeq]): Unit = {
/*
"convert to JsExp" in {
mandatory.set(example)
mandatory.asJs mustEqual jsexp
}*/
"convert to JValue" in {
mandatory.set(example)
mandatory.asJValue mustEqual jvalue
}
if (!example.isInstanceOf[Pattern]) { // Patterns don't compare well
"get set from JValue" in {
mandatory.setFromJValue(jvalue) mustEqual Full(example)
mandatory.value mustEqual example
() // does not compile without this: no implicit argument matching parameter type scala.reflect.Manifest[org.specs.specification.Result[mandatory.MyType]]
}
}
formPattern foreach { fp =>
"convert to form XML" in {
mandatory.set(example)
val session = new LiftSession("", randomString(20), Empty)
S.initIfUninitted(session) {
val formXml = mandatory.toForm
formXml must notBeEmpty
formXml foreach { fprime =>
val f = ("* [name]" #> ".*" & "select *" #> (((ns: NodeSeq) => ns.filter {
case e: Elem => e.attribute("selected").map(_.text) == Some("selected")
case _ => false
}) andThen "* [value]" #> ".*"))(fprime)
val ret: Boolean = Helpers.compareXml(f, fp)
ret must_== true
}
}
}
}
}
"DateField" should {
val rec = MongoFieldTypeTestRecord.createRecord
val now = new Date
val nowStr = rec.meta.formats.dateFormat.format(now)
passBasicTests(now, rec.mandatoryDateField, rec.legacyOptionalDateField, false)
passConversionTests(
now,
rec.mandatoryDateField,
JsObj(("$dt", Str(nowStr))),
JObject(List(JField("$dt", JString(nowStr)))),
Full(<input name=".*" type="text" tabindex="1" value={nowStr} id="mandatoryDateField_id"></input>)
)
}
"JsonObjectField" should {
val rec = MongoFieldTypeTestRecord.createRecord
val ttjo = TypeTestJsonObject(1, "jsonobj1", Map("x" -> "a"))
val json = ("intField" -> 1) ~ ("stringField" -> "jsonobj1") ~ ("mapField" -> (("x" -> "a")))
passBasicTests(ttjo, rec.mandatoryJsonObjectField, rec.legacyOptionalJsonObjectField)
passConversionTests(
ttjo,
rec.mandatoryJsonObjectField,
new JsExp {
def toJsCmd = Printer.compact(render(json))
},
json,
Empty
)
}
"ObjectIdField" should {
val rec = MongoFieldTypeTestRecord.createRecord
val oid = ObjectId.get
passBasicTests(oid, rec.mandatoryObjectIdField, rec.legacyOptionalObjectIdField, false)
passConversionTests(
oid,
rec.mandatoryObjectIdField,
JsObj(("$oid", oid.toString)),
JObject(List(JField("$oid", JString(oid.toString)))),
Full(<input name=".*" type="text" tabindex="1" value={oid.toString} id="mandatoryObjectIdField_id"></input>)
)
}
"PatternField" should {
val rec = MongoFieldTypeTestRecord.createRecord
val ptrn = Pattern.compile("^Mo", Pattern.CASE_INSENSITIVE)
passBasicTests(ptrn, rec.mandatoryPatternField, rec.legacyOptionalPatternField, false)
passConversionTests(
ptrn,
rec.mandatoryPatternField,
JsObj(("$regex", Str(ptrn.toString)), ("$flags", Num(2))),
JObject(List(JField("$regex", JString(ptrn.toString)), JField("$flags", JInt(2)))),
Empty
)
}
"UUIDField" should {
val rec = MongoFieldTypeTestRecord.createRecord
val uuid = UUID.randomUUID
passBasicTests(uuid, rec.mandatoryUUIDField, rec.legacyOptionalUUIDField, false)
passConversionTests(
uuid,
rec.mandatoryUUIDField,
JsObj(("$uuid", Str(uuid.toString))),
JObject(List(JField("$uuid", JString(uuid.toString)))),
Full(<input name=".*" type="text" tabindex="1" value={uuid.toString} id="mandatoryUUIDField_id"></input>)
)
}
"PasswordField" should {
"require a nonempty password" in {
val rec = PasswordTestRecord.createRecord
rec.password.setPassword("")
rec.validate must_== (
FieldError(rec.password, Text(S.??("password.must.be.set"))) ::
Nil
)
}
"require at least 3 character password" in {
val rec = PasswordTestRecord.createRecord
rec.password.setPassword("ab")
rec.validate must_== (
FieldError(rec.password, Text(S.??("password.too.short"))) ::
Nil
)
}
}
"MongoListField (String)" should {
"function correctly" in {
val rec = ListTestRecord.createRecord
val lst = List("abc", "def", "ghi")
passBasicTests(lst, rec.mandatoryStringListField, rec.legacyOptionalStringListField)
passConversionTests(
lst,
rec.mandatoryStringListField,
JsArray(Str("abc"), Str("def"), Str("ghi")),
JArray(List(JString("abc"), JString("def"), JString("ghi"))),
Empty
)
}
}
"MongoListField (Int)" should {
"function correctly" in {
val rec = ListTestRecord.createRecord
val lst = List(4, 5, 6)
passBasicTests(lst, rec.mandatoryIntListField, rec.legacyOptionalIntListField)
passConversionTests(
lst,
rec.mandatoryIntListField,
JsArray(Num(4), Num(5), Num(6)),
JArray(List(JInt(4), JInt(5), JInt(6))),
Empty
)
}
}
"MongoJsonObjectListField" should {
"function correctly" in {
val rec = ListTestRecord.createRecord
val lst = List(TypeTestJsonObject(1, "jsonobj1", Map("x" -> "1")), TypeTestJsonObject(2, "jsonobj2", Map("x" -> "2")))
val json = List(
("intField" -> 1) ~ ("stringField" -> "jsonobj1") ~ ("mapField" -> (("x" -> "1"))),
("intField" -> 2) ~ ("stringField" -> "jsonobj2") ~ ("mapField" -> (("x" -> "2")))
)
passBasicTests(lst, rec.mandatoryMongoJsonObjectListField, rec.legacyOptionalMongoJsonObjectListField)
passConversionTests(
lst,
rec.mandatoryMongoJsonObjectListField,
new JsExp {
def toJsCmd = Printer.compact(render(json))
},
json,
Empty
)
}
}
"MongoCaseClassListField" should {
"setFromAny a List" in {
val rec = ListTestRecord.createRecord
val lst = List(MongoCaseClassTestObject(1,"str1"))
rec.mongoCaseClassListField.setFromAny(lst)
rec.mongoCaseClassListField.value must_== lst
}
}
"MongoMapField (String)" should {
"function correctly" in {
val rec = MapTestRecord.createRecord
val map = Map("a" -> "abc", "b" -> "def", "c" -> "ghi")
passBasicTests(map, rec.mandatoryStringMapField, rec.legacyOptionalStringMapField)
passConversionTests(
map,
rec.mandatoryStringMapField,
JsObj(("a", Str("abc")), ("b", Str("def")), ("c", Str("ghi"))),
JObject(List(
JField("a", JString("abc")),
JField("b", JString("def")),
JField("c", JString("ghi"))
)),
Empty
)
}
}
"MongoMapField (Int)" should {
"function correctly" in {
val rec = MapTestRecord.createRecord
val map = Map("a" -> 4, "b" -> 5, "c" -> 6)
passBasicTests(map, rec.mandatoryIntMapField, rec.legacyOptionalIntMapField)
passConversionTests(
map,
rec.mandatoryIntMapField,
JsObj(("a", Num(4)), ("b", Num(5)), ("c", Num(6))),
JObject(List(
JField("a", JInt(4)),
JField("b", JInt(5)),
JField("c", JInt(6))
)),
Empty
)
}
}
"BsonRecordField" should {
"function correctly" in {
val rec = SubRecordTestRecord.createRecord
val subRec = SubRecord.createRecord.name("subrecord")
val srJson =
JObject(List(
JField("name", JString("subrecord")),
JField("subsub", JObject(List(
JField("name", JString(""))
))),
JField("subsublist", JArray(List())),
JField("when", JObject(List(
JField("$dt", JString(rec.meta.formats.dateFormat.format(subRec.when.value)))
))),
JField("slist", JArray(List())),
JField("smap", JObject(List())),
JField("oid", JObject(List(JField("$oid", JString(subRec.oid.value.toString))))),
JField("pattern", JObject(List(
JField("$regex", JString(subRec.pattern.value.pattern)),
JField("$flags", JInt(subRec.pattern.value.flags))
))),
JField("uuid", JObject(List(JField("$uuid", JString(subRec.uuid.value.toString)))))
))
val srJsExp = new JsExp {
def toJsCmd = Printer.compact(render(srJson))
}
passBasicTests(subRec, rec.mandatoryBsonRecordField, rec.legacyOptionalBsonRecordField, false)
passConversionTests(
subRec,
rec.mandatoryBsonRecordField,
srJsExp,
srJson,
Empty
)
}
}
"BsonRecordListField" should {
"function correctly" in {
val rec = SubRecordTestRecord.createRecord
val lst = List(SubRecord.createRecord.name("subrec1"), SubRecord.createRecord.name("subrec2"))
val sr1Json =
JObject(List(
JField("name", JString("subrec1")),
JField("subsub", JObject(List(
JField("name", JString(""))
))),
JField("subsublist", JArray(List())),
JField("when", JObject(List(
JField("$dt", JString(rec.meta.formats.dateFormat.format(lst(0).when.value)))
))),
JField("slist", JArray(List())),
JField("smap", JObject(List())),
JField("oid", JObject(List(JField("$oid", JString(lst(0).oid.value.toString))))),
JField("pattern", JObject(List(
JField("$regex", JString(lst(0).pattern.value.pattern)),
JField("$flags", JInt(lst(0).pattern.value.flags))
))),
JField("uuid", JObject(List(JField("$uuid", JString(lst(0).uuid.value.toString)))))
))
val sr2Json =
JObject(List(
JField("name", JString("subrec2")),
JField("subsub", JObject(List(
JField("name", JString(""))
))),
JField("subsublist", JArray(List())),
JField("when", JObject(List(
JField("$dt", JString(rec.meta.formats.dateFormat.format(lst(1).when.value)))
))),
JField("slist", JArray(List())),
JField("smap", JObject(List())),
JField("oid", JObject(List(JField("$oid", JString(lst(1).oid.value.toString))))),
JField("pattern", JObject(List(
JField("$regex", JString(lst(1).pattern.value.pattern)),
JField("$flags", JInt(lst(1).pattern.value.flags))
))),
JField("uuid", JObject(List(JField("$uuid", JString(lst(1).uuid.value.toString)))))
))
val sr1JsExp = new JsExp {
def toJsCmd = compact(render(sr1Json))
}
val sr2JsExp = new JsExp {
def toJsCmd = compact(render(sr2Json))
}
passBasicTests(lst, rec.mandatoryBsonRecordListField, rec.legacyOptionalBsonRecordListField)
passConversionTests(
lst,
rec.mandatoryBsonRecordListField,
JsArray(sr1JsExp, sr2JsExp),
JArray(List(sr1Json, sr2Json)),
Empty
)
}
}
}