This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 6fe97dc7b6b Add test cases on ShardingSphereTableRowDataPersistService
(#32979)
6fe97dc7b6b is described below
commit 6fe97dc7b6b54235a27be32b6a6b4c9a90ea3966
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Sep 24 15:47:32 2024 +0800
Add test cases on ShardingSphereTableRowDataPersistService (#32979)
---
.../ShardingSphereTableRowDataPersistService.java | 9 +--
...ardingSphereTableRowDataPersistServiceTest.java | 94 ++++++++++++++++++++++
2 files changed, 97 insertions(+), 6 deletions(-)
diff --git
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ShardingSphereTableRowDataPersistService.java
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ShardingSphereTableRowDataPersistService.java
index 423d30b3782..bea4bc8ee8c 100644
---
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ShardingSphereTableRowDataPersistService.java
+++
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/schema/ShardingSphereTableRowDataPersistService.java
@@ -49,13 +49,10 @@ public final class ShardingSphereTableRowDataPersistService
implements TableRowD
@Override
public void persist(final String databaseName, final String schemaName,
final String tableName, final Collection<YamlShardingSphereRowData> rows) {
if (rows.isEmpty()) {
- persistTable(databaseName, schemaName, tableName);
+
repository.persist(ShardingSphereDataNode.getTablePath(databaseName,
schemaName, tableName.toLowerCase()), "");
+ } else {
+ rows.forEach(each ->
repository.persist(ShardingSphereDataNode.getTableRowPath(databaseName,
schemaName, tableName.toLowerCase(), each.getUniqueKey()),
YamlEngine.marshal(each)));
}
- rows.forEach(each ->
repository.persist(ShardingSphereDataNode.getTableRowPath(databaseName,
schemaName, tableName.toLowerCase(), each.getUniqueKey()),
YamlEngine.marshal(each)));
- }
-
- private void persistTable(final String databaseName, final String
schemaName, final String tableName) {
- repository.persist(ShardingSphereDataNode.getTablePath(databaseName,
schemaName, tableName.toLowerCase()), "");
}
/**
diff --git
a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/ShardingSphereTableRowDataPersistServiceTest.java
b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/ShardingSphereTableRowDataPersistServiceTest.java
new file mode 100644
index 00000000000..746d185446b
--- /dev/null
+++
b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/schema/ShardingSphereTableRowDataPersistServiceTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.metadata.persist.service.schema;
+
+import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
+import
org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData;
+import org.apache.shardingsphere.mode.spi.PersistRepository;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class ShardingSphereTableRowDataPersistServiceTest {
+
+ private ShardingSphereTableRowDataPersistService persistService;
+
+ @Mock
+ private PersistRepository repository;
+
+ @BeforeEach
+ void setUp() {
+ persistService = new
ShardingSphereTableRowDataPersistService(repository);
+ }
+
+ @Test
+ void assertPersistWithoutRows() {
+ persistService.persist("foo_db", "foo_schema", "foo_tbl",
Collections.emptyList());
+
verify(repository).persist("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl",
"");
+ }
+
+ @Test
+ void assertPersistWithRows() {
+ YamlShardingSphereRowData rowData = new YamlShardingSphereRowData();
+ rowData.setUniqueKey("foo_key");
+ persistService.persist("foo_db", "foo_schema", "foo_tbl",
Collections.singletonList(rowData));
+
verify(repository).persist("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/foo_key",
"uniqueKey: foo_key\n");
+ }
+
+ @Test
+ void assertDelete() {
+ YamlShardingSphereRowData rowData = new YamlShardingSphereRowData();
+ rowData.setUniqueKey("foo_key");
+ persistService.delete("foo_db", "foo_schema", "foo_tbl",
Collections.singletonList(rowData));
+
verify(repository).delete("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/foo_key");
+ }
+
+ @Test
+ void assertLoadWithRowData() {
+
when(repository.getChildrenKeys("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl")).thenReturn(Collections.singletonList("foo_tbl"));
+
when(repository.query("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/foo_tbl")).thenReturn("uniqueKey:
foo_key\n");
+ ShardingSphereTable table = mock(ShardingSphereTable.class);
+ ShardingSphereTableData actual = persistService.load("foo_db",
"foo_schema", "foo_tbl", table);
+ assertThat(actual.getName(), is("foo_tbl"));
+ assertThat(actual.getRows().size(), is(1));
+ assertThat(actual.getRows().iterator().next().getUniqueKey(),
is("foo_key"));
+ }
+
+ @Test
+ void assertLoadWithoutRowData() {
+
when(repository.getChildrenKeys("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl")).thenReturn(Collections.singletonList("foo_tbl"));
+
when(repository.query("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/foo_tbl")).thenReturn("");
+ ShardingSphereTable table = mock(ShardingSphereTable.class);
+ ShardingSphereTableData actual = persistService.load("foo_db",
"foo_schema", "foo_tbl", table);
+ assertThat(actual.getName(), is("foo_tbl"));
+ assertTrue(actual.getRows().isEmpty());
+ }
+}