Skip to content

Commit

Permalink
Add test cases on SingleTableLoadUtils (#33604)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 9, 2024
1 parent f489e89 commit d5d3802
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
* Single table load utils.
Expand Down Expand Up @@ -72,10 +73,9 @@ public static Collection<String> getFeatureRequiredSingleTables(final Collection
if (!ruleAttribute.isPresent()) {
continue;
}
if (ruleAttribute.get().getEnhancedTableNames().isEmpty() || !ruleAttribute.get().getDistributedTableNames().isEmpty()) {
continue;
if (!ruleAttribute.get().getEnhancedTableNames().isEmpty() && ruleAttribute.get().getDistributedTableNames().isEmpty()) {
result.addAll(ruleAttribute.get().getEnhancedTableNames());
}
result.addAll(ruleAttribute.get().getEnhancedTableNames());
}
return result;
}
Expand Down Expand Up @@ -107,11 +107,7 @@ public static Collection<String> splitTableLines(final Collection<String> tables
* @return data nodes
*/
public static Collection<DataNode> convertToDataNodes(final String databaseName, final DatabaseType databaseType, final Collection<String> tables) {
Collection<DataNode> result = new LinkedHashSet<>(tables.size(), 1F);
for (String each : tables) {
result.add(new DataNode(databaseName, databaseType, each));
}
return result;
return tables.stream().map(each -> new DataNode(databaseName, databaseType, each)).collect(Collectors.toCollection(() -> new LinkedHashSet<>(tables.size(), 1F)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.single.util;

import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.TreeSet;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(AutoMockExtension.class)
class SingleTableLoadUtilsTest {

private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");

@Test
void assertGetExcludedTables() {
ShardingSphereRule builtRule1 = mock(ShardingSphereRule.class);
TableMapperRuleAttribute tableMapperRuleAttribute = mock(TableMapperRuleAttribute.class);
when(tableMapperRuleAttribute.getDistributedTableNames()).thenReturn(Collections.singleton("dist_tbl"));
when(tableMapperRuleAttribute.getActualTableNames()).thenReturn(Collections.singleton("actual_tbl"));
when(builtRule1.getAttributes()).thenReturn(new RuleAttributes(tableMapperRuleAttribute));
ShardingSphereRule builtRule2 = mock(ShardingSphereRule.class);
when(builtRule2.getAttributes()).thenReturn(new RuleAttributes());
assertThat(SingleTableLoadUtils.getExcludedTables(Arrays.asList(builtRule1, builtRule2)), is(new TreeSet<>(Arrays.asList("dist_tbl", "actual_tbl"))));
}

@Test
void assertGetFeatureRequiredSingleTablesWithEmptyEnhancedTableNames() {
ShardingSphereRule builtRule1 = mock(ShardingSphereRule.class);
TableMapperRuleAttribute tableMapperRuleAttribute = mock(TableMapperRuleAttribute.class);
when(tableMapperRuleAttribute.getEnhancedTableNames()).thenReturn(Collections.emptyList());
when(builtRule1.getAttributes()).thenReturn(new RuleAttributes(tableMapperRuleAttribute));
ShardingSphereRule builtRule2 = mock(ShardingSphereRule.class);
when(builtRule2.getAttributes()).thenReturn(new RuleAttributes());
assertThat(SingleTableLoadUtils.getFeatureRequiredSingleTables(Arrays.asList(builtRule1, builtRule2)), is(Collections.emptySet()));
}

@Test
void assertGetFeatureRequiredSingleTablesWithDistributedTableNames() {
ShardingSphereRule builtRule1 = mock(ShardingSphereRule.class);
TableMapperRuleAttribute tableMapperRuleAttribute = mock(TableMapperRuleAttribute.class);
when(tableMapperRuleAttribute.getEnhancedTableNames()).thenReturn(Collections.singleton("enhanced_tbl"));
when(tableMapperRuleAttribute.getDistributedTableNames()).thenReturn(Collections.singleton("dist_tbl"));
when(builtRule1.getAttributes()).thenReturn(new RuleAttributes(tableMapperRuleAttribute));
ShardingSphereRule builtRule2 = mock(ShardingSphereRule.class);
when(builtRule2.getAttributes()).thenReturn(new RuleAttributes());
assertThat(SingleTableLoadUtils.getFeatureRequiredSingleTables(Arrays.asList(builtRule1, builtRule2)), is(Collections.emptySet()));
}

@Test
void assertGetFeatureRequiredSingleTablesWithoutDistributedTableNames() {
ShardingSphereRule builtRule1 = mock(ShardingSphereRule.class);
TableMapperRuleAttribute tableMapperRuleAttribute = mock(TableMapperRuleAttribute.class);
when(tableMapperRuleAttribute.getEnhancedTableNames()).thenReturn(Collections.singleton("enhanced_tbl"));
when(tableMapperRuleAttribute.getDistributedTableNames()).thenReturn(Collections.emptyList());
when(builtRule1.getAttributes()).thenReturn(new RuleAttributes(tableMapperRuleAttribute));
ShardingSphereRule builtRule2 = mock(ShardingSphereRule.class);
when(builtRule2.getAttributes()).thenReturn(new RuleAttributes());
assertThat(SingleTableLoadUtils.getFeatureRequiredSingleTables(Arrays.asList(builtRule1, builtRule2)), is(Collections.singleton("enhanced_tbl")));
}

@Test
void assertSplitTableLines() {
assertThat(SingleTableLoadUtils.splitTableLines(Arrays.asList("foo_tbl", "bar_tbl0,bar_tbl1")), is(new LinkedHashSet<>(Arrays.asList("foo_tbl", "bar_tbl0", "bar_tbl1"))));
}

@Test
void assertConvertToDataNodes() {
DataNode expectedDataNode1 = new DataNode("foo_ds.foo_tbl");
expectedDataNode1.setSchemaName("foo_db");
DataNode expectedDataNode2 = new DataNode("bar_ds.bar_tbl");
expectedDataNode2.setSchemaName("foo_db");
assertThat(SingleTableLoadUtils.convertToDataNodes("foo_db", databaseType, Arrays.asList("foo_ds.foo_tbl", "bar_ds.bar_tbl")),
is(new LinkedHashSet<>(Arrays.asList(expectedDataNode1, expectedDataNode2))));
}

@Test
void assertGetAllTablesNodeStr() {
assertThat(SingleTableLoadUtils.getAllTablesNodeStr(databaseType), is("*.*"));
}

@Test
void assertGetAllTablesNodeStrFromDataSource() {
assertThat(SingleTableLoadUtils.getAllTablesNodeStrFromDataSource(databaseType, "foo_ds", "foo_schema"), is("foo_ds.*"));
}

@Test
void assertGetDataNodeString() {
assertThat(SingleTableLoadUtils.getDataNodeString(databaseType, "foo_ds", "foo_schema", "foo_tbl"), is("foo_ds.foo_tbl"));
}
}

0 comments on commit d5d3802

Please sign in to comment.