Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coverage test for TableGrepFilter #1651

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@

package schemacrawler.filter;

import static java.util.Objects.requireNonNull;
import java.util.List;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.util.Objects.requireNonNull;
import schemacrawler.inclusionrule.InclusionRule;
import schemacrawler.schema.Column;
import schemacrawler.schema.Table;
Expand Down Expand Up @@ -74,6 +74,14 @@ public boolean test(final Table table) {
final boolean checkIncludeForDefinitions = grepDefinitionInclusionRule != null;

if (!checkIncludeForTables && !checkIncludeForColumns && !checkIncludeForDefinitions) {
if (invertMatch) {
LOGGER.log(
Level.FINE,
new StringFormat(
"Ignoring the invert match setting for table <%s>, "
+ "since no inclusion rules are set",
table));
}
return true;
}

Expand All @@ -100,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 All @@ -114,10 +123,8 @@ private boolean checkIncludeForColumns(final Table table) {

private boolean checkIncludeForDefinitions(final Table table) {
if (grepDefinitionInclusionRule != null) {
if (grepDefinitionInclusionRule.test(table.getRemarks())) {
return true;
}
if (grepDefinitionInclusionRule.test(table.getDefinition())) {
if (grepDefinitionInclusionRule.test(table.getRemarks())
|| grepDefinitionInclusionRule.test(table.getDefinition())) {
return true;
}
for (final Trigger trigger : table.getTriggers()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package schemacrawler.filter;

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 GrepOptions grepOptions = GrepOptionsBuilder.builder().toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

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

@Test
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);

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().includeGreppedColumns(grepColumnInclusionRule).toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

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

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

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(true));
}

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

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

@Test
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_1");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder().includeGreppedColumns(grepColumnInclusionRule).toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

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

@Test
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_remarks");
final GrepOptions grepOptions =
GrepOptionsBuilder.builder()
.includeGreppedDefinitions(grepDefinitionInclusionRule)
.toOptions();
final TableGrepFilter tableGrepFilter = new TableGrepFilter(grepOptions);

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

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

assertThat(tableGrepFilter.test(table), is(true));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package schemacrawler.test.utility.crawl;

import static java.util.Objects.requireNonNull;
import static us.fatehi.utility.Utility.isBlank;
import static us.fatehi.utility.Utility.requireNotBlank;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -12,7 +8,10 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static java.util.Objects.requireNonNull;
import static us.fatehi.utility.Utility.isBlank;
import static us.fatehi.utility.Utility.requireNotBlank;
import static us.fatehi.utility.Utility.trimToEmpty;
import schemacrawler.schema.Column;
import schemacrawler.schema.ForeignKey;
import schemacrawler.schema.Index;
Expand All @@ -38,12 +37,16 @@ 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;

public LightTable(final Schema schema, final String name) {
this.schema = requireNonNull(schema, "No schema provided");
this.name = requireNotBlank(name, "No table name provided");
attributes = new HashMap<>();
columns = new ArrayList<>();
triggers = new ArrayList<>();
}

public LightTable(final String name) {
Expand All @@ -56,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 All @@ -66,10 +75,7 @@ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
final LightTable other = (LightTable) obj;
Expand All @@ -90,9 +96,8 @@ public <T> T getAttribute(final String name) {
public <T> T getAttribute(final String name, final T defaultValue) throws ClassCastException {
if (hasAttribute(name)) {
return getAttribute(name);
} else {
return defaultValue;
}
return defaultValue;
}

@Override
Expand All @@ -102,12 +107,12 @@ public Map<String, Object> getAttributes() {

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

@Override
public String getDefinition() {
return "";
return trimToEmpty(definition);
}

@Override
Expand Down Expand Up @@ -163,7 +168,7 @@ public Collection<Table> getRelatedTables(final TableRelationshipType tableRelat

@Override
public String getRemarks() {
return "";
return trimToEmpty(remarks);
}

@Override
Expand All @@ -183,7 +188,7 @@ public TableType getTableType() {

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

@Override
Expand Down Expand Up @@ -286,8 +291,14 @@ public <T> void setAttribute(final String name, final T value) {
attributes.put(name, value);
}

public void setDefinition(String definition) {
this.definition = definition;
}

@Override
public void setRemarks(final String remarks) {}
public void setRemarks(final String remarks) {
this.remarks = remarks;
}

@Override
public String toString() {
Expand Down
Loading
Loading