package org.pdxfinder.dataloaders.updog.tablevalidation.error;

import org.junit.Test;
import org.pdxfinder.dataloaders.updog.tablevalidation.ColumnReference;
import org.pdxfinder.dataloaders.updog.tablevalidation.Relation;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class BrokenRelationErrorTest {
    private BrokenRelationErrorCreator brokenTableRelationErrorCreator = new BrokenRelationErrorCreator();

    @Test public void toString_givenBrokenRelationMissingRightColumn_returnsAppropriateMessage() {
        String expected =
            "Error in [bar.tsv] for provider [TEST]: Broken table_key relation [(foo.tsv) foo_id -> foo_id (bar.tsv)]" +
                ": because [bar.tsv] is missing column [foo_id]:\n" +
                " not_foo_id  |\n" +
                "--------------";
        Relation relation = Relation.betweenTableKeys(
            ColumnReference.of("foo.tsv", "foo_id"),
            ColumnReference.of("bar.tsv", "foo_id"));
        Table tableMissingColumn = Table.create().addColumns(StringColumn.create("not_foo_id"));
        BrokenRelationError error = brokenTableRelationErrorCreator.create(
            "bar.tsv",
            relation,
            tableMissingColumn,
            "because [bar.tsv] is missing column [foo_id]",
            "TEST"
        );

        assertEquals(
            expected,
            error.verboseMessage()
        );
    }


    @Test public void verboseMessage_givenBrokenRelationOrphanIdsInRightColumn_returnsAppropriateMessage() {
        String expected =
            "Error in [bar.tsv] for provider [PROVIDER-BC]: " +
                "Broken table_key relation [(foo.tsv) foo_id -> foo_id (bar.tsv)]: " +
                "2 orphan row(s) found in [bar.tsv]:\n" +
                " foo_id  |\n" +
                "----------\n" +
                "      1  |\n" +
                "      1  |";
        Relation relation = Relation.betweenTableKeys(
            ColumnReference.of("foo.tsv", "foo_id"),
            ColumnReference.of("bar.tsv", "foo_id"));
        Table tableMissingValues = Table.create().addColumns(
            StringColumn.create("foo_id", Arrays.asList("1", "1")));
        BrokenRelationError error = brokenTableRelationErrorCreator.create(
            "bar.tsv",
            relation,
            tableMissingValues,
            "2 orphan row(s) found in [bar.tsv]",
            "PROVIDER-BC"
        );

        assertEquals(
            expected,
            error.verboseMessage()
        );
    }

    @Test public void message_givenError_returnsAppropriateMessage() {
        String expected =
            "Error in [bar.tsv] for provider [PROVIDER-BC]: " +
                "Broken table_key relation [(foo.tsv) foo_id -> foo_id (bar.tsv)]: " +
                "2 orphan row(s) found in [bar.tsv]";
        Relation relation = Relation.betweenTableKeys(
            ColumnReference.of("foo.tsv", "foo_id"),
            ColumnReference.of("bar.tsv", "foo_id"));
        Table tableMissingValues = Table.create().addColumns(
            StringColumn.create("foo_id", Arrays.asList("1", "1")));
        BrokenRelationError error = brokenTableRelationErrorCreator.create(
            "bar.tsv",
            relation,
            tableMissingValues,
            "2 orphan row(s) found in [bar.tsv]",
            "PROVIDER-BC"
        );
        assertEquals(
            expected,
            error.message()
        );
    }
}
@@ -57,16 +57,87 @@
 error;%0A%0A
+import static org.junit.Assert.assertEquals;%0A%0Aimport java.util.Arrays;%0A
 import o
@@ -151,16 +151,16 @@
 t.Test;%0A
-
 import o
@@ -364,80 +364,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0Aimport static org.junit.Assert.assertEquals;%0A%0A
 publ
@@ -688,33 +688,33 @@
 ST%5D: Broken 
-table_key
+TABLE_KEY
  relation %5B(
@@ -1714,33 +1714,33 @@
     %22Broken 
-table_key
+TABLE_KEY
  relation %5B(
@@ -2788,17 +2788,17 @@
 ken 
-table_key
+TABLE_KEY
  rel

Fix BrokenRelationErrorTest