Skip to content

Commit a54c5c6

Browse files
authored
Merge pull request #1994 from OneSignal/ci/fix-unit-tests-2024-02-14
CI - Fix failing test 2024-02-14; Includes fixes for RobolectricTest, updating to Android compile API 34, and fixing a variety of tests
2 parents f325142 + 900d76a commit a54c5c6

File tree

39 files changed

+276
-769
lines changed

39 files changed

+276
-769
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: "[Test] SDK Unit Tests"
2727
working-directory: OneSignalSDK
2828
run: |
29-
./gradlew test --console=plain
29+
./gradlew testReleaseUnitTest --console=plain --continue
3030
- name: Unit tests results
3131
if: failure()
3232
uses: actions/upload-artifact@v3

OneSignalSDK/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ buildscript {
44

55
ext {
66
buildVersions = [
7-
compileSdkVersion: 31,
8-
targetSdkVersion: 31,
7+
compileSdkVersion: 34,
8+
targetSdkVersion: 34,
99
minSdkVersion: 19
1010
]
1111
androidGradlePluginVersion = '7.2.0'

OneSignalSDK/onesignal/core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ dependencies {
7878
}
7979
}
8080

81+
testImplementation(project(':OneSignal:testhelpers'))
8182
testImplementation("junit:junit:$junitVersion")
8283
testImplementation("io.kotest:kotest-runner-junit4:$kotestVersion")
8384
testImplementation("io.kotest:kotest-runner-junit4-jvm:$kotestVersion")

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/impl/SubscriptionBackendService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ internal class SubscriptionBackendService(
1717
aliasValue: String,
1818
subscription: SubscriptionObject,
1919
): String? {
20-
val requestJSON =
21-
JSONObject()
22-
.put("subscription", JSONConverter.convertToJSON(subscription))
20+
val jsonSubscription = JSONConverter.convertToJSON(subscription)
21+
jsonSubscription.remove("id")
22+
val requestJSON = JSONObject().put("subscription", jsonSubscription)
2323

2424
val response = _httpClient.post("apps/$appId/users/by/$aliasLabel/$aliasValue/subscriptions", requestJSON)
2525

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/application/ApplicationServiceTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.onesignal.common.threading.suspendifyOnThread
88
import com.onesignal.core.internal.application.impl.ApplicationService
99
import com.onesignal.debug.LogLevel
1010
import com.onesignal.debug.internal.logging.Logging
11-
import com.onesignal.extensions.RobolectricTest
11+
import com.onesignal.testhelpers.extensions.RobolectricTest
1212
import io.kotest.core.spec.style.FunSpec
1313
import io.kotest.matchers.shouldBe
1414
import io.kotest.runner.junit4.KotestTestRunner

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/database/OSDatabaseTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import com.onesignal.core.internal.database.impl.OSDatabase
66
import com.onesignal.core.internal.database.impl.OneSignalDbContract
77
import com.onesignal.debug.LogLevel
88
import com.onesignal.debug.internal.logging.Logging
9-
import com.onesignal.extensions.RobolectricTest
109
import com.onesignal.session.internal.outcomes.impl.OutcomeTableProvider
10+
import com.onesignal.testhelpers.extensions.RobolectricTest
1111
import io.kotest.core.spec.style.FunSpec
1212
import io.kotest.matchers.shouldBe
1313
import io.kotest.runner.junit4.KotestTestRunner

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/preferences/PreferencesServiceTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import androidx.test.core.app.ApplicationProvider
55
import com.onesignal.core.internal.preferences.impl.PreferencesService
66
import com.onesignal.debug.LogLevel
77
import com.onesignal.debug.internal.logging.Logging
8-
import com.onesignal.extensions.RobolectricTest
98
import com.onesignal.mocks.AndroidMockHelper
109
import com.onesignal.mocks.MockHelper
10+
import com.onesignal.testhelpers.extensions.RobolectricTest
1111
import io.kotest.assertions.throwables.shouldThrowUnit
1212
import io.kotest.core.spec.style.FunSpec
1313
import io.kotest.matchers.shouldBe

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/extensions/RobolectricExtension.kt

Lines changed: 0 additions & 89 deletions
This file was deleted.

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/session/internal/outcomes/OutcomeEventsRepositoryTests.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.onesignal.session.internal.outcomes
22

33
import com.onesignal.debug.LogLevel
44
import com.onesignal.debug.internal.logging.Logging
5-
import com.onesignal.extensions.RobolectricTest
65
import com.onesignal.mocks.DatabaseMockHelper
76
import com.onesignal.session.internal.influence.Influence
87
import com.onesignal.session.internal.influence.InfluenceChannel
@@ -13,6 +12,7 @@ import com.onesignal.session.internal.outcomes.impl.OutcomeEventsRepository
1312
import com.onesignal.session.internal.outcomes.impl.OutcomeEventsTable
1413
import com.onesignal.session.internal.outcomes.impl.OutcomeSource
1514
import com.onesignal.session.internal.outcomes.impl.OutcomeSourceBody
15+
import com.onesignal.testhelpers.extensions.RobolectricTest
1616
import io.kotest.core.spec.style.FunSpec
1717
import io.kotest.matchers.shouldBe
1818
import io.kotest.matchers.shouldNotBe
@@ -171,13 +171,15 @@ class OutcomeEventsRepositoryTests : FunSpec({
171171
OutcomeEventsTable.COLUMN_NAME_NAME to "outcomeId1",
172172
OutcomeEventsTable.COLUMN_NAME_WEIGHT to 0.2f,
173173
OutcomeEventsTable.COLUMN_NAME_TIMESTAMP to 1111L,
174+
OutcomeEventsTable.COLUMN_NAME_SESSION_TIME to 1L,
174175
OutcomeEventsTable.COLUMN_NAME_NOTIFICATION_INFLUENCE_TYPE to "unattributed",
175176
OutcomeEventsTable.COLUMN_NAME_IAM_INFLUENCE_TYPE to "unattributed",
176177
),
177178
mapOf(
178179
OutcomeEventsTable.COLUMN_NAME_NAME to "outcomeId2",
179180
OutcomeEventsTable.COLUMN_NAME_WEIGHT to 0.4f,
180181
OutcomeEventsTable.COLUMN_NAME_TIMESTAMP to 2222L,
182+
OutcomeEventsTable.COLUMN_NAME_SESSION_TIME to 2L,
181183
OutcomeEventsTable.COLUMN_NAME_NOTIFICATION_INFLUENCE_TYPE to "indirect",
182184
OutcomeEventsTable.COLUMN_NAME_NOTIFICATION_IDS to "[\"notificationId1\",\"notificationId2\"]",
183185
OutcomeEventsTable.COLUMN_NAME_IAM_INFLUENCE_TYPE to "indirect",
@@ -187,6 +189,7 @@ class OutcomeEventsRepositoryTests : FunSpec({
187189
OutcomeEventsTable.COLUMN_NAME_NAME to "outcomeId3",
188190
OutcomeEventsTable.COLUMN_NAME_WEIGHT to 0.6f,
189191
OutcomeEventsTable.COLUMN_NAME_TIMESTAMP to 3333L,
192+
OutcomeEventsTable.COLUMN_NAME_SESSION_TIME to 3L,
190193
OutcomeEventsTable.COLUMN_NAME_NOTIFICATION_INFLUENCE_TYPE to "direct",
191194
OutcomeEventsTable.COLUMN_NAME_NOTIFICATION_IDS to "[\"notificationId3\"]",
192195
OutcomeEventsTable.COLUMN_NAME_IAM_INFLUENCE_TYPE to "direct",
@@ -205,12 +208,14 @@ class OutcomeEventsRepositoryTests : FunSpec({
205208
events[0].outcomeId shouldBe "outcomeId1"
206209
events[0].weight shouldBe 0.2f
207210
events[0].timestamp shouldBe 1111L
211+
events[0].sessionTime shouldBe 1L
208212
events[0].outcomeSource shouldNotBe null
209213
events[0].outcomeSource!!.directBody shouldBe null
210214
events[0].outcomeSource!!.indirectBody shouldBe null
211215
events[1].outcomeId shouldBe "outcomeId2"
212216
events[1].weight shouldBe 0.4f
213217
events[1].timestamp shouldBe 2222L
218+
events[1].sessionTime shouldBe 2L
214219
events[1].outcomeSource shouldNotBe null
215220
events[1].outcomeSource!!.directBody shouldBe null
216221
events[1].outcomeSource!!.indirectBody shouldNotBe null
@@ -223,6 +228,7 @@ class OutcomeEventsRepositoryTests : FunSpec({
223228
events[2].outcomeId shouldBe "outcomeId3"
224229
events[2].weight shouldBe 0.6f
225230
events[2].timestamp shouldBe 3333L
231+
events[2].sessionTime shouldBe 3L
226232
events[2].outcomeSource shouldNotBe null
227233
events[2].outcomeSource!!.indirectBody shouldBe null
228234
events[2].outcomeSource!!.directBody shouldNotBe null

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/backend/SubscriptionBackendServiceTests.kt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.onesignal.core.internal.http.HttpResponse
55
import com.onesignal.core.internal.http.IHttpClient
66
import com.onesignal.debug.LogLevel
77
import com.onesignal.debug.internal.logging.Logging
8-
import com.onesignal.extensions.RobolectricTest
98
import com.onesignal.user.internal.backend.impl.SubscriptionBackendService
109
import com.onesignal.user.internal.subscriptions.SubscriptionStatus
1110
import io.kotest.assertions.throwables.shouldThrowUnit
@@ -17,7 +16,8 @@ import io.mockk.coVerify
1716
import io.mockk.mockk
1817
import org.junit.runner.RunWith
1918

20-
@RobolectricTest
19+
// WARNING: Adding @RobolectricTest will cause JSONObject.map() to stop working
20+
// at runtime.
2121
@RunWith(KotestTestRunner::class)
2222
class SubscriptionBackendServiceTests : FunSpec({
2323
beforeAny {
@@ -29,7 +29,7 @@ class SubscriptionBackendServiceTests : FunSpec({
2929
val aliasLabel = "onesignal_id"
3030
val aliasValue = "11111111-1111-1111-1111-111111111111"
3131
val spyHttpClient = mockk<IHttpClient>()
32-
coEvery { spyHttpClient.post(any(), any()) } returns HttpResponse(202, "{id: \"subscriptionId\"}")
32+
coEvery { spyHttpClient.post(any(), any()) } returns HttpResponse(202, "{ \"subscription\": { id: \"subscriptionId\" } }")
3333
val subscriptionBackendService = SubscriptionBackendService(spyHttpClient)
3434

3535
// When
@@ -48,13 +48,14 @@ class SubscriptionBackendServiceTests : FunSpec({
4848
response shouldBe "subscriptionId"
4949
coVerify {
5050
spyHttpClient.post(
51-
"apps/appId/user/by/$aliasLabel/$aliasValue/subscription",
51+
"apps/appId/users/by/$aliasLabel/$aliasValue/subscriptions",
5252
withArg {
53-
it.has("id") shouldBe false
54-
it.getString("type") shouldBe "AndroidPush"
55-
it.getString("token") shouldBe "pushToken"
56-
it.getBoolean("enabled") shouldBe true
57-
it.getInt("notification_types") shouldBe 1
53+
val sub = it.getJSONObject("subscription")
54+
sub.has("id") shouldBe false
55+
sub.getString("type") shouldBe "AndroidPush"
56+
sub.getString("token") shouldBe "pushToken"
57+
sub.getBoolean("enabled") shouldBe true
58+
sub.getInt("notification_types") shouldBe 1
5859
},
5960
)
6061
}
@@ -92,13 +93,14 @@ class SubscriptionBackendServiceTests : FunSpec({
9293
// Then
9394
coVerify {
9495
spyHttpClient.post(
95-
"apps/appId/user/by/$aliasLabel/$aliasValue/subscription",
96+
"apps/appId/users/by/$aliasLabel/$aliasValue/subscriptions",
9697
withArg {
97-
it.has("id") shouldBe false
98-
it.getString("type") shouldBe "AndroidPush"
99-
it.getString("token") shouldBe "pushToken"
100-
it.getBoolean("enabled") shouldBe true
101-
it.getInt("notification_types") shouldBe 1
98+
val sub = it.getJSONObject("subscription")
99+
sub.has("id") shouldBe false
100+
sub.getString("type") shouldBe "AndroidPush"
101+
sub.getString("token") shouldBe "pushToken"
102+
sub.getBoolean("enabled") shouldBe true
103+
sub.getInt("notification_types") shouldBe 1
102104
},
103105
)
104106
}

0 commit comments

Comments
 (0)