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 #210 : Add Tests for NumericInputIsLessThanOrEquaToRuleClassifierProvider #1775

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b0eb1da
Merge pull request #2 from oppia/develop
prayutsu Aug 21, 2020
038bd14
Merge remote-tracking branch 'upstream/develop' into develop
prayutsu Aug 21, 2020
620c534
Merge pull request #3 from oppia/develop
prayutsu Aug 22, 2020
4195b6a
Merge pull request #6 from oppia/develop
prayutsu Aug 24, 2020
92bc7ab
Merge remote-tracking branch 'upstream/develop' into develop
prayutsu Aug 24, 2020
81f13e9
Merge branch 'develop' of https://github.com/prayutsu/oppia-android i…
prayutsu Aug 26, 2020
f06d1be
Merge pull request #13 from oppia/develop
prayutsu Aug 26, 2020
4ab3ef4
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
prayutsu Aug 26, 2020
0b02af9
Merge remote-tracking branch 'origin/develop' into develop
prayutsu Aug 26, 2020
1affc4e
Merge pull request #14 from oppia/develop
prayutsu Aug 26, 2020
da6256c
check fork
prayutsu Aug 26, 2020
93f9a17
Merge remote-tracking branch 'origin/develop' into develop
prayutsu Aug 26, 2020
398b582
local develop branch synced with remote branch
prayutsu Aug 26, 2020
ae9014b
Merge remote-tracking branch 'upstream/develop' into develop
prayutsu Aug 28, 2020
c08e66c
Merge remote-tracking branch 'upstream/develop' into develop
prayutsu Aug 29, 2020
d347418
Merge remote-tracking branch 'upstream/develop' into develop
prayutsu Aug 30, 2020
ef916a2
Merge remote-tracking branch 'upstream/develop' into develop
prayutsu Aug 31, 2020
da7d6e8
Merge remote-tracking branch 'upstream/develop' into develop
prayutsu Sep 1, 2020
a41b210
Merge remote-tracking branch 'upstream/develop' into develop
prayutsu Sep 2, 2020
f793875
Merge remote-tracking branch 'upstream/develop' into develop
prayutsu Sep 2, 2020
e3d3ced
Added tests for NumericInputIsLessThanOrEqualToRuleClassifierProvider
prayutsu Sep 2, 2020
f77983f
Added more tests for Integers
prayutsu Sep 3, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ internal class NumericInputIsLessThanOrEqualToRuleClassifierProvider @Inject con
)
}

// TODO(#210): Add tests for this classifier.
override fun matches(answer: Double, input: Double): Boolean {
return answer <= input
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package org.oppia.domain.classify.rules

import android.app.Application
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import dagger.BindsInstance
import dagger.Component
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.oppia.app.model.InteractionObject
import org.oppia.domain.classify.rules.numericinput.NumericInputIsLessThanOrEqualToRuleClassifierProvider
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.reflect.KClass
import kotlin.reflect.full.cast
import kotlin.test.fail

/** Tests for [NumericInputIsLessThanOrEqualToRuleClassifierProvider]. */
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
@Config(manifest = Config.NONE)
class NumericInputIsLessThanOrEqualToRuleClassifierProviderTest {

private val POSITIVE_REAL_VALUE_1_5 = createReal(value = 1.5)
private val POSITIVE_REAL_VALUE_3_5 = createReal(value = 3.5)
private val NEGATIVE_REAL_VALUE_1_5 = createReal(value = -1.5)
private val NEGATIVE_REAL_VALUE_3_5 = createReal(value = -3.5)
private val STRING_VALUE = createString(value = "test")
private val POSITIVE_INT_VALUE_3 = createInt(value = 3)
private val POSITIVE_INT_VALUE_1 = createInt(value = 1)
private val NEGATIVE_INT_VALUE_1 = createInt(value = -1)
private val NEGATIVE_INT_VALUE_3 = createInt(value = -3)

@Inject
internal lateinit var numericInputIsLessThanOrEqualToRuleClassifierProvider:
NumericInputIsLessThanOrEqualToRuleClassifierProvider

private val inputIsLessThanOrEqualToRuleClassifier by lazy {
numericInputIsLessThanOrEqualToRuleClassifierProvider.createRuleClassifier()
}

@Before
fun setUp() {
setUpTestApplicationComponent()
}

@Test
fun testPositiveRealAnswer_positiveRealInput_sameExactValues_answerLesserOrEqual() {
val inputs = mapOf("x" to POSITIVE_REAL_VALUE_1_5)

val matches =
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = POSITIVE_REAL_VALUE_1_5, inputs = inputs)

assertThat(matches).isTrue()
}

@Test
fun testNegativeRealAnswer_negativeRealInput_sameExactValues_answerLesserOrEqual() {
val inputs = mapOf("x" to NEGATIVE_REAL_VALUE_1_5)

val matches =
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = NEGATIVE_REAL_VALUE_1_5, inputs = inputs)

assertThat(matches).isTrue()
}

@Test
fun testPositiveRealAnswer_positiveRealInput_answerValueLesser_answerLesserOrEqual() {
val inputs = mapOf("x" to POSITIVE_REAL_VALUE_3_5)

val matches =
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = POSITIVE_REAL_VALUE_1_5, inputs = inputs)

assertThat(matches).isTrue()
}

@Test
fun testPositiveRealAnswer_positiveRealInput_answerValueGreater_answerNotLesserOrEqual() {
val inputs = mapOf("x" to POSITIVE_REAL_VALUE_1_5)

val matches =
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = POSITIVE_REAL_VALUE_3_5, inputs = inputs)

assertThat(matches).isFalse()
}

@Test
fun testNegativeRealAnswer_negativeRealInput_answerValueLesser_answerLesserOrEqual() {
val inputs = mapOf("x" to NEGATIVE_REAL_VALUE_1_5)

val matches =
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = NEGATIVE_REAL_VALUE_3_5, inputs = inputs)

assertThat(matches).isTrue()
}

@Test
fun testNegativeRealAnswer_negativeRealInput_answerValueGreater_answerNotLesserOrEqual() {
val inputs = mapOf("x" to NEGATIVE_REAL_VALUE_3_5)

val matches =
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = NEGATIVE_REAL_VALUE_1_5, inputs = inputs)

assertThat(matches).isFalse()
}

@Test
fun testNegativeRealAnswer_positiveRealInput_answerValueLesser_answerLesserOrEqual() {
val inputs = mapOf("x" to POSITIVE_REAL_VALUE_1_5)

val matches =
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = NEGATIVE_REAL_VALUE_3_5, inputs = inputs)

assertThat(matches).isTrue()
}

@Test
fun testPositiveRealAnswer_negativeRealInput_answerValueGreater_answerNotLesserOrEqual() {
val inputs = mapOf("x" to NEGATIVE_REAL_VALUE_1_5)

val matches =
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = POSITIVE_REAL_VALUE_1_5, inputs = inputs)

assertThat(matches).isFalse()
}

@Test
fun testPositiveIntAnswer_negativeIntInput_answerValueGreater_answerNotLesserOrEqual() {
val inputs = mapOf("x" to NEGATIVE_INT_VALUE_3)

val matches =
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = POSITIVE_INT_VALUE_1, inputs = inputs)

assertThat(matches).isFalse()
}

@Test
fun testNegativeIntAnswer_positiveIntInput_answerValueLesser_answerLesserOrEqual() {
val inputs = mapOf("x" to POSITIVE_INT_VALUE_3)

val matches =
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = NEGATIVE_INT_VALUE_1, inputs = inputs)

assertThat(matches).isTrue()
}

@Test
fun testRealAnswer_missingInput_throwsException() {
val inputs = mapOf("y" to POSITIVE_REAL_VALUE_1_5)

val exception = assertThrows(IllegalStateException::class) {
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = POSITIVE_REAL_VALUE_1_5, inputs = inputs)
}

assertThat(exception)
.hasMessageThat()
.contains("Expected classifier inputs to contain parameter with name 'x'")
}

@Test
fun testRealAnswer_stringInput_throwsException() {
val inputs = mapOf("x" to STRING_VALUE)

val exception = assertThrows(IllegalStateException::class) {
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = POSITIVE_REAL_VALUE_1_5, inputs = inputs)
}

assertThat(exception)
.hasMessageThat()
.contains("Expected input value to be of type REAL not NORMALIZED_STRING")
}

@Test
fun testIntAnswer_missingInput_throwsException() {
val inputs = mapOf("y" to POSITIVE_INT_VALUE_1)

val exception = assertThrows(IllegalStateException::class) {
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = POSITIVE_INT_VALUE_1, inputs = inputs)
}

assertThat(exception)
.hasMessageThat()
.contains("Expected classifier inputs to contain parameter with name 'x'")
}

@Test
fun testIntAnswer_stringInput_throwsException() {
val inputs = mapOf("x" to STRING_VALUE)

val exception = assertThrows(IllegalStateException::class) {
inputIsLessThanOrEqualToRuleClassifier
.matches(answer = POSITIVE_INT_VALUE_1, inputs = inputs)
}

assertThat(exception)
.hasMessageThat()
.contains("Expected input value to be of type REAL not NORMALIZED_STRING")
}

private fun createReal(value: Double): InteractionObject {
return InteractionObject.newBuilder().setReal(value).build()
}

private fun createString(value: String): InteractionObject {
return InteractionObject.newBuilder().setNormalizedString(value).build()
}

private fun createInt(value: Int): InteractionObject {
return InteractionObject.newBuilder().setReal(value.toDouble()).build()
}

private fun setUpTestApplicationComponent() {
DaggerNumericInputIsLessThanOrEqualToRuleClassifierProviderTest_TestApplicationComponent
.builder()
.setApplication(ApplicationProvider.getApplicationContext())
.build()
.inject(this)
}

// TODO(#89): Move to a common test library.
private fun <T : Throwable> assertThrows(type: KClass<T>, operation: () -> Unit): T {
try {
operation()
fail("Expected to encounter exception of $type")
} catch (t: Throwable) {
if (type.isInstance(t)) {
return type.cast(t)
}
// Unexpected exception; throw it.
throw t
}
}

// TODO(#89): Move this to a common test application component.
@Singleton
@Component(modules = [])
interface TestApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun setApplication(application: Application): Builder

fun build(): TestApplicationComponent
}

fun inject(test: NumericInputIsLessThanOrEqualToRuleClassifierProviderTest)
}
}