You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/10/09 15:54:53 UTC
[shardingsphere] branch master updated: Add test cases for sharding
strategy (#7721)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 10a05dd Add test cases for sharding strategy (#7721)
10a05dd is described below
commit 10a05dd600d4f24ed6a8a482150a4be49d47752e
Author: Serendipity <ja...@163.com>
AuthorDate: Fri Oct 9 23:54:30 2020 +0800
Add test cases for sharding strategy (#7721)
---
.../type/complex/ComplexShardingStrategyTest.java | 49 ++++++++++++++++
.../standard/StandardShardingStrategyTest.java | 68 ++++++++++++++++++++++
2 files changed, 117 insertions(+)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/strategy/type/complex/ComplexShardingStrategyTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/strategy/type/complex/ComplexShardingStrategyTest.java
new file mode 100644
index 0000000..6be6ba5
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/strategy/type/complex/ComplexShardingStrategyTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.sharding.route.strategy.type.complex;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Range;
+import com.google.common.collect.Sets;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
+import org.apache.shardingsphere.sharding.route.engine.condition.value.RangeShardingConditionValue;
+import org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingConditionValue;
+import org.apache.shardingsphere.sharding.route.strategy.fixture.ComplexKeysShardingAlgorithmFixture;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class ComplexShardingStrategyTest {
+
+ @Test
+ public void assertDoSharding() {
+ Collection<String> targets = Sets.newHashSet("1", "2", "3");
+ ComplexShardingStrategy complexShardingStrategy = new ComplexShardingStrategy("column1, column2", new ComplexKeysShardingAlgorithmFixture());
+ List<ShardingConditionValue> shardingConditionValues =
+ Lists.newArrayList(new ListShardingConditionValue<>("column1", "logicTable", Collections.singletonList(1)), new RangeShardingConditionValue<>("column2", "logicTable", Range.open(1, 3)));
+ Collection<String> actualSharding = complexShardingStrategy.doSharding(targets, shardingConditionValues, new ConfigurationProperties(new Properties()));
+ assertThat(actualSharding.size(), is(3));
+ assertThat(actualSharding, is(targets));
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/strategy/type/standard/StandardShardingStrategyTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/strategy/type/standard/StandardShardingStrategyTest.java
new file mode 100644
index 0000000..45be1c4
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/strategy/type/standard/StandardShardingStrategyTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.sharding.route.strategy.type.standard;
+
+import com.google.common.collect.Range;
+import com.google.common.collect.Sets;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Properties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
+import org.apache.shardingsphere.sharding.route.engine.condition.value.RangeShardingConditionValue;
+import org.apache.shardingsphere.sharding.route.strategy.fixture.StandardShardingAlgorithmFixture;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class StandardShardingStrategyTest {
+
+ private final Collection<String> targets = Sets.newHashSet("1", "2", "3");
+
+ private StandardShardingStrategy standardShardingStrategy;
+
+ @Before
+ public void setUp() {
+ standardShardingStrategy = new StandardShardingStrategy("column", new StandardShardingAlgorithmFixture());
+ }
+
+ @Test
+ public void assertDoShardingForRangeSharding() {
+ Collection<String> actualRangeSharding = standardShardingStrategy
+ .doSharding(targets, Collections.singletonList(new RangeShardingConditionValue<>("column", "logicTable", Range.open(1, 3))), new ConfigurationProperties(new Properties()));
+ assertThat(actualRangeSharding.size(), is(1));
+ assertThat(actualRangeSharding.iterator().next(), is("1"));
+ }
+
+ @Test
+ public void assertDoShardingForListSharding() {
+ Collection<String> actualListSharding = standardShardingStrategy
+ .doSharding(targets, Collections.singletonList(new ListShardingConditionValue<>("column", "logicTable", Collections.singletonList(1))), new ConfigurationProperties(new Properties()));
+ assertThat(actualListSharding.size(), is(1));
+ assertThat(actualListSharding.iterator().next(), is("1"));
+ }
+
+ @Test
+ public void assertGetShardingColumns() {
+ Collection<String> actualShardingColumns = standardShardingStrategy.getShardingColumns();
+ assertThat(actualShardingColumns.size(), is(1));
+ assertThat(actualShardingColumns.iterator().next(), is("column"));
+ }
+}