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

Adding unit tests for live location sharing aggregation code (PSF-1063) #6267

Merged
merged 8 commits into from
Jun 20, 2022

Conversation

mnaturel
Copy link
Contributor

@mnaturel mnaturel commented Jun 9, 2022

Type of change

  • Feature
  • Bugfix
  • Technical
  • Other : unit tests

Content

Adding unit tests to cover code of aggregation process of live location share events.

Motivation and context

Closes #6155

Screenshots / GIFs

Tests

  • Run unit tests

Tested devices

  • Physical
  • Emulator
  • OS version(s):

Checklist

@mnaturel mnaturel marked this pull request as ready for review June 9, 2022 08:17
@mnaturel mnaturel requested review from a team, Florian14, fedrunov and ouchadam and removed request for a team and Florian14 June 9, 2022 08:17
@mnaturel mnaturel changed the title Feature/mna/6155 tests lls aggregation Adding unit tests for live locatiion sharing aggregation code Jun 9, 2022
@mnaturel mnaturel changed the title Adding unit tests for live locatiion sharing aggregation code Adding unit tests for live locatiion sharing aggregation code (PSF-1063) Jun 9, 2022
val event = Event(senderId = A_SENDER_ID)
val beaconLocationData = MessageBeaconLocationDataContent()

val resultNoRelatedEventId = liveLocationAggregationProcessor.handleBeaconLocationData(
Copy link
Contributor

Choose a reason for hiding this comment

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

as we're actioning different inputs no related event id and empty related event id, this feels like two separate tests (similar comment for the test below)

a parameterised test could potentially cover all these validation cases (orrrrr the naive parameterised tests I've started doing like this


for the parameters themselves, are null/empty ids possible? would be nice if we could catch them further up the chain and avoid invalid empty ids

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 agree with your point. I created a loop on a list of ignored values to group the similar tests: see 912244a. I didn't use parametrized tests since I don't like the boilerplate syntax in Junit 4. I am wondering if it is possible to switch to Junit 5 in Android: the syntax for parametrized tests is much nicer.

For you last point, I agree but I didn't change anything since it would require to change the events aggregation process in a more general way.

aggregatedEntity.isActive shouldBeEqualTo true
aggregatedEntity.endOfLiveTimestampMillis shouldBeEqualTo A_TIMESTAMP + A_TIMEOUT_MILLIS
aggregatedEntity.lastLocationContent shouldBeEqualTo null
previousEntities.forEach { entity ->
Copy link
Contributor

@ouchadam ouchadam Jun 9, 2022

Choose a reason for hiding this comment

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

val previousEntities = mockPreviousLiveLocationShareAggregatedSummaryEntities()

previousEntities.forEach { entity ->
  entity.isActive shouldBeEqualTo false
}

it looks like the previousEntities aren't used?

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 fact, they are. They are modified internally by the aggregation process. And here we check the result is correct. I could not find a better way to ensure the aggregation is correct. I am open to any suggestion that could improve that.

savedLocationData?.getBestLocationInfo()?.geoUri shouldBeEqualTo A_GEO_URI
}

private fun mockLiveLocationShareAggregatedSummaryEntityForEvent(
Copy link
Contributor

Choose a reason for hiding this comment

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

it could be helpful to name these helpers with the data they're fetching

givenLastRoomBeaconQueryReturns(roomId, eventId, beaconLocation)
val activeSummary = LiveLocationShareAggregatedSummaryEntity(
  eventId = "ignored-value",
  roomId = A_ROOM_ID,
  userId = A_SENDER_ID,
  isActive = true
)
givenLocationSummaryQueryReturns(activeSummary)

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 clarified the namings here: ed13244

@@ -21,16 +21,59 @@ import io.mockk.mockk
import io.realm.Realm
import io.realm.RealmModel
import io.realm.RealmQuery
import io.realm.RealmResults
import io.realm.kotlin.where

internal class FakeRealm {
Copy link
Contributor

Choose a reason for hiding this comment

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

💯 for updating the fake!

val instance = mockk<WorkManagerProvider>()

init {
val workManager = mockk<WorkManager>()
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we should be explicitly expecting the tests to mock enqueueUniqueWork cancelUniqueWork rather than applying it by default, we can make the test cases more resilient by also verifying against the arguments

class FakeWorkManagerProvider(val fakeWorkManager = FakeWorkManager()) {
    val instance = mockk<WorkManagerProvider>().also {
        every { workManager } returns fakeWorkManager.instance
    }
}

class FakeWorkManager {
    val instance = mockk<WorkManager>()

    fun expectCancelUniqueWork() {
        every { instance.cancelUniqueWork(any()) } returns mockk()
    }

   fun verifyCancelUniqueWork(name: String) {
     verify { instance.cancelUniqueWork(name) }
   }

}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right. I will make an update.

Copy link
Contributor Author

@mnaturel mnaturel Jun 10, 2022

Choose a reason for hiding this comment

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

Updated here: 454209a

@mnaturel mnaturel requested a review from ouchadam June 13, 2022 08:18
Copy link
Contributor

@ouchadam ouchadam left a comment

Choose a reason for hiding this comment

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

💯 thanks for the updates!

@mnaturel mnaturel changed the title Adding unit tests for live locatiion sharing aggregation code (PSF-1063) Adding unit tests for live location sharing aggregation code (PSF-1063) Jun 13, 2022
@mnaturel mnaturel force-pushed the feature/mna/6155-tests-lls-aggregation branch from 912244a to 00a5830 Compare June 13, 2022 12:09
@mnaturel mnaturel requested review from a team and Florian14 and removed request for fedrunov and a team June 15, 2022 09:57
@mnaturel mnaturel force-pushed the feature/mna/6155-tests-lls-aggregation branch from 00a5830 to 12880e0 Compare June 17, 2022 09:45
@mnaturel mnaturel force-pushed the feature/mna/6155-tests-lls-aggregation branch from 12880e0 to ac4b336 Compare June 17, 2022 15:58
@sonarcloud
Copy link

sonarcloud bot commented Jun 17, 2022

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

@mnaturel mnaturel merged commit 539d134 into develop Jun 20, 2022
@mnaturel mnaturel deleted the feature/mna/6155-tests-lls-aggregation branch June 20, 2022 07:19
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.

[SDK] Add unit tests for LiveLocationAggregationProcessor
3 participants