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

AC-1092: Add Tests for Observation Repository #1010

Merged
merged 3 commits into from
Aug 27, 2023

Conversation

shubhamsgit
Copy link
Member

Description of what I changed

  • Added Unit tests for Observation Repository

Issue I worked on

JIRA Issue: https://issues.openmrs.org/browse/AC-1092

Checklist: I completed these to help reviewers :)

  • My pull request only contains ONE single commit
    (the number above, next to the 'Commits' tab is 1).
  • I have added tests to cover my changes. (If you refactored
    existing code that was well tested you do not have to add tests)
  • All new and existing tests passed.
  • My pull request is based on the latest changes of the master branch.

@shubhamsgit shubhamsgit added Tests SDK Changes for the SDK labels Jul 26, 2023
@shubhamsgit shubhamsgit marked this pull request as draft July 29, 2023 15:31
@shubhamsgit shubhamsgit marked this pull request as ready for review August 6, 2023 15:56
getObservationByUuid(observationResource.uuid!!).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ observation ->
observationList.add(observation)
},
{ error ->
Log.e("Observation Repository", "Error: ${error.message}")
},
{
Log.d("Observation Repository", "Observable completed")
}
)
val observation = getObservationByUuid(observationResource.uuid!!)
observationList.add(observation.execute())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we removing this subscriber and observer???

Copy link
Member Author

Choose a reason for hiding this comment

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

@rishabh-997 I was the one who added this code but when unit tests were failing I noticed this was a bug. I replaced it with "execute()" method which returns the immediate result of observable available.
"fun Observable.execute(): T = this.single().toBlocking().first()"
Like this I did many refactoring in my earlier written code.
Regardless the api call is still kept as an observable so that implementers of SDK can implement it in that pattern if wanted.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So we are blocking the thread till the execution is completed instead of handling in backgraound and wait for results?

Copy link
Member Author

Choose a reason for hiding this comment

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

RestApi function gives the result immediately. And the result won't emit any addition to the observale too, since there are no triggers to the restApi function except for once in this ObservationRepository function.

I guess the restApi functions were kept to return observales since It's a good practice to maintain consistency in the codebase, if the majority of codebase uses reactive programming, returning an Observable or a Flow from Retrofit API functions would align well with the overall architecture of the SDK.

So here we are just fetching the immediate result, instead of waiting for more results because there are absolutely zero changes to the result after the initial execution. So, It won't be any worth for the thread to keep waiting.

@rishabh-997 rishabh-997 merged commit e38d442 into master Aug 27, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SDK Changes for the SDK Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants