Skip to content

Commit

Permalink
fix: [ANDROAPP-6535] filter in RelationshipsRepository by relationshi…
Browse files Browse the repository at this point in the history
…p type

Signed-off-by: andresmr <[email protected]>
  • Loading branch information
andresmr authored and Balcan committed Dec 23, 2024
1 parent 44fc4b6 commit e2a449e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ class EventRelationshipsRepository(
.blockingGet()

val relationships = d2.relationshipModule().relationships()
.getByItem(
.byItem(
RelationshipItem.builder()
.event(
RelationshipItemEvent.builder().event(eventUid).build(),
).relationshipItemType(constraintType)
.build(),
).filter {
it.relationshipType() == relationshipSection.uid
}.mapNotNull { relationship ->
).byRelationshipType().eq(relationshipSection.uid)
.byDeleted().isFalse
.withItems()
.blockingGet()
.mapNotNull { relationship ->
mapToRelationshipModel(
relationship = relationship,
relationshipType = relationshipType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class TrackerRelationshipsRepository(
.blockingGet()

val relationships = d2.relationshipModule().relationships()
.getByItem(
.byItem(
RelationshipItem.builder()
.trackedEntityInstance(
RelationshipItemTrackedEntityInstance.builder()
Expand All @@ -88,9 +88,11 @@ class TrackerRelationshipsRepository(
).relationshipItemType(constraintType)
.build(),
)
.filter {
it.relationshipType() == relationshipSection.uid
}.mapNotNull { relationship ->
.byRelationshipType().eq(relationshipSection.uid)
.byDeleted().isFalse
.withItems()
.blockingGet()
.mapNotNull { relationship ->
mapToRelationshipModel(
relationship = relationship,
relationshipType = relationshipType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@ import org.dhis2.tracker.relationships.model.RelationshipDirection
import org.dhis2.tracker.relationships.model.RelationshipModel
import org.dhis2.tracker.relationships.model.RelationshipOwnerType
import org.dhis2.tracker.relationships.model.RelationshipSection
import org.hisp.dhis.android.core.common.ObjectWithUid
import org.hisp.dhis.android.core.relationship.Relationship
import org.hisp.dhis.android.core.relationship.RelationshipConstraint
import org.hisp.dhis.android.core.relationship.RelationshipType
import org.mockito.kotlin.mock
import java.util.Date

val relationshipSection1 = RelationshipSection(
uid = "uid1",
title = "Relationship title 1",
uid = "relationshipTypeUid1",
title = "RelationshipType1 FROM",
relationships = emptyList(),
side = RelationshipConstraintSide.FROM,
entityToAdd = null,
entityToAdd = "trackedEntityType2",
)

val relationshipSection2 =
RelationshipSection(
uid = "uid2",
title = "Relationship title 2",
uid = "relationshipTypeUid2",
title = "RelationshipType2 FROM",
relationships = emptyList(),
side = RelationshipConstraintSide.FROM,
entityToAdd = null,
entityToAdd = "trackedEntityType2",
)

val relationshipModel1 = RelationshipModel(
Expand Down Expand Up @@ -72,4 +75,45 @@ val relationshipSection2 =
toGeometry = null,
direction = RelationshipDirection.FROM,
relationship = Relationship.builder().uid("uid2").build()
)
)

val relationshipTypeTeiToTei = RelationshipType.builder()
.uid("relationshipTypeUid1")
.fromToName("RelationshipType1 FROM")
.toFromName("RelationshipType1 TO")
.displayName("Tei to Tei relationship")
.fromConstraint(
RelationshipConstraint.builder()
.trackedEntityType(
ObjectWithUid.create("trackedEntityType1")
).build()
)
.toConstraint(
RelationshipConstraint.builder()
.trackedEntityType(
ObjectWithUid.create("trackedEntityType2")
)
.build()
)
.build()

val relationshipTypeEventToTei = RelationshipType.builder()
.uid("relationshipTypeUid2")
.fromToName("RelationshipType2 FROM")
.toFromName("RelationshipType2 TO")
.displayName("Event to Tei relationship")
.fromConstraint(
RelationshipConstraint.builder()
.programStage(
ObjectWithUid.create("programStageUid")
)
.build()
)
.toConstraint(
RelationshipConstraint.builder()
.trackedEntityType(
ObjectWithUid.create("trackedEntityType2")
)
.build()
)
.build()
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import org.dhis2.commons.resources.ResourceManager
import org.dhis2.tracker.data.ProfilePictureProvider
import org.dhis2.tracker.relationships.relationshipSection1
import org.dhis2.tracker.relationships.relationshipSection2
import org.dhis2.tracker.relationships.relationshipTypeEventToTei
import org.dhis2.tracker.relationships.relationshipTypeTeiToTei
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.common.ObjectWithUid
import org.hisp.dhis.android.core.enrollment.Enrollment
import org.hisp.dhis.android.core.relationship.RelationshipConstraint
import org.hisp.dhis.android.core.relationship.RelationshipConstraintType
import org.hisp.dhis.android.core.relationship.RelationshipType
import org.hisp.dhis.android.core.relationship.RelationshipTypeWithEntitySide
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance
import org.junit.Assert.assertEquals
Expand All @@ -34,7 +33,7 @@ class TrackerRelationshipsRepositoryTest {

private val teiUid = "teiUid"
private val enrollmentUid = "enrollmentUid"
private val trackedEntityType = "trackedEntityType"
private val trackedEntityType = "trackedEntityType1"

@Before
fun setup() {
Expand Down Expand Up @@ -85,49 +84,21 @@ class TrackerRelationshipsRepositoryTest {


//Then a relationshipSectionList is returned
assertEquals(relationshipTypes, expectedResult)
}



private val relationshipTypeTeiToTei = RelationshipType.builder()
.uid("relationshipTypeUid1")
.fromToName("RelationshipType1 FROM")
.toFromName("RelationshipType1 TO")
.displayName("Tei to Tei relationship")
.fromConstraint(
RelationshipConstraint.builder()
.trackedEntityType(
ObjectWithUid.create(trackedEntityType)
).build()
val expectedResult = listOf(
relationshipSection1,
relationshipSection2,
)
.toConstraint(
RelationshipConstraint.builder()
.trackedEntityType(
ObjectWithUid.create("trackedEntityTypeUid2")
)
.build()
)
.build()
assertEquals(expectedResult, relationshipTypes)
}

private val relationshipWithEntitySideList = listOf(
RelationshipTypeWithEntitySide(
relationshipType = relationshipTypeTeiToTei,
entitySide = RelationshipConstraintType.FROM
),
RelationshipTypeWithEntitySide(
relationshipType = RelationshipType.builder()
.uid("relationshipTypeUid2")
.fromToName("RelationshipType2 FROM")
.toFromName("RelationshipType2 TO")
.displayName("relationshipType2")
.build(),
relationshipType = relationshipTypeEventToTei,
entitySide = RelationshipConstraintType.FROM
)
)

private val expectedResult = listOf(
relationshipSection1,
relationshipSection2,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class GetRelationshipsByTypeTest {

@Test
fun `invoke should return relationship sections grouped by type`() = runTest {

//Given a list of relationship types and relationships
whenever(relationshipsRepository.getRelationshipTypes()) doReturn getRelationshipSectionsMock()
whenever(
Expand All @@ -52,22 +53,20 @@ class GetRelationshipsByTypeTest {

// When calling the use case to get the relationships grouped by type
val result = getRelationshipsByType()
assertEquals(expectedResult, result)


// Then a list of RelationshipSections with their relationships should be returned
val expectedResult = listOf(
relationshipSection1.copy(
relationships = listOf(relationshipModel1),
),
relationshipSection2.copy(
relationships = listOf(relationshipModel2),
)
)
assertEquals(expectedResult, result)

}

private val expectedResult = listOf(
relationshipSection1.copy(
relationships = listOf(relationshipModel1),
),
relationshipSection2.copy(
relationships = listOf(relationshipModel2),
)
)

private fun getRelationshipSectionsMock(): List<RelationshipSection> {
return listOf(
relationshipSection1,
Expand Down

0 comments on commit e2a449e

Please sign in to comment.