From 934c8a018fdee719092aa8c35187c4b2b1944a10 Mon Sep 17 00:00:00 2001
From: kaloyane <kaloyane>
Date: Wed, 29 Jun 2011 14:49:24 +0000
Subject: [PATCH] [LMS-2332] improved unit testing

SVN: 21904
---
 .../dist/etc/sanofi-dropbox/sanofi-dropbox.py | 42 ++++++----
 .../dss/test/SanofiDropboxJythonTest.java     | 83 ++++++++++++++++---
 2 files changed, 99 insertions(+), 26 deletions(-)

diff --git a/sanofi/dist/etc/sanofi-dropbox/sanofi-dropbox.py b/sanofi/dist/etc/sanofi-dropbox/sanofi-dropbox.py
index 6399023c814..a264b81b6ae 100644
--- a/sanofi/dist/etc/sanofi-dropbox/sanofi-dropbox.py
+++ b/sanofi/dist/etc/sanofi-dropbox/sanofi-dropbox.py
@@ -57,9 +57,12 @@ class PlateInitializer:
     
     LIBRARY_TEMPLATE_PROPNAME = "LIBRARY_TEMPLATE"
     
-    WELL_TYPE = "COMPOUND_WELL"
-    WELL_CONCENTRATION_PROPNAME = "CONCENTRATION"
-    WELL_MATERIAL_PROPNAME = "COMPOUND_BATCH"
+    POSITIVE_CONTROL_TYPE = "POSITIVE_CONTROL"
+    NEGATIVE_CONTROL_TYPE = "NEGATIVE_CONTROL"
+    
+    COMPOUND_WELL_TYPE = "COMPOUND_WELL"
+    COMPOUND_WELL_CONCENTRATION_PROPNAME = "CONCENTRATION"
+    COMPOUND_WELL_MATERIAL_PROPNAME = "COMPOUND_BATCH"
     
     MATERIAL_TYPE = "COMPOUND_BATCH"
     MATERIAL_ID_PROPNAME = "COMPOUND_ID"
@@ -116,8 +119,8 @@ class PlateInitializer:
         
         sanofiMaterials = []
         for materialMap in list(queryResult):
-            wellCode = str(materialMap['WELLCODE'])
-            materialCode = str(materialMap['MATERIALCODE'])
+            wellCode = str(materialMap['WELL_CODE'])
+            materialCode = str(materialMap['MATERIAL_CODE'])
             sanofiId = str(materialMap['ABASE_COMPOUND_ID'])
             sanofiBatchId = str(materialMap['ABASE_COMPOUND_BATCH_ID'])
             material = SanofiMaterial(wellCode, materialCode, sanofiId, sanofiBatchId)
@@ -170,22 +173,33 @@ class PlateInitializer:
         raise RuntimeError("No material found for wellCode " + wellCode)
     
     def createWells(self, library, sanofiMaterials, openbisMaterials):
-        controlWellTypes = { "H" : "posti", "L" : "negative"};
+        controlWellTypes = { "H" : self.POSITIVE_CONTROL_TYPE, \
+                             "L" : self.NEGATIVE_CONTROL_TYPE};
+                             
         for wellCode in library:    
-           libraryValue = library[wellCode].uppercase()
+           libraryValue = library[wellCode].upper()
+           prefixedWellCode = plate.code + ":" + wellCode
+           
            if libraryValue in controlWellTypes:
+               # CONTROL_WELL
+               wellType = controlWellTypes[libraryValue]
+               well = transaction.createNewSample(prefixedWellCode, wellType)
+               well.setContainer(plate)
                pass
-           else:
-               try 
+           else: 
+               # COMPOUND_WELL
+               concentration = libraryValue
+               try:
+                   float(concentration)
                except ValueError:
-                   raise RuntimeError("TODO...")
-               well = transaction.createNewSample(plate.code +":" + wellCode, self.WELL_TYPE)
+                   raise RuntimeError("A non-numeric value '%s' detected in well '%s'" % (libraryValue, wellCode))
+               well = transaction.createNewSample(prefixedWellCode, self.COMPOUND_WELL_TYPE)
                well.setContainer(plate)
-               concentration = library[wellCode]
-               well.setPropertyValue(self.WELL_CONCENTRATION_PROPNAME, concentration)
+               well.setPropertyValue(self.COMPOUND_WELL_CONCENTRATION_PROPNAME, concentration)
                materialCode = self.getByWellCode(wellCode, sanofiMaterials).materialCode
                material = openbisMaterials[materialCode]
-               well.setMaterialPropertyValue(self.WELL_MATERIAL_PROPNAME, material)
+               # TODO KE: make material property creation possible
+               #well.setPropertyValue(self.COMPOUND_WELL_MATERIAL_PROPNAME, material)
         
 
     def createWellsAndMaterials(self):
diff --git a/sanofi/sourceTest/java/ch/systemsx/cisd/sanofi/dss/test/SanofiDropboxJythonTest.java b/sanofi/sourceTest/java/ch/systemsx/cisd/sanofi/dss/test/SanofiDropboxJythonTest.java
index f69e8b5b9de..6c9d26b7a87 100644
--- a/sanofi/sourceTest/java/ch/systemsx/cisd/sanofi/dss/test/SanofiDropboxJythonTest.java
+++ b/sanofi/sourceTest/java/ch/systemsx/cisd/sanofi/dss/test/SanofiDropboxJythonTest.java
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
@@ -42,6 +43,7 @@ import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria.MatchCl
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetType;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.IEntityProperty;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ListMaterialCriteria;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewSample;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.builders.ExperimentBuilder;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.builders.SampleBuilder;
@@ -51,6 +53,7 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ExperimentIdentifi
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ExperimentIdentifierFactory;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifier;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifierFactory;
+import ch.systemsx.cisd.openbis.generic.shared.util.EntityHelper;
 
 /**
  * <pre>
@@ -69,12 +72,23 @@ public class SanofiDropboxJythonTest extends AbstractJythonDataSetHandlerTest
     private static final String LIBRARY_TEMPLATE_PROPNAME = "LIBRARY_TEMPLATE";
 
     private static final String MATERIAL_TYPE = "COMPOUND_BATCH";
+    
+    private static final String POSITIVE_CONTROL_TYPE = "POSITIVE_CONTROL";
+
+    private static final String NEGATIVE_CONTROL_TYPE = "NEGATIVE_CONTROL";
+
+    private static final String COMPOUND_WELL_TYPE = "COMPOUND_WELL";
+
+    private static final String COMPOUND_WELL_CONCENTRATION_PROPNAME = "CONCENTRATION";
+
+    // TODO KE: implement me
+    // private static final String COMPOUND_WELL_MATERIAL_PROPNAME = "COMPOUND_BATCH";
 
     private static final String DATASET_DIR_NAME = "batchNr_plateCode.variant_2011.06.28";
 
     private static final String DATA_SET_CODE = "data-set-code";
 
-    private static final DataSetType DATA_SET_TYPE = new DataSetType("DATA_SET_TYPE");
+    private static final DataSetType DATA_SET_TYPE = new DataSetType("HCS_IMAGE_RAW");
 
     private static final String EXPERIMENT_IDENTIFIER = "/SANOFI/PROJECT/EXP";
     private static final String PLATE_IDENTIFIER = "/SANOFI/TEST-PLATE";
@@ -89,8 +103,8 @@ public class SanofiDropboxJythonTest extends AbstractJythonDataSetHandlerTest
         queryService = context.mock(IDataSourceQueryService.class);
     }
 
-    @Test(enabled = false)
-    public void testSimpleTransaction() throws IOException
+    @Test
+    public void testHappyCaseWithLibraryCreation() throws IOException
     {
         setUpHomeDataBaseExpectations();
         Properties properties = createThreadPropertiesRelativeToScriptsFolder("sanofi-dropbox.py");
@@ -120,11 +134,8 @@ public class SanofiDropboxJythonTest extends AbstractJythonDataSetHandlerTest
                     one(openBisService).listMaterials(with(materialCriteria), with(equal(true)));
                     will(returnValue(Collections.emptyList()));
                     
-                    one(openBisService).createPermId();
-                    will(returnValue("A0-permId"));
-
-                    one(openBisService).createPermId();
-                    will(returnValue("B0-permId"));
+                    exactly(4).of(openBisService).createPermId();
+                    will(returnValue("well-permId"));
 
                     one(openBisService).createDataSetCode();
                     will(returnValue(DATA_SET_CODE));
@@ -147,20 +158,68 @@ public class SanofiDropboxJythonTest extends AbstractJythonDataSetHandlerTest
 
         handler.handle(markerFile);
 
-        assertEquals(MATERIAL_TYPE, materialCriteria.recordedObject().tryGetMaterialType());
+        assertEquals(MATERIAL_TYPE, materialCriteria.recordedObject().tryGetMaterialType()
+                .getCode());
         assertEquals(true, queryResult.hasCloseBeenInvoked());
 
-        assertEquals(1, atomicatOperationDetails.recordedObject().getDataSetRegistrations().size());
+        List<NewSample> registeredSamples =
+                atomicatOperationDetails.recordedObject().getSampleRegistrations();
+        
+        assertCompoundWell(registeredSamples, "A0", "1.45");
+        assertPositiveControl(registeredSamples, "A1");
+        assertCompoundWell(registeredSamples, "B0", "0.13");
+        assertNegativeControl(registeredSamples, "B1");
 
-        NewExternalData dataSet =
-                atomicatOperationDetails.recordedObject().getDataSetRegistrations().get(0);
+        List<? extends NewExternalData> dataSetsRegistered =
+                atomicatOperationDetails.recordedObject().getDataSetRegistrations();
+        assertEquals(1, dataSetsRegistered.size());
 
+        NewExternalData dataSet = dataSetsRegistered.get(0);
         assertEquals(DATA_SET_CODE, dataSet.getCode());
         assertEquals(DATA_SET_TYPE, dataSet.getDataSetType());
 
         context.assertIsSatisfied();
     }
 
+    private NewSample findByWellCode(List<NewSample> newSamples, String wellCode)
+    {
+        for (NewSample newSample : newSamples)
+        {
+            if (newSample.getIdentifier().endsWith(":" + wellCode))
+            {
+                return newSample;
+            }
+        }
+        throw new RuntimeException("Failed to find sample registration for well " + wellCode);
+    }
+
+    private void assertNegativeControl(List<NewSample> newSamples, String wellCode)
+    {
+        NewSample newSample = findByWellCode(newSamples, wellCode);
+        assertEquals(NEGATIVE_CONTROL_TYPE, newSample.getSampleType().getCode());
+        assertEquals(0, newSample.getProperties().length);
+    }
+
+    private void assertPositiveControl(List<NewSample> newSamples, String wellCode)
+    {
+        NewSample newSample = findByWellCode(newSamples, wellCode);
+        assertEquals(POSITIVE_CONTROL_TYPE, newSample.getSampleType().getCode());
+        assertEquals(0, newSample.getProperties().length);
+    }
+
+    private void assertCompoundWell(List<NewSample> newSamples, String wellCode,
+            String concentration)
+    {
+        NewSample newSample = findByWellCode(newSamples, wellCode);
+        assertEquals(COMPOUND_WELL_TYPE, newSample.getSampleType().getCode());
+        IEntityProperty concentrationProp =
+                EntityHelper.tryFindProperty(newSample.getProperties(),
+                        COMPOUND_WELL_CONCENTRATION_PROPNAME);
+        assertNotNull(concentrationProp);
+        assertEquals("Invalid concentration value for well '" + wellCode + "': ", concentration,
+                concentrationProp.tryGetAsString());
+    }
+
     private void setUpPlateSearchExpectations(final Sample plate)
     {
         context.checking(new Expectations()
-- 
GitLab