Repository: jtablesaw/tablesaw
Pull request #743 (2020-01-19 15:55:47)

move useful test method into a class for reuse in other tests
Thanks for contributing.

- [ X] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

Moved assertTablesEquals test helper into a standalone class for use in other tests and PR's

## Testing

Existing SortTest continues to pass



Patch:
diff --git a/core/src/main/java/tech/tablesaw/api/BooleanColumn.java b/core/src/main/java/tech/tablesaw/api/BooleanColumn.java
index 5c95258f..4b8f4d2f 100644
--- a/core/src/main/java/tech/tablesaw/api/BooleanColumn.java
+++ b/core/src/main/java/tech/tablesaw/api/BooleanColumn.java
@@ -175,13 +175,13 @@ public class BooleanColumn extends AbstractColumn<BooleanColumn, Boolean>
 
     Table table = Table.create(name());
 
-    BooleanColumn booleanColumn = create("Value");
+    StringColumn label = StringColumn.create("Value");
     DoubleColumn countColumn = DoubleColumn.create("Count");
-    table.addColumns(booleanColumn);
+    table.addColumns(label);
     table.addColumns(countColumn);
 
     for (Map.Entry<Byte, Integer> entry : counts.byte2IntEntrySet()) {
-      booleanColumn.append(entry.getKey());
+      label.append(entry.getKey() == 1 ? "true" : "false");
       countColumn.append(entry.getValue());
     }
     return table;
diff --git a/core/src/main/java/tech/tablesaw/columns/numbers/Stats.java b/core/src/main/java/tech/tablesaw/columns/numbers/Stats.java
index 7ef9a3fe..8e6cfce4 100644
--- a/core/src/main/java/tech/tablesaw/columns/numbers/Stats.java
+++ b/core/src/main/java/tech/tablesaw/columns/numbers/Stats.java
@@ -130,7 +130,7 @@ public class Stats {
     t.addColumns(measure);
     t.addColumns(value);
 
-    measure.append("n");
+    measure.append("Count");
     value.append(n);
 
     measure.append("sum");
diff --git a/core/src/main/java/tech/tablesaw/table/Relation.java b/core/src/main/java/tech/tablesaw/table/Relation.java
index 456c2517..6d6625a9 100644
--- a/core/src/main/java/tech/tablesaw/table/Relation.java
+++ b/core/src/main/java/tech/tablesaw/table/Relation.java
@@ -240,19 +240,25 @@ public abstract class Relation implements Iterable<Row> {
     return structure;
   }
 
-  public String summary() {
-    StringBuilder builder = new StringBuilder();
-    builder
-        .append(System.lineSeparator())
-        .append("Table summary for: ")
-        .append(name())
-        .append(System.lineSeparator());
-    for (Column<?> column : columns()) {
-      builder.append(column.summary().print());
-      builder.append(System.lineSeparator());
+  public Table summary() {
+    Table summaryTable = null;
+
+    for (int i = 0; i < this.columnCount(); i++) {
+      Table columnSummary = this.column(i).summary();
+      columnSummary.column(1).setName(this.column(i).name());
+
+      if (summaryTable == null) {
+        // First column, so just create the table from this
+        summaryTable = columnSummary;
+        summaryTable.setName(this.name());
+      } else {
+        String name = summaryTable.column(0).name();
+        summaryTable =
+            summaryTable.joinOn(name).fullOuter(columnSummary, columnSummary.column(0).name());
+      }
     }
-    builder.append(System.lineSeparator());
-    return builder.toString();
+    summaryTable.column(0).setName("Summary");
+    return summaryTable;
   }
 
   public BooleanColumn booleanColumn(int columnIndex) {


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/SortTest.java b/core/src/test/java/tech/tablesaw/SortTest.java
index 0e273386..f1dfa615 100644
--- a/core/src/test/java/tech/tablesaw/SortTest.java
+++ b/core/src/test/java/tech/tablesaw/SortTest.java
@@ -16,6 +16,7 @@ package tech.tablesaw;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static tech.tablesaw.TableAssertions.assertTablesEquals;
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Disabled;
@@ -148,26 +149,4 @@ public class SortTest {
 
     assertEquals("Unrecognized Column: '>col2'", thrown.getMessage());
   }
-
-  /**
-   * Make sure each row in each table match
-   *
-   * @param compareWith the table that was sorted using some external means e.g. excel. i.e known
-   *     good data
-   * @param sortedTable the table that was sorted with Tablesaw
-   */
-  private void assertTablesEquals(Table compareWith, Table sortedTable) {
-    assertEquals(
-        sortedTable.rowCount(), compareWith.rowCount(), "both tables have the same number of rows");
-    int maxRows = sortedTable.rowCount();
-    int numberOfColumns = sortedTable.columnCount();
-    for (int rowIndex = 0; rowIndex < maxRows; rowIndex++) {
-      for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
-        assertEquals(
-            sortedTable.get(rowIndex, columnIndex),
-            compareWith.get(rowIndex, columnIndex),
-            "cells[" + rowIndex + ", " + columnIndex + "] do not match");
-      }
-    }
-  }
 }
diff --git a/core/src/test/java/tech/tablesaw/TableAssertions.java b/core/src/test/java/tech/tablesaw/TableAssertions.java
new file mode 100644
index 00000000..cb85dd76
--- /dev/null
+++ b/core/src/test/java/tech/tablesaw/TableAssertions.java
@@ -0,0 +1,30 @@
+package tech.tablesaw;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import tech.tablesaw.api.Table;
+
+public class TableAssertions {
+  private TableAssertions() {}
+  /**
+   * Make sure each row in each table match
+   *
+   * @param compareWith the table that was sorted using some external means e.g. excel. i.e known
+   *     good data
+   * @param sortedTable the table that was sorted with Tablesaw
+   */
+  public static void assertTablesEquals(Table compareWith, Table sortedTable) {
+    assertEquals(
+        sortedTable.rowCount(), compareWith.rowCount(), "both tables have the same number of rows");
+    int maxRows = sortedTable.rowCount();
+    int numberOfColumns = sortedTable.columnCount();
+    for (int rowIndex = 0; rowIndex < maxRows; rowIndex++) {
+      for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
+        assertEquals(
+            sortedTable.get(rowIndex, columnIndex),
+            compareWith.get(rowIndex, columnIndex),
+            "cells[" + rowIndex + ", " + columnIndex + "] do not match");
+      }
+    }
+  }
+}
diff --git a/core/src/test/java/tech/tablesaw/table/TableSummaryTest.java b/core/src/test/java/tech/tablesaw/table/TableSummaryTest.java
new file mode 100644
index 00000000..ca2e3855
--- /dev/null
+++ b/core/src/test/java/tech/tablesaw/table/TableSummaryTest.java
@@ -0,0 +1,73 @@
+package tech.tablesaw.table;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.time.LocalDate;
+import org.junit.jupiter.api.Test;
+import tech.tablesaw.api.BooleanColumn;
+import tech.tablesaw.api.DateColumn;
+import tech.tablesaw.api.DoubleColumn;
+import tech.tablesaw.api.StringColumn;
+import tech.tablesaw.api.Table;
+
+public class TableSummaryTest {
+  @Test
+  public void summaryTestTwoDoubleColumnsStatistics() {
+    Table testTable =
+        Table.create(
+            "Data",
+            DoubleColumn.create("value1", new double[] {1.0, 1.1, 1.2}),
+            DoubleColumn.create("value2", new double[] {2.0, 2.1, 2.2}));
+    Table result = testTable.summary();
+    assertEquals(
+        "                            Data                             \n"
+            + " Summary   |         value1         |        value2         |\n"
+            + "-------------------------------------------------------------\n"
+            + "    Count  |                     3  |                    3  |\n"
+            + "      sum  |                   3.3  |                  6.3  |\n"
+            + "     Mean  |                   1.1  |                  2.1  |\n"
+            + "      Min  |                     1  |                    2  |\n"
+            + "      Max  |                   1.2  |                  2.2  |\n"
+            + "    Range  |   0.19999999999999996  |  0.20000000000000018  |\n"
+            + " Variance  |  0.009999999999999995  |  0.01000000000000004  |\n"
+            + " Std. Dev  |   0.09999999999999998  |   0.1000000000000002  |",
+        result.print());
+  }
+
+  @Test
+  public void summaryMixedTypes() {
+    Table testTable =
+        Table.create(
+            "Data",
+            StringColumn.create("label", new String[] {"yellow", "yellow", "green"}),
+            DoubleColumn.create("value1", new double[] {1.0, 1.1, 1.2}),
+            BooleanColumn.create("truthy", new boolean[] {true, false, true}),
+            DateColumn.create(
+                "dates",
+                new LocalDate[] {
+                  LocalDate.of(2001, 1, 1), LocalDate.of(2002, 1, 1), LocalDate.of(2001, 1, 1)
+                }));
+    Table result = testTable.summary();
+    assertEquals(
+        "                                   Data                                    \n"
+            + "  Summary   |  label   |         value1         |  truthy  |    dates     |\n"
+            + "---------------------------------------------------------------------------\n"
+            + "     Count  |       3  |                     3  |          |           3  |\n"
+            + "    Unique  |       2  |                        |          |              |\n"
+            + "       Top  |  yellow  |                        |          |              |\n"
+            + " Top Freq.  |       2  |                        |          |              |\n"
+            + "       sum  |          |                   3.3  |          |              |\n"
+            + "      Mean  |          |                   1.1  |          |              |\n"
+            + "       Min  |          |                     1  |          |              |\n"
+            + "       Max  |          |                   1.2  |          |              |\n"
+            + "     Range  |          |   0.19999999999999996  |          |              |\n"
+            + "  Variance  |          |  0.009999999999999995  |          |              |\n"
+            + "  Std. Dev  |          |   0.09999999999999998  |          |              |\n"
+            + "     false  |          |                        |       1  |              |\n"
+            + "      true  |          |                        |       2  |              |\n"
+            + "   Missing  |          |                        |          |           0  |\n"
+            + "  Earliest  |          |                        |          |  2001-01-01  |\n"
+            + "    Latest  |          |                        |          |  2002-01-01  |",
+        result.print());
+  }
+}