Skip to content

Commit

Permalink
Add unit tests for new client side dedupe
Browse files Browse the repository at this point in the history
Summary: In this diff, we add new unit tests to support the refactored client side dedupe.

Reviewed By: jjiang10

Differential Revision:
D66390847

Privacy Context Container: L1285343

fbshipit-source-id: 5314b29b49c9cd0d3add606bc00ed44c1dacad6e
  • Loading branch information
maxalbrightmeta authored and facebook-github-bot committed Jan 2, 2025
1 parent 6b5ef53 commit 9a05289
Showing 1 changed file with 134 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {
}

@Test
fun testCacheDeDupPurchaseOnFirstTimeLoggingWithNewIAPImplementation() {
fun testCacheDeDupPurchaseOnFirstTimeLogging() {
// Construct purchase details map
val mockPurchaseDetailsMap: MutableMap<String, JSONObject> = mutableMapOf()
val purchaseDetailJson1 =
Expand All @@ -129,10 +129,13 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1730358000000L)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1730358000000L)

}

@Test
fun testCacheDeDupPurchaseOnFirstTimeLoggingWithNewIAPImplementationAndNewPurchase() {
fun testCacheDeDupPurchaseOnFirstTimeLoggingAndNewPurchase() {
// Construct purchase details map
val mockPurchaseDetailsMap: MutableMap<String, JSONObject> = mutableMapOf()
val purchaseDetailJson1 =
Expand All @@ -159,10 +162,12 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1730358000001)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1730358000000)
}

@Test
fun testCacheDeDupPurchaseOnFirstTimeLoggingWithNewIAPImplementationAndInvalidCacheHistory() {
fun testCacheDeDupPurchaseOnFirstTimeLoggingAndInvalidCacheHistory() {
// Construct purchase details map
val mockPurchaseDetailsMap: MutableMap<String, JSONObject> = mutableMapOf()
val purchaseDetailJson1 =
Expand All @@ -189,14 +194,13 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1730358000001)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1730358000000)
}

@Test
fun testCacheDeDupPurchaseNotFirstTimeLoggingWithNewIAPImplementation() {

whenever(mockNewCachePrefs.getLong(eq(TIME_OF_LAST_LOGGED_PURCHASE_KEY), any())).thenReturn(
1630000000000
)
fun testCacheDeDupPurchaseNotFirstTimeLogging() {
mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY] = 1630000000000

// Construct purchase details map
val mockPurchaseDetailsMap: MutableMap<String, JSONObject> = mutableMapOf()
Expand All @@ -206,19 +210,22 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {
)
mockPurchaseDetailsMap["espresso"] = purchaseDetailJson1


InAppPurchaseLoggerManager.migrateOldCacheHistory()
// Test duplicate purchase event can be successfully removed from purchase details map
val cachedMap = InAppPurchaseLoggerManager.cacheDeDupPurchase(
mockPurchaseDetailsMap,
false,
)
Assertions.assertThat(cachedMap).isEmpty()

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY]).isNull()
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1730358000000)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1730358000000)
}

@Test
fun testCacheDeDupPurchaseNotFirstTimeLoggingWithNewIAPImplementationAndNewPurchase() {
fun testCacheDeDupPurchaseNotFirstTimeLoggingAndNewPurchase() {

mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY] = 1620000000000

Expand All @@ -230,7 +237,7 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {
)
mockPurchaseDetailsMap["espresso"] = purchaseDetailJson1


InAppPurchaseLoggerManager.migrateOldCacheHistory()
// Test duplicate purchase event can be successfully removed from purchase details map
val cachedMap = InAppPurchaseLoggerManager.cacheDeDupPurchase(
mockPurchaseDetailsMap,
Expand All @@ -240,10 +247,12 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1630000000000000)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1730358000000)
}

@Test
fun testCacheDeDupSubscriptionOnFirstTimeLoggingWithNewIAPImplementation() {
fun testCacheDeDupSubscriptionOnFirstTimeLogging() {
mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY] = 0
// Construct purchase details map
val mockPurchaseDetailsMap: MutableMap<String, JSONObject> = mutableMapOf()
Expand Down Expand Up @@ -272,10 +281,12 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1730358000000)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1730358000000)
}

@Test
fun testCacheDeDupSubscriptionOnFirstTimeLoggingWithNewIAPImplementationAndNewPurchase() {
fun testCacheDeDupSubscriptionOnFirstTimeLoggingAndNewPurchase() {
mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY] = 0
// Construct purchase details map
val mockPurchaseDetailsMap: MutableMap<String, JSONObject> = mutableMapOf()
Expand All @@ -302,10 +313,109 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1730358000000)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1730358000001)
}

@Test
fun testCacheDeDupSubscriptionOnFirstTimeLoggingAndNewerPurchasesInOldCache() {
mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY] = 0
// Construct purchase details map
val mockPurchaseDetailsMap: MutableMap<String, JSONObject> = mutableMapOf()
val purchaseDetailJson1 =
JSONObject(
"{\"productId\":\"espresso\",\"purchaseToken\":\"token123\",\"purchaseTime\":1730358000001,\"developerPayload\":null,\"packageName\":\"sample.packagename\"}"
)
mockPurchaseDetailsMap["espresso"] = purchaseDetailJson1

// Construct cached purchase map
val lastClearedTime = 1_800_000_000L
val cachedPurchaseMap =
mutableMapOf(
"otherpurchasetoken" to
lastClearedTime
)
putMapInOldCache(cachedPurchaseMap)
InAppPurchaseLoggerManager.migrateOldCacheHistory()
val cachedMap = InAppPurchaseLoggerManager.cacheDeDupPurchase(
mockPurchaseDetailsMap,
true,
)
Assertions.assertThat(cachedMap).isEmpty()

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1800000000000)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1800000000000)
}

@Test
fun testCacheDeDupSubscriptionNotFirstTimeLoggingWithNewIAPImplementation() {
fun testCacheDeDupSubscriptionOnFirstTimeLoggingAndNewPurchasesInOldCache() {
mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY] = 0
// Construct purchase details map
val mockPurchaseDetailsMap: MutableMap<String, JSONObject> = mutableMapOf()
val purchaseDetailJson1 =
JSONObject(
"{\"productId\":\"espresso\",\"purchaseToken\":\"token123\",\"purchaseTime\":1835113600001,\"developerPayload\":null,\"packageName\":\"sample.packagename\"}"
)
mockPurchaseDetailsMap["espresso"] = purchaseDetailJson1

// Construct cached purchase map
val lastClearedTime = 1_800_000_000L
val cachedPurchaseMap =
mutableMapOf(
"otherpurchasetoken" to
lastClearedTime
)
putMapInOldCache(cachedPurchaseMap)
InAppPurchaseLoggerManager.migrateOldCacheHistory()
val cachedMap = InAppPurchaseLoggerManager.cacheDeDupPurchase(
mockPurchaseDetailsMap,
true,
)
Assertions.assertThat(cachedMap).isNotEmpty()

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1835113600001)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1800000000000)
}

@Test
fun testCacheDeDupSubscriptionOnFirstTimeLoggingAndInvalidValueInOldCache() {
mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY] = 0
// Construct purchase details map
val mockPurchaseDetailsMap: MutableMap<String, JSONObject> = mutableMapOf()
val purchaseDetailJson1 =
JSONObject(
"{\"productId\":\"espresso\",\"purchaseToken\":\"token123\",\"purchaseTime\":1730358000001,\"developerPayload\":null,\"packageName\":\"sample.packagename\"}"
)
mockPurchaseDetailsMap["espresso"] = purchaseDetailJson1

// Construct cached purchase map
val lastClearedTime = 1_800L
val cachedPurchaseMap =
mutableMapOf(
"otherpurchasetoken" to
lastClearedTime
)
putMapInOldCache(cachedPurchaseMap)
InAppPurchaseLoggerManager.migrateOldCacheHistory()
val cachedMap = InAppPurchaseLoggerManager.cacheDeDupPurchase(
mockPurchaseDetailsMap,
true,
)
Assertions.assertThat(cachedMap).isNotEmpty()

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1730358000000)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1730358000001)

}

@Test
fun testCacheDeDupSubscriptionNotFirstTimeLogging() {
mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY] = 1620000000002

// Construct purchase details map
Expand All @@ -316,19 +426,22 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {
)
mockPurchaseDetailsMap["espresso"] = purchaseDetailJson1


InAppPurchaseLoggerManager.migrateOldCacheHistory()
// Test duplicate purchase event can be successfully removed from purchase details map
val cachedMap = InAppPurchaseLoggerManager.cacheDeDupPurchase(
mockPurchaseDetailsMap,
true,
)
Assertions.assertThat(cachedMap).isEmpty()

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY]).isNull()
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1730358000000)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1730358000000)
}

@Test
fun testCacheDeDupSubscriptionNotFirstTimeLoggingWithNewIAPImplementationAndNewPurchase() {
fun testCacheDeDupSubscriptionNotFirstTimeLoggingAndNewPurchase() {

mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY] = 1620000000000

Expand All @@ -340,7 +453,7 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {
)
mockPurchaseDetailsMap["espresso"] = purchaseDetailJson1


InAppPurchaseLoggerManager.migrateOldCacheHistory()
// Test duplicate purchase event can be successfully removed from purchase details map
val cachedMap = InAppPurchaseLoggerManager.cacheDeDupPurchase(
mockPurchaseDetailsMap,
Expand All @@ -350,6 +463,8 @@ class InAppPurchaseLoggerManagerTest : FacebookPowerMockTestCase() {

Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_SUBSCRIPTION_KEY])
.isEqualTo(1620000000000001)
Assertions.assertThat(mockNewCachedMap[TIME_OF_LAST_LOGGED_PURCHASE_KEY])
.isEqualTo(1730358000000)
}


Expand Down

0 comments on commit 9a05289

Please sign in to comment.