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 #1889 : Throughly test NumericInputIsWithinToleranceRuleClassifier #1922

Conversation

prayutsu
Copy link
Contributor

@prayutsu prayutsu commented Sep 30, 2020

Explanation

Fixes #1889 : Test cases are added to thoroughly test NumericInputIsWithinToleranceRuleClassifier

Checklist

  • The PR title starts with "Fix #bugnum: ", followed by a short, clear summary of the changes. (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".)
  • The PR explanation includes the words "Fixes #bugnum: ..." (or "Fixes part of #bugnum" if the PR only partially fixes an issue).
  • The PR follows the style guide.
  • The PR does not contain any unnecessary auto-generated code from Android Studio.
  • The PR is made from a branch that's not called "develop".
  • The PR is made from a branch that is up-to-date with "develop".
  • The PR's branch is based on "develop" and not on any other branch.
  • The PR is assigned to an appropriate reviewer in both the Assignees and the Reviewers sections.

All tests are passing -
Screenshot from 2020-09-30 14-32-05

@prayutsu
Copy link
Contributor Author

@MohamedMedhat1998 PTAL

@aggarwalpulkit596 aggarwalpulkit596 self-assigned this Sep 30, 2020
}

@Test
fun testIntAnswer_secondStringInput_throwsException() {
Copy link
Contributor

Choose a reason for hiding this comment

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

this one seems a bit redundant to me as we have already checked that the second input won't work with string. Fix this elsewhere too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@prayutsu
Copy link
Contributor Author

@aggarwalpulkit596 I have also updated the screenshot in the PR description. Please take a look.

}

@Test
fun testIntAnswer_missingSecondInput_throwsException() {
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

}

@Test
fun testRealAnswer_firstStringInput_throwsException() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a test case with 3 inputs as well and I guess i wasn't clear enough in my previous comments but what i wanted was to have kept the existing test cases in which there was incorrect input variable like c or anything

Copy link
Contributor Author

@prayutsu prayutsu Sep 30, 2020

Choose a reason for hiding this comment

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

If I test with 3 inputs, exception's error is a bit weird

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aggarwalpulkit596 Also isn't that case covered in missing inputs and incorrect inputs ?

Copy link
Contributor

Choose a reason for hiding this comment

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

@prayutsu what's the error for 3 inputs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Screenshot from 2020-09-30 15-40-07

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MohamedMedhat1998 What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

@prayutsu having test cases that check the case where we have 3 inputs is a good idea.
I suggest trying to pass a map that contains both "x" and "tol" + some other value ("y" for example) and observe which exception you will get. After you know the exception, you can write a test case that checks whether this exception is thrown or not.
It's kinda weird to gerenrate an exception then test whether it is generated or not 😅, but I think it is ok.

Copy link
Contributor Author

@prayutsu prayutsu Sep 30, 2020

Choose a reason for hiding this comment

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

@MohamedMedhat1998 I tried the same, but see the exception in the screenshot, it does not throw the exception of the "same type" that we get in other test cases

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aggarwalpulkit596 I think this will not work because, currently exception message is of the form Expected classifier inputs to contain parameter with name 'x' but had: [c, tol] but in case of three inputs it can't display the range

Please, see this comment @MohamedMedhat1998

Copy link
Contributor

Choose a reason for hiding this comment

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

@MohamedMedhat1998 I tried the same, but see the exception in the screenshot, it does not throw the exception of the "same type" that we get in other test cases

Can you share your code, please?

Copy link
Contributor

@MohamedMedhat1998 MohamedMedhat1998 left a comment

Choose a reason for hiding this comment

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

The overall PR is very good but I have left some comments, PTAL.

}

@Test
fun testPositiveIntAnswer_positiveRealInput_positiveRealTolerance_answerWithinTolerance() {
Copy link
Contributor

Choose a reason for hiding this comment

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

testPositiveIntAnswer_positiveRealInput_positiveRealTolerance_answerWithinTolerance to testPositiveRealAnswer_positiveRealInput_positiveRealTolerance_answerWithinTolerance

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MohamedMedhat1998 Test cases with Int are covering separate tests, they have been added intentionally, please confirm once and I'll change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see that you have added test cases for both int and real, but in this test case you are using real as an answer (POSITIVE_REAL_VALUE_2_5) but the name is int

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay this would be fixed in the next commit

}

@Test
fun testNegativeRealAnswer_negativeRealInput_positiveIntTolerance_answerWithinTolerance() {
Copy link
Contributor

Choose a reason for hiding this comment

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

testNegativeRealAnswer_negativeRealInput_positiveIntTolerance_answerWithinTolerance to testNegativeIntAnswer_negativeIntInput_positiveIntTolerance_answerWithinTolerance

Copy link
Member

Choose a reason for hiding this comment

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

Was this fixed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, this is fixed now

@prayutsu
Copy link
Contributor Author

@MohamedMedhat1998 Note - All changes are not fixed yet


@Test
fun testIntAnswer_extraInput_throwsException() {
val inputs = mapOf(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MohamedMedhat1998 This is the testcase

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, thanks.
Btw you didn't have to push the changes. If you are unsure of the code and you don't want to push it, you can simply add it as a code snippet in a comment like this:

  @Test
  fun testIntAnswer_extraInput_throwsException() {
    val inputs = mapOf(
      "c" to POSITIVE_INT_VALUE_2,
      "x" to POSITIVE_INT_VALUE_3,
      "tol" to POSITIVE_INT_VALUE_1
    )
    val exception = assertThrows(IllegalStateException::class) {
      inputIsWithinToleranceRuleClassifier
        .matches(answer = POSITIVE_INT_VALUE_2, inputs = inputs)
    }

    assertThat(exception)
      .hasMessageThat()
      .contains("Expected classifier inputs to contain parameter with name 'tol' but had: [x]")
  }

Try removing any assertion related to the exception and simply use an incorrect map in the normal test cases like testNegativeRealAnswer_negativeRealInput_positiveRealTolerance_answerWithinTolerance and observe the output

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried this -

  @Test
  fun testIntAnswer_extraInput_throwsException() {
    val inputs = mapOf(
      "c" to POSITIVE_INT_VALUE_2,
      "x" to POSITIVE_INT_VALUE_3,
      "tol" to POSITIVE_INT_VALUE_1
    )
    val matches =
      inputIsWithinToleranceRuleClassifier
        .matches(answer = POSITIVE_INT_VALUE_2, inputs = inputs)

    assertThat(matches).isFalse()
  }

And the result is -
Screenshot from 2020-09-30 21-25-32

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  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
    }
  }

@MohamedMedhat1998 I think we are getting an unexpected exception

Copy link
Member

Choose a reason for hiding this comment

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

This is sort of a general "how do we handle exceptional cases" question. In this case, adding a check for extra input helps make things more predictable, but extra parameters don't actually interfere with the classifier's ability to do its job.

I'm also curious to know what others think about this.

Copy link
Contributor

Choose a reason for hiding this comment

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

@MohamedMedhat1998 I think we can add test to check exceptions.

Copy link
Contributor

Choose a reason for hiding this comment

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

@MohamedMedhat1998 I think we can add test to check exceptions.

But actually, no exception is thrown in this case. So, whenever we add test cases for the exceptions, they fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

@BenHenning Any suggestions here as per above comment?

Copy link
Member

Choose a reason for hiding this comment

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

That's actually what I was curious about other people weighing in on. :)

I'm actually on the fence here, but given that I think we should just add the check & a new exception for it.

This should probably be tracked in a separate issue since will need to retroactively add tests for this case in each classifier being tested so far.

@prayutsu
Copy link
Contributor Author

prayutsu commented Oct 1, 2020

@aggarwalpulkit596 @MohamedMedhat1998 Any updates on this?

@MohamedMedhat1998
Copy link
Contributor

@aggarwalpulkit596 @MohamedMedhat1998 Any updates on this?

@prayutsu concerning the multiple inputs tests, we still need the opinions of the others.
@rt4914 @aggarwalpulkit596 can you take a look at this comment, please?

Concerning the negative tolerance, I don't think we need them, but let's wait for the final decision from Rajat, @rt4914 can you finalize this, please?

Other than that, everything is really simple (removing some existing tests or leaving them - we can finalize this quickly), but for now, let's focus on the multiple inputs tests first.

@prayutsu
Copy link
Contributor Author

prayutsu commented Oct 1, 2020

@MohamedMedhat1998 Sure

@prayutsu
Copy link
Contributor Author

prayutsu commented Oct 1, 2020

@rt4914 @aggarwalpulkit596 @BenHenning @MohamedMedhat1998
Once we finalize the discussion, we can update the checkpoints in the PR description, which will make it easy for me to keep a check on all the points.

@prayutsu
Copy link
Contributor Author

prayutsu commented Oct 2, 2020

@rt4914 @aggarwalpulkit596 Can we please have a final verdict on this?

@rt4914
Copy link
Contributor

rt4914 commented Oct 4, 2020

@aggarwalpulkit596 @MohamedMedhat1998 Any updates on this?

@prayutsu concerning the multiple inputs tests, we still need the opinions of the others.
@rt4914 @aggarwalpulkit596 can you take a look at this comment, please?

Concerning the negative tolerance, I don't think we need them, but let's wait for the final decision from Rajat, @rt4914 can you finalize this, please?

Other than that, everything is really simple (removing some existing tests or leaving them - we can finalize this quickly), but for now, let's focus on the multiple inputs tests first.

@prayutsu @MohamedMedhat1998 I have replied to all the comments addressed to me.

This PR is blocked on two points:

  1. The tests are failing
  2. @aggarwalpulkit596 needs to approve this.

@prayutsu prayutsu removed their assignment Oct 5, 2020
@rt4914
Copy link
Contributor

rt4914 commented Oct 6, 2020

Update: I have message @aggarwalpulkit596 personally to have a look at this PR. Lets wait for him.

@MohamedMedhat1998 MohamedMedhat1998 removed their assignment Oct 6, 2020
Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Thanks @prayutsu! This is looking really good to me. I have a suggestions for a few more tests, then I think this PR will be ready for merging.

}

@Test
fun testPositiveRealAnswer_negativeRealInput_negativeRealTolerance_answerNotWithinTolerance() {
Copy link
Member

Choose a reason for hiding this comment

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

Thanks. I actually intended that we add an exception to catch this case, but I'm backing off on that. I think we should instead:

  1. Add a test here to verify that negative tolerance fails in a case where it would have passed for positive tolerance (to verify that we properly "no-op" in this case)
  2. @rt4914 or someone else involved with this PR should file an Oppia web issue to add validation to ensure tolerance is always >= 0

@prayutsu can you take care of (1)?

@BenHenning BenHenning assigned rt4914 and prayutsu and unassigned BenHenning Oct 6, 2020
@prayutsu prayutsu assigned BenHenning and unassigned rt4914 Oct 6, 2020
Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Thanks @prayutsu. This LGTM!

@BenHenning
Copy link
Member

Given other approvals, I'm going to go ahead and merge this if all checks pass.

@rt4914 see the comment thread above when you get a chance.

@prayutsu
Copy link
Contributor Author

prayutsu commented Oct 6, 2020

Thanks @prayutsu. This LGTM!

@BenHenning Thanks

@BenHenning BenHenning merged commit 544578e into oppia:develop Oct 6, 2020
@prayutsu
Copy link
Contributor Author

prayutsu commented Oct 6, 2020

@BenHenning Can you please put a comment - "give @prayutsu 25 points" ?

@prayutsu prayutsu deleted the throughly-test-numeric_input_is_within_tolerance_rule_classifier branch October 6, 2020 20:04
@BenHenning
Copy link
Member

@MohamedMedhat1998 or @aggarwalpulkit596 could you do the scoring here?

@prayutsu since I'm not an SLoP mentor, I can't actually score the PRs.

@prayutsu
Copy link
Contributor Author

prayutsu commented Oct 6, 2020

@MohamedMedhat1998 or @aggarwalpulkit596 could you do the scoring here?

@prayutsu since I'm not an SLoP mentor, I can't actually score the PRs.

No issues, @MohamedMedhat1998 will update this tomorrow, thanks for merging it

@MohamedMedhat1998
Copy link
Contributor

give @prayutsu 25 points

@rt4914
Copy link
Contributor

rt4914 commented Oct 7, 2020

Issue filed for following comment: #1922 (comment)

oppia/oppia#10899

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Throughly test NumericInputIsWithinToleranceRuleClassifier
5 participants