Skip to content

Commit

Permalink
Add full coverage for table grep
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed Aug 10, 2024
1 parent a763bf0 commit bc6f33e
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ private boolean checkIncludeForTables(final Table table) {
}

private boolean checkIncludeForColumns(final Table table) {

final List<Column> columns = table.getColumns();
if (columns.isEmpty()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,91 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import schemacrawler.inclusionrule.InclusionRule;
import schemacrawler.inclusionrule.RegularExpressionInclusionRule;
import schemacrawler.schema.Table;
import schemacrawler.schemacrawler.GrepOptions;
import schemacrawler.schemacrawler.GrepOptionsBuilder;
import schemacrawler.test.utility.crawl.LightTable;
import schemacrawler.test.utility.crawl.LightTrigger;

class TableGrepFilterTest {

private Table table;

@BeforeEach
public void setUp() {
final LightTable table = new LightTable("test_table");
table.addColumn("test_column");
table.setDefinition("test_definition");
table.setRemarks("test_remarks");

final LightTrigger trigger = new LightTrigger(table, "test_trigger");
trigger.setActionStatement("test_action_statement");
table.addTrigger(trigger);

this.table = table;
}

@Test
void testTableGrepFilter() {
final Table table = new LightTable("test_table");

final GrepOptions grepOptions = GrepOptionsBuilder.builder().toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

assertThat(tableGrepFilter.test(table), is(true));
}

@Test
void testTableGrepFilterWithInclusionRule() {
final Table table = new LightTable("test_table");
void testTableGrepFilterWithColumnInclusionRule() {
final InclusionRule grepColumnInclusionRule =
new RegularExpressionInclusionRule("test_table\\.test_column");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder().includeGreppedColumns(grepColumnInclusionRule).toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

final InclusionRule grepTableInclusionRule = new RegularExpressionInclusionRule("test_table");
assertThat(tableGrepFilter.test(table), is(true));
}

@Test
void testTableGrepFilterWithColumnInclusionRuleWithNoColumns() {
table = new LightTable("test_table");

final InclusionRule grepColumnInclusionRule =
new RegularExpressionInclusionRule("test_table\\.test_column");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder().includeGreppedTables(grepTableInclusionRule).toOptions();
GrepOptionsBuilder.builder().includeGreppedColumns(grepColumnInclusionRule).toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

assertThat(tableGrepFilter.test(table), is(true));
}

@Test
void testTableGrepFilterWithNonMatchingInclusionRule() {
final Table table = new LightTable("test_table");
void testTableGrepFilterWithDefinitionInclusionRule() {
final InclusionRule grepDefinitionInclusionRule =
new RegularExpressionInclusionRule("test_definition");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder()
.includeGreppedDefinitions(grepDefinitionInclusionRule)
.toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

final InclusionRule grepTableInclusionRule =
new RegularExpressionInclusionRule("non_matching_table");
assertThat(tableGrepFilter.test(table), is(true));
}

@Test
void testTableGrepFilterWithInclusionRule() {
final InclusionRule grepTableInclusionRule = new RegularExpressionInclusionRule("test_table");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder().includeGreppedTables(grepTableInclusionRule).toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

assertThat(tableGrepFilter.test(table), is(false));
assertThat(tableGrepFilter.test(table), is(true));
}

@Test
void testTableGrepFilterWithInvertMatch() {
final Table table = new LightTable("test_table");

final InclusionRule grepTableInclusionRule = new RegularExpressionInclusionRule("test_table");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder()
Expand All @@ -63,26 +99,76 @@ void testTableGrepFilterWithInvertMatch() {
}

@Test
void testTableGrepFilterWithColumnInclusionRule() {
final LightTable table = new LightTable("test_table");
table.addColumn("test_column");
void testTableGrepFilterWithInvertMatchForNoMatch() {
final InclusionRule grepTableInclusionRule = new RegularExpressionInclusionRule("test_table_1");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder()
.includeGreppedTables(grepTableInclusionRule)
.invertGrepMatch(true)
.toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

assertThat(tableGrepFilter.test(table), is(true));
}

@Test
void testTableGrepFilterWithJustInvertMatch() {
final GrepOptions grepOptions = GrepOptionsBuilder.builder().invertGrepMatch(true).toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

assertThat(tableGrepFilter.test(table), is(true));
}

@Test
void testTableGrepFilterWithNonMatchingColumnInclusionRule() {
final InclusionRule grepColumnInclusionRule =
new RegularExpressionInclusionRule("test_table\\.test_column");
new RegularExpressionInclusionRule("test_table\\.test_column_1");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder().includeGreppedColumns(grepColumnInclusionRule).toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

assertThat(tableGrepFilter.test(table), is(true));
assertThat(tableGrepFilter.test(table), is(false));
}

@Test
void testTableGrepFilterWithDefinitionInclusionRule() {
final LightTable table = new LightTable("test_table");
table.setDefinition("test_definition");
void testTableGrepFilterWithNonMatchingDefinitionInclusionRule() {
final InclusionRule grepDefinitionInclusionRule =
new RegularExpressionInclusionRule("non_matching_definition");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder()
.includeGreppedDefinitions(grepDefinitionInclusionRule)
.toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

assertThat(tableGrepFilter.test(table), is(false));
}

@Test
void testTableGrepFilterWithNonMatchingInclusionRule() {
final InclusionRule grepTableInclusionRule =
new RegularExpressionInclusionRule("non_matching_table");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder().includeGreppedTables(grepTableInclusionRule).toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

assertThat(tableGrepFilter.test(table), is(false));
}

@Test
void testTableGrepFilterWithoutColumnInclusionRule() {
final InclusionRule grepColumnInclusionRule =
new RegularExpressionInclusionRule("test_table\\.test_column_1");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder().includeGreppedColumns(grepColumnInclusionRule).toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

assertThat(tableGrepFilter.test(table), is(false));
}

@Test
void testTableGrepFilterWithRemarksInclusionRule() {
final InclusionRule grepDefinitionInclusionRule =
new RegularExpressionInclusionRule("test_definition");
new RegularExpressionInclusionRule("test_remarks");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder()
.includeGreppedDefinitions(grepDefinitionInclusionRule)
Expand All @@ -93,18 +179,15 @@ void testTableGrepFilterWithDefinitionInclusionRule() {
}

@Test
void testTableGrepFilterWithNonMatchingDefinitionInclusionRule() {
final LightTable table = new LightTable("test_table");
table.setDefinition("test_definition");

void testTableGrepFilterWithTriggerActionItemInclusionRule() {
final InclusionRule grepDefinitionInclusionRule =
new RegularExpressionInclusionRule("non_matching_definition");
new RegularExpressionInclusionRule("test_action_statement");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder()
.includeGreppedDefinitions(grepDefinitionInclusionRule)
.toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

assertThat(tableGrepFilter.test(table), is(false));
assertThat(tableGrepFilter.test(table), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public final class LightTable implements Table {
private final String name;
private final List<Column> columns;
private final Map<String, Object> attributes;
private final Collection<Trigger> triggers;
private String definition;
private String remarks;

Expand All @@ -45,6 +46,7 @@ public LightTable(final Schema schema, final String name) {
this.name = requireNotBlank(name, "No table name provided");
attributes = new HashMap<>();
columns = new ArrayList<>();
triggers = new ArrayList<>();
}

public LightTable(final String name) {
Expand All @@ -57,6 +59,12 @@ public LightColumn addColumn(final String name) {
return column;
}

public void addTrigger(final Trigger trigger) {
if (trigger != null) {
triggers.add(trigger);
}
}

@Override
public int compareTo(final NamedObject o) {
return name.compareTo(o.getName());
Expand Down Expand Up @@ -99,7 +107,7 @@ public Map<String, Object> getAttributes() {

@Override
public List<Column> getColumns() {
return columns;
return new ArrayList<>(columns);
}

@Override
Expand Down Expand Up @@ -180,7 +188,7 @@ public TableType getTableType() {

@Override
public Collection<Trigger> getTriggers() {
return Collections.emptyList();
return new ArrayList<>(triggers);
}

@Override
Expand Down
Loading

0 comments on commit bc6f33e

Please sign in to comment.