Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Part of #4236 : Add tests for AsyncResultSubject #5670

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file from test_file_exemptions

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done .

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import com.google.common.truth.extensions.proto.LiteProtoTruth.assertThat
import com.google.protobuf.MessageLite
import org.oppia.android.util.data.AsyncResult

// TODO(#4236): Add tests for this class.

/**
* Truth subject for verifying properties of [AsyncResult]s.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.oppia.android.testing.data

import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.oppia.android.util.data.AsyncResult
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@RunWith(RobolectricTestRunner::class)
@RunWith(JUnit4::class)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried switching to JUnit4 as suggested, but I encountered an error:
java.lang.UnsatisfiedLinkError: 'long android.os.SystemClock.uptimeMillis()'.
Since this is related to Android-specific functionality, I’m reverting to RobolectricTestRunner to ensure the tests run correctly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per yesterday’s discussion, Ben mentioned that we don’t generally use the Robolectric runner, so one way to fix this error while using the JUnit runner is to inject FakeOppiaClock in this class, and then add an @before method to initialize the FakeOppiaClock object to uptimemillis or fake time mode as needed for these tests

class AsyncResultSubjectTest {

@Test
fun testAsyncResultPending_isPending() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun testAsyncResultPending_isPending() {
fun testAsyncResultSubject_pendingResult_checkIsPending() {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

val pendingResult: AsyncResult<String> = AsyncResult.Pending()

AsyncResultSubject.assertThat(pendingResult).isPending()
}

@Test
fun testAsyncResultPending_isNotSuccess() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun testAsyncResultPending_isNotSuccess() {
fun testAsyncResultSubject_pendingResult_checkIsNotSuccess() {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are also missing a corresponding "isNotFailure()" test.

val pendingResult: AsyncResult<String> = AsyncResult.Pending()

AsyncResultSubject.assertThat(pendingResult).isNotSuccess()
}

@Test
fun testAsyncResultSuccess_isSuccess() {
val successResult: AsyncResult<String> = AsyncResult.Success("Success value")

AsyncResultSubject.assertThat(successResult).isSuccess()
}

@Test
fun testAsyncResultSuccess_hasSuccessValueWhere_matchesExpected() {
val successResult: AsyncResult<String> = AsyncResult.Success("Success value")

AsyncResultSubject.assertThat(successResult).hasSuccessValueWhere {
// Here we are verifying that the value is "Success value"
assertThat(this).isEqualTo("Success value")
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to reduce ambiguity.

Suggested change
@Test
fun testAsyncResultSuccess_hasSuccessValueWhere_matchesExpected() {
val successResult: AsyncResult<String> = AsyncResult.Success("Success value")
AsyncResultSubject.assertThat(successResult).hasSuccessValueWhere {
// Here we are verifying that the value is "Success value"
assertThat(this).isEqualTo("Success value")
}
}
@Test
fun testAsyncResultSuccess_hasSuccessValueWhere_matchesExpected() {
val successResult: AsyncResult<String> = AsyncResult.Success("Some string")
AsyncResultSubject.assertThat(successResult).hasSuccessValueWhere { result ->
assertThat(result).isEqualTo("Some string")
}
}


@Test
fun testAsyncResultSuccess_isStringSuccessThat() {
val successResult: AsyncResult<String> = AsyncResult.Success("Success value")

AsyncResultSubject.assertThat(successResult).isStringSuccessThat().isEqualTo("Success value")
}

@Test
fun testAsyncResultFailure_isFailure() {
val failureResult: AsyncResult<String> = AsyncResult.Failure(Throwable("Error"))

AsyncResultSubject.assertThat(failureResult).isFailure()
}

@Test
fun testAsyncResultFailure_isFailureThat_matchesExpectedError() {
val failureResult: AsyncResult<String> = AsyncResult.Failure(Throwable("Error"))

AsyncResultSubject.assertThat(failureResult).isFailureThat().hasMessageThat().contains("Error")
}

@Test
fun testAsyncResultSuccess_isNewerOrSameAgeAs() {
val successResult1: AsyncResult<String> = AsyncResult.Success("First")
val successResult2: AsyncResult<String> = AsyncResult.Success("Second")

AsyncResultSubject.assertThat(successResult1).isNewerOrSameAgeAs(successResult2)
}

@Test
fun testAsyncResultFailure_hasSameEffectiveValueAs_differentError() {
val failureResult1: AsyncResult<String> = AsyncResult.Failure(Throwable("Error"))
val failureResult2: AsyncResult<String> = AsyncResult.Failure(Throwable("Different Error"))

AsyncResultSubject.assertThat(failureResult1).hasSameEffectiveValueAs(failureResult2).isFalse()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,21 @@ oppia_android_test(
"//utility/src/main/java/org/oppia/android/util/networking:debug_module",
],
)

# Adding test for AsyncResultSubject
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not required.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

oppia_android_test(
name = "AsyncResultSubjectTest",
srcs = ["AsyncResultSubjectTest.kt"],
custom_package = "org.oppia.android.testing.data",
test_class = "org.oppia.android.testing.data.AsyncResultSubjectTest",
test_manifest = "//testing:test_manifest",
deps = [
"//testing/src/main/java/org/oppia/android/testing/data:async_result_subject",
"//testing/src/main/java/org/oppia/android/testing/robolectric:test_module",
"//third_party:androidx_test_ext_junit",
"//third_party:com_google_truth_truth",
"//third_party:junit_junit",
"//third_party:org_robolectric_robolectric",
"//third_party:robolectric_android-all",
],
)
Loading