Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
package com.duckduckgo.app.attributed.metrics.store

import com.duckduckgo.app.attributed.metrics.api.EventStats
import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import javax.inject.Inject

interface EventRepository {
Expand All @@ -40,8 +36,6 @@ interface EventRepository {
class RealEventRepository @Inject constructor(
private val eventDao: EventDao,
private val attributedMetricsDateUtils: AttributedMetricsDateUtils,
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
private val dispatcherProvider: DispatcherProvider,
) : EventRepository {
override suspend fun collectEvent(eventName: String) {
val today = attributedMetricsDateUtils.getCurrentDate()
Expand Down Expand Up @@ -73,8 +67,6 @@ class RealEventRepository @Inject constructor(
}

override suspend fun deleteAllEvents() {
appCoroutineScope.launch(dispatcherProvider.io()) {
eventDao.deleteAllEvents()
}
eventDao.deleteAllEvents()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove app launch to simplify the logic. There's not need for that.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.duckduckgo.app.attributed.metrics.FakeAttributedMetricsDateUtils
import com.duckduckgo.common.test.CoroutineTestRule
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
Expand All @@ -31,7 +29,6 @@ import org.junit.Test
import org.junit.runner.RunWith
import java.time.LocalDate

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class RealEventRepositoryTest {
@get:Rule
Expand All @@ -52,13 +49,10 @@ class RealEventRepositoryTest {
).build()
eventDao = db.eventDao()
testDateProvider = FakeAttributedMetricsDateUtils(LocalDate.of(2025, 10, 3))
repository =
RealEventRepository(
eventDao = eventDao,
attributedMetricsDateUtils = testDateProvider,
appCoroutineScope = coroutineTestRule.testScope,
dispatcherProvider = coroutineTestRule.testDispatcherProvider,
)
repository = RealEventRepository(
eventDao = eventDao,
attributedMetricsDateUtils = testDateProvider,
)
}

@After
Expand Down Expand Up @@ -147,19 +141,15 @@ class RealEventRepositoryTest {
}

@Test
fun whenDeleteAllEventsThenRemoveAllEvents() =
runTest {
// Setup data
eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-10-03"))
eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-10-02"))
eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-09-03"))

repository.deleteAllEvents()
fun whenDeleteAllEventsThenRemoveAllEvents() = runTest {
// Setup data
eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-10-03"))
eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-10-02"))
eventDao.insertEvent(EventEntity("test_event", count = 1, day = "2025-09-03"))

// Wait for the background coroutine to complete
advanceUntilIdle()
repository.deleteAllEvents()

val remainingEvents = eventDao.getEventsByNameAndTimeframe("test_event", "2025-09-03", "2025-10-03")
assert(remainingEvents.isEmpty())
}
val remainingEvents = eventDao.getEventsByNameAndTimeframe("test_event", "2025-09-03", "2025-10-03")
assert(remainingEvents.isEmpty())
}
}
Loading