name:
src/test/java/io/protobase/f7/testutils/TestExecution.java
-rw-r--r--
5033
1package io.protobase.f7.testutils;
2
3import io.protobase.f7.models.A1Key;
4import io.protobase.f7.models.Cell;
5import io.protobase.f7.models.ColumnRowKey;
6import io.protobase.f7.models.Grid;
7import io.protobase.f7.spreadsheet.SpreadsheetExecutor;
8
9import java.util.HashMap;
10import java.util.HashSet;
11import java.util.Map;
12import java.util.Optional;
13import java.util.Set;
14
15import static com.google.common.truth.Truth.assertThat;
16
17/**
18 * Extendable test execution class to make it easier to load values into grids, and load the grids into a sheet,
19 * and execute the sheet, querying the resulting grids for expected values.
20 */
21public class TestExecution {
22 protected static Runner runner() {
23 return new Runner();
24 }
25
26 /**
27 * Runner class lets us build tests dynamically, by adding individual cells with out a ton of boiler plating.
28 */
29 public static class Runner {
30 Map<String, Grid<Cell>> grids = new HashMap<>();
31 Map<String, Map<String, Object>> expectedGridValues = new HashMap<>();
32 Map<String, Set<String>> expectedEmptyKeys = new HashMap<>();
33 Map<String, Set<String>> expectedEmptyComputedValueKeys = new HashMap<>();
34 Map<String, String> namedRanges = new HashMap<>();
35
36 public Runner addGrid(String gridName, Map<String, String> values) {
37 Grid<Cell> grid = new Grid<>();
38 values.forEach((key, value) -> grid.set(A1Key.fromString(key).toColumnRowKey(), new Cell(value)));
39 grids.put(gridName, grid);
40 return this;
41 }
42
43 public Runner addCell(String gridName, String a1Key, String value) {
44 grids.putIfAbsent(gridName, new Grid<>());
45 grids.get(gridName).set(A1Key.fromString(a1Key).toColumnRowKey(), new Cell(value));
46 return this;
47 }
48
49 public Runner addExpectedValue(String gridName, String a1Key, Object exepctedValue) {
50 expectedGridValues.putIfAbsent(gridName, new HashMap<>());
51 expectedGridValues.get(gridName).put(a1Key, exepctedValue);
52 return this;
53 }
54
55 public Runner addExpectedEmptyValues(String gridName, Set<String> keys) {
56 expectedEmptyKeys.put(gridName, keys);
57 return this;
58 }
59
60 public Runner addExpectedEmptyValue(String gridName, String key) {
61 expectedEmptyKeys.putIfAbsent(gridName, new HashSet<>());
62 expectedEmptyKeys.getOrDefault(gridName, new HashSet<>()).add(key);
63 return this;
64 }
65
66 public Runner addExpectedEmptyComputedValue(String gridName, String key) {
67 expectedEmptyComputedValueKeys.putIfAbsent(gridName, new HashSet<>());
68 expectedEmptyComputedValueKeys.getOrDefault(gridName, new HashSet<>()).add(key);
69 return this;
70 }
71
72 public Runner addNamedRange(String name, String range) {
73 namedRanges.put(name, range);
74 return this;
75 }
76
77 /**
78 * Run the tests in several steps:
79 * 1) Build sheet executor and run it.
80 * 2) For each expected entry, expect result.
81 * 3) For each empty value, ensure it's empty.
82 * 4) For each computed empty value, ensure it's empty.
83 */
84 public void run() {
85 // 1) Build sheet executor and run it.
86 SpreadsheetExecutor sheet = SpreadsheetExecutor.builder().addGrids(grids).addNamedRanges(namedRanges).build();
87 sheet.execute();
88
89 // 2) For each expected entry, expect result.
90 for (Map.Entry<String, Map<String, Object>> expectedEntry : expectedGridValues.entrySet()) {
91 String gridName = expectedEntry.getKey();
92 Map<String, Object> expectedEntryValues = expectedEntry.getValue();
93 for (Map.Entry<String, Object> expected : expectedEntryValues.entrySet()) {
94 ColumnRowKey key = A1Key.fromString(expected.getKey()).toColumnRowKey();
95 Object value = expected.getValue();
96 assertThat(sheet.getCell(gridName, key).get().getComputedValue()).isEqualTo(value);
97 }
98 }
99
100 //3) For each empty value, ensure it's empty.
101 for (Map.Entry<String, Set<String>> expectedGridsToHaveNullValues : expectedEmptyKeys.entrySet()) {
102 String grid = expectedGridsToHaveNullValues.getKey();
103 Set<String> keys = expectedGridsToHaveNullValues.getValue();
104 for (String keyString : keys) {
105 ColumnRowKey key = A1Key.fromString(keyString).toColumnRowKey();
106 Optional<Cell> foundCell = sheet.getCell(grid, key);
107 if (foundCell.isPresent()) {
108 assertThat(foundCell.get().getComputedValue()).isNull();
109 } else {
110 assertThat(foundCell.isPresent()).isFalse();
111 }
112 }
113 }
114
115 // 4) For each computed empty value, ensure it's empty.
116 for (Map.Entry<String, Set<String>> expectedGridsToHaveNullValues : expectedEmptyComputedValueKeys.entrySet()) {
117 String grid = expectedGridsToHaveNullValues.getKey();
118 Set<String> keys = expectedGridsToHaveNullValues.getValue();
119 for (String keyString : keys) {
120 ColumnRowKey key = A1Key.fromString(keyString).toColumnRowKey();
121 assertThat(sheet.getCell(grid, key).get().getComputedValue()).isNull();
122 }
123 }
124 }
125 }
126}