-
Notifications
You must be signed in to change notification settings - Fork 275
/
Fixtures.scala
460 lines (374 loc) · 18.7 KB
/
Fixtures.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
/*
* 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 mongodb
package record
package fixtures
import field._
import common.{Box, Empty, Failure, Full}
import json.ext.JsonBoxSerializer
import util.FieldError
import java.math.MathContext
import scala.xml.Text
import net.liftweb.record._
import net.liftweb.record.field._
object MyTestEnum extends Enumeration {
val ONE = Value("ONE")
val TWO = Value("TWO")
val THREE = Value("THREE")
}
trait HarnessedLifecycleCallbacks extends LifecycleCallbacks {
this: BaseField =>
var beforeValidationHarness: () => Unit = () => ()
override def beforeValidation = beforeValidationHarness()
var afterValidationHarness: () => Unit = () => ()
override def afterValidation = afterValidationHarness()
var beforeSaveHarness: () => Unit = () => ()
override def beforeSave = beforeSaveHarness()
var beforeCreateHarness: () => Unit = () => ()
override def beforeCreate = beforeCreateHarness()
var beforeUpdateHarness: () => Unit = () => ()
override def beforeUpdate = beforeUpdateHarness()
var afterSaveHarness: () => Unit = () => ()
override def afterSave = afterSaveHarness()
var afterCreateHarness: () => Unit = () => ()
override def afterCreate = afterCreateHarness()
var afterUpdateHarness: () => Unit = () => ()
override def afterUpdate = afterUpdateHarness()
var beforeDeleteHarness: () => Unit = () => ()
override def beforeDelete = beforeDeleteHarness()
var afterDeleteHarness: () => Unit = () => ()
override def afterDelete = afterDeleteHarness()
}
class FieldTypeTestRecord private () extends MongoRecord[FieldTypeTestRecord] with ObjectIdPk[FieldTypeTestRecord] {
def meta = FieldTypeTestRecord
object mandatoryBooleanField extends BooleanField(this)
object legacyOptionalBooleanField extends BooleanField(this) { override def optional_? = true }
object optionalBooleanField extends OptionalBooleanField(this)
object mandatoryCountryField extends CountryField(this)
object legacyOptionalCountryField extends CountryField(this) { override def optional_? = true }
object optionalCountryField extends OptionalCountryField(this)
/*
object mandatoryDateTimeField extends DateTimeField(this)
object legacyOptionalDateTimeField extends DateTimeField(this) { override def optional_? = true }
object optionalDateTimeField extends OptionalDateTimeField(this)
*/
object mandatoryDecimalField extends DecimalField(this, MathContext.UNLIMITED, 2)
object legacyOptionalDecimalField extends DecimalField(this, MathContext.UNLIMITED, 2) { override def optional_? = true }
object optionalDecimalField extends OptionalDecimalField(this, MathContext.UNLIMITED, 2)
object mandatoryDoubleField extends DoubleField(this)
object legacyOptionalDoubleField extends DoubleField(this) { override def optional_? = true }
object optionalDoubleField extends OptionalDoubleField(this)
object mandatoryEmailField extends EmailField(this, 100)
object legacyOptionalEmailField extends EmailField(this, 100) { override def optional_? = true }
object optionalEmailField extends OptionalEmailField(this, 100)
object mandatoryEnumField extends EnumField(this, MyTestEnum)
object legacyOptionalEnumField extends EnumField(this, MyTestEnum) { override def optional_? = true }
object optionalEnumField extends OptionalEnumField(this, MyTestEnum)
object mandatoryIntField extends IntField(this)
object legacyOptionalIntField extends IntField(this) { override def optional_? = true }
object optionalIntField extends OptionalIntField(this)
object mandatoryLocaleField extends LocaleField(this)
object legacyOptionalLocaleField extends LocaleField(this) { override def optional_? = true }
object optionalLocaleField extends OptionalLocaleField(this)
object mandatoryLongField extends LongField(this)
object legacyOptionalLongField extends LongField(this) { override def optional_? = true }
object optionalLongField extends OptionalLongField(this)
// FIXME would be nice to have some of these PostalCode fields depend on an OptionalCountryField, but the type sig of
// PostalCodeField does not yet allow it.
object mandatoryPostalCodeField extends PostalCodeField(this, mandatoryCountryField)
object legacyOptionalPostalCodeField extends PostalCodeField(this, mandatoryCountryField) { override def optional_? = true }
object optionalPostalCodeField extends OptionalPostalCodeField(this, mandatoryCountryField)
object mandatoryStringField extends StringField(this, 100)
object legacyOptionalStringField extends StringField(this, 100) { override def optional_? = true }
object optionalStringField extends OptionalStringField(this, 100)
object mandatoryTextareaField extends TextareaField(this, 100)
object legacyOptionalTextareaField extends TextareaField(this, 100) { override def optional_? = true }
object optionalTextareaField extends OptionalTextareaField(this, 100)
object mandatoryTimeZoneField extends TimeZoneField(this)
object legacyOptionalTimeZoneField extends TimeZoneField(this) { override def optional_? = true }
object optionalTimeZoneField extends OptionalTimeZoneField(this)
override def equals(other: Any): Boolean = other match {
case that: FieldTypeTestRecord =>
this.id.value == that.id.value &&
this.mandatoryBooleanField.value == that.mandatoryBooleanField.value &&
this.mandatoryCountryField.value == that.mandatoryCountryField.value &&
this.mandatoryDecimalField.value == that.mandatoryDecimalField.value &&
this.mandatoryDoubleField.value == that.mandatoryDoubleField.value &&
this.mandatoryEmailField.value == that.mandatoryEmailField.value &&
this.mandatoryEnumField.value == that.mandatoryEnumField.value &&
this.mandatoryIntField.value == that.mandatoryIntField.value &&
this.mandatoryLocaleField.value == that.mandatoryLocaleField.value &&
this.mandatoryLongField.value == that.mandatoryLongField.value &&
this.mandatoryPostalCodeField.value == that.mandatoryPostalCodeField.value &&
this.mandatoryStringField.value == that.mandatoryStringField.value &&
this.mandatoryTextareaField.value == that.mandatoryTextareaField.value &&
this.mandatoryTimeZoneField.value == that.mandatoryTimeZoneField.value
case _ => false
}
}
object FieldTypeTestRecord extends FieldTypeTestRecord with MongoMetaRecord[FieldTypeTestRecord]
class BinaryFieldTestRecord extends MongoRecord[BinaryFieldTestRecord] with IntPk[BinaryFieldTestRecord] {
def meta = BinaryFieldTestRecord
object mandatoryBinaryField extends BinaryField(this) {
// compare the elements of the Array
override def equals(other: Any): Boolean = other match {
case that: BinaryField[Any] =>
this.value.zip(that.value).filter(t => t._1 != t._2).length == 0
case _ => false
}
}
object legacyOptionalBinaryField extends BinaryField(this) {
override def optional_? = true
// compare the elements of the Array
override def equals(other: Any): Boolean = other match {
case that: BinaryField[Any] => (this.valueBox, that.valueBox) match {
case (Empty, Empty) => true
case (Full(a), Full(b)) =>
a.zip(b).filter(t => t._1 != t._2).length == 0
case _ => false
}
case _ => false
}
}
object optionalBinaryField extends OptionalBinaryField(this) {
// compare the elements of the Array
override def equals(other: Any): Boolean = other match {
case that: OptionalBinaryField[Any] => (this.valueBox, that.valueBox) match {
case (Empty, Empty) => true
case (Full(a), Full(b)) =>
a.zip(b).filter(t => t._1 != t._2).length == 0
case _ => false
}
case _ => false
}
}
override def equals(other: Any): Boolean = other match {
case that:BinaryFieldTestRecord =>
this.id.value == that.id.value &&
this.mandatoryBinaryField == that.mandatoryBinaryField &&
this.legacyOptionalBinaryField == that.legacyOptionalBinaryField &&
this.optionalBinaryField == that.optionalBinaryField
case _ => false
}
}
object BinaryFieldTestRecord extends BinaryFieldTestRecord with MongoMetaRecord[BinaryFieldTestRecord]
case class TypeTestJsonObject(
intField: Int,
stringField: String,
mapField: Map[String, String]
) extends JsonObject[TypeTestJsonObject]
{
// TODO: Add more types
def meta = TypeTestJsonObject
}
object TypeTestJsonObject extends JsonObjectMeta[TypeTestJsonObject]
class DBRefTestRecord private () extends MongoRecord[DBRefTestRecord] with MongoId[DBRefTestRecord] {
def meta = DBRefTestRecord
}
object DBRefTestRecord extends DBRefTestRecord with MongoMetaRecord[DBRefTestRecord]
class MongoFieldTypeTestRecord private () extends MongoRecord[MongoFieldTypeTestRecord] with ObjectIdPk[MongoFieldTypeTestRecord] {
def meta = MongoFieldTypeTestRecord
object mandatoryDateField extends DateField(this)
object legacyOptionalDateField extends DateField(this) { override def optional_? = true }
object mandatoryJsonObjectField extends JsonObjectField(this, TypeTestJsonObject) {
def defaultValue = TypeTestJsonObject(0, "", Map[String, String]())
}
object legacyOptionalJsonObjectField extends JsonObjectField(this, TypeTestJsonObject) {
override def optional_? = true
def defaultValue = TypeTestJsonObject(0, "", Map[String, String]())
}
object mandatoryObjectIdField extends ObjectIdField(this)
object legacyOptionalObjectIdField extends ObjectIdField(this) { override def optional_? = true }
object mandatoryPatternField extends PatternField(this)
object legacyOptionalPatternField extends PatternField(this) { override def optional_? = true }
object mandatoryUUIDField extends UUIDField(this)
object legacyOptionalUUIDField extends UUIDField(this) { override def optional_? = true }
override def equals(other: Any): Boolean = other match {
case that: MongoFieldTypeTestRecord =>
this.id.value == that.id.value &&
this.mandatoryDateField.value == that.mandatoryDateField.value &&
this.mandatoryJsonObjectField.value == that.mandatoryJsonObjectField.value &&
this.mandatoryObjectIdField.value == that.mandatoryObjectIdField.value &&
this.mandatoryPatternField.value.pattern == that.mandatoryPatternField.value.pattern &&
this.mandatoryPatternField.value.flags == that.mandatoryPatternField.value.flags &&
this.mandatoryUUIDField.value == that.mandatoryUUIDField.value
case _ => false
}
}
object MongoFieldTypeTestRecord extends MongoFieldTypeTestRecord with MongoMetaRecord[MongoFieldTypeTestRecord] {
override def formats = allFormats
}
class PasswordTestRecord private () extends MongoRecord[PasswordTestRecord] with ObjectIdPk[PasswordTestRecord] {
def meta = PasswordTestRecord
object password extends MongoPasswordField(this, 3)
}
object PasswordTestRecord extends PasswordTestRecord with MongoMetaRecord[PasswordTestRecord]
case class MongoCaseClassTestObject(intField: Int, stringField: String)
class ListTestRecord private () extends MongoRecord[ListTestRecord] with UUIDPk[ListTestRecord] {
def meta = ListTestRecord
object mandatoryStringListField extends MongoListField[ListTestRecord, String](this)
object legacyOptionalStringListField extends MongoListField[ListTestRecord, String](this) { override def optional_? = true }
object mandatoryIntListField extends MongoListField[ListTestRecord, Int](this)
object legacyOptionalIntListField extends MongoListField[ListTestRecord, Int](this) { override def optional_? = true }
object mandatoryMongoJsonObjectListField extends MongoJsonObjectListField(this, TypeTestJsonObject)
object legacyOptionalMongoJsonObjectListField extends MongoJsonObjectListField(this, TypeTestJsonObject) { override def optional_? = true }
object mongoCaseClassListField extends MongoCaseClassListField[ListTestRecord, MongoCaseClassTestObject](this)
// TODO: More List types
override def equals(other: Any): Boolean = other match {
case that: ListTestRecord =>
this.id.value == that.id.value &&
this.mandatoryStringListField.value == that.mandatoryStringListField.value &&
this.mandatoryIntListField.value == that.mandatoryIntListField.value &&
this.mandatoryMongoJsonObjectListField.value == that.mandatoryMongoJsonObjectListField.value
case _ => false
}
}
object ListTestRecord extends ListTestRecord with MongoMetaRecord[ListTestRecord] {
override def formats = allFormats
}
class MapTestRecord private () extends MongoRecord[MapTestRecord] with StringPk[MapTestRecord] {
def meta = MapTestRecord
object mandatoryStringMapField extends MongoMapField[MapTestRecord, String](this)
object legacyOptionalStringMapField extends MongoMapField[MapTestRecord, String](this) { override def optional_? = true }
object mandatoryIntMapField extends MongoMapField[MapTestRecord, Int](this)
object legacyOptionalIntMapField extends MongoMapField[MapTestRecord, Int](this) { override def optional_? = true }
// TODO: More Map types, including JsonObject (will require a new Field type)
override def equals(other: Any): Boolean = other match {
case that: MapTestRecord =>
this.id.value == that.id.value &&
this.mandatoryStringMapField.value == that.mandatoryStringMapField.value &&
this.mandatoryIntMapField.value == that.mandatoryIntMapField.value
case _ => false
}
}
object MapTestRecord extends MapTestRecord with MongoMetaRecord[MapTestRecord] {
override def formats = allFormats
}
class LifecycleTestRecord private ()
extends MongoRecord[LifecycleTestRecord]
with MongoId[LifecycleTestRecord]
{
def meta = LifecycleTestRecord
def foreachCallback(f: LifecycleCallbacks => Any): Unit =
meta.foreachCallback(this, f)
object stringFieldWithCallbacks extends StringField(this, 100) with HarnessedLifecycleCallbacks
}
object LifecycleTestRecord extends LifecycleTestRecord with MongoMetaRecord[LifecycleTestRecord]
/*
* SubRecord fields
*/
class SubRecord private () extends BsonRecord[SubRecord] {
def meta = SubRecord
object name extends StringField(this, 12)
object subsub extends BsonRecordField(this, SubSubRecord)
object subsublist extends BsonRecordListField(this, SubSubRecord)
object when extends DateField(this)
object slist extends MongoListField[SubRecord, String](this)
object smap extends MongoMapField[SubRecord, String](this)
object oid extends ObjectIdField(this)
object pattern extends PatternField(this)
object uuid extends UUIDField(this)
override def equals(other: Any): Boolean = other match {
case that: SubRecord => this.toString == that.toString
case _ => false
}
}
object SubRecord extends SubRecord with BsonMetaRecord[SubRecord] {
override def formats = allFormats
}
class SubSubRecord private () extends BsonRecord[SubSubRecord] {
def meta = SubSubRecord
object name extends StringField(this, 12)
override def equals(other: Any): Boolean = other match {
case that: SubSubRecord =>
this.name.value == that.name.value
case _ => false
}
}
object SubSubRecord extends SubSubRecord with BsonMetaRecord[SubSubRecord] {
override def formats = allFormats
}
class SubRecordTestRecord private () extends MongoRecord[SubRecordTestRecord] with ObjectIdPk[SubRecordTestRecord] {
def meta = SubRecordTestRecord
object mandatoryBsonRecordField extends BsonRecordField(this, SubRecord)
object legacyOptionalBsonRecordField extends BsonRecordField(this, SubRecord) {
override def optional_? = true
}
object mandatoryBsonRecordListField extends BsonRecordListField(this, SubRecord)
object legacyOptionalBsonRecordListField extends BsonRecordListField(this, SubRecord) {
override def optional_? = true
}
override def equals(other: Any): Boolean = other match {
case that: SubRecordTestRecord => this.toString == that.toString
case _ => false
}
}
object SubRecordTestRecord extends SubRecordTestRecord with MongoMetaRecord[SubRecordTestRecord] {
override def formats = allFormats
}
case class JsonObj(id: String, name: String) extends JsonObject[JsonObj] {
def meta = JsonObj
}
object JsonObj extends JsonObjectMeta[JsonObj]
class NullTestRecord private () extends MongoRecord[NullTestRecord] with IntPk[NullTestRecord] {
def meta = NullTestRecord
object nullstring extends StringField(this, 32) {
override def optional_? = true
}
object jsonobj extends JsonObjectField[NullTestRecord, JsonObj](this, JsonObj) {
def defaultValue = JsonObj("1", null)
}
object jsonobjlist extends MongoJsonObjectListField[NullTestRecord, JsonObj](this, JsonObj)
override def equals(other: Any): Boolean = other match {
case that: NullTestRecord => this.toString == that.toString
case _ => false
}
}
object NullTestRecord extends NullTestRecord with MongoMetaRecord[NullTestRecord]
case class BoxTestJsonObj(id: String, boxEmpty: Box[String], boxFull: Box[String], boxFail: Box[String])
extends JsonObject[BoxTestJsonObj] {
def meta = BoxTestJsonObj
}
object BoxTestJsonObj extends JsonObjectMeta[BoxTestJsonObj]
class BoxTestRecord private () extends MongoRecord[BoxTestRecord] with LongPk[BoxTestRecord] {
def meta = BoxTestRecord
object jsonobj extends JsonObjectField[BoxTestRecord, BoxTestJsonObj](this, BoxTestJsonObj) {
def defaultValue = BoxTestJsonObj("0", Empty, Full("Full String"), Failure("Failure"))
}
object jsonobjlist extends MongoJsonObjectListField[BoxTestRecord, BoxTestJsonObj](this, BoxTestJsonObj)
override def equals(other: Any): Boolean = other match {
case that: BoxTestRecord => this.toString == that.toString
case _ => false
}
}
object BoxTestRecord extends BoxTestRecord with MongoMetaRecord[BoxTestRecord] {
override def formats = super.formats + new JsonBoxSerializer
}
/*
* MongoRefFields
*/
class RefFieldTestRecord private () extends MongoRecord[RefFieldTestRecord] with ObjectIdPk[RefFieldTestRecord] {
def meta = RefFieldTestRecord
object mandatoryObjectIdRefField extends ObjectIdRefField(this, FieldTypeTestRecord)
object mandatoryUUIDRefField extends UUIDRefField(this, ListTestRecord)
object mandatoryStringRefField extends StringRefField(this, MapTestRecord, 100)
object mandatoryIntRefField extends IntRefField(this, NullTestRecord)
object mandatoryLongRefField extends LongRefField(this, BoxTestRecord)
}
object RefFieldTestRecord extends RefFieldTestRecord with MongoMetaRecord[RefFieldTestRecord] {
override def formats = allFormats
}