From d54ab58e82a68ba62bcd83145819741419d8e2dc Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Fri, 20 May 2022 14:05:56 +0500 Subject: [PATCH 1/3] Fix child siblings query --- .../java/org/smartregister/child/task/GetSiblingsTask.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java b/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java index 29abc04b0..a31a347e9 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java +++ b/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java @@ -36,11 +36,14 @@ protected ArrayList doInBackground(Void... params) { String motherBaseEntityId = Utils.getValue(childDetails.getColumnmaps(), Constants.KEY.RELATIONAL_ID, false); if (!TextUtils.isEmpty(motherBaseEntityId) && !TextUtils.isEmpty(baseEntityId)) { + String whereClause = " WHERE " + Utils.metadata().getRegisterQueryProvider().getChildDetailsTable() + ".relational_id IN ('" + motherBaseEntityId + "') AND " + + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".date_removed IS NULL AND " + + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".dod IS NULL AND " + + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".is_closed = 0"; List> childList = ChildLibrary.getInstance() .eventClientRepository() .rawQuery(ChildLibrary.getInstance().getRepository().getReadableDatabase(), - Utils.metadata().getRegisterQueryProvider().mainRegisterQuery() - + " WHERE " + Utils.metadata().getRegisterQueryProvider().getChildDetailsTable() + ".relational_id IN ('" + motherBaseEntityId + "')"); + Utils.metadata().getRegisterQueryProvider().mainRegisterQuery() + whereClause ); List children = new ArrayList<>(); From 336a970e9831930e09f62159bffc05f3eabc3665 Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Fri, 20 May 2022 14:06:13 +0500 Subject: [PATCH 2/3] Bump up child version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 04f7dbcbe..d1e01a598 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=0.8.3-SNAPSHOT +VERSION_NAME=0.8.4-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Child Library From c2812f288722701ae0233b431c3d70d48af6362e Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Fri, 20 May 2022 15:25:00 +0500 Subject: [PATCH 3/3] Increase test coverage --- .../child/task/GetSiblingsTask.java | 14 ++++-- .../child/task/GetSiblingsTaskTest.java | 47 +++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 opensrp-child/src/test/java/org/smartregister/child/task/GetSiblingsTaskTest.java diff --git a/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java b/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java index a31a347e9..7b6ab959d 100644 --- a/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java +++ b/opensrp-child/src/main/java/org/smartregister/child/task/GetSiblingsTask.java @@ -36,14 +36,11 @@ protected ArrayList doInBackground(Void... params) { String motherBaseEntityId = Utils.getValue(childDetails.getColumnmaps(), Constants.KEY.RELATIONAL_ID, false); if (!TextUtils.isEmpty(motherBaseEntityId) && !TextUtils.isEmpty(baseEntityId)) { - String whereClause = " WHERE " + Utils.metadata().getRegisterQueryProvider().getChildDetailsTable() + ".relational_id IN ('" + motherBaseEntityId + "') AND " + - Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".date_removed IS NULL AND " + - Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".dod IS NULL AND " + - Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".is_closed = 0"; + String whereClause = constructWhereClause(motherBaseEntityId); List> childList = ChildLibrary.getInstance() .eventClientRepository() .rawQuery(ChildLibrary.getInstance().getRepository().getReadableDatabase(), - Utils.metadata().getRegisterQueryProvider().mainRegisterQuery() + whereClause ); + Utils.metadata().getRegisterQueryProvider().mainRegisterQuery() + whereClause); List children = new ArrayList<>(); @@ -67,6 +64,13 @@ protected ArrayList doInBackground(Void... params) { return null; } + private String constructWhereClause(String motherBaseEntityId) { + return " WHERE " + Utils.metadata().getRegisterQueryProvider().getChildDetailsTable() + ".relational_id IN ('" + motherBaseEntityId + "') AND " + + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".date_removed IS NULL AND " + + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".dod IS NULL AND " + + Utils.metadata().getRegisterQueryProvider().getDemographicTable() + ".is_closed = 0"; + } + @Override protected void onPostExecute(ArrayList baseEntityIds) { super.onPostExecute(baseEntityIds); diff --git a/opensrp-child/src/test/java/org/smartregister/child/task/GetSiblingsTaskTest.java b/opensrp-child/src/test/java/org/smartregister/child/task/GetSiblingsTaskTest.java new file mode 100644 index 000000000..79918602c --- /dev/null +++ b/opensrp-child/src/test/java/org/smartregister/child/task/GetSiblingsTaskTest.java @@ -0,0 +1,47 @@ +package org.smartregister.child.task; + +import static org.junit.Assert.assertEquals; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.child.BaseUnitTest; +import org.smartregister.child.contract.IGetSiblings; +import org.smartregister.child.util.Constants; +import org.smartregister.child.util.DBConstants; +import org.smartregister.commonregistry.CommonPersonObjectClient; + +import java.util.HashMap; + +public class GetSiblingsTaskTest extends BaseUnitTest { + + private GetSiblingsTask getSiblingsTask; + + @Mock + private IGetSiblings getSiblings; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + HashMap details = new HashMap<>(); + details.put(DBConstants.KEY.ZEIR_ID, "123123"); + CommonPersonObjectClient commonPersonObjectClient = new CommonPersonObjectClient("baseEntityId", details, Constants.KEY.CHILD); + getSiblingsTask = new GetSiblingsTask(commonPersonObjectClient, getSiblings); + } + + @Test + public void testConstructorNotNull() { + Assert.assertNotNull(getSiblingsTask); + } + + @Test + public void testConstructWhereClauseFiltersDeadChild() { + String whereClause = ReflectionHelpers.callInstanceMethod(getSiblingsTask, "constructWhereClause", ReflectionHelpers.ClassParameter.from(String.class, "123")); + assertEquals(" WHERE ec_child_details.relational_id IN ('123') AND ec_client.date_removed IS NULL AND ec_client.dod IS NULL AND ec_client.is_closed = 0", whereClause); + } + +}