Skip to content

Commit

Permalink
Add unit tests for serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
uniumuniu committed Jan 14, 2024
1 parent 2229f2d commit c576116
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 31 deletions.
3 changes: 0 additions & 3 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Feature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ internal fun FeaturevisorInstance.getMatchedTraffic(
): Traffic? {

return traffic.firstOrNull { trafficItem ->
// logger?.debug("trafficItem.segments: ${trafficItem.segments}")
allGroupSegmentsAreMatched(trafficItem.segments, context, datafileReader)
}
}
Expand Down Expand Up @@ -71,11 +70,9 @@ internal fun FeaturevisorInstance.getMatchedTrafficAndAllocation(
var matchedAllocation: Allocation? = null
val matchedTraffic = traffic.firstOrNull { trafficItem ->
if (allGroupSegmentsAreMatched(trafficItem.segments, context, datafileReader)) {
// logger?.debug("getMatchedTrafficAndAllocation, traffic: $trafficItem, allGroupSegmentsAreMatched")
matchedAllocation = getMatchedAllocation(trafficItem, bucketValue)
true
} else {
// logger?.debug("getMatchedTrafficAndAllocation, traffic: $trafficItem, allGroupSegmentsAreMatched.not()")
false
}
}
Expand Down
66 changes: 38 additions & 28 deletions src/test/kotlin/com/featurevisor/sdk/InstanceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,41 @@
*/
package com.featurevisor.sdk

//TODO: Add unit tests for Instance.kt later
//class InstanceTest {
//
// private val systemUnderTest = FeaturevisorInstance.createInstance(
// options = InstanceOptions(
// bucketKeySeparator = "",
// configureBucketKey = { feature: Feature, map: Map, s: String -> },
// configureBucketValue = { feature: Feature, map: Map, i: Int -> },
// datafile = null,
// datafileUrl = null,
// handleDatafileFetch = {},
// initialFeatures = mapOf(),
// interceptContext = {},
// logger = null,
// onActivation = {},
// onReady = {},
// onRefresh = {},
// onUpdate = {},
// refreshInterval = null,
// stickyFeatures = mapOf()
// )
// )
//
// @Test
// fun `instance initialised properly`() {
//
// }
//}
import com.featurevisor.types.DatafileContent
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

class InstanceTest {

private val systemUnderTest = FeaturevisorInstance.createInstance(
options = InstanceOptions(
bucketKeySeparator = "",
configureBucketKey = null,
configureBucketValue = null,
datafile = DatafileContent(
schemaVersion = "0",
revision = "0",
attributes = listOf(),
segments = listOf(),
features = listOf()
),
datafileUrl = null,
handleDatafileFetch = null,
initialFeatures = mapOf(),
interceptContext = null,
logger = null,
onActivation = {},
onReady = {},
onRefresh = {},
onUpdate = {},
refreshInterval = null,
stickyFeatures = mapOf(),
onError = {},
)
)

@Test
fun `instance initialised properly`() {
systemUnderTest.statuses.ready shouldBe true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.featurevisor.sdk.serializers

import com.featurevisor.types.Condition
import com.featurevisor.types.ConditionValue
import com.featurevisor.types.Operator
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeTypeOf
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Test

class ConditionSerializerTest {

@Test
fun `decode PLAIN condition`() {
val element = """
{
"attribute": "version",
"operator": "equals",
"value": "1.2.3"
}
""".trimIndent()

val condition = Json.decodeFromString<Condition>(element)

condition.shouldBeTypeOf<Condition.Plain>()
condition.attributeKey shouldBe "version"
condition.operator shouldBe Operator.EQUALS
condition.value shouldBe ConditionValue.StringValue("1.2.3")
}

@Test
fun `decode AND condition`() {
val element = """
{
"and": [{
"attribute": "version",
"operator": "equals",
"value": "1.2.3"
}, {
"attribute": "age",
"operator": "greaterThanOrEqual",
"value": "18"
}]
}
""".trimIndent()

val condition = Json.decodeFromString<Condition>(element)

condition.shouldBeTypeOf<Condition.And>()
(condition.and[0] as Condition.Plain).run {
attributeKey shouldBe "version"
operator shouldBe Operator.EQUALS
value shouldBe ConditionValue.StringValue("1.2.3")
}
(condition.and[1] as Condition.Plain).run {
attributeKey shouldBe "age"
operator shouldBe Operator.GREATER_THAN_OR_EQUAL
value shouldBe ConditionValue.IntValue(18)
}
}

@Test
fun `decode OR condition`() {
val element = """
{
"or": [{
"attribute": "version",
"operator": "equals",
"value": "1.2.3"
}, {
"attribute": "age",
"operator": "greaterThanOrEqual",
"value": "18"
}]
}
""".trimIndent()

val condition = Json.decodeFromString<Condition>(element)

condition.shouldBeTypeOf<Condition.Or>()
(condition.or[0] as Condition.Plain).run {
attributeKey shouldBe "version"
operator shouldBe Operator.EQUALS
value shouldBe ConditionValue.StringValue("1.2.3")
}
(condition.or[1] as Condition.Plain).run {
attributeKey shouldBe "age"
operator shouldBe Operator.GREATER_THAN_OR_EQUAL
value shouldBe ConditionValue.IntValue(18)
}
}

@Test
fun `decode NOT condition`() {
val element = """
{
"not": [{
"attribute": "version",
"operator": "equals",
"value": "1.2.3"
}]
}
""".trimIndent()

val condition = Json.decodeFromString<Condition>(element)

condition.shouldBeTypeOf<Condition.Not>()
(condition.not[0] as Condition.Plain).run {
attributeKey shouldBe "version"
operator shouldBe Operator.EQUALS
value shouldBe ConditionValue.StringValue("1.2.3")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.featurevisor.sdk.serializers

import com.featurevisor.types.ConditionValue
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeTypeOf
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Test
import kotlinx.serialization.decodeFromString

class ConditionValueTest {

@Test
fun `decode int value with correct type`() {
val element = """
1
""".trimIndent()

val result = Json.decodeFromString<ConditionValue>(element)

result.shouldBeTypeOf<ConditionValue.IntValue>()
result.value shouldBe 1
}

@Test
fun `decode boolean value with correct type`() {
val element = """
true
""".trimIndent()

val result = Json.decodeFromString<ConditionValue>(element)

result.shouldBeTypeOf<ConditionValue.BooleanValue>()
result.value shouldBe true
}

@Test
fun `decode double value with correct type`() {
val element = """
1.2
""".trimIndent()

val result = Json.decodeFromString<ConditionValue>(element)

result.shouldBeTypeOf<ConditionValue.DoubleValue>()
result.value shouldBe 1.2
}

@Test
fun `decode string value with correct type`() {
val element = """
test
""".trimIndent()

val result = Json.decodeFromString<ConditionValue>(element)

result.shouldBeTypeOf<ConditionValue.StringValue>()
result.value shouldBe "test"
}

@Test
fun `decode array value with correct type`() {
val element = """
[ "test1", "test2"]
""".trimIndent()

val result = Json.decodeFromString<ConditionValue>(element)

result.shouldBeTypeOf<ConditionValue.ArrayValue>()
result.values[0] shouldBe "test1"
result.values[1] shouldBe "test2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.featurevisor.sdk.serializers

import com.featurevisor.types.GroupSegment
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeTypeOf
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Test

class GroupSegmentTest {

@Test
fun `decode PLAIN segment`() {
val element = """
testSegment
""".trimIndent()

val groupSegment = Json.decodeFromString<GroupSegment>(element)

groupSegment.shouldBeTypeOf<GroupSegment.Plain>()
groupSegment.segment shouldBe "testSegment"
}

@Test
fun `decode AND group segment`() {
val element = """
{
"and": ["testSegment1", "testSegment2"]
}
""".trimIndent()

val groupSegment = Json.decodeFromString<GroupSegment>(element)

groupSegment.shouldBeTypeOf<GroupSegment.And>()
groupSegment.segment.and[0] shouldBe GroupSegment.Plain("testSegment1")
groupSegment.segment.and[1] shouldBe GroupSegment.Plain("testSegment2")
}

@Test
fun `decode OR group segment`() {
val element = """
{
"or": ["testSegment1", "testSegment2"]
}
""".trimIndent()

val groupSegment = Json.decodeFromString<GroupSegment>(element)

groupSegment.shouldBeTypeOf<GroupSegment.Or>()
groupSegment.segment.or[0] shouldBe GroupSegment.Plain("testSegment1")
groupSegment.segment.or[1] shouldBe GroupSegment.Plain("testSegment2")
}

@Test
fun `decode NOT group segment`() {
val element = """
{
"not": ["testSegment1", "testSegment2"]
}
""".trimIndent()

val groupSegment = Json.decodeFromString<GroupSegment>(element)

groupSegment.shouldBeTypeOf<GroupSegment.Not>()
groupSegment.segment.not[0] shouldBe GroupSegment.Plain("testSegment1")
groupSegment.segment.not[1] shouldBe GroupSegment.Plain("testSegment2")
}
}

0 comments on commit c576116

Please sign in to comment.