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

COH-50 : Cohort resources cannot be voided #26

Merged
merged 15 commits into from
Mar 22, 2023

Conversation

ManojLL
Copy link
Contributor

@ManojLL ManojLL commented Mar 10, 2023

Issue worked on:

https://issues.openmrs.org/browse/COH-50?filter=-1

Description

This PR fixed the issue in (COH-50) DELETE Cohort endpoint.

@ManojLL ManojLL marked this pull request as ready for review March 10, 2023 17:06
@ManojLL ManojLL marked this pull request as draft March 10, 2023 17:07
@ManojLL ManojLL marked this pull request as ready for review March 12, 2023 11:54
Copy link

@Piumal1999 Piumal1999 left a comment

Choose a reason for hiding this comment

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

Hi @ibacher, could you please review this PR? I added some comments as well.

@dkayiwa
Copy link
Member

dkayiwa commented Mar 12, 2023

@ManojLL can you add some tests for this?

@ManojLL
Copy link
Contributor Author

ManojLL commented Mar 13, 2023

@dkayiwa I added some tests for voidCohort method. Can you review?

@ManojLL ManojLL requested review from ibacher and removed request for ibacher March 14, 2023 16:26
@ManojLL ManojLL requested review from ibacher and removed request for ibacher March 16, 2023 19:37
@ManojLL
Copy link
Contributor Author

ManojLL commented Mar 20, 2023

@ibacher @dkayiwa can you review this PR?

cohort.setVoided(true);
cohort.setVoidReason(reason);

for (CohortMember cohortMember : cohort.getCohortMembers()) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cohort won't voided automatically. That is why manually set voided true.

Copy link
Member

Choose a reason for hiding this comment

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

Do you have a test to demonstrate it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I attached test screenshots.
Screenshot from 2023-03-20 15-03-37
Screenshot from 2023-03-20 15-03-54

Copy link
Member

Choose a reason for hiding this comment

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

I mean tests in this pull request. Not a screenshot.

Copy link
Member

@dkayiwa dkayiwa Mar 20, 2023

Choose a reason for hiding this comment

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

In other words, tests which would fail if you removed that for loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In other words, tests which would fail if you removed that for loop

If I remove for loop tests wont fail

Copy link
Member

Choose a reason for hiding this comment

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

Which means that you do not have tests to demonstrate the problem that you claim to require the for loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dkayiwa I updated the test can you review ?

Comment on lines 97 to 98
cohortMember.setVoided(true);
cohortMember.setVoidReason("Cohort voided");
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason we need these two explicit setVoided() and setVoidReason() calls rather than calling the service method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ibacher I thought that, it would be better to call the two explicit would be better than calling Context.getService multiple times inside the for loop considering the performance, that is why I use setVoided and setVoidReasin methods.

Copy link
Member

Choose a reason for hiding this comment

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

I'd tend to prefer the service call. You could always do:

CohortMemberService cms = Context.getService(CohortMemberService.class);
// loop
   cms.voidCohortMember();
// end loop

That way, it's the CohortMemberService's responsibility to know how to void a cohort member (and also it fixes the real problem which is that calling these methods depends on Hibernate's implicit context to flush those changes to the DB whereas calling the service method will actually ensure that those changes are always written.

Copy link
Contributor Author

@ManojLL ManojLL Mar 21, 2023

Choose a reason for hiding this comment

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

@ibacher After rename the voidChort method to voidCohortM ,no need to call cohortMemberservice void method to make cohortmember to voided. when delete a cohort , the child cohortmember getting Voided.

@@ -167,9 +167,11 @@ public void shouldVoidCohort() {
cohortMember.setPatient(patient);
cohortMember.setUuid("4834jk3-n34nm30-34nm34-348nl");
cohortMember.setCohort(cohortM);
cohortM.addMemberships(cohortMember);
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to write a separate test for this. The test here isn't really valid because ultimately we mock the createOrUpdate() call...

Copy link
Contributor Author

@ManojLL ManojLL Mar 21, 2023

Choose a reason for hiding this comment

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

@ibacher This test is to check whether the child cohortMember is voided when the parent cohort is deleted. So I added the cohortMember to the cohort and used assertions to check the cohort and cohortMember is voided after deleting the cohort

Copy link
Member

Choose a reason for hiding this comment

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

So, what I was trying to get at is the way in which the mock changes this from how the code would run in production because the key thing about voiding the memberships is that the voiding gets written to the database, but because we've mocked the call to createOrUpdate() nothing is getting written to a database, so, ultimately, this test ends up verifying that cohortMember.setVoided() was called, but not that the cohortMember was actually saved in a voided state.

@dkayiwa
Copy link
Member

dkayiwa commented Mar 21, 2023

@ManojLL the method needs renaming to voidCohortM for this to work automatically as seen here: https://github.com/openmrs/openmrs-core/blob/2.6.0/api/src/main/java/org/openmrs/aop/RequiredDataAdvice.java#L191-L218

If we want other platform AOP things to work for other methods like saveCohort, etc, we would need to rename those methods too. But any way, that would be a different ticket.

@ManojLL
Copy link
Contributor Author

ManojLL commented Mar 21, 2023

@dkayiwa rename the voidCohort metod to voidcohortM, Can you review the changes?

@dkayiwa
Copy link
Member

dkayiwa commented Mar 21, 2023

@ManojLL do you think you can add a test?

@ManojLL
Copy link
Contributor Author

ManojLL commented Mar 21, 2023

@ManojLL do you think you can add a test?

@dkayiwa I tried to write test for this method, but unable to find a way to check whether the cohort or cohortMember is voided after mocking voidCohortM method.

@dkayiwa
Copy link
Member

dkayiwa commented Mar 21, 2023

@ManojLL
Copy link
Contributor Author

ManojLL commented Mar 21, 2023

@ManojLL would it be easier for you to just put un mocked tests here? https://github.com/openmrs/openmrs-module-cohort/blob/master/api/src/test/java/org/openmrs/module/cohort/api/dao/CohortGenericDaoTest.java

@dkayiwa I added tests for voidcohortM method and voidCohortType method, you can review.

@dkayiwa
Copy link
Member

dkayiwa commented Mar 21, 2023

I do not see your test calling the service.voidCohortM method of interest.

@ManojLL
Copy link
Contributor Author

ManojLL commented Mar 22, 2023

@dkayiwa I updated the test

@dkayiwa
Copy link
Member

dkayiwa commented Mar 22, 2023

The test that you added passes even when you do not fix the reported problem.
Can you use the real service without mocking it?

@ManojLL
Copy link
Contributor Author

ManojLL commented Mar 22, 2023

@dkayiwa updated the tests, You can review.

@Test
public void shouldVoidCohortM() {
CohortM cohortToVoid = dao.get(COHORT_UUID);
cohortToVoid.setVoided(true);
Copy link
Member

Choose a reason for hiding this comment

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

Why are you setting the void properties of the cohort and then at the same time call the voidCohort service method? It is the service method that is supposed to take care of that.

Copy link
Contributor Author

@ManojLL ManojLL Mar 22, 2023

Choose a reason for hiding this comment

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

@dkayiwa I thought that, the cohort which is passing to the voidCoghortM method needs be voided true.
btw I updated test.

@dkayiwa dkayiwa merged commit d73dd40 into openmrs:master Mar 22, 2023
@Piumal1999
Copy link

Thank you all for getting this done.

@dkayiwa @ibacher will it be possible to do a release with this fix?
Because the error with the delete cohort is a blocker for completing this PR: openmrs/openmrs-esm-patient-management#578.

@dkayiwa
Copy link
Member

dkayiwa commented Mar 22, 2023

@Piumal1999 feel empowered to do the release by yourself. Here are the instructions: https://wiki.openmrs.org/display/docs/Releasing+a+Module+from+Bamboo

@Piumal1999
Copy link

Sure, I would love to. I just created a help desk ticket to get access to the bamboo dashboard.
Thank you @dkayiwa

@ibacher
Copy link
Member

ibacher commented Mar 22, 2023

I’d prefer if we hold off on releasing this just yet. There’s no reason we need to do that to unblock the issue; we can just update the O3 release to use the snapshot and then, once we inevitably find another problem or two, we can fix things without all the hassle. We’ll cut a release of this the next time we need to release the 3.x RefApp (which will likely be soon anyways).

@Piumal1999
Copy link

I’d prefer if we hold off on releasing this just yet. There’s no reason we need to do that to unblock the issue; we can just update the O3 release to use the snapshot and then, once we inevitably find another problem or two, we can fix things without all the hassle. We’ll cut a release of this the next time we need to release the 3.x RefApp (which will likely be soon anyways).

Oh sorry @ibacher. I already did the release.

we can just update the O3 release to use the snapshot

Btw, how do we change the module versions used in dev3? Is it done by updating this?

@dkayiwa
Copy link
Member

dkayiwa commented Mar 23, 2023

Is it done by updating this?

Yes.

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.

4 participants