Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Apr 17, 2024
1 parent 8e018f6 commit b168471
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.mullvad.mullvadvpn.relaylist

import io.mockk.mockk
import io.mockk.unmockkAll
import net.mullvad.mullvadvpn.model.Ownership
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
Expand All @@ -21,13 +22,17 @@ class RelayNameComparatorTest {
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relay10 =
RelayItem.Relay(
name = "se10-wireguard",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)

relay9 assertOrderBothDirection relay10
Expand All @@ -41,13 +46,17 @@ class RelayNameComparatorTest {
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relay9b =
RelayItem.Relay(
name = "se9-wireguard",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)

assertTrue(RelayNameComparator.compare(relay9a, relay9b) == 0)
Expand All @@ -57,13 +66,41 @@ class RelayNameComparatorTest {
@Test
fun `comparator should be able to handle name of only numbers`() {
val relay001 =
RelayItem.Relay(name = "001", location = mockk(), locationName = "mock", active = false)
RelayItem.Relay(
name = "001",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relay1 =
RelayItem.Relay(name = "1", location = mockk(), locationName = "mock", active = false)
RelayItem.Relay(
name = "1",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relay3 =
RelayItem.Relay(name = "3", location = mockk(), locationName = "mock", active = false)
RelayItem.Relay(
name = "3",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relay100 =
RelayItem.Relay(name = "100", location = mockk(), locationName = "mock", active = false)
RelayItem.Relay(
name = "100",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)

relay001 assertOrderBothDirection relay1
relay001 assertOrderBothDirection relay3
Expand All @@ -79,13 +116,17 @@ class RelayNameComparatorTest {
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relay9b =
RelayItem.Relay(
name = "se-wireguard",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)

assertTrue(RelayNameComparator.compare(relay9a, relay9b) == 0)
Expand All @@ -100,13 +141,17 @@ class RelayNameComparatorTest {
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relay005 =
RelayItem.Relay(
name = "se005-wireguard",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)

relay001 assertOrderBothDirection relay005
Expand All @@ -120,27 +165,35 @@ class RelayNameComparatorTest {
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relayAr8 =
RelayItem.Relay(
name = "ar8-wireguard",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relaySe5 =
RelayItem.Relay(
name = "se5-wireguard",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relaySe10 =
RelayItem.Relay(
name = "se10-wireguard",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)

relayAr2 assertOrderBothDirection relayAr8
Expand All @@ -155,14 +208,18 @@ class RelayNameComparatorTest {
name = "se2-cloud",
location = mockk(),
locationName = "mock",
active = false
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relay2w =
RelayItem.Relay(
name = "se2-wireguard",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)

relay2c assertOrderBothDirection relay2w
Expand All @@ -175,14 +232,18 @@ class RelayNameComparatorTest {
name = "se22",
location = mockk(),
locationName = "mock",
active = false
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
val relay22b =
RelayItem.Relay(
name = "se22-wireguard",
location = mockk(),
locationName = "mock",
active = false,
providerName = "Provider",
ownership = Ownership.MullvadOwned
)

relay22a assertOrderBothDirection relay22b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class CustomListActionUseCaseTest {
listOf(locationCode)
)
} returns UpdateCustomListResult.Ok
coEvery { mockRelayListUseCase.relayList() } returns flowOf(mockLocations)
coEvery { mockRelayListUseCase.fullRelayList() } returns flowOf(mockLocations)
every { mockLocations.getRelayItemsByCodes(listOf(locationCode)) } returns mockLocations

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.mullvad.mullvadvpn.compose.communication.CustomListResult
import net.mullvad.mullvadvpn.compose.state.CustomListLocationsUiState
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.model.GeographicLocationConstraint
import net.mullvad.mullvadvpn.model.Ownership
import net.mullvad.mullvadvpn.relaylist.RelayItem
import net.mullvad.mullvadvpn.relaylist.descendants
import net.mullvad.mullvadvpn.usecase.RelayListUseCase
Expand All @@ -31,7 +32,7 @@ class CustomListLocationsViewModelTest {

@BeforeEach
fun setup() {
every { mockRelayListUseCase.relayList() } returns relayListFlow
every { mockRelayListUseCase.fullRelayList() } returns relayListFlow
every { mockRelayListUseCase.customLists() } returns customListFlow
}

Expand Down Expand Up @@ -283,7 +284,9 @@ class CustomListLocationsViewModelTest {
"SE",
"GBG",
"gbg-1"
)
),
providerName = "Provider",
ownership = Ownership.MullvadOwned
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SelectLocationViewModelTest {
private val mockServiceConnectionManager: ServiceConnectionManager = mockk()
private lateinit var viewModel: SelectLocationViewModel
private val relayListWithSelectionFlow =
MutableStateFlow(RelayList(emptyList(), emptyList(), null))
MutableStateFlow(RelayList(emptyList(), emptyList(), emptyList(), null))
private val mockRelayListUseCase: RelayListUseCase = mockk()
private val mockCustomListActionUseCase: CustomListActionUseCase = mockk(relaxed = true)
private val selectedOwnership = MutableStateFlow<Constraint<Ownership>>(Constraint.Any())
Expand Down Expand Up @@ -93,10 +93,11 @@ class SelectLocationViewModelTest {
fun `given relayListWithSelection emits update uiState should contain new update`() = runTest {
// Arrange
val mockCountries = listOf<RelayItem.Country>(mockk(), mockk())
val mockCustomList = listOf<RelayItem.CustomList>(mockk())
val mockCustomList = listOf<RelayItem.CustomList>(mockk(relaxed = true))
val selectedItem: RelayItem = mockk()
every { mockCountries.filterOnSearchTerm(any(), selectedItem) } returns mockCountries
relayListWithSelectionFlow.value = RelayList(mockCustomList, mockCountries, selectedItem)
relayListWithSelectionFlow.value =
RelayList(mockCustomList, mockCountries, mockCountries, selectedItem)

// Act, Assert
viewModel.uiState.test {
Expand All @@ -111,12 +112,12 @@ class SelectLocationViewModelTest {
fun `given relayListWithSelection emits update with no selections selectedItem should be null`() =
runTest {
// Arrange
val mockCustomList = listOf<RelayItem.CustomList>(mockk())
val mockCustomList = listOf<RelayItem.CustomList>(mockk(relaxed = true))
val mockCountries = listOf<RelayItem.Country>(mockk(), mockk())
val selectedItem: RelayItem? = null
every { mockCountries.filterOnSearchTerm(any(), selectedItem) } returns mockCountries
relayListWithSelectionFlow.value =
RelayList(mockCustomList, mockCountries, selectedItem)
RelayList(mockCustomList, mockCountries, mockCountries, selectedItem)

// Act, Assert
viewModel.uiState.test {
Expand Down Expand Up @@ -155,15 +156,16 @@ class SelectLocationViewModelTest {
@Test
fun `on onSearchTermInput call uiState should emit with filtered countries`() = runTest {
// Arrange
val mockCustomList = listOf<RelayItem.CustomList>(mockk())
val mockCustomList = listOf<RelayItem.CustomList>(mockk(relaxed = true))
val mockCountries = listOf<RelayItem.Country>(mockk(), mockk())
val selectedItem: RelayItem? = null
val mockRelayList: List<RelayItem.Country> = mockk(relaxed = true)
val mockSearchString = "SEARCH"
every { mockRelayList.filterOnSearchTerm(mockSearchString, selectedItem) } returns
mockCountries
every { mockCustomList.filterOnSearchTerm(mockSearchString) } returns mockCustomList
relayListWithSelectionFlow.value = RelayList(mockCustomList, mockRelayList, selectedItem)
relayListWithSelectionFlow.value =
RelayList(mockCustomList, mockRelayList, mockRelayList, selectedItem)

// Act, Assert
viewModel.uiState.test {
Expand All @@ -184,15 +186,16 @@ class SelectLocationViewModelTest {
@Test
fun `when onSearchTermInput returns empty result uiState should return empty list`() = runTest {
// Arrange
val mockCustomList = listOf<RelayItem.CustomList>(mockk())
val mockCustomList = listOf<RelayItem.CustomList>(mockk(relaxed = true))
val mockCountries = emptyList<RelayItem.Country>()
val selectedItem: RelayItem? = null
val mockRelayList: List<RelayItem.Country> = mockk(relaxed = true)
val mockSearchString = "SEARCH"
every { mockRelayList.filterOnSearchTerm(mockSearchString, selectedItem) } returns
mockCountries
every { mockCustomList.filterOnSearchTerm(mockSearchString) } returns mockCustomList
relayListWithSelectionFlow.value = RelayList(mockCustomList, mockRelayList, selectedItem)
relayListWithSelectionFlow.value =
RelayList(mockCustomList, mockRelayList, mockRelayList, selectedItem)

// Act, Assert
viewModel.uiState.test {
Expand Down

0 comments on commit b168471

Please sign in to comment.