Skip to content

Commit

Permalink
fix: [ANDROAPP-6535] Add AddRelationshipTest
Browse files Browse the repository at this point in the history
Signed-off-by: andresmr <[email protected]>
  • Loading branch information
andresmr committed Dec 12, 2024
1 parent b1a4ec1 commit be4c940
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.dhis2.tracker.relationships.domain

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.runTest
import org.dhis2.commons.viewmodel.DispatcherProvider
import org.dhis2.tracker.relationships.data.RelationshipsRepository
import org.dhis2.tracker.relationships.model.RelationshipDirection
import org.hisp.dhis.android.core.relationship.Relationship
import org.junit.Before
import org.junit.Test
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever

class AddRelationshipTest {

private lateinit var addRelationship: AddRelationship

private val dispatcherProvider: DispatcherProvider = mock {
on { io() } doReturn Dispatchers.Unconfined
}
private val repository: RelationshipsRepository = mock()

@Before
fun setup() {
addRelationship = AddRelationship(
dispatcher = dispatcherProvider,
repository = repository
)
}

@Test
fun shouldAddRelationship() = runTest {
// Given user tries to add a relationship
val selectedTeiUid = "selectedTeiUid"
val relationshipTypeUid = "relationshipTypeUid"
val direction = RelationshipDirection.TO

val relationship = Relationship.builder()
.uid("relationshipUid")
.build()

// When
whenever(
repository.createRelationship(selectedTeiUid, relationshipTypeUid, direction)
) doReturn relationship
whenever(
repository.addRelationship(relationship)
) doReturn Result.success("relationshipUid")

val result = addRelationship(selectedTeiUid, relationshipTypeUid, direction)

// Then
assert(result.isSuccess)
}

@Test
fun shouldFailWhenAddRelationship() = runTest {
// Given user tries to add a relationship
val selectedTeiUid = "selectedTeiUid"
val relationshipTypeUid = "relationshipTypeUid"
val direction = RelationshipDirection.TO

val relationship = Relationship.builder()
.uid("relationshipUid")
.build()

// When
whenever(
repository.createRelationship(selectedTeiUid, relationshipTypeUid, direction)
) doReturn relationship
whenever(
repository.addRelationship(relationship)
) doReturn Result.failure(Exception("Failed to add relationship"))

val result = addRelationship(selectedTeiUid, relationshipTypeUid, direction)

// Then there is an error when adding relationship
assert(result.isFailure)
assert(result.exceptionOrNull()?.message == "Failed to add relationship")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import java.util.Date

class GetRelationshipsByTypeTest {

lateinit var getRelationshipsByType: GetRelationshipsByType
private lateinit var getRelationshipsByType: GetRelationshipsByType

private val relationshipsRepository: RelationshipsRepository = mock()
private val dateLabelProvider: DateLabelProvider = mock()
Expand Down

0 comments on commit be4c940

Please sign in to comment.