Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-vd committed Feb 24, 2023
1 parent aff9f4d commit a02535d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ public static boolean childBelongsToCurrentFacility(@NotNull Map<String, String>
public static boolean isChildTemporaryOOC(@NotNull Map<String, String> childDetails) {
return !childBelongsToCurrentFacility(childDetails) && (ChildLibrary.getInstance()
.getProperties().isTrue(ChildAppProperties.KEY.NOVEL.OUT_OF_CATCHMENT)
&& Boolean.valueOf(org.smartregister.util.Utils.getValue(childDetails, Constants.Client.IS_OUT_OF_CATCHMENT, false)));
&& Boolean.parseBoolean(org.smartregister.util.Utils.getValue(childDetails, Constants.Client.IS_OUT_OF_CATCHMENT, false)));
}

public static String getLocationIdFromChildTempOOCEvent(String baseEntityId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.smartregister.child.utils;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;

import android.app.Activity;
import android.graphics.Typeface;
import android.view.View;
Expand All @@ -11,6 +15,8 @@

import com.google.common.collect.ImmutableMap;

import net.sqlcipher.database.SQLiteDatabase;

import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -33,8 +39,11 @@
import org.smartregister.child.ChildLibrary;
import org.smartregister.child.R;
import org.smartregister.child.domain.ChildMetadata;
import org.smartregister.child.provider.RegisterQueryProvider;
import org.smartregister.child.util.ChildAppProperties;
import org.smartregister.child.util.Constants;
import org.smartregister.child.util.Utils;
import org.smartregister.domain.db.EventClient;
import org.smartregister.growthmonitoring.domain.Height;
import org.smartregister.growthmonitoring.domain.HeightWrapper;
import org.smartregister.growthmonitoring.domain.Weight;
Expand All @@ -46,13 +55,15 @@
import org.smartregister.immunization.domain.Vaccine;
import org.smartregister.immunization.repository.VaccineRepository;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.repository.EventClientRepository;
import org.smartregister.repository.Repository;
import org.smartregister.util.AppProperties;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@PrepareForTest({VaccineRepo.class, ImmunizationLibrary.class})
Expand Down Expand Up @@ -522,4 +533,72 @@ public void testGetWeeksDueWithNullDateReturnsZero() {
int weeksDue = Utils.getWeeksDue(date);
Assert.assertEquals(0, weeksDue);
}

@Test
public void testChildBelongsToCurrentFacilityShouldReturnTrueWhenChildExistsInDB() {
HashMap<String, String> childDetails = new HashMap<>();
childDetails.put("base_entity_id", "case_id");
childDetails.put("is_out_of_catchment", "false");
ArrayList<HashMap<String, String>> childDetailsList = new ArrayList<>();
childDetailsList.add(childDetails);

EventClientRepository eventClientRepository = Mockito.mock(EventClientRepository.class);
Repository repository = Mockito.mock(Repository.class);
Mockito.doReturn(eventClientRepository).when(childLibrary).eventClientRepository();
Mockito.doReturn(repository).when(childLibrary).getRepository();
Mockito.when(repository.getReadableDatabase()).thenReturn(Mockito.mock(SQLiteDatabase.class));
Mockito.doReturn(childDetailsList).when(eventClientRepository).rawQuery(any(SQLiteDatabase.class), anyString());
ChildMetadata metadata = Mockito.mock(ChildMetadata.class);
RegisterQueryProvider registerQueryProvider = Mockito.mock(RegisterQueryProvider.class);
Mockito.doReturn(metadata).when(childLibrary).metadata();
Mockito.doReturn(registerQueryProvider).when(metadata).getRegisterQueryProvider();
Mockito.doReturn("").when(registerQueryProvider).mainRegisterQuery();
Mockito.doReturn("").when(registerQueryProvider).getDemographicTable();

Assert.assertTrue(Utils.childBelongsToCurrentFacility(childDetails));
}

@Test
public void testIsChildTemporaryOOCShouldReturnTrueWhenOOCEvent() {
EventClientRepository eventClientRepository = Mockito.mock(EventClientRepository.class);
Repository repository = Mockito.mock(Repository.class);
Mockito.doReturn(eventClientRepository).when(childLibrary).eventClientRepository();
Mockito.doReturn(repository).when(childLibrary).getRepository();
Mockito.when(repository.getReadableDatabase()).thenReturn(Mockito.mock(SQLiteDatabase.class));
Mockito.doReturn(null).when(eventClientRepository).rawQuery(any(SQLiteDatabase.class), anyString());
ChildMetadata metadata = Mockito.mock(ChildMetadata.class);
RegisterQueryProvider registerQueryProvider = Mockito.mock(RegisterQueryProvider.class);
Mockito.doReturn(metadata).when(childLibrary).metadata();
Mockito.doReturn(registerQueryProvider).when(metadata).getRegisterQueryProvider();
Mockito.doReturn("").when(registerQueryProvider).mainRegisterQuery();
Mockito.doReturn("").when(registerQueryProvider).getDemographicTable();
Mockito.doReturn(true).when(appProperties).isTrue(eq(ChildAppProperties.KEY.NOVEL.OUT_OF_CATCHMENT));

Map<String, String> childDetails = new HashMap<>();
childDetails.put("base_entity_id", "case_id");
childDetails.put("is_out_of_catchment", "true");

Assert.assertTrue(Utils.isChildTemporaryOOC(childDetails));
}

@Test
public void testGetLocationIdFromChildTempOOCEventRetursCorrectLocation() {
EventClientRepository repository = Mockito.mock(EventClientRepository.class);
Mockito.when(childLibrary.eventClientRepository()).thenReturn(repository);

List<EventClient> eventClientList = new ArrayList<>();
org.smartregister.domain.Event event = new org.smartregister.domain.Event();
event.setBaseEntityId("case_id");
org.smartregister.domain.Obs obs = new org.smartregister.domain.Obs();
obs.setFormSubmissionField("From_LocationId");
obs.addToValueList("location_123");
event.addObs(obs);
EventClient eventClient = new EventClient(event);
eventClientList.add(eventClient);

Mockito.doReturn(eventClientList).when(repository).fetchEventClientsCore(anyString(), any());

String locationId = Utils.getLocationIdFromChildTempOOCEvent("case_id");
Assert.assertEquals("location_123", locationId);
}
}

0 comments on commit a02535d

Please sign in to comment.