Skip to content

Commit 3c447fe

Browse files
Add sample finder test for multi choice field (#2903)
#### Rationale Add sample finder test for multi choice field #### Related Pull Requests - [https://github.com/LabKey/limsModules/pull/2021](https://github.com/LabKey/limsModules/pull/2021) #### Changes - create TestArrayDataUtils to move duplicated methods for multi choice --------- Co-authored-by: Trey Chadick <tchad@labkey.com>
1 parent 03efcd1 commit 3c447fe

5 files changed

Lines changed: 131 additions & 15 deletions

File tree

src/org/labkey/test/components/ui/grids/ResponsiveGrid.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ private GridFilterModal initFilterColumn(CharSequence columnIdentifier, Filter.O
256256
{
257257
filterPanel.selectArrayFilterOperator(operator);
258258
}
259-
if (value != null)
259+
if (value != null && !((List<String>) value).isEmpty())
260260
{
261261
List<String> values = (List<String>) value;
262262
filterPanel.selectValue(values.get(0));

src/org/labkey/test/params/FieldDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ public boolean isMeasureByDefault()
617617
ColumnType Sample = new ColumnTypeImpl("Sample", "int", "http://www.labkey.org/exp/xml#sample", new IntLookup( "exp", "Materials"));
618618
ColumnType Barcode = new ColumnTypeImpl("Unique ID", "string", "http://www.labkey.org/types#storageUniqueId", null);
619619
ColumnType TextChoice = new ColumnTypeImpl("Text Choice", "string", "http://www.labkey.org/types#textChoice", null);
620-
ColumnType MultiValueTextChoice = new ColumnTypeImpl("Text Choice", "string", "http://cpas.fhcrc.org/exp/xml#multiChoice", null);
620+
ColumnType MultiValueTextChoice = new ColumnTypeImpl("Text Choice", "http://cpas.fhcrc.org/exp/xml#multiChoice", null, null);
621621
ColumnType SMILES = new ColumnTypeImpl("SMILES", "string", "http://www.labkey.org/exp/xml#smiles", null);
622622
ColumnType Calculation = new ColumnTypeImpl("Calculation", null, "http://www.labkey.org/exp/xml#calculated", null);
623623
/**

src/org/labkey/test/util/TestDataGenerator.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import java.util.Date;
5858
import java.util.HashMap;
5959
import java.util.Iterator;
60+
import java.util.LinkedHashSet;
6061
import java.util.List;
6162
import java.util.Map;
6263
import java.util.Objects;
@@ -183,6 +184,13 @@ else if (fieldDefinition.getType().equals(FieldDefinition.ColumnType.TextChoice)
183184
else
184185
entityData.put(key, textChoices.get(textChoiceIndex));
185186
}
187+
else if (fieldDefinition.getType().equals(FieldDefinition.ColumnType.MultiValueTextChoice))
188+
{
189+
FieldDefinition.TextChoiceValidator validator =
190+
(FieldDefinition.TextChoiceValidator) fieldDefinition.getValidators().getFirst();
191+
List<String> values = shuffleSelect(validator.getValues());
192+
entityData.put(key, values);
193+
}
186194
}
187195
}
188196

@@ -491,6 +499,20 @@ public static String randomString(int size)
491499
return randomString(size, null);
492500
}
493501

502+
public static List<String> randomTextChoice(int size)
503+
{
504+
Set<String> textChoices = new LinkedHashSet<>();
505+
while (textChoices.size() < size)
506+
{
507+
String generated = randomString(randomInt(1, 25)).trim();
508+
if (!generated.isEmpty())
509+
{
510+
textChoices.add(generated);
511+
}
512+
}
513+
return List.copyOf(textChoices);
514+
}
515+
494516
public static String randomString(int size, @Nullable String exclusion)
495517
{
496518
return randomString(size, exclusion, CHARSET_STRING);
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package org.labkey.test.util.data;
2+
3+
import org.apache.commons.csv.CSVFormat;
4+
import org.apache.commons.csv.CSVParser;
5+
import org.apache.commons.csv.CSVRecord;
6+
import org.labkey.remoteapi.query.Filter;
7+
8+
import java.io.IOException;
9+
import java.io.StringReader;
10+
import java.util.Comparator;
11+
import java.util.LinkedHashMap;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.stream.Collectors;
15+
16+
import static org.labkey.test.util.samplemanagement.SMTestUtils.COL_SAMPLE_ID_NAME;
17+
import static org.labkey.test.util.samplemanagement.SMTestUtils.COL_SAMPLE_NAME_NAME;
18+
19+
public class TestArrayDataUtils
20+
{
21+
22+
public static <T> Map<String, T> getMapWithIdAndMultiChoiceField(List<Map<String, T>> data)
23+
{
24+
return data.stream()
25+
.collect(Collectors.toMap(
26+
row -> String.valueOf(row.get(COL_SAMPLE_NAME_NAME) != null ? row.get(COL_SAMPLE_NAME_NAME) : row.get(COL_SAMPLE_ID_NAME)),
27+
row ->
28+
{
29+
String complexKey = row.keySet().stream()
30+
.filter(k -> k.contains("Multi Choice"))
31+
.findFirst()
32+
.orElse("");
33+
return row.get(complexKey);
34+
}
35+
));
36+
}
37+
38+
/**
39+
* Filtering Map according to filter and then sorting values in alphabetical order.
40+
*
41+
* @return filtered Map
42+
*/
43+
public static <T> Map<String, List<String>> filterMap(Map<String, T> map, List<String> searchValues, Filter.Operator filterType)
44+
{
45+
return map.entrySet().stream()
46+
.filter(entry -> entry.getValue() instanceof List)
47+
.map(entry -> Map.entry(entry.getKey(), (List<String>) entry.getValue()))
48+
.filter(entry -> isMatch(entry.getValue(), searchValues, filterType))
49+
.collect(Collectors.toMap(
50+
Map.Entry::getKey,
51+
e -> e.getValue().stream()
52+
// Standard alphabetical sort that accounts for symbols and numbers.
53+
// But uppercase letters are positioned before lowercase letters.
54+
.sorted(Comparator
55+
.comparing((String s) -> s.substring(0, 1).toLowerCase())
56+
.thenComparing(s -> s.substring(0, 1))
57+
.thenComparing(s -> s))
58+
.collect(Collectors.toList()),
59+
(e1, e2) -> e1,
60+
LinkedHashMap::new
61+
));
62+
}
63+
64+
public static Map<String, String> prepareMapForCheck(Map<String, List<String>> map)
65+
{
66+
return map.entrySet().stream()
67+
.collect(Collectors.toMap(
68+
Map.Entry::getKey,
69+
entry -> String.join(", ", entry.getValue()),
70+
(e1, e2) -> e1,
71+
LinkedHashMap::new
72+
));
73+
}
74+
75+
public static <T> Map<String, String> filterAndPrepareMap(Map<String, T> map, List<String> searchValues, Filter.Operator filterType)
76+
{
77+
return prepareMapForCheck(filterMap(map, searchValues, filterType));
78+
}
79+
80+
public static List<String> parseMultiValueText(String multiValueString) throws IOException
81+
{
82+
CSVFormat format = CSVFormat.RFC4180.builder()
83+
.setIgnoreSurroundingSpaces(true).setTrim(true).get();
84+
try (CSVParser parser = format.parse(new StringReader(multiValueString)))
85+
{
86+
List<CSVRecord> records = parser.getRecords();
87+
if (records.size() != 1)
88+
throw new IllegalArgumentException("Invalid multi-value text string: " + multiValueString);
89+
return records.getFirst().toList();
90+
}
91+
}
92+
93+
private static boolean isMatch(List<String> actualValues, List<String> searchValues, Filter.Operator type)
94+
{
95+
return switch (type)
96+
{
97+
case ARRAY_CONTAINS_ALL -> actualValues.containsAll(searchValues);
98+
case ARRAY_CONTAINS_ANY -> searchValues.stream().anyMatch(actualValues::contains);
99+
case ARRAY_CONTAINS_EXACT -> actualValues.size() == searchValues.size() && actualValues.containsAll(searchValues);
100+
case ARRAY_CONTAINS_NONE -> searchValues.stream().noneMatch(actualValues::contains);
101+
case ARRAY_CONTAINS_NOT_EXACT -> !(actualValues.size() == searchValues.size() && actualValues.containsAll(searchValues));
102+
case ARRAY_ISEMPTY -> actualValues.isEmpty();
103+
case ARRAY_ISNOTEMPTY -> !actualValues.isEmpty();
104+
default -> throw new IllegalArgumentException("Invalid filter type " + type);
105+
};
106+
}
107+
}

src/org/labkey/test/util/data/TestDataUtils.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -578,19 +578,6 @@ public static List<List<String>> readRowsFromFile(File file, CSVFormat format) t
578578
}
579579
}
580580

581-
public static List<String> parseMultiValueText(String multiValueString) throws IOException
582-
{
583-
CSVFormat format = CSVFormat.RFC4180.builder()
584-
.setIgnoreSurroundingSpaces(true).setTrim(true).get();
585-
try (CSVParser parser = format.parse(new StringReader(multiValueString)))
586-
{
587-
List<CSVRecord> records = parser.getRecords();
588-
if (records.size() != 1)
589-
throw new IllegalArgumentException("Invalid multi-value text string: " + multiValueString);
590-
return records.getFirst().toList();
591-
}
592-
}
593-
594581
public static <T> String stringFromRows(List<List<T>> rows, CSVFormat format)
595582
{
596583
StringWriter stringWriter = new StringWriter();

0 commit comments

Comments
 (0)