Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Unit Tests improvements #137

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

anselmoalexandre
Copy link

Hi dear reviewer 👋🏿😀

I found out about this on one of @tamzi talks so I decided to have a look.
And when I did, saw that you guys are doing great job.

Congrats 🎉 🥳 👏 🍾 🙌 🎊 🥂

By just rumbling out on the code, among the other minor things that I saw that needs improvements, I decided to step up and contribute (dunno if I did 😀).

I added few modifications/improvements on Unit tests (I can continue reviewing the code if it's okay).

Let me know what you guys think. Also, if you need any help I can contribute more on this project/app.

Thanks ☺️
Anselmo Alexandre

@@ -19,7 +19,7 @@ import kotlinx.coroutines.flow.Flow

interface TokenProvider {

suspend fun fetch(): Flow<String?>
suspend fun fetch(): Flow<String?> // @Allan -> Don't suspend Flows
Copy link
Contributor

@chepsi chepsi Oct 28, 2022

Choose a reason for hiding this comment

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

Remove comment. We can add it to the readme instead.

Copy link
Member

Choose a reason for hiding this comment

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

@anselmoalexandre Good catch

Remove the suspend and return a flow 👯

Copy link
Author

Choose a reason for hiding this comment

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

Hey @chepsi
This comment is just a heads up to @jumaallan so he can refactor this. @jumaallan please check this

Copy link
Member

Choose a reason for hiding this comment

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

@anselmoalexandre saw it

I can raise a PR, but if you want, please feel free to refactor it here

Copy link
Author

Choose a reason for hiding this comment

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

@anselmoalexandre saw it

I can raise a PR, but if you want, please feel free to refactor it here

You can do the PR it's okay 👌🏿. Also, this was only heads up 😄

@@ -23,5 +23,5 @@ import com.android254.data.db.model.Session
interface SessionDao : BaseDao<Session> {

@Query("SELECT * FROM SESSION")
fun fetchSessions(): List<Session>
suspend fun fetchSessions(): List<Session> // This should be suspended or return FLow [Note: Don't suspend Flows]
Copy link
Contributor

@chepsi chepsi Oct 28, 2022

Choose a reason for hiding this comment

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

Remove comment. We can add it to the readme instead.

Copy link
Member

Choose a reason for hiding this comment

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

@anselmoalexandre We should not suspend these queries. I think best way is to return flows instead

Copy link
Author

@anselmoalexandre anselmoalexandre Oct 28, 2022

Choose a reason for hiding this comment

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

@jumaallan any reason why we shouldn't suspend it? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

@anselmoalexandre - maybe this explains it better - https://stackoverflow.com/a/68750634

Flows are different - their whole point is to provide the results at arbitrary times, and potentially without ever stopping! The data items are delivered whenever they're emitted, instead of all at once in a collection.

I am just thinking we should prefer using flows instead?

Copy link
Author

Choose a reason for hiding this comment

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

@anselmoalexandre - maybe this explains it better - https://stackoverflow.com/a/68750634

Flows are different - their whole point is to provide the results at arbitrary times, and potentially without ever stopping! The data items are delivered whenever they're emitted, instead of all at once in a collection.

I am just thinking we should prefer using flows instead?

Yep, Flows are great. But you also need to question yourself: is it needed to use Flow? 🤔 Or I can just suspend this. 😄

Copy link
Member

Choose a reason for hiding this comment

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

Not sure what everyone else thinks here (I would prefer using flows 😸 )

@michaelbukachi @wangerekaharun @misshannah @chepsi

Copy link

Choose a reason for hiding this comment

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

I support @jumaallan idea of using a flow.To better support this I can attach this link for reference https://medium.com/androiddevelopers/room-flow-273acffe5b57

Comment on lines 87 to 89
dependencies {
implementation("com.google.truth:truth:1.0.1")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need another assertion library.

Copy link
Author

Choose a reason for hiding this comment

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

I don't think we need another assertion library.

Well, the Truth library makes assertions more readable and easy to understand unlike the normal assertions used throughout the hole tests (from what I have seen).
Perhaps we can replace the project assertions library by this one. Read a bit about it, you might change your mind 😀

Copy link
Contributor

Choose a reason for hiding this comment

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

I know about it 😅. I just don't think we need it. Tests can be made more readable without it. Please remove it.

Comment on lines -34 to -39
@RunWith(RobolectricTestRunner::class)
class SessionDaoTest {

private lateinit var session: Session
@MockK
private lateinit var sessionDao: SessionDao
private lateinit var db: Database
Copy link
Contributor

Choose a reason for hiding this comment

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

We are using Robolectric because we want to test against a real db.

Copy link
Author

Choose a reason for hiding this comment

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

@michaelbukachi With robolectric: it slows down the tests a lot, but it's sometimes helpful if we really want to test the integration with the Android system

Copy link
Author

Choose a reason for hiding this comment

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

I'd suggest to use it only in a few places, not everywhere. Considering that tests performance are also very important and should be fast as possible

Copy link
Author

Choose a reason for hiding this comment

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

We are using Robolectric because we want to test against a real db.

But it's okay if you guys think you want to stick with this.

Copy link
Contributor

Choose a reason for hiding this comment

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

@anselmoalexandre We are aware of its drawbacks. We used it to ensure all our queries were running as expected.

@@ -84,6 +84,9 @@ kotlin {
languageSettings.apply {
optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
}
dependencies {
implementation("com.google.truth:truth:1.0.1")
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @anselmoalexandre For all dependencies, you need to add them to the lib.versions file. You can check the Readme on why we do this.

Great PR!

Copy link
Author

Choose a reason for hiding this comment

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

Hey @anselmoalexandre For all dependencies, you need to add them to the lib.versions file. You can check the Readme on why we do this.

Great PR!

🤦🏿‍♂️ my bad. Let me add it to version catalog

Copy link
Author

Choose a reason for hiding this comment

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

Hey @anselmoalexandre For all dependencies, you need to add them to the lib.versions file. You can check the Readme on why we do this.

Great PR!

Done. Added the dependency to lib.versions file

@chepsi chepsi self-requested a review October 28, 2022 07:37
@anselmoalexandre anselmoalexandre requested review from wangerekaharun and michaelbukachi and removed request for chepsi October 28, 2022 08:15
@misshannah
Copy link
Collaborator

@anselmoalexandre can you resolve the conflicts before we merge this?

@anselmoalexandre
Copy link
Author

@anselmoalexandre can you resolve the conflicts before we merge this?

Sure

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

Successfully merging this pull request may close these issues.

7 participants