Skip to content

Commit

Permalink
Add equality tests (#141)
Browse files Browse the repository at this point in the history
* Add hash code override methods where equals method is overwritten

make test classes and methods public

fix method reference in equals method, apply spotless

fix missing fields in equals methods, cleanup methods and use equalsverifier library for tests

test bloomTermId in ConditionConfigTest

add ConditionConfigTest

apply spotless

add hash code override methods where equals method is overwritten

* Add equality tests

---------

Co-authored-by: elliVM <[email protected]>
  • Loading branch information
51-code and elliVM authored Dec 12, 2024
1 parent 95b57df commit 9c6c070
Show file tree
Hide file tree
Showing 18 changed files with 469 additions and 79 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/teragrep/pth_06/config/ConditionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public boolean streamQuery() {
return streamQuery;
}

//* DSLContext must be same instance to be equal */
@Override
public boolean equals(Object object) {
if (this == object) {
Expand All @@ -120,11 +121,12 @@ public boolean equals(Object object) {
}
final ConditionConfig cast = (ConditionConfig) object;
return this.bloomEnabled == cast.bloomEnabled && this.streamQuery == cast.streamQuery
&& this.withoutFilters == cast.withoutFilters && this.ctx == cast.ctx;
&& this.withoutFilters == cast.withoutFilters && this.ctx == cast.ctx
&& this.bloomTermId == cast.bloomTermId;
}

@Override
public int hashCode() {
return Objects.hash(ctx, streamQuery, bloomEnabled, withoutFilters);
return Objects.hash(ctx, streamQuery, bloomEnabled, withoutFilters, bloomTermId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ public boolean equals(final Object object) {
return false;
}
final IndexStatementCondition cast = (IndexStatementCondition) object;
return this.value.equals(cast.value) && this.config.equals(cast.config);
return this.value.equals(cast.value) && this.config.equals(cast.config) && this.condition.equals(cast.condition)
&& this.tableSet.equals(cast.tableSet);
}

@Override
public int hashCode() {
return Objects.hash(value, config);
return Objects.hash(value, config, condition, tableSet);
}
}
95 changes: 95 additions & 0 deletions src/test/java/com/teragrep/pth_06/config/ConditionConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Teragrep Archive Datasource (pth_06)
* Copyright (C) 2021-2024 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.pth_06.config;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.jooq.tools.jdbc.MockConnection;
import org.jooq.tools.jdbc.MockResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ConditionConfigTest {

DSLContext ctx = DSL.using(new MockConnection(c -> new MockResult[0]));

@Test
void testEquality() {
ConditionConfig cond1 = new ConditionConfig(ctx, false, false, 1L);
ConditionConfig cond2 = new ConditionConfig(ctx, false, false, 1L);
Assertions.assertEquals(cond1, cond2);
}

@Test
void testNonEquality() {
ConditionConfig cond1 = new ConditionConfig(ctx, false, false);
ConditionConfig cond2 = new ConditionConfig(ctx, true, false);
ConditionConfig cond3 = new ConditionConfig(ctx, false, true);
ConditionConfig cond4 = new ConditionConfig(ctx, false, true, 1L);
Assertions.assertNotEquals(cond1, cond2);
Assertions.assertNotEquals(cond1, cond3);
Assertions.assertNotEquals(cond1, cond4);
}

@Test
void testHashCode() {
ConditionConfig cond1 = new ConditionConfig(ctx, false, false);
ConditionConfig cond2 = new ConditionConfig(ctx, false, false);
ConditionConfig cond3 = new ConditionConfig(ctx, true, false);
ConditionConfig cond4 = new ConditionConfig(ctx, false, true);
ConditionConfig cond5 = new ConditionConfig(ctx, false, false, 1L);
Assertions.assertEquals(cond1.hashCode(), cond2.hashCode());
Assertions.assertNotEquals(cond1.hashCode(), cond3.hashCode());
Assertions.assertNotEquals(cond1.hashCode(), cond4.hashCode());
Assertions.assertNotEquals(cond1.hashCode(), cond5.hashCode());
}

@Test
public void equalsHashCodeContractTest() {
EqualsVerifier.forClass(ConditionConfig.class).verify();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*/
package com.teragrep.pth_06.planner.bloomfilter;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.apache.spark.util.sketch.BloomFilter;
import org.jooq.DSLContext;
import org.jooq.Table;
Expand Down Expand Up @@ -108,7 +109,7 @@ public void setup() {
}

@BeforeEach
void createTargetTable() {
public void createTargetTable() {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("CREATE SCHEMA IF NOT EXISTS BLOOMDB").execute();
conn.prepareStatement("USE BLOOMDB").execute();
Expand All @@ -123,7 +124,7 @@ void createTargetTable() {
}

@AfterAll
void tearDown() {
public void tearDown() {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("DROP ALL OBJECTS").execute(); //h2 clear database
conn.close();
Expand Down Expand Up @@ -254,6 +255,39 @@ public void testDifferentDSLContextNotEquals() {
Assertions.assertNotEquals(table1, table2);
}

@Test
public void testHashCode() {
fillTargetTable();
DSLContext ctx = DSL.using(conn);
Table<?> target1 = ctx
.meta()
.filterSchemas(s -> s.getName().equals("bloomdb"))
.filterTables(t -> !t.getName().equals("filtertype"))
.getTables()
.get(0);
Table<?> target2 = ctx
.meta()
.filterSchemas(s -> s.getName().equals("bloomdb"))
.filterTables(t -> !t.getName().equals("filtertype"))
.getTables()
.get(0);
CategoryTableImpl table1 = new CategoryTableImpl(ctx, target1, 1L, "one");
CategoryTableImpl table2 = new CategoryTableImpl(ctx, target2, 1L, "one");
CategoryTableImpl notEq1 = new CategoryTableImpl(ctx, target1, 2L, "one");
CategoryTableImpl notEq2 = new CategoryTableImpl(ctx, target1, 1L, "two");
Assertions.assertEquals(table1.hashCode(), table2.hashCode());
Assertions.assertNotEquals(table1.hashCode(), notEq1.hashCode());
Assertions.assertNotEquals(table1.hashCode(), notEq2.hashCode());
}

@Test
public void equalsHashCodeContractTest() {
EqualsVerifier
.forClass(CategoryTableImpl.class)
.withNonnullFields("ctx", "originTable", "bloomTermId", "tableFilters")
.verify();
}

void fillTargetTable() {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("CREATE SCHEMA IF NOT EXISTS BLOOMDB").execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*/
package com.teragrep.pth_06.planner.bloomfilter;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.apache.spark.util.sketch.BloomFilter;
import org.jooq.DSLContext;
import org.jooq.Named;
Expand Down Expand Up @@ -76,7 +77,7 @@ public class ConditionMatchBloomDBTablesTest {
final Connection conn = Assertions.assertDoesNotThrow(() -> DriverManager.getConnection(url, userName, password));

@BeforeAll
void setup() {
public void setup() {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("CREATE SCHEMA IF NOT EXISTS BLOOMDB").execute();
conn.prepareStatement("USE BLOOMDB").execute();
Expand Down Expand Up @@ -127,7 +128,7 @@ void setup() {
}

@AfterAll
void tearDown() {
public void tearDown() {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("DROP ALL OBJECTS").execute(); //h2 clear database
conn.close();
Expand Down Expand Up @@ -216,6 +217,11 @@ public void differentDSLContextNotEqualsTest() {
Assertions.assertNotEquals(eq2, eq1);
}

@Test
public void equalsVerifierTest() {
EqualsVerifier.forClass(ConditionMatchBloomDBTables.class).withNonnullFields("ctx", "condition").verify();
}

private void writeFilter(String tableName, int filterId) {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("CREATE SCHEMA IF NOT EXISTS BLOOMDB").execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*/
package com.teragrep.pth_06.planner.bloomfilter;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.apache.spark.util.sketch.BloomFilter;
import org.jooq.DSLContext;
import org.jooq.Record;
Expand All @@ -63,7 +64,7 @@
import java.util.List;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TableFilterTypesFromMetadataResultTest {
public class TableFilterTypesFromMetadataResultTest {

final String url = "jdbc:h2:mem:test;MODE=MariaDB;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE";
final String userName = "sa";
Expand Down Expand Up @@ -103,7 +104,7 @@ public void setup() {
}

@BeforeEach
void createTargetTable() {
public void createTargetTable() {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("CREATE SCHEMA IF NOT EXISTS BLOOMDB").execute();
conn.prepareStatement("USE BLOOMDB").execute();
Expand All @@ -118,15 +119,15 @@ void createTargetTable() {
}

@AfterAll
void tearDown() {
public void tearDown() {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("DROP ALL OBJECTS").execute(); //h2 clear database
conn.close();
});
}

@Test
void testNoFilterTypes() {
public void testNoFilterTypes() {
DSLContext ctx = DSL.using(conn);
Table<?> table = ctx
.meta()
Expand All @@ -140,7 +141,7 @@ void testNoFilterTypes() {
}

@Test
void testOneFilterType() {
public void testOneFilterType() {
insertSizedFilterIntoTargetTable(1);
DSLContext ctx = DSL.using(conn);
Table<?> table = ctx
Expand All @@ -157,7 +158,7 @@ void testOneFilterType() {
}

@Test
void testMultipleFilterTypes() {
public void testMultipleFilterTypes() {
insertSizedFilterIntoTargetTable(1);
insertSizedFilterIntoTargetTable(2);
DSLContext ctx = DSL.using(conn);
Expand Down Expand Up @@ -210,7 +211,31 @@ public void testNotEquals() {
Assertions.assertNotEquals(result1, result2);
}

void insertSizedFilterIntoTargetTable(int filterTypeId) {
@Test
public void testHashCode() {
DSLContext ctx = DSL.using(conn);
Table<?> table = ctx
.meta()
.filterSchemas(s -> s.getName().equals("bloomdb"))
.filterTables(t -> !t.getName().equals("filtertype"))
.getTables()
.get(0);
TableFilterTypesFromMetadata result1 = new TableFilterTypesFromMetadata(ctx, table, 0L);
TableFilterTypesFromMetadata result2 = new TableFilterTypesFromMetadata(ctx, table, 0L);
TableFilterTypesFromMetadata notEq = new TableFilterTypesFromMetadata(ctx, table, 1L);
Assertions.assertEquals(result1.hashCode(), result2.hashCode());
Assertions.assertNotEquals(result1.hashCode(), notEq.hashCode());
}

@Test
public void equalsHashCodeContractTest() {
EqualsVerifier
.forClass(TableFilterTypesFromMetadata.class)
.withNonnullFields("ctx", "table", "expectedField", "fppField", "patternField", "filterTypeIdField")
.verify();
}

private void insertSizedFilterIntoTargetTable(int filterTypeId) {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("CREATE SCHEMA IF NOT EXISTS BLOOMDB").execute();
conn.prepareStatement("USE BLOOMDB").execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
import java.util.List;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TableFiltersTest {
public class TableFiltersTest {

final String url = "jdbc:h2:mem:test;MODE=MariaDB;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE";
final String userName = "sa";
Expand Down Expand Up @@ -104,7 +104,7 @@ public void setup() {
}

@BeforeEach
void createTargetTable() {
public void createTargetTable() {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("CREATE SCHEMA IF NOT EXISTS BLOOMDB").execute();
conn.prepareStatement("USE BLOOMDB").execute();
Expand All @@ -122,7 +122,7 @@ void createTargetTable() {
}

@AfterAll
void tearDown() {
public void tearDown() {
Assertions.assertDoesNotThrow(() -> {
conn.prepareStatement("DROP ALL OBJECTS").execute(); //h2 clear database
conn.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
import java.util.Set;
import java.util.stream.Collectors;

class TokenizedValueTest {
public class TokenizedValueTest {

@Test
void testTokenization() {
public void testTokenization() {
TokenizedValue result = new TokenizedValue("test.nest");
Set<String> tokens = result.tokens().stream().map(Token::toString).collect(Collectors.toSet());
Assertions.assertTrue(tokens.contains("nest"));
Expand All @@ -69,7 +69,7 @@ void testTokenization() {
}

@Test
void testEquality() {
public void testEquality() {
TokenizedValue value1 = new TokenizedValue("test");
TokenizedValue value2 = new TokenizedValue("test");
Assertions.assertEquals(value1, value2);
Expand All @@ -78,7 +78,7 @@ void testEquality() {
}

@Test
void testNotEquals() {
public void testNotEquals() {
TokenizedValue value1 = new TokenizedValue("test");
TokenizedValue value2 = new TokenizedValue("nest");
Assertions.assertNotEquals(value1, value2);
Expand Down
Loading

0 comments on commit 9c6c070

Please sign in to comment.