File tree Expand file tree Collapse file tree 3 files changed +44
-5
lines changed
src/test/scala/dev/mongocamp/driver/mongodb/bson Expand file tree Collapse file tree 3 files changed +44
-5
lines changed Original file line number Diff line number Diff line change 8585 </check >
8686 <check level =" warning" class =" org.scalastyle.scalariform.CyclomaticComplexityChecker" enabled =" true" >
8787 <parameters >
88- <parameter name =" maximum" ><![CDATA[ 30 ]]> </parameter >
88+ <parameter name =" maximum" ><![CDATA[ 35 ]]> </parameter >
8989 </parameters >
9090 </check >
9191 <check level =" warning" class =" org.scalastyle.scalariform.UppercaseLChecker" enabled =" true" ></check >
Original file line number Diff line number Diff line change 11package dev .mongocamp .driver .mongodb .bson
22
33import org .bson .BsonValue
4- import org .joda .time .DateTime
5- import org .mongodb .scala .bson .{BsonDateTime , BsonNull }
4+ import org .joda .time .{ DateTime , Duration }
5+ import org .mongodb .scala .bson .{BsonDateTime , BsonNull , BsonString }
66
77class JodaConverterPlugin extends AbstractConverterPlugin {
8- override def customClassList : List [Class [_]] = List (classOf [DateTime ])
8+ override def customClassList : List [Class [_]] = List (classOf [DateTime ], classOf [ Duration ] )
99
1010 override def toBson (value : Any ): BsonValue =
1111 value match {
12- case dt : DateTime => BsonDateTime (dt.toDate)
12+ case dt : DateTime =>
13+ BsonDateTime (dt.toDate)
14+ case dt : org.joda.time.Duration =>
15+ BsonString (s " ${dt.getMillis}ms " )
1316 case _ =>
1417 BsonNull ()
1518
Original file line number Diff line number Diff line change 1+ package dev .mongocamp .driver .mongodb .bson
2+
3+ import org .joda .time .DateTime
4+ import org .specs2 .mutable .Specification
5+ import org .specs2 .specification .BeforeAfterAll
6+
7+ import scala .concurrent .duration .Duration
8+
9+ class JodaConverterPluginSpec extends Specification with BeforeAfterAll {
10+
11+ sequential
12+
13+ " JodaConverterPlugin" should {
14+
15+ " convert joda dates to bson dates" in {
16+ val dateTime = new DateTime (" 2023-11-02" )
17+ val bsonDocument = BsonConverter .toBson(dateTime)
18+ (bsonDocument.toString must be).equalTo(" BsonDateTime{value=1698879600000}" )
19+ }
20+
21+ " convert joda duration to bson string" in {
22+ val duration = org.joda.time.Duration .standardDays(1 )
23+ val bsonDocument = BsonConverter .toBson(duration)
24+ (bsonDocument.toString must be).equalTo(" BsonString{value='86400000ms'}" )
25+ (Duration (" 86400000ms" ).toMillis must be).equalTo(duration.getMillis)
26+ }
27+
28+ }
29+
30+ override def beforeAll (): Unit = {
31+ BsonConverter .converterPlugin = new JodaConverterPlugin ()
32+ }
33+ override def afterAll (): Unit = {
34+ BsonConverter .converterPlugin = new BaseConverterPlugin ()
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments