Skip to content

Commit 3854555

Browse files
author
Roman Janusz
committed
test improvements
1 parent 099a8a5 commit 3854555

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

commons-mongo/jvm/src/main/scala/com/avsystem/commons/mongo/BsonInputOutput.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package mongo
33

44
import com.avsystem.commons.serialization.GenCodec.ReadFailure
55
import com.avsystem.commons.serialization.{InputAndSimpleInput, InputMetadata, OutputAndSimpleOutput, TypeMarker}
6-
import org.bson.{BsonInvalidOperationException, BsonType, BsonValue}
76
import org.bson.types.{Decimal128, ObjectId}
7+
import org.bson.{BsonInvalidOperationException, BsonType, BsonValue}
88

99
import java.nio.ByteBuffer
1010

commons-mongo/jvm/src/test/scala/com/avsystem/commons/mongo/BsonInputOutputTest.scala

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.avsystem.commons
22
package mongo
33

4-
import java.nio.ByteBuffer
5-
6-
import com.avsystem.commons.serialization.{GenCodecRoundtripTest, Input, ObjectInput, Output}
4+
import com.avsystem.commons.serialization._
75
import org.bson._
86
import org.bson.io.BasicOutputBuffer
97
import org.bson.json.{JsonMode, JsonWriterSettings}
8+
import org.bson.types.Decimal128
109
import org.scalactic.source.Position
1110
import org.scalatest.funsuite.AnyFunSuite
1211
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
1312

13+
import java.nio.ByteBuffer
14+
1415
class BinaryBsonGenCodecRoundtripTest extends GenCodecRoundtripTest {
1516
type Raw = Array[Byte]
1617

@@ -137,12 +138,44 @@ class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks {
137138
forAll(SomethingComplex.gen)(valueEncoding(_, legacyOptionEncoding = true))
138139
}
139140

140-
test("Int32 to Long decoding") {
141-
val doc = new BsonDocument("value", new BsonInt32(42))
142-
val reader = new BsonDocumentReader(doc)
143-
val input = new BsonReaderInput(reader, false)
144-
val sth = SomethingLong.codec.read(input)
145-
assert(sth == SomethingLong(42))
141+
private case class Wrap[+T](v: T)
142+
private object Wrap extends HasPolyGenCodec[Wrap]
143+
144+
def testRoundtripAndRepr[T: GenCodec](value: T, expectedRepr: BsonValue)(implicit pos: Position): Unit = {
145+
val repr = BsonValueOutput.write(value)
146+
assert(repr == expectedRepr)
147+
assert(BsonValueInput.read[T](repr) == value)
148+
149+
val output = new BasicOutputBuffer
150+
val writer = new BsonBinaryWriter(output)
151+
GenCodec.write(new BsonWriterOutput(writer), Wrap(value))
152+
writer.flush()
153+
writer.close()
154+
155+
val bytes = output.toByteArray
156+
val reprFromBinary = BsonValueUtils.decode(new BsonBinaryReader(ByteBuffer.wrap(bytes))).asDocument().get("v")
157+
assert(reprFromBinary == expectedRepr)
158+
assert(GenCodec.read[Wrap[T]](new BsonReaderInput(new BsonBinaryReader(ByteBuffer.wrap(bytes)))).v == value)
159+
}
160+
161+
test("Long encoding") {
162+
testRoundtripAndRepr[Long](1, new BsonInt32(1))
163+
testRoundtripAndRepr[Long](Int.MaxValue + 1L, new BsonInt64(Int.MaxValue + 1L))
164+
}
165+
166+
test("BigInt encoding") {
167+
testRoundtripAndRepr[BigInt](1, new BsonInt32(1))
168+
testRoundtripAndRepr[BigInt](BigInt("1"), new BsonInt32(1))
169+
testRoundtripAndRepr[BigInt](Int.MaxValue + 1L, new BsonInt64(Int.MaxValue + 1L))
170+
testRoundtripAndRepr[BigInt](BigInt("123123123123123123123"), new BsonDecimal128(Decimal128.parse("123123123123123123123")))
171+
testRoundtripAndRepr[BigInt](BigInt("123123123123123123123123123123123123123123"), new BsonBinary(Base64.decode("AWnTiveIuE7XC8Q4zzH4T/Oz")))
172+
}
173+
174+
test("BigDecimal encoding") {
175+
testRoundtripAndRepr[BigDecimal](1, new BsonDecimal128(new Decimal128(1)))
176+
testRoundtripAndRepr[BigDecimal](BigDecimal("0.00001"), new BsonDecimal128(Decimal128.parse("0.00001")))
177+
testRoundtripAndRepr[BigDecimal](BigDecimal("123123123123.123123123"), new BsonDecimal128(Decimal128.parse("123123123123.123123123")))
178+
testRoundtripAndRepr[BigDecimal](BigDecimal("123123123123123123.123123123123123123123123"), new BsonBinary(Base64.decode("AWnTiveIuE7XC8Q4zzH4T/OzAAAAGA==")))
146179
}
147180

148181
test("All encoding schemes with problematic map keys") {

0 commit comments

Comments
 (0)