Skip to content

Commit 05cd13e

Browse files
committed
test: try to fix ci test problem
1 parent a27e03d commit 05cd13e

5 files changed

Lines changed: 35 additions & 27 deletions

File tree

.github/workflows/main_test_release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
mongodb-version: ['4.4', '5.0', '6.0', '7.0']
16+
mongodb-version: ['4.4', '5.0', '6.0', '7.0', '8.0']
1717
java: [ '21', '23' ]
1818
steps:
1919
- uses: actions/checkout@main

.github/workflows/other_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
mongodb-version: [ '4.4', '5.0', '6.0', '7.0' ]
16+
mongodb-version: [ '4.4', '5.0', '6.0', '7.0', '8.0' ]
1717
java: [ '21', '23' ]
1818
steps:
1919
- uses: actions/checkout@main

src/main/scala/dev/mongocamp/driver/mongodb/lucene/LuceneQueryConverter.scala

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import dev.mongocamp.driver.mongodb.exception.NotSupportedException
66
import org.apache.lucene.queryparser.classic.QueryParser
77
import org.apache.lucene.search._
88
import org.apache.lucene.search.BooleanClause.Occur
9+
import org.joda.time.DateTime
910
import org.mongodb.scala.bson.conversions.Bson
1011

1112
import java.text.SimpleDateFormat
1213
import java.util.Date
1314
import scala.collection.mutable
1415
import scala.collection.mutable.ArrayBuffer
1516
import scala.jdk.CollectionConverters._
17+
import scala.util.Try
1618

1719
object LuceneQueryConverter extends LazyLogging {
1820

@@ -217,25 +219,23 @@ object LuceneQueryConverter extends LazyLogging {
217219
try {
218220
val convertedValue: Option[Any] =
219221
(List() ++ checkOrReturn(() => s.toDouble) ++ checkOrReturn(() => s.toLong) ++ checkOrReturn(() => s.toBoolean)).headOption
220-
convertedValue.getOrElse({
221-
val parsedOptions: Option[Date] = datePatters
222-
.map(pattern => {
223-
try {
224-
val formatter = new SimpleDateFormat(pattern)
225-
Option(formatter.parse(s))
226-
}
227-
catch {
228-
case _: Exception =>
229-
None
230-
}
231-
})
232-
.find(_.nonEmpty)
233-
.flatten
234-
parsedOptions.getOrElse(s)
222+
val response = convertedValue.getOrElse({
223+
val parsedOptions: List[Date] = Try(new DateTime(s).toDate).toOption.toList ++ datePatters.flatMap(pattern => {
224+
try {
225+
val formatter = new SimpleDateFormat(pattern)
226+
Option(formatter.parse(s))
227+
}
228+
catch {
229+
case _: Exception =>
230+
None
231+
}
232+
}).distinct
233+
parsedOptions.headOption.getOrElse(s)
235234
})
235+
response
236236
}
237237
catch {
238-
case _: Exception =>
238+
case _: Throwable =>
239239
s
240240
}
241241
}

src/test/scala/dev/mongocamp/driver/mongodb/bson/JodaConverterPluginSpec.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ class JodaConverterPluginSpec extends Specification with BeforeAfterAll {
1313
"JodaConverterPlugin" should {
1414

1515
"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}")
16+
val dateTime = new DateTime("2023-11-02")
17+
val bsonDocument = BsonConverter.toBson(dateTime)
18+
val roundTripDate = new DateTime(bsonDocument.asDateTime().getValue)
19+
(roundTripDate must be).equalTo(dateTime)
1920
}
2021

2122
"convert joda duration to bson string" in {

src/test/scala/dev/mongocamp/driver/mongodb/lucene/LuceneSearchSpec.scala

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ package dev.mongocamp.driver.mongodb.lucene
33
import dev.mongocamp.driver.mongodb._
44
import dev.mongocamp.driver.mongodb.dao.PersonSpecification
55
import dev.mongocamp.driver.mongodb.test.TestDatabase._
6+
import org.joda.time.DateTime
7+
8+
import java.util.TimeZone
69

710
class LuceneSearchSpec extends PersonSpecification {
8-
lazy val sortByBalance: Map[String,Int] = Map("balance" -> -1)
11+
lazy val sortByBalance: Map[String, Int] = Map("balance" -> -1)
12+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
913

1014
"LuceneSearch" should {
1115

1216
"search with with number in string" in {
1317
val luceneQuery = LuceneQueryConverter.parse("stringNumber: 123", "id")
14-
val search2 = PersonDAO.find(LuceneQueryConverter.toDocument(luceneQuery), sortByBalance).resultList()
18+
val search2 = PersonDAO.find(LuceneQueryConverter.toDocument(luceneQuery), sortByBalance).resultList()
1519
search2 must haveSize(0)
1620
val search = PersonDAO.find(LuceneQueryConverter.toDocument(luceneQuery, searchWithValueAndString = true), sortByBalance).resultList()
1721
search must haveSize(1)
@@ -60,16 +64,19 @@ class LuceneSearchSpec extends PersonSpecification {
6064
}
6165

6266
"between filter for date value" in {
63-
val luceneQuery = LuceneQueryConverter.parse("[2014-04-20T00:00:00Z TO 2014-04-22T23:59:59Z]", "registered")
64-
val search = PersonDAO.find(LuceneQueryConverter.toDocument(luceneQuery), sortByBalance).resultList()
65-
search must haveSize(10)
67+
val luceneQuery = LuceneQueryConverter.parse("[2014-04-20T00:00:00Z TO 2014-04-22T23:59:59Z]", "registered")
68+
val luceneDocument = LuceneQueryConverter.toDocument(luceneQuery)
69+
val expected = "Iterable((registered,{\"$lte\": {\"$date\": \"2014-04-22T23:59:59Z\"}, \"$gte\": {\"$date\": \"2014-04-20T00:00:00Z\"}}))"
70+
luceneDocument.toString must beEqualTo(expected)
71+
val search = PersonDAO.find(luceneDocument, sortByBalance).resultList()
72+
search must haveSize(7)
6673
search.head.age mustEqual 25
6774
search.head.name mustEqual "Allison Turner"
6875
search.head.balance mustEqual 3961.0
6976
}
7077

7178
"equals Query with Date" in {
72-
val luceneQuery = LuceneQueryConverter.parse("registered:20140420T004427000+0200", "unbekannt")
79+
val luceneQuery = LuceneQueryConverter.parse("registered:20140419T224427000\\+0200", "unbekannt")
7380
val search = PersonDAO.find(LuceneQueryConverter.toDocument(luceneQuery), sortByBalance).resultList()
7481
search must haveSize(1)
7582
search.head.age mustEqual 31

0 commit comments

Comments
 (0)