Skip to content

Commit

Permalink
Remove useless ShardingSphereTable.putColumn (#33785)
Browse files Browse the repository at this point in the history
* Remove useless ShardingSphereTable.putColumn

* Remove useless ShardingSphereTable.putColumn
  • Loading branch information
terrymanu authored Nov 24, 2024
1 parent d8e0c48 commit 65011f9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.apache.shardingsphere.infra.metadata.database.schema.model;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -33,121 +33,99 @@

class ShardingSphereTableTest {

private ShardingSphereTable shardingSphereTable;

@BeforeEach
void setUp() {
shardingSphereTable = new ShardingSphereTable();
}

@Test
void assertPutColumn() {
ShardingSphereColumn column1 = new ShardingSphereColumn("foo_column_1", Types.INTEGER, true, true, false, true, false, false);
ShardingSphereColumn column2 = new ShardingSphereColumn("foo_column_2", Types.INTEGER, false, true, false, true, false, false);
shardingSphereTable.putColumn(column1);
shardingSphereTable.putColumn(column2);
assertThat(shardingSphereTable.getColumn("foo_column_1"), is(column1));
assertThat(shardingSphereTable.getColumn("foo_column_2"), is(column2));
assertThat(shardingSphereTable.getColumnValues(), hasSize(2));
}

@Test
void assertGetColumn() {
ShardingSphereColumn column = new ShardingSphereColumn("foo_column", Types.INTEGER, true, true, false, true, false, false);
shardingSphereTable.putColumn(column);
assertThat(shardingSphereTable.getColumn("foo_column"), is(column));
assertThat(shardingSphereTable.getColumn("FOO_COLUMN"), is(column));
ShardingSphereColumn column = new ShardingSphereColumn("foo_col", Types.INTEGER, true, true, false, true, false, false);
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Collections.singleton(column), Collections.emptyList(), Collections.emptyList());
assertThat(shardingSphereTable.getColumn("foo_col"), is(column));
assertThat(shardingSphereTable.getColumn("FOO_COL"), is(column));
assertNull(shardingSphereTable.getColumn("invalid"));
}

@Test
void assertGetColumns() {
ShardingSphereColumn column1 = new ShardingSphereColumn("foo_column_1", Types.INTEGER, true, true, false, true, false, false);
ShardingSphereColumn column2 = new ShardingSphereColumn("foo_column_2", Types.INTEGER, false, true, false, true, false, false);
shardingSphereTable.putColumn(column1);
shardingSphereTable.putColumn(column2);
void assertGetColumnValues() {
ShardingSphereColumn column1 = new ShardingSphereColumn("foo_col_1", Types.INTEGER, true, true, false, true, false, false);
ShardingSphereColumn column2 = new ShardingSphereColumn("foo_col_2", Types.INTEGER, false, true, false, true, false, false);
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Arrays.asList(column1, column2), Collections.emptyList(), Collections.emptyList());
assertThat(shardingSphereTable.getColumnValues(), hasItems(column1, column2));
assertThat(shardingSphereTable.getColumnValues(), hasSize(2));
}

@Test
void assertContainsColumn() {
ShardingSphereColumn column = new ShardingSphereColumn("foo_column", Types.INTEGER, true, true, false, true, false, false);
shardingSphereTable.putColumn(column);
assertTrue(shardingSphereTable.containsColumn("foo_column"));
ShardingSphereColumn column = new ShardingSphereColumn("foo_col", Types.INTEGER, true, true, false, true, false, false);
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Collections.singleton(column), Collections.emptyList(), Collections.emptyList());
assertTrue(shardingSphereTable.containsColumn("foo_col"));
assertFalse(shardingSphereTable.containsColumn("invalid"));
}

@Test
void assertPutIndex() {
ShardingSphereIndex index1 = new ShardingSphereIndex("foo_index_1");
ShardingSphereIndex index2 = new ShardingSphereIndex("foo_index_2");
shardingSphereTable.putIndex(index1);
shardingSphereTable.putIndex(index2);
assertThat(shardingSphereTable.getIndex("foo_index_1"), is(index1));
assertThat(shardingSphereTable.getIndex("foo_index_2"), is(index2));
ShardingSphereIndex index1 = new ShardingSphereIndex("foo_idx_1");
ShardingSphereIndex index2 = new ShardingSphereIndex("foo_idx_2");
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Arrays.asList(index1, index2), Collections.emptyList());
assertThat(shardingSphereTable.getIndex("foo_idx_1"), is(index1));
assertThat(shardingSphereTable.getIndex("foo_idx_2"), is(index2));
assertNull(shardingSphereTable.getIndex("invalid"));
assertThat(shardingSphereTable.getIndexValues(), hasSize(2));
}

@Test
void assertGetIndex() {
ShardingSphereIndex index = new ShardingSphereIndex("foo_index");
shardingSphereTable.putIndex(index);
assertThat(shardingSphereTable.getIndex("foo_index"), is(index));
assertThat(shardingSphereTable.getIndex("FOO_INDEX"), is(index));
ShardingSphereIndex index = new ShardingSphereIndex("foo_idx");
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.singleton(index), Collections.emptyList());
assertThat(shardingSphereTable.getIndex("foo_idx"), is(index));
assertThat(shardingSphereTable.getIndex("FOO_IDX"), is(index));
assertNull(shardingSphereTable.getIndex("invalid"));
}

@Test
void assertRemoveIndex() {
ShardingSphereIndex index1 = new ShardingSphereIndex("foo_index_1");
ShardingSphereIndex index2 = new ShardingSphereIndex("foo_index_2");
shardingSphereTable.putIndex(index1);
shardingSphereTable.putIndex(index2);
shardingSphereTable.removeIndex("foo_index_1");
assertNull(shardingSphereTable.getIndex("foo_index_1"));
ShardingSphereIndex index1 = new ShardingSphereIndex("foo_idx_1");
ShardingSphereIndex index2 = new ShardingSphereIndex("foo_idx_2");
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Arrays.asList(index1, index2), Collections.emptyList());
shardingSphereTable.removeIndex("foo_idx_1");
assertNull(shardingSphereTable.getIndex("foo_idx_1"));
shardingSphereTable.removeIndex("invalid");
assertThat(shardingSphereTable.getIndex("foo_index_2"), is(index2));
assertThat(shardingSphereTable.getIndex("foo_idx_2"), is(index2));
assertThat(shardingSphereTable.getIndexValues(), hasSize(1));
}

@Test
void assertGetIndexes() {
ShardingSphereIndex index1 = new ShardingSphereIndex("foo_index_1");
ShardingSphereIndex index2 = new ShardingSphereIndex("foo_index_2");
shardingSphereTable.putIndex(index1);
shardingSphereTable.putIndex(index2);
ShardingSphereIndex index1 = new ShardingSphereIndex("foo_idx_1");
ShardingSphereIndex index2 = new ShardingSphereIndex("foo_idx_2");
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Arrays.asList(index1, index2), Collections.emptyList());
assertThat(shardingSphereTable.getIndexValues(), hasItems(index1, index2));
assertThat(shardingSphereTable.getIndexValues(), hasSize(2));
}

@Test
void assertContainsIndex() {
ShardingSphereIndex index1 = new ShardingSphereIndex("foo_index_1");
ShardingSphereIndex index2 = new ShardingSphereIndex("foo_index_2");
shardingSphereTable.putIndex(index1);
shardingSphereTable.putIndex(index2);
assertTrue(shardingSphereTable.containsIndex("foo_index_1"));
assertTrue(shardingSphereTable.containsIndex("foo_index_2"));
ShardingSphereIndex index1 = new ShardingSphereIndex("foo_idx_1");
ShardingSphereIndex index2 = new ShardingSphereIndex("foo_idx_2");
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Arrays.asList(index1, index2), Collections.emptyList());
assertTrue(shardingSphereTable.containsIndex("foo_idx_1"));
assertTrue(shardingSphereTable.containsIndex("foo_idx_2"));
assertFalse(shardingSphereTable.containsIndex("invalid"));
}

@Test
void assertGetConstraints() {
ShardingSphereConstraint constraint = new ShardingSphereConstraint("t_order_foreign_key", "t_user");
ShardingSphereTable table = new ShardingSphereTable("t_order", Collections.emptyList(), Collections.emptyList(), Collections.singletonList(constraint));
ShardingSphereConstraint constraint = new ShardingSphereConstraint("foo_tbl_foreign_key", "foo_tbl");
ShardingSphereTable table = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.singletonList(constraint));
assertThat(table.getConstraintValues(), hasItems(constraint));
assertThat(table.getConstraintValues(), hasSize(1));
}

@Test
void assertTableEquals() {
shardingSphereTable.putColumn(new ShardingSphereColumn("foo_column_1", Types.INTEGER, true, true, false, true, false, false));
shardingSphereTable.putIndex(new ShardingSphereIndex("foo_index_1"));
ShardingSphereTable otherTable = new ShardingSphereTable();
otherTable.putColumn(new ShardingSphereColumn("foo_column_1", Types.INTEGER, true, true, false, true, false, false));
otherTable.putIndex(new ShardingSphereIndex("foo_index_1"));
assertThat(shardingSphereTable, is(otherTable));
void assertEquals() {
ShardingSphereTable table1 = new ShardingSphereTable("foo_tbl",
Collections.singleton(new ShardingSphereColumn("foo_col", Types.INTEGER, true, true, false, true, false, false)),
Collections.singleton(new ShardingSphereIndex("foo_idx")), Collections.emptyList());
ShardingSphereTable table2 = new ShardingSphereTable("foo_tbl",
Collections.singleton(new ShardingSphereColumn("foo_col", Types.INTEGER, true, true, false, true, false, false)),
Collections.singleton(new ShardingSphereIndex("foo_idx")), Collections.emptyList());
assertThat(table1, is(table2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.junit.jupiter.api.Test;

import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -49,10 +51,10 @@ void assertFindColumnsOfParameterMarkersForInsertStatement() {
}

private ShardingSphereSchema prepareSchema() {
ShardingSphereTable table = new ShardingSphereTable();
table.putColumn(new ShardingSphereColumn("id", Types.BIGINT, true, false, false, false, true, false));
table.putColumn(new ShardingSphereColumn("name", Types.VARCHAR, false, false, false, false, false, false));
table.putColumn(new ShardingSphereColumn("age", Types.SMALLINT, false, false, false, false, true, false));
ShardingSphereTable table = new ShardingSphereTable("user", Arrays.asList(
new ShardingSphereColumn("id", Types.BIGINT, true, false, false, false, true, false),
new ShardingSphereColumn("name", Types.VARCHAR, false, false, false, false, false, false),
new ShardingSphereColumn("age", Types.SMALLINT, false, false, false, false, true, false)), Collections.emptyList(), Collections.emptyList());
ShardingSphereSchema result = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME);
result.getTables().put("user", table);
return result;
Expand Down

0 comments on commit 65011f9

Please sign in to comment.