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 2024/11/17 07:31:47 UTC
(shardingsphere) branch master updated: Add more test cases on IntervalShardingAlgorithm (#33697)
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 75da004fc4d Add more test cases on IntervalShardingAlgorithm (#33697)
75da004fc4d is described below
commit 75da004fc4dbb876003896a0a141d5cec3a151eb
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Nov 17 15:31:40 2024 +0800
Add more test cases on IntervalShardingAlgorithm (#33697)
---
.../datetime/IntervalShardingAlgorithm.java | 35 ++++++++++------------
.../datetime/IntervalShardingAlgorithmTest.java | 14 +++++++++
2 files changed, 29 insertions(+), 20 deletions(-)
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
index ed829da38a3..2e7bb54dc07 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
@@ -303,42 +303,37 @@ public final class IntervalShardingAlgorithm implements StandardShardingAlgorith
private LocalDateTime parseLocalDateTime(final Comparable<?> endpoint) {
String dateTimeText = getDateTimeText(endpoint);
- if (dateTimeText.length() >= dateTimePatternLength) {
- return LocalDateTime.parse(dateTimeText.substring(0, dateTimePatternLength), dateTimeFormatter);
- }
- return LocalDateTime.parse(dateTimeText, createRelaxedDateTimeFormatter(dateTimeText));
+ return dateTimeText.length() >= dateTimePatternLength
+ ? LocalDateTime.parse(dateTimeText.substring(0, dateTimePatternLength), dateTimeFormatter)
+ : LocalDateTime.parse(dateTimeText, createRelaxedDateTimeFormatter(dateTimeText));
}
private LocalDate parseLocalDate(final Comparable<?> endpoint) {
String dateTimeText = getDateTimeText(endpoint);
- if (dateTimeText.length() >= dateTimePatternLength) {
- return LocalDate.parse(dateTimeText.substring(0, dateTimePatternLength), dateTimeFormatter);
- }
- return LocalDate.parse(dateTimeText, createRelaxedDateTimeFormatter(dateTimeText));
+ return dateTimeText.length() >= dateTimePatternLength
+ ? LocalDate.parse(dateTimeText.substring(0, dateTimePatternLength), dateTimeFormatter)
+ : LocalDate.parse(dateTimeText, createRelaxedDateTimeFormatter(dateTimeText));
}
private LocalTime parseLocalTime(final Comparable<?> endpoint) {
String dateTimeText = getDateTimeText(endpoint);
- if (dateTimeText.length() >= dateTimePatternLength) {
- return LocalTime.parse(dateTimeText.substring(0, dateTimePatternLength), dateTimeFormatter);
- }
- return LocalTime.parse(dateTimeText, createRelaxedDateTimeFormatter(dateTimeText));
+ return dateTimeText.length() >= dateTimePatternLength
+ ? LocalTime.parse(dateTimeText.substring(0, dateTimePatternLength), dateTimeFormatter)
+ : LocalTime.parse(dateTimeText, createRelaxedDateTimeFormatter(dateTimeText));
}
private Year parseYear(final Comparable<?> endpoint) {
String dateTimeText = getDateTimeText(endpoint);
- if (dateTimeText.length() >= dateTimePatternLength) {
- return Year.parse(dateTimeText.substring(0, dateTimePatternLength), dateTimeFormatter);
- }
- return Year.parse(dateTimeText, createRelaxedDateTimeFormatter(dateTimeText));
+ return dateTimeText.length() >= dateTimePatternLength
+ ? Year.parse(dateTimeText.substring(0, dateTimePatternLength), dateTimeFormatter)
+ : Year.parse(dateTimeText, createRelaxedDateTimeFormatter(dateTimeText));
}
private YearMonth parseYearMonth(final Comparable<?> endpoint) {
String dateTimeText = getDateTimeText(endpoint);
- if (dateTimeText.length() >= dateTimePatternLength) {
- return YearMonth.parse(dateTimeText.substring(0, dateTimePatternLength), dateTimeFormatter);
- }
- return YearMonth.parse(dateTimeText, createRelaxedDateTimeFormatter(dateTimeText));
+ return dateTimeText.length() >= dateTimePatternLength
+ ? YearMonth.parse(dateTimeText.substring(0, dateTimePatternLength), dateTimeFormatter)
+ : YearMonth.parse(dateTimeText, createRelaxedDateTimeFormatter(dateTimeText));
}
/**
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
index 26af2bc50fb..64c2cd8575e 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
@@ -19,10 +19,12 @@ package org.apache.shardingsphere.sharding.algorithm.sharding.datetime;
import com.google.common.collect.Range;
import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
+import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.util.datetime.DateTimeFormatterFactory;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.sharding.exception.data.InvalidDatetimeFormatException;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
@@ -106,6 +108,18 @@ class IntervalShardingAlgorithmTest {
}
}
+ @Test
+ void assertInitFailedWithInvalidDatetimeFormat() {
+ assertThrows(InvalidDatetimeFormatException.class,
+ () -> TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", PropertiesBuilder.build(new Property("datetime-pattern", "yyyy"), new Property("datetime-lower", "invalid"))));
+ }
+
+ @Test
+ void assertInitFailedWithInvalidStepUnit() {
+ assertThrows(UnsupportedSQLOperationException.class, () -> TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", PropertiesBuilder.build(
+ new Property("datetime-pattern", "yy"), new Property("datetime-lower", "16"), new Property("sharding-suffix-pattern", "yy"), new Property("datetime-interval-unit", "invalid"))));
+ }
+
@Test
void assertPreciseDoShardingByQuarter() {
assertThat(shardingAlgorithmByQuarter.doSharding(availableTablesForQuarterDataSources,