Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix child siblings query #298

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ protected ArrayList<String> doInBackground(Void... params) {
String motherBaseEntityId = Utils.getValue(childDetails.getColumnmaps(), Constants.KEY.RELATIONAL_ID, false);
if (!TextUtils.isEmpty(motherBaseEntityId) && !TextUtils.isEmpty(baseEntityId)) {

String whereClause = constructWhereClause(motherBaseEntityId);
List<HashMap<String, String>> 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<CommonPersonObject> children = new ArrayList<>();
Expand All @@ -64,6 +64,13 @@ protected ArrayList<String> 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<String> baseEntityIds) {
super.onPostExecute(baseEntityIds);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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);
}

}