← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20600: TEI query. Using left join for attribute values. Added tests.

 

------------------------------------------------------------
revno: 20600
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-10-09 11:23:30 +0200
message:
  TEI query. Using left join for attribute values. Added tests.
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStoreTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2015-10-09 03:21:10 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2015-10-09 09:23:30 +0000
@@ -121,7 +121,7 @@
 
     private String buildTrackedEntityInstanceHql( TrackedEntityInstanceQueryParams params )
     {
-        String hql = "select distinct tei from TrackedEntityInstance tei inner join fetch tei.attributeValues";
+        String hql = "select distinct tei from TrackedEntityInstance tei left join fetch tei.attributeValues";
         SqlHelper hlp = new SqlHelper( true );
 
         if ( params.hasTrackedEntity() )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStoreTest.java	2015-10-08 14:12:50 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStoreTest.java	2015-10-09 09:23:30 +0000
@@ -33,9 +33,17 @@
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.List;
+
 import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.analytics.AggregationType;
+import org.hisp.dhis.common.QueryItem;
+import org.hisp.dhis.common.QueryOperator;
+import org.hisp.dhis.common.ValueType;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
+import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -46,7 +54,7 @@
     extends DhisSpringTest
 {
     @Autowired
-    private TrackedEntityInstanceStore entityInstanceStore;
+    private TrackedEntityInstanceStore teiStore;
 
     @Autowired
     private OrganisationUnitService organisationUnitService;
@@ -54,75 +62,123 @@
     @Autowired
     private TrackedEntityAttributeService attributeService;
 
-    private TrackedEntityInstance entityInstanceA1;
-
-    private TrackedEntityInstance entityInstanceB1;
-
-    private OrganisationUnit organisationUnit;
+    @Autowired
+    private TrackedEntityAttributeValueService attributeValueService;
+
+    private TrackedEntityInstance teiA;
+    private TrackedEntityInstance teiB;
+    private TrackedEntityInstance teiC;
+    private TrackedEntityInstance teiD;
+    private TrackedEntityInstance teiE;
+    private TrackedEntityInstance teiF;
+
+    private TrackedEntityAttribute atA;
+    private TrackedEntityAttribute atB;
+    
+    private OrganisationUnit ouA;
+    private OrganisationUnit ouB;
+    private OrganisationUnit ouC;
     
     @Override
     public void setUpTest()
     {
-        organisationUnit = createOrganisationUnit( 'A' );
-        organisationUnitService.addOrganisationUnit( organisationUnit );
-
-        OrganisationUnit organisationUnitB = createOrganisationUnit( 'B' );
-        organisationUnitService.addOrganisationUnit( organisationUnitB );
-
-        TrackedEntityAttribute entityInstanceAttributeB = createTrackedEntityAttribute( 'B' );
-        entityInstanceAttributeB.setUnique( true );
-        attributeService.addTrackedEntityAttribute( entityInstanceAttributeB );
+        ouA = createOrganisationUnit( 'A' );
+        ouB = createOrganisationUnit( 'B', ouA );
+        ouC = createOrganisationUnit( 'C', ouB );
+        
+        organisationUnitService.addOrganisationUnit( ouA );
+        organisationUnitService.addOrganisationUnit( ouB );
+        organisationUnitService.addOrganisationUnit( ouC );
+
+        atA = createTrackedEntityAttribute( 'A' );
+        atB = createTrackedEntityAttribute( 'B' );
+        atB.setUnique( true );
+
+        attributeService.addTrackedEntityAttribute( atA );
+        attributeService.addTrackedEntityAttribute( atB );
       
-        entityInstanceA1 = createTrackedEntityInstance( 'A', organisationUnit );
-        entityInstanceB1 = createTrackedEntityInstance( 'B', organisationUnit );
+        teiA = createTrackedEntityInstance( 'A', ouA );
+        teiB = createTrackedEntityInstance( 'B', ouB );
+        teiC = createTrackedEntityInstance( 'C', ouB );
+        teiD = createTrackedEntityInstance( 'D', ouC );
+        teiE = createTrackedEntityInstance( 'E', ouC );
+        teiF = createTrackedEntityInstance( 'F', ouC );
     }
 
     @Test
     public void testAddGet()
     {
-        int idA = entityInstanceStore.save( entityInstanceA1 );
-        int idB = entityInstanceStore.save( entityInstanceB1 );
+        int idA = teiStore.save( teiA );
+        int idB = teiStore.save( teiB );
 
-        assertNotNull( entityInstanceStore.get( idA ) );
-        assertNotNull( entityInstanceStore.get( idB ) );
+        assertNotNull( teiStore.get( idA ) );
+        assertNotNull( teiStore.get( idB ) );
     }
 
     @Test
     public void testAddGetbyOu()
     {
-        int idA = entityInstanceStore.save( entityInstanceA1 );
-        int idB = entityInstanceStore.save( entityInstanceB1 );
+        int idA = teiStore.save( teiA );
+        int idB = teiStore.save( teiB );
 
-        assertEquals( entityInstanceA1.getName(), entityInstanceStore.get( idA ).getName() );
-        assertEquals( entityInstanceB1.getName(), entityInstanceStore.get( idB ).getName() );
+        assertEquals( teiA.getName(), teiStore.get( idA ).getName() );
+        assertEquals( teiB.getName(), teiStore.get( idB ).getName() );
     }
 
     @Test
     public void testDelete()
     {
-        int idA = entityInstanceStore.save( entityInstanceA1 );
-        int idB = entityInstanceStore.save( entityInstanceB1 );
-
-        assertNotNull( entityInstanceStore.get( idA ) );
-        assertNotNull( entityInstanceStore.get( idB ) );
-
-        entityInstanceStore.delete( entityInstanceA1 );
-
-        assertNull( entityInstanceStore.get( idA ) );
-        assertNotNull( entityInstanceStore.get( idB ) );
-
-        entityInstanceStore.delete( entityInstanceB1 );
-
-        assertNull( entityInstanceStore.get( idA ) );
-        assertNull( entityInstanceStore.get( idB ) );
+        int idA = teiStore.save( teiA );
+        int idB = teiStore.save( teiB );
+
+        assertNotNull( teiStore.get( idA ) );
+        assertNotNull( teiStore.get( idB ) );
+
+        teiStore.delete( teiA );
+
+        assertNull( teiStore.get( idA ) );
+        assertNotNull( teiStore.get( idB ) );
+
+        teiStore.delete( teiB );
+
+        assertNull( teiStore.get( idA ) );
+        assertNull( teiStore.get( idB ) );
     }
 
     @Test
     public void testGetAll()
     {
-        entityInstanceStore.save( entityInstanceA1 );
-        entityInstanceStore.save( entityInstanceB1 );
-
-        assertTrue( equals( entityInstanceStore.getAll(), entityInstanceA1, entityInstanceB1 ) );
+        teiStore.save( teiA );
+        teiStore.save( teiB );
+
+        assertTrue( equals( teiStore.getAll(), teiA, teiB ) );
+    }
+    
+    @Test
+    public void testQuery()
+    {
+        teiStore.save( teiA );
+        teiStore.save( teiB );
+        teiStore.save( teiC );
+        teiStore.save( teiD );
+        teiStore.save( teiE );
+        teiStore.save( teiF );
+        
+        attributeValueService.addTrackedEntityAttributeValue( new TrackedEntityAttributeValue( atA, teiD, "Male" ) );
+        attributeValueService.addTrackedEntityAttributeValue( new TrackedEntityAttributeValue( atA, teiE, "Male" ) );
+        attributeValueService.addTrackedEntityAttributeValue( new TrackedEntityAttributeValue( atA, teiF, "Female" ) );
+        
+        TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
+        
+        List<TrackedEntityInstance> teis = teiStore.getTrackedEntityInstances( params );
+        
+        assertEquals( 6, teis.size() );
+        
+        params = new TrackedEntityInstanceQueryParams();
+        params.addFilter( new QueryItem( atA, QueryOperator.EQ, "Male", ValueType.TEXT, AggregationType.NONE, null ) );
+        
+        teis = teiStore.getTrackedEntityInstances( params );
+
+        assertEquals( 2, teis.size() );
     }
 }