From f15a8587a697d3edf50cb72b8d74f51d2c5b864c Mon Sep 17 00:00:00 2001 From: andresmr Date: Wed, 18 Dec 2024 10:57:09 +0100 Subject: [PATCH] fix: [ANDROAPP-6535] filter in RelationshipsRepository by relationship type Signed-off-by: andresmr --- gradle/libs.versions.toml | 2 +- .../data/EventRelationshipsRepository.kt | 10 ++-- .../data/TrackerRelationshipsRepository.kt | 10 ++-- .../relationships/RelationshipFakeModels.kt | 58 ++++++++++++++++--- .../TrackerRelationshipsRepositoryTest.kt | 47 +++------------ .../domain/GetRelationshipsByTypeTest.kt | 21 ++++--- 6 files changed, 83 insertions(+), 65 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 41f6f3dabd..660f658fc4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,7 +8,7 @@ kotlin = '2.0.20' hilt = '2.47' jacoco = '0.8.10' designSystem = "0.4.1-SNAPSHOT" -dhis2sdk = "1.11.1-20241213.081754-8" +dhis2sdk = "1.11.1-20241218.080719-10" ruleEngine = "3.0.0" expressionParser = "1.1.0" appcompat = "1.6.1" diff --git a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/EventRelationshipsRepository.kt b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/EventRelationshipsRepository.kt index 2e66cb95c4..039755afa0 100644 --- a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/EventRelationshipsRepository.kt +++ b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/EventRelationshipsRepository.kt @@ -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, diff --git a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepository.kt b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepository.kt index 6f1fb596aa..c3b9b922de 100644 --- a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepository.kt +++ b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepository.kt @@ -79,7 +79,7 @@ class TrackerRelationshipsRepository( .blockingGet() val relationships = d2.relationshipModule().relationships() - .getByItem( + .byItem( RelationshipItem.builder() .trackedEntityInstance( RelationshipItemTrackedEntityInstance.builder() @@ -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, diff --git a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/RelationshipFakeModels.kt b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/RelationshipFakeModels.kt index 43d3dafb56..70deaf7309 100644 --- a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/RelationshipFakeModels.kt +++ b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/RelationshipFakeModels.kt @@ -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( @@ -72,4 +75,45 @@ val relationshipSection2 = toGeometry = null, direction = RelationshipDirection.FROM, relationship = Relationship.builder().uid("uid2").build() -) \ No newline at end of file +) + +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() \ No newline at end of file diff --git a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepositoryTest.kt b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepositoryTest.kt index e14dc6d334..3189421f9e 100644 --- a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepositoryTest.kt +++ b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepositoryTest.kt @@ -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 @@ -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() { @@ -85,30 +84,12 @@ 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( @@ -116,18 +97,8 @@ class TrackerRelationshipsRepositoryTest { 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, - ) } \ No newline at end of file diff --git a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt index 027800ff77..05d871f2e5 100644 --- a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt +++ b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt @@ -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( @@ -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 { return listOf( relationshipSection1,