Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kmruiz committed Jan 27, 2025
1 parent 5702b27 commit c66f12e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.bson.types.ObjectId
import org.owasp.encoder.Encode
import java.math.BigDecimal
import java.math.BigInteger
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -249,7 +250,8 @@ private fun serializePrimitive(value: Any?): String = when (value) {
is Boolean -> value.toString()
is ObjectId -> "ObjectId(\"${Encode.forJavaScript(value.toHexString())}\")"
is String -> '"' + Encode.forJavaScript(value) + '"'
is Date, is Instant, is LocalDate, is LocalDateTime, is TemporalAccessor ->
is Date -> "ISODate(\"${SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(value)}\")"
is Instant, is LocalDate, is LocalDateTime, is TemporalAccessor ->
"ISODate(\"${DateTimeFormatter.ISO_DATE_TIME.format(value as TemporalAccessor)}\")"
is UUID -> "UUID(\"$value\")"
is Collection<*> -> value.joinToString(separator = ", ", prefix = "[", postfix = "]") {
Expand All @@ -259,7 +261,7 @@ private fun serializePrimitive(value: Any?): String = when (value) {
is Map<*, *> -> value.entries.joinToString(separator = ", ", prefix = "{", postfix = "}") {
"\"${it.key}\": ${serializePrimitive(it.value)}"
}
is QueryContext.AsIs -> value.value.toString()
is AsIs -> value.value.toString()

Check notice

Code scanning / CodeQL

Useless toString on String Note

Redundant call to 'toString' on a String object.
null -> "null"
else -> "{}"
}
Expand All @@ -269,7 +271,7 @@ private fun defaultValueOfBsonType(type: BsonType): Any? = when (type) {
is BsonAnyOf -> defaultValueOfBsonType(type.types.firstOrNull { it !is BsonNull } ?: BsonAny)
is BsonArray -> emptyList<Any>()
BsonBoolean -> false
BsonDate -> Date.from(Instant.parse(MONGODB_FIRST_RELEASE))
BsonDate -> LocalDateTime.of(2009, 2, 11, 18, 0)
BsonDecimal128 -> BigInteger.ZERO
BsonDouble -> 0.0
BsonInt32 -> 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import java.math.BigDecimal
import java.math.BigInteger
import java.time.LocalDateTime
import java.util.*

class MongoshBackendTest {
Expand Down Expand Up @@ -46,13 +47,13 @@ class MongoshBackendTest {
""".trimIndent()
) {
emitDbAccess()
emitDatabaseAccess(registerVariable("myDb", BsonString))
emitCollectionAccess(registerVariable("myColl", BsonString))
emitDatabaseAccess(registerVariable("myDb", BsonString, null))
emitCollectionAccess(registerVariable("myColl", BsonString, null))
emitFunctionName("find")
emitFunctionCall(long = false, {
emitObjectStart()
emitObjectKey(registerConstant("field"))
emitContextValue(registerVariable("myValue", BsonString))
emitContextValue(registerVariable("myValue", BsonString, null))
emitObjectEnd()
})
}
Expand All @@ -70,13 +71,13 @@ class MongoshBackendTest {
""".trimIndent()
) {
emitDbAccess()
emitDatabaseAccess(registerVariable("myDb", BsonString))
emitCollectionAccess(registerVariable("myColl", BsonString))
emitDatabaseAccess(registerVariable("myDb", BsonString, null))
emitCollectionAccess(registerVariable("myColl", BsonString, null))
emitFunctionName("update")
emitFunctionCall(long = false, {
emitObjectStart()
emitObjectKey(registerConstant("field"))
emitContextValue(registerVariable("myValue", BsonString))
emitContextValue(registerVariable("myValue", BsonString, null))
emitObjectEnd()
}, {
emitObjectStart()
Expand Down Expand Up @@ -111,7 +112,7 @@ class MongoshBackendTest {
arg
""".trimIndent()
) {
emitContextValue(registerVariable("arg", type))
emitContextValue(registerVariable("arg", type, null))
}
}

Expand All @@ -121,12 +122,13 @@ class MongoshBackendTest {
1 to "1",
1.5 to "1.5",
"myString" to "\"myString\"",
Date() to "ISODate()",
LocalDateTime.of(2000, 5, 10, 5, 0) to "ISODate(\"2000-05-10T05:00:00\")",
BigInteger("5234") to "Decimal128(\"5234\")",
BigDecimal("5234.5234") to "Decimal128(\"5234.5234\")",
true to "true",
ObjectId("66e02569aa5b362fa36f2416") to "ObjectId(\"66e02569aa5b362fa36f2416\")",
listOf(1, 2.2, Date()) to "[1, 2.2, ISODate()]",
listOf(1, 2.2, LocalDateTime.of(2000, 5, 10, 5, 0)) to
"[1, 2.2, ISODate(\"2000-05-10T05:00:00\")]",
mapOf("a" to "1", "b" to 2) to "{\"a\": \"1\", \"b\": 2}",
SomeObject(1, "2") to "{}", // we won't serialize unknown objects
)
Expand All @@ -138,7 +140,7 @@ class MongoshBackendTest {
BsonAnyOf(BsonNull, BsonString, BsonInt64) to "\"\"",
BsonArray(BsonAny) to "[]",
BsonBoolean to "false",
BsonDate to "ISODate(\"2009-02-11T18:00:00.000Z\")",
BsonDate to "ISODate(\"2009-02-11T18:00:00\")",
BsonDecimal128 to "Decimal128(\"0\")",
BsonDouble to "0.0",
BsonInt32 to "0",
Expand Down

0 comments on commit c66f12e

Please sign in to comment.