-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: [ANDROAPP-6535] Add AddRelationshipTest
Signed-off-by: andresmr <[email protected]>
- Loading branch information
Showing
2 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/AddRelationshipTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters