Skip to content

Commit

Permalink
fix: lint, test
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed Jul 23, 2024
1 parent 2c18a19 commit 7aabc6f
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 61 deletions.
23 changes: 23 additions & 0 deletions evaluation-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,30 @@ kotlin {
dependencies {
implementation(kotlin("test"))
implementation("io.ktor:ktor-client-core:$ktorVersion")
}
}
val jvmTest by getting {
dependencies {
implementation("io.ktor:ktor-client-cio:$ktorVersion")
}
}
val macosArm64Test by getting {
dependencies {
implementation("io.ktor:ktor-client-curl:$ktorVersion")
}
}
val macosX64Test by getting {
dependencies {
implementation("io.ktor:ktor-client-curl:$ktorVersion")
}
}
val linuxArm64Test by getting {
dependencies {
implementation("io.ktor:ktor-client-curl:$ktorVersion")
}
}
val linuxX64Test by getting {
dependencies {
implementation("io.ktor:ktor-client-curl:$ktorVersion")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ data class EvaluationAllocation(
val range: List<Int>,

// The distribution of variants if allocated.
val distributions: List<EvaluationDistribution>,
val distributions: List<EvaluationDistribution>
)
2 changes: 1 addition & 1 deletion evaluation-core/src/commonMain/kotlin/EvaluationBucket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ data class EvaluationBucket(

// Determines which variant, if any, should be returned based on the
// result of the hash functions.
val allocations: List<EvaluationAllocation>,
val allocations: List<EvaluationAllocation>
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ data class EvaluationDistribution(

// The distribution range [start, end), where the max value is 42949672.
// E.g. [0, 42949673] = [0%, 100%]
val range: List<Int>,
val range: List<Int>
)
2 changes: 1 addition & 1 deletion evaluation-core/src/commonMain/kotlin/EvaluationEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class EvaluationEngineImpl(private val log: Logger? = null) : EvaluationEngine {
propValue: String,
op: String,
filterValues: Set<String>,
transformer: (String) -> T?,
transformer: (String) -> T?
): Boolean {
val propValueTransformed: T? = transformer.invoke(propValue)
val filterValuesTransformed: Set<T> = filterValues.mapNotNull(transformer).toSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.doubleOrNull
import kotlinx.serialization.json.intOrNull
import kotlinx.serialization.json.longOrNull
import kotlin.jvm.JvmField
import kotlin.jvm.JvmSynthetic

internal val json = Json {
ignoreUnknownKeys = true
Expand All @@ -40,7 +38,7 @@ internal fun Collection<*>.toJsonArray(): JsonArray = JsonArray(map { it.toJsonE
internal fun Map<*, *>.toJsonObject(): JsonObject = JsonObject(
mapNotNull {
(it.key as? String ?: return@mapNotNull null) to it.value.toJsonElement()
}.toMap(),
}.toMap()
)

internal fun JsonElement.toAny(): Any? {
Expand Down
2 changes: 1 addition & 1 deletion evaluation-core/src/commonMain/kotlin/EvaluationVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class EvaluationVariant(
val key: String,
val value: Any? = null,
val payload: Any? = null,
val metadata: Map<String, Any?>? = null,
val metadata: Map<String, Any?>? = null
) : Selectable {
override fun select(selector: String): Any? {
return when (selector) {
Expand Down
2 changes: 1 addition & 1 deletion evaluation-core/src/commonMain/kotlin/TopologicalSort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun topologicalSort(
private fun parentTraversal(
flagKey: String,
available: MutableMap<String, EvaluationFlag>,
path: MutableSet<String> = mutableSetOf(),
path: MutableSet<String> = mutableSetOf()
): List<EvaluationFlag>? {
val flag = available[flagKey] ?: return null
if (flag.dependencies.isNullOrEmpty()) {
Expand Down
53 changes: 30 additions & 23 deletions evaluation-core/src/commonTest/kotlin/EvaluationIntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import com.amplitude.experiment.evaluation.util.FlagApi
import kotlinx.coroutines.runBlocking
import kotlin.test.DefaultAsserter
import kotlin.test.Test
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.measureTime

private const val DEPLOYMENT_KEY = "server-NgJxxvg8OGwwBsWVXqyxQbdiflbhvugy"

Expand Down Expand Up @@ -123,7 +120,7 @@ class EvaluationIntegrationTest {
userId = "user_id",
deviceId = "device_id",
userProperties = mapOf(
"[Experiment] test-sticky-bucketing" to "on",
"[Experiment] test-sticky-bucketing" to "on"
)
)
var result = engine.evaluate(user, flags)["test-sticky-bucketing"]
Expand All @@ -142,7 +139,7 @@ class EvaluationIntegrationTest {
userId = "user_id",
deviceId = "device_id",
userProperties = mapOf(
"[Experiment] test-sticky-bucketing" to "off",
"[Experiment] test-sticky-bucketing" to "off"
)
)
result = engine.evaluate(user, flags)["test-sticky-bucketing"]
Expand All @@ -161,7 +158,7 @@ class EvaluationIntegrationTest {
userId = "user_id",
deviceId = "device_id",
userProperties = mapOf(
"[Experiment] test-sticky-bucketing" to "not-a-variant",
"[Experiment] test-sticky-bucketing" to "not-a-variant"
)
)
result = engine.evaluate(user, flags)["test-sticky-bucketing"]
Expand Down Expand Up @@ -215,7 +212,7 @@ class EvaluationIntegrationTest {
userProperties = mapOf(
"key-1" to "value-1",
"key-2" to "value-2",
"key-3" to "value-3",
"key-3" to "value-3"
)
)
var result = engine.evaluate(user, flags)["test-multiple-conditions-and-values"]
Expand All @@ -228,7 +225,7 @@ class EvaluationIntegrationTest {
user = userContext(
userProperties = mapOf(
"key-1" to "value-1",
"key-2" to "value-2",
"key-2" to "value-2"
)
)
result = engine.evaluate(user, flags)["test-multiple-conditions-and-values"]
Expand Down Expand Up @@ -656,9 +653,11 @@ class EvaluationIntegrationTest {

@Test
fun `test version less`() {
val user = freeformUserContext(mapOf(
"version" to "1.9.0"
))
val user = freeformUserContext(
mapOf(
"version" to "1.9.0"
)
)

val result = engine.evaluate(user, flags.filter { it.key == "test-version-less" })["test-version-less"]
DefaultAsserter.assertEquals(
Expand All @@ -670,9 +669,11 @@ class EvaluationIntegrationTest {

@Test
fun `test version less or equal`() {
val user = freeformUserContext(mapOf(
"version" to "1.10.0"
))
val user = freeformUserContext(
mapOf(
"version" to "1.10.0"
)
)
val result = engine.evaluate(user, flags)["test-version-less-or-equal"]
DefaultAsserter.assertEquals(
"Unexpected evaluation result",
Expand All @@ -683,9 +684,11 @@ class EvaluationIntegrationTest {

@Test
fun `test version greater`() {
val user = freeformUserContext(mapOf(
"version" to "1.10.0"
))
val user = freeformUserContext(
mapOf(
"version" to "1.10.0"
)
)
val result = engine.evaluate(user, flags)["test-version-greater"]
DefaultAsserter.assertEquals(
"Unexpected evaluation result",
Expand All @@ -696,9 +699,11 @@ class EvaluationIntegrationTest {

@Test
fun `test version greater or equal`() {
val user = freeformUserContext(mapOf(
"version" to "1.9.0"
))
val user = freeformUserContext(
mapOf(
"version" to "1.9.0"
)
)
val result = engine.evaluate(user, flags)["test-version-greater-or-equal"]
DefaultAsserter.assertEquals(
"Unexpected evaluation result",
Expand Down Expand Up @@ -864,9 +869,11 @@ class EvaluationIntegrationTest {

@Test
fun `test version compare falls back on string comparison`() {
val user = freeformUserContext(mapOf(
"version" to "1.10."
))
val user = freeformUserContext(
mapOf(
"version" to "1.10."
)
)

val result = engine.evaluate(user, flags.filter { it.key == "test-version-less" })["test-version-less"]
DefaultAsserter.assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class EvaluationSerializationTest {
)

private val primitiveTypeArray = listOf<Any?>(
null, "value", 13, 13.12, true
null,
"value",
13,
13.12,
true
)

private val nestedJsonObject = primitiveTypeObject.toMutableMap().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import kotlin.test.assertEquals
*/
private const val MURMUR_SEED = 0x7f3a21ea

class Murmur3Test {
class Murmur3EnglishTest {

private val englishWords = ENGLISH_WORDS.trim()
private val hash3X8632 = HASH3_X86_32.trim()

@Test
fun testMurMur3HashSimple() {
val input = "brian".toByteArray()
val result = Murmur3.hash32x86(input, input.size, MURMUR_SEED).toLong()
val result = Murmur3.hash32x86(input, input.size, MURMUR_SEED).toLong() and 0xffffffff
val expected = (3948467465 and 0xffffffff)
assertEquals(3948467465, expected)
assertEquals(result, expected)
}

@Test
Expand Down
7 changes: 5 additions & 2 deletions evaluation-core/src/commonTest/kotlin/SelectableTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class SelectableTest {
)

private val primitiveArray = listOf<Any?>(
null, "value", 13, 13.12, true
null,
"value",
13,
13.12,
true
)

private val nestedArray = primitiveArray.toMutableList().apply {
Expand All @@ -30,7 +34,6 @@ class SelectableTest {

@Test
fun testSelectableEvaluationContextTypes() {

val contextMap = primitiveObject.toMutableMap().apply {
put("array", nestedArray)
put("object", nestedObject)
Expand Down
Loading

0 comments on commit 7aabc6f

Please sign in to comment.