Skip to content

Commit

Permalink
Unit test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Nov 1, 2024
1 parent 0dd3779 commit 83592b7
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class JsonSchemaBuilder(
}

val ser = serializer.unwrap()
overrides[ser.descriptor.serialName.substringBefore('<')]?.let {
overrides[ser.descriptor.serialName.substringBefore('/').substringBefore('<')]?.let {
return defining(ser) { it(ser) }.applyAnnotations(annos)
}
return when (ser.descriptor.kind) {
Expand All @@ -433,7 +433,7 @@ class JsonSchemaBuilder(

SerialKind.ENUM -> defining(serializer) {
JsonSchemaType(
title = ser.descriptor.serialName.substringBefore('<').substringAfterLast('.').titleCase(),
title = ser.descriptor.serialName.substringBefore('/').substringBefore('<').substringAfterLast('.').titleCase(),
type = JsonType3(JsonType2.STRING),
oneOf = (0 until ser.descriptor.elementsCount)
.map {
Expand Down Expand Up @@ -462,7 +462,7 @@ class JsonSchemaBuilder(

StructureKind.CLASS -> defining(serializer) {
JsonSchemaType(
title = ser.descriptor.serialName.substringBefore('<').substringAfterLast('.').titleCase(),
title = ser.descriptor.serialName.substringBefore('/').substringBefore('<').substringAfterLast('.').titleCase(),
type = JsonType3(JsonType2.OBJECT),
properties = ser.serializableProperties?.associate {
val propTitle = it.name.titleCase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.junit.Test
import java.io.File
import kotlin.test.assertEquals
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.milliseconds

class RamAggregationsTest: AggregationsTest() {
override val database: Database = InMemoryDatabase()
Expand All @@ -41,22 +42,22 @@ class RamMetaTest: MetaTest() {
}

class DelayRamAggregationsTest: AggregationsTest() {
override val database: Database = InMemoryDatabase().delayed(5L)
override val database: Database = InMemoryDatabase().delayed(5.milliseconds..6.milliseconds)
}
class DelayRamConditionTests: ConditionTests() {
override val database: Database = InMemoryDatabase().delayed(5L)
override val database: Database = InMemoryDatabase().delayed(5.milliseconds..6.milliseconds)
}
class DelayRamModificationTests: ModificationTests() {
override val database: Database = InMemoryDatabase().delayed(5L)
override val database: Database = InMemoryDatabase().delayed(5.milliseconds..6.milliseconds)
}
class DelayRamOperationsTests: OperationsTests() {
override val database: Database = InMemoryDatabase().delayed(5L)
override val database: Database = InMemoryDatabase().delayed(5.milliseconds..6.milliseconds)
}
class DelayRamSortTest: SortTest() {
override val database: Database = InMemoryDatabase().delayed(5L)
override val database: Database = InMemoryDatabase().delayed(5.milliseconds..6.milliseconds)
}
class DelayRamMetaTest: MetaTest() {
override val database: Database = InMemoryDatabase().delayed(5L)
override val database: Database = InMemoryDatabase().delayed(5.milliseconds..6.milliseconds)
}

class LocalCacheTest: CacheTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class JsonSchemaTest {
val schema = Serialization.jsonWithoutDefaults.schema(HasServerFiles.serializer())
.also(::println)
.also { println(Json(Serialization.jsonWithoutDefaults) { prettyPrint = true }.encodeToString(it)) }
assertEquals("#/definitions/com.lightningkite.lightningserver.files.ServerFile", schema.definitions["com.lightningkite.lightningdb.test.HasServerFiles"]!!.properties!!["file"]!!.ref)
assertEquals("#/definitions/com.lightningkite.lightningserver.files.ServerFile/external", schema.definitions["com.lightningkite.lightningdb.test.HasServerFiles"]!!.properties!!["file"]!!.ref)
}
}
}
8 changes: 5 additions & 3 deletions shared/src/commonMain/kotlin/com/lightningkite/StringAlts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ interface IsRawString : Comparable<IsRawString> {

companion object {
val serialNames = setOf(
"TrimmedString",
"CaselessString",
"TrimmedCaselessString",
"com.lightningkite.TrimmedString",
"com.lightningkite.CaselessString",
"com.lightningkite.TrimmedCaselessString",
"com.lightningkite.EmailAddress",
"com.lightningkite.PhoneNumber",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ConditionSerializer<T>(val inner: KSerializer<T>): MySealedClassSerializer
MySealedClassSerializer<Condition<T>>("com.lightningkite.lightningdb.Condition", {
val r = when {
inner.descriptor.isNullable -> nullableOptions(inner.innerElement() as KSerializer<Any>)
inner.descriptor.serialName == "kotlin.String" -> stringOptions
inner.descriptor.serialName.substringBefore('/') == "kotlin.String" -> stringOptions
IsRawString.Companion.serialNames.contains(inner.descriptor.serialName) -> rawStringOptions(inner as KSerializer<IsRawString>)
inner.descriptor.serialName == "kotlin.Int" -> intOptions
inner.descriptor.serialName == "com.lightningkite.GeoCoordinate" -> geocoordinateOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ data class ModificationSerializer<T>(val inner: KSerializer<T>) :
MySealedClassSerializer<Modification<T>>("com.lightningkite.lightningdb.Modification", {
val r = when {
inner.nullElement() != null -> nullableOptions(inner.nullElement()!! as KSerializer<Any>)
inner.descriptor.serialName == "kotlin.String" -> stringOptions
inner.descriptor.serialName.substringBefore('/') == "kotlin.String" -> stringOptions
inner.descriptor.serialName in numlist -> numberOptions(inner as KSerializer<Int>)
IsRawString.Companion.serialNames.contains(inner.descriptor.serialName) -> rawStringOptions(inner as KSerializer<IsRawString>)
inner.descriptor.kind == StructureKind.MAP -> stringMapOptions(inner.mapValueElement()!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ object DefaultDecoder : Decoder {
defaults[InstantIso8601Serializer.descriptor.serialName] = Instant.fromEpochMilliseconds(0)
defaults[LocalTimeIso8601Serializer.descriptor.serialName] = LocalTime(0, 0, 0)
defaults[LocalDateIso8601Serializer.descriptor.serialName] = LocalDate(1970, 1, 1)
defaults[LocalDateTimeIso8601Serializer.descriptor.serialName] =
LocalDateTime(LocalDate(1970, 1, 1), LocalTime(0, 0, 0))
defaults[ZonedDateTimeIso8601Serializer.descriptor.serialName] =
ZonedDateTime(LocalDateTime(LocalDate(1970, 1, 1), LocalTime(0, 0, 0)), TimeZone.UTC)
defaults[OffsetDateTimeIso8601Serializer.descriptor.serialName] =
OffsetDateTime(LocalDateTime(LocalDate(1970, 1, 1), LocalTime(0, 0, 0)), UtcOffset.ZERO)
defaults[LocalDateTimeIso8601Serializer.descriptor.serialName] = LocalDateTime(LocalDate(1970, 1, 1), LocalTime(0, 0, 0))
defaults[kotlinx.datetime.serializers.InstantIso8601Serializer.descriptor.serialName] = Instant.fromEpochMilliseconds(0)
defaults[kotlinx.datetime.serializers.LocalTimeIso8601Serializer.descriptor.serialName] = LocalTime(0, 0, 0)
defaults[kotlinx.datetime.serializers.LocalDateIso8601Serializer.descriptor.serialName] = LocalDate(1970, 1, 1)
defaults[kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer.descriptor.serialName] = LocalDateTime(LocalDate(1970, 1, 1), LocalTime(0, 0, 0))
defaults[ZonedDateTimeIso8601Serializer.descriptor.serialName] = ZonedDateTime(LocalDateTime(LocalDate(1970, 1, 1), LocalTime(0, 0, 0)), TimeZone.UTC)
defaults[OffsetDateTimeIso8601Serializer.descriptor.serialName] = OffsetDateTime(LocalDateTime(LocalDate(1970, 1, 1), LocalTime(0, 0, 0)), UtcOffset.ZERO)
}

override var serializersModule: SerializersModule = ClientModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ fun <K> DataClassPathPartial<K>.setMap(key: K, out: Partial<K>) {
run {
@Suppress("UNCHECKED_CAST")
val prop = properties.last() as SerializableProperty<Any?, *>
prop.serializer.serializableProperties?.let { props ->
val unwrapped = prop.serializer.nullElement() ?: prop.serializer
unwrapped.serializableProperties?.let { props ->
current.parts[properties.last() as SerializableProperty<Any?, *>] = getAny(key)?.let {
@Suppress("UNCHECKED_CAST")
Partial<Any>(it, props.map { DataClassPathAccess<Any, Any, Any?>(DataClassPathSelf(prop.serializer as KSerializer<Any>), it as SerializableProperty<Any, Any?>) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class HelpersKtTest {
)
}
@Test fun requireTermsPresent() {
assertTrue(TextQuery(loose = setOf("one", "two")).fuzzyPresent("one two three"))
assertTrue(TextQuery(loose = setOf("one", "twoo")).fuzzyPresent("one two three"))
assertTrue(TextQuery(loose = setOf("one", "too")).fuzzyPresent("one two three"))
assertTrue(TextQuery(loose = setOf("ONE", "TOO")).fuzzyPresent("one two three"))
assertFalse(TextQuery(loose = setOf("one", "four")).fuzzyPresent("one two three"))
assertFalse(TextQuery(loose = setOf("one", "fsd")).fuzzyPresent("one two three"))
assertTrue(TextQuery(loose = setOf("four", "three")).fuzzyPresent("one two three four"))
assertTrue(TextQuery(loose = setOf("four", "threee")).fuzzyPresent("one two three four"))
assertTrue(TextQuery(loose = setOf("four", "thre")).fuzzyPresent("one two three four"))
assertTrue(TextQuery(loose = setOf("FOUR", "THRE")).fuzzyPresent("one two three four"))
assertFalse(TextQuery(loose = setOf("four", "five")).fuzzyPresent("one two three four"))
assertFalse(TextQuery(loose = setOf("four", "fsd")).fuzzyPresent("one two three four"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ class SerializationTest {
assertEquals(part, restored)
println(restored)
}
@Test fun partial6() {
val serializer = PartialSerializer(LargeTestModel.serializer())
val part = Partial(LargeTestModel(embeddedNullable = ClassUsedForEmbedding()), setOf(path<LargeTestModel>().embeddedNullable))
val asText = myJson.encodeToString(serializer, part)
println(asText)
val restored = myJson.decodeFromString(serializer, asText)
assertEquals(part, restored)
println(restored)
}
@Test fun partial7() {
val serializer = PartialSerializer(LargeTestModel.serializer())
val part = Partial(LargeTestModel(), setOf(path<LargeTestModel>().embeddedNullable))
val asText = myJson.encodeToString(serializer, part)
println(asText)
val restored = myJson.decodeFromString(serializer, asText)
assertEquals(part, restored)
println(restored)
}
@Test fun partial5() {
val serializer = PartialSerializer(LargeTestModel.serializer())
val part = Partial(LargeTestModel(), setOf(path<LargeTestModel>().embedded))
Expand Down

0 comments on commit 83592b7

Please sign in to comment.