|
1 | 1 | package com.avsystem.commons |
2 | 2 | package mongo |
3 | 3 |
|
4 | | -import java.nio.ByteBuffer |
5 | | - |
6 | | -import com.avsystem.commons.serialization.{GenCodecRoundtripTest, Input, ObjectInput, Output} |
| 4 | +import com.avsystem.commons.serialization._ |
7 | 5 | import org.bson._ |
8 | 6 | import org.bson.io.BasicOutputBuffer |
9 | 7 | import org.bson.json.{JsonMode, JsonWriterSettings} |
| 8 | +import org.bson.types.Decimal128 |
10 | 9 | import org.scalactic.source.Position |
11 | 10 | import org.scalatest.funsuite.AnyFunSuite |
12 | 11 | import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks |
13 | 12 |
|
| 13 | +import java.nio.ByteBuffer |
| 14 | + |
14 | 15 | class BinaryBsonGenCodecRoundtripTest extends GenCodecRoundtripTest { |
15 | 16 | type Raw = Array[Byte] |
16 | 17 |
|
@@ -137,12 +138,44 @@ class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks { |
137 | 138 | forAll(SomethingComplex.gen)(valueEncoding(_, legacyOptionEncoding = true)) |
138 | 139 | } |
139 | 140 |
|
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=="))) |
146 | 179 | } |
147 | 180 |
|
148 | 181 | test("All encoding schemes with problematic map keys") { |
|
0 commit comments