1
1
package com .avsystem .commons
2
2
package mongo
3
3
4
+ import com .avsystem .commons .serialization .json .{JsonStringInput , JsonStringOutput }
4
5
import com .avsystem .commons .serialization .{GenCodec , HasGenCodecWithDeps }
6
+ import org .bson ._
5
7
import org .bson .json .JsonReader
6
8
import org .bson .types .{Decimal128 , ObjectId }
7
- import org .bson . _
9
+ import org .scalactic . source . Position
8
10
import org .scalatest .funsuite .AnyFunSuite
9
11
10
12
case class AllTypesInABag (
@@ -23,7 +25,7 @@ case class AllTypesInABag(
23
25
)
24
26
object AllTypesInABag extends HasGenCodecWithDeps [BsonGenCodecs .type , AllTypesInABag ]
25
27
26
- class BsonValueCodecsTest extends AnyFunSuite {
28
+ class BsonValueCodecsTest extends AnyFunSuite with BsonGenCodecs {
27
29
test(" codec roundtrip" ) {
28
30
val doc = new BsonDocument (JList (
29
31
new BsonElement (" someInt64" , new BsonInt64 (64 )),
@@ -69,8 +71,6 @@ class BsonValueCodecsTest extends AnyFunSuite {
69
71
}
70
72
71
73
test(" null handling" ) {
72
- import BsonGenCodecs .bsonDocumentCodec
73
-
74
74
val reader = new JsonReader (""" {"key": null}""" )
75
75
val input = new BsonReaderInput (reader)
76
76
val document = GenCodec .read[BsonDocument ](input)
@@ -83,11 +83,22 @@ class BsonValueCodecsTest extends AnyFunSuite {
83
83
}
84
84
85
85
test(" null in doc in array" ) {
86
- import BsonGenCodecs .bsonArrayCodec
87
-
88
86
val reader = new JsonReader (""" [{"key": null}]""" )
89
87
val input = new BsonReaderInput (reader)
90
88
val array = GenCodec .read[BsonArray ](input)
91
89
assert(array === new BsonArray (JList (new BsonDocument (" key" , BsonNull .VALUE ))))
92
90
}
91
+
92
+ def testJsonRoundtrip [T : GenCodec ](value : T )(implicit pos : Position ): Unit = {
93
+ val json = JsonStringOutput .write(value)
94
+ val readValue = JsonStringInput .read[T ](json)
95
+ assert(value == readValue)
96
+ }
97
+
98
+ test(" JSON string roundtrip for BsonValue" ) {
99
+ testJsonRoundtrip(new BsonBoolean (true ))
100
+ testJsonRoundtrip(new BsonInt32 (42 ))
101
+ testJsonRoundtrip(new BsonInt64 (42 ))
102
+ testJsonRoundtrip(new BsonDocument (" v" , new BsonInt32 (42 )))
103
+ }
93
104
}
0 commit comments