Skip to content

Commit

Permalink
[#21] Add platform date test
Browse files Browse the repository at this point in the history
  • Loading branch information
blyscuit committed Dec 16, 2022
1 parent ccccccf commit 1a0b08c
Show file tree
Hide file tree
Showing 30 changed files with 147 additions and 98 deletions.
3 changes: 2 additions & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ kover {
"*Mock*",
"co.nimblehq.blisskmmic.di*",
"*helpers.extensions.ios*",
"SharedBuildConfig*"
"SharedBuildConfig*",
"*domain.platform.*"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.nimblehq.blisskmmic.data.repository

import co.nimblehq.blisskmmic.domain.model.DateComponents
import co.nimblehq.blisskmmic.domain.model.DateComponent
import co.nimblehq.blisskmmic.domain.repository.DeviceInfoRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
Expand All @@ -10,12 +10,10 @@ class DeviceInfoRepositoryImpl(
private val clock: Clock
): DeviceInfoRepository {

override fun getCurrentDate(): Flow<DateComponents> {
val datetimeInSystemZone = clock.now().toLocalDateTime(TimeZone.currentSystemDefault())
val dateHeader = DateComponents(
datetimeInSystemZone.dayOfMonth,
datetimeInSystemZone.monthNumber,
datetimeInSystemZone.dayOfWeek.isoDayNumber
override fun getCurrentDate(): Flow<DateComponent> {
val now = clock.now()
val dateHeader = DateComponent(
now.epochSeconds
)
return flow {
emit(dateHeader)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package co.nimblehq.blisskmmic.domain.model

data class DateComponent (
val timeInterval: Long
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package co.nimblehq.blisskmmic.domain.repository

import co.nimblehq.blisskmmic.domain.model.DateComponents
import co.nimblehq.blisskmmic.domain.model.DateComponent
import kotlinx.coroutines.flow.Flow

interface DeviceInfoRepository {

fun getCurrentDate(): Flow<DateComponents>
fun getCurrentDate(): Flow<DateComponent>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package co.nimblehq.blisskmmic.domain.usecase

import co.nimblehq.blisskmmic.domain.model.DateComponents
import co.nimblehq.blisskmmic.domain.model.DateReadable
import co.nimblehq.blisskmmic.domain.model.DateComponent
import co.nimblehq.blisskmmic.domain.repository.DeviceInfoRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand All @@ -10,25 +9,15 @@ import kotlinx.datetime.Month

interface GetCurrentDateUseCase {

operator fun invoke(): Flow<DateReadable>
operator fun invoke(): Flow<DateComponent>
}

class GetCurrentDateUseCaseImpl(
private val deviceInfoRepository: DeviceInfoRepository
) : GetCurrentDateUseCase {

override operator fun invoke(): Flow<DateReadable> {
override operator fun invoke(): Flow<DateComponent> {
return deviceInfoRepository
.getCurrentDate()
.map {
// Ideally should be mapped with localize, but with date-time is sufficient
val dayOfWeek = DayOfWeek(it.dayOfWeek)
val month = Month(it.month)
DateReadable(
it.day,
month.name,
dayOfWeek.name
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LocalDataSourceTest: TestsWithMocks() {
}

@Test
fun `When saving token, it saves the correct key and value to dataStore`() = runTest {
fun `When saving token- it saves the correct key and value to dataStore`() = runTest {
mocker.every {
dataStore.save(
TokenDatabaseModel.serializer(),
Expand All @@ -53,7 +53,7 @@ class LocalDataSourceTest: TestsWithMocks() {
}

@Test
fun `When calling getToken, dataStore returns correct value`() = runTest {
fun `When calling getToken- dataStore returns correct value`() = runTest {
mocker.every {
dataStore.get(
TokenDatabaseModel.serializer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NetworkDataSourceTest {
// Log in

@Test
fun `When calling log in with success response, it returns correct object`() = runTest {
fun `When calling log in with success response- it returns correct object`() = runTest {
val engine = jsonMockEngine(LOG_IN_JSON_RESULT, "oauth/token")
val networkClient = NetworkClient(engine = engine)
val dataSource = NetworkDataSourceImpl(networkClient)
Expand All @@ -36,7 +36,7 @@ class NetworkDataSourceTest {
}

@Test
fun `When calling log in with failure response, it returns correct error`() = runTest {
fun `When calling log in with failure response- it returns correct error`() = runTest {
val engine = jsonMockEngine(ERROR_JSON_RESULT, "oauth/token")
val networkClient = NetworkClient(engine = engine)
val dataSource = NetworkDataSourceImpl(networkClient)
Expand All @@ -53,7 +53,7 @@ class NetworkDataSourceTest {
// Reset password

@Test
fun `When calling reset password with success response, it returns correct object`() = runTest {
fun `When calling reset password with success response- it returns correct object`() = runTest {
val engine = jsonMockEngine(RESET_PASSWORD_JSON_RESULT, "passwords")
val networkClient = NetworkClient(engine = engine)
val dataSource = NetworkDataSourceImpl(networkClient)
Expand All @@ -70,7 +70,7 @@ class NetworkDataSourceTest {
// Survey

@Test
fun `When calling survey with success response, it returns correct object`() = runTest {
fun `When calling survey with success response- it returns correct object`() = runTest {
val engine = jsonMockEngine(SURVEY_LIST_JSON_RESULT, "surveys")
val networkClient = NetworkClient(engine = engine)
val dataSource = NetworkDataSourceImpl(networkClient)
Expand All @@ -86,7 +86,7 @@ class NetworkDataSourceTest {
}

@Test
fun `When calling survey with failure response, it returns correct error`() = runTest {
fun `When calling survey with failure response- it returns correct error`() = runTest {
val engine = jsonMockEngine(ERROR_JSON_RESULT, "surveys")
val networkClient = NetworkClient(engine = engine)
val dataSource = NetworkDataSourceImpl(networkClient)
Expand All @@ -103,7 +103,7 @@ class NetworkDataSourceTest {
// Profile

@Test
fun `When calling profile with success response, it returns correct object`() = runTest {
fun `When calling profile with success response- it returns correct object`() = runTest {
val engine = jsonMockEngine(USER_PROFILE_JSON_RESULT, "me")
val networkClient = NetworkClient(engine = engine)
val dataSource = NetworkDataSourceImpl(networkClient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DataStoreTest : TestsWithMocks() {
}

@Test
fun `When saving a token, it sets settings with the correct key and value`() = runTest {
fun `When saving a token- it sets settings with the correct key and value`() = runTest {
val dataStore = DataStoreImpl(settings)
dataStore.save(
TokenDatabaseModel.serializer(),
Expand All @@ -53,7 +53,7 @@ class DataStoreTest : TestsWithMocks() {
}

@Test
fun `When retrieving the token, it gets the correct key and value from settings`() = runTest {
fun `When retrieving the token- it gets the correct key and value from settings`() = runTest {
settings.encodeValue(TokenDatabaseModel.serializer(), testKey, token)

val dataStore = DataStoreImpl(settings)
Expand All @@ -65,7 +65,7 @@ class DataStoreTest : TestsWithMocks() {
}

@Test
fun `When getting a key with no saved value, it returns the correct error`() = runTest {
fun `When getting a key with no saved value- it returns the correct error`() = runTest {
val dataStore = DataStoreImpl(settings)

dataStore.get(TokenDatabaseModel.serializer(), testKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class NetworkClientTest {
}

@Test
fun `when calling fetch, it returns correct object`() = runTest {
fun `when calling fetch- it returns correct object`() = runTest {
val engine = jsonMockEngine(NETWORK_MOCK_MODEL_RESULT, path)
val networkClient = NetworkClient(engine = engine)
networkClient
Expand All @@ -42,7 +42,7 @@ class NetworkClientTest {
}

@Test
fun `when calling fetchWithMeta, it returns correct object`() = runTest {
fun `when calling fetchWithMeta- it returns correct object`() = runTest {
val engine = jsonMockEngine(NETWORK_META_MOCK_MODEL_RESULT, path)
val networkClient = NetworkClient(engine = engine)
networkClient
Expand All @@ -56,7 +56,7 @@ class NetworkClientTest {
}

@Test
fun `when calling fetch with invalid path, it returns correct object`() = runTest {
fun `when calling fetch with invalid path- it returns correct object`() = runTest {
val engine = jsonMockEngine(NETWORK_MOCK_MODEL_RESULT, "")
val networkClient = NetworkClient(engine = engine)
networkClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TokenizedNetworkClientTest {
}

@Test
fun `when calling fetch, it returns correct object`() = runTest {
fun `when calling fetch- it returns correct object`() = runTest {
mocker.every {
localDataSource.getToken()
} returns flowOf(token)
Expand All @@ -67,7 +67,7 @@ class TokenizedNetworkClientTest {
}

@Test
fun `when calling fetchWithMeta, it returns correct object`() = runTest {
fun `when calling fetchWithMeta- it returns correct object`() = runTest {
mocker.every {
localDataSource.getToken()
} returns flowOf(token)
Expand All @@ -88,7 +88,7 @@ class TokenizedNetworkClientTest {
}

@Test
fun `when calling fetch with incorrect token, it returns correct error`() = runTest {
fun `when calling fetch with incorrect token- it returns correct error`() = runTest {
mocker.every {
localDataSource.getToken()
} returns flowOf(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AccountRecoveryRepositoryTest {
}

@Test
fun `When calling reset with success response, it returns correct object`() = runTest {
fun `When calling reset with success response- it returns correct object`() = runTest {
mocker.every {
networkDataSource.resetPassword(isAny())
} returns flow { emit(resetPasswordMeta) }
Expand All @@ -44,7 +44,7 @@ class AccountRecoveryRepositoryTest {
}

@Test
fun `When calling reset with failure response, it returns correct error`() = runTest {
fun `When calling reset with failure response- it returns correct error`() = runTest {
mocker.every {
networkDataSource.resetPassword(isAny())
} returns flow { error("Fail") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AuthenticationRepositoryTest: TestsWithMocks() {
}

@Test
fun `When calling log in with success response, it returns correct object`() = runTest {
fun `When calling log in with success response- it returns correct object`() = runTest {
mocker.every {
networkDataSource.logIn(isAny())
} returns flow { emit(token) }
Expand All @@ -53,7 +53,7 @@ class AuthenticationRepositoryTest: TestsWithMocks() {
}

@Test
fun `When calling log in with failure response, it returns correct error`() = runTest {
fun `When calling log in with failure response- it returns correct error`() = runTest {
mocker.every {
networkDataSource.logIn(isAny())
} returns flow { error("Fail") }
Expand All @@ -68,7 +68,7 @@ class AuthenticationRepositoryTest: TestsWithMocks() {
}

@Test
fun `When logging in successfully, it saves data to localDataSource`() = runTest {
fun `When logging in successfully- it saves data to localDataSource`() = runTest {
var saveCount = 0
mocker.every {
networkDataSource.logIn(isAny())
Expand All @@ -87,7 +87,7 @@ class AuthenticationRepositoryTest: TestsWithMocks() {
}

@Test
fun `When saving a token, it stores the correct key and value to localDataSource`() = runTest {
fun `When saving a token- it stores the correct key and value to localDataSource`() = runTest {
var saveCount = 0
mocker.every {
localDataSource.save(tokenDB)
Expand All @@ -103,7 +103,7 @@ class AuthenticationRepositoryTest: TestsWithMocks() {
}

@Test
fun `When calling getToken, it returns the correct value`() = runTest {
fun `When calling getToken- it returns the correct value`() = runTest {
mocker.every {
localDataSource.getToken()
} returns flow { emit(tokenDB) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import org.kodein.mock.UsesMocks
import kotlin.test.BeforeTest
import kotlin.test.Test

private const val TIME: Long = 100_000

@UsesMocks(Clock::class)
@ExperimentalCoroutinesApi
class DeviceInfoRepositoryTest {


private val mocker = Mocker()
private val clock = MockClock(mocker)
private val instant = Instant.DISTANT_PAST
private val instant = Instant.fromEpochSeconds(TIME)
private val deviceInfoRepository = DeviceInfoRepositoryImpl(clock)

@BeforeTest
Expand All @@ -25,18 +28,14 @@ class DeviceInfoRepositoryTest {
}

@Test
fun `When calling profile with success response, it returns correct object`() = runTest {
fun `When calling profile with success response- it returns correct object`() = runTest {
mocker.every {
clock.now()
} returns instant
deviceInfoRepository
.getCurrentDate()
.test {
val item = awaitItem()
val local = instant.toLocalDateTime(TimeZone.currentSystemDefault())
item.day shouldBe local.dayOfMonth
item.month shouldBe local.monthNumber
item.dayOfWeek shouldBe local.dayOfWeek.isoDayNumber
awaitItem().timeInterval shouldBe TIME
awaitComplete()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SurveyRepositoryTest: TestsWithMocks() {
}

@Test
fun `When calling survey with success response, it returns correct object`() = runTest {
fun `When calling survey with success response- it returns correct object`() = runTest {
mocker.every {
networkDataSource.survey(isAny())
} returns flowOf(Pair(listOf(survey), meta))
Expand All @@ -50,7 +50,7 @@ class SurveyRepositoryTest: TestsWithMocks() {
}

@Test
fun `When calling survey with failure response, it returns correct error`() = runTest {
fun `When calling survey with failure response- it returns correct error`() = runTest {
mocker.every {
networkDataSource.survey(isAny())
} returns flow { error("Fail") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class UserRepositoryTest {
}

@Test
fun `When calling getProfile with success response, it returns correct object`() = runTest {
fun `When calling getProfile with success response- it returns correct object`() = runTest {
mocker.every {
networkDataSource.profile(isAny())
} returns flowOf(user)
Expand All @@ -47,7 +47,7 @@ class UserRepositoryTest {
}

@Test
fun `When calling getProfile with failure response, it returns correct error`() = runTest {
fun `When calling getProfile with failure response- it returns correct error`() = runTest {
mocker.every {
networkDataSource.profile(isAny())
} returns flow { error("Fail") }
Expand Down
Loading

0 comments on commit 1a0b08c

Please sign in to comment.