Skip to content

Commit

Permalink
update Variant.toJson, exposureInternal and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Yiu authored and Tim Yiu committed Sep 7, 2023
1 parent c8a8217 commit 7b425d0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ internal class DefaultExperimentClient internal constructor(
val exposedUser = getUserMergedWithProvider()
val event = OldExposureEvent(exposedUser, key, variant, source)
// Track the exposure event if an analytics provider is set
if (source.isFallback() || variant.value == null) {
if (source.isFallback() || variant.key == null) {
userSessionExposureTracker?.track(Exposure(key, null, variant.expKey), exposedUser)
analyticsProvider?.unsetUserProperty(event)
} else {
userSessionExposureTracker?.track(Exposure(key, variant.value, variant.expKey), exposedUser)
userSessionExposureTracker?.track(Exposure(key, variant.key, variant.expKey), exposedUser)
analyticsProvider?.setUserProperty(event)
analyticsProvider?.track(event)
}
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/main/java/com/amplitude/experiment/util/Variant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import org.json.JSONObject
internal fun Variant.toJson(): String {
val jsonObject = JSONObject()
try {
jsonObject.put("value", value)
jsonObject.put("key", key)
if (value != null) {
jsonObject.put("value", value)
}
if (payload != null) {
jsonObject.put("payload", payload)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ class ExperimentClientTest {
debug = true,
exposureTrackingProvider = exposureTrackingProvider,
source = Source.INITIAL_VARIANTS,
initialVariants = mapOf("flagKey" to Variant("variant", null, "experimentKey"))
initialVariants = mapOf("flagKey" to Variant("variant", null, null, "experimentKey"))
),
OkHttpClient(),
InMemoryStorage(),
Expand Down
17 changes: 5 additions & 12 deletions sdk/src/test/java/com/amplitude/experiment/VariantTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,24 @@ class VariantTest {
@Test
fun `json object to variant`() {
val jsonObject = JSONObject()
jsonObject.put("key", "key")
jsonObject.put("value", "value")
jsonObject.put("payload", "payload")
jsonObject.put("expKey", "expKey")
val variant = jsonObject.toVariant()
Assert.assertNotNull(variant)
Assert.assertEquals("value", variant!!.value)
Assert.assertEquals("key", variant!!.key)
Assert.assertEquals("value", variant.value)
Assert.assertEquals("payload", variant.payload)
Assert.assertEquals("expKey", variant.expKey)
}

@Test
fun `json object to variant deprecated field`() {
val jsonObject = JSONObject()
jsonObject.put("key", "value")
val variant = jsonObject.toVariant()
Assert.assertNotNull(variant)
Assert.assertEquals("value", variant!!.value)
Assert.assertNull(variant.payload)
}

@Test
fun `variant to json object`() {
run {
val variant = Variant("value", null, "expKey")
val variant = Variant("key","value", null, "expKey")
val jsonObject = JSONObject()
jsonObject.put("key", "key")
jsonObject.put("value", "value")
jsonObject.put("expKey", "expKey")
Assert.assertEquals(jsonObject.toString(), variant.toJson())
Expand Down

0 comments on commit 7b425d0

Please sign in to comment.