-
Notifications
You must be signed in to change notification settings - Fork 57
Unit Tests improvements #137
base: main
Are you sure you want to change the base?
Unit Tests improvements #137
Conversation
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👯
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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? 🤔
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. 😄
There was a problem hiding this comment.
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 😸 )
There was a problem hiding this comment.
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
data/build.gradle.kts
Outdated
dependencies { | ||
implementation("com.google.truth:truth:1.0.1") | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 😀
There was a problem hiding this comment.
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.
@RunWith(RobolectricTestRunner::class) | ||
class SessionDaoTest { | ||
|
||
private lateinit var session: Session | ||
@MockK | ||
private lateinit var sessionDao: SessionDao | ||
private lateinit var db: Database |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
data/build.gradle.kts
Outdated
@@ -84,6 +84,9 @@ kotlin { | |||
languageSettings.apply { | |||
optIn("kotlinx.coroutines.ExperimentalCoroutinesApi") | |||
} | |||
dependencies { | |||
implementation("com.google.truth:truth:1.0.1") |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
@anselmoalexandre can you resolve the conflicts before we merge this? |
Sure |
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