Skip to content

Commit

Permalink
Add ShardingSphereDatabaseDataTest (#34080)
Browse files Browse the repository at this point in the history
* Add ShardingSphereDatabaseDataTest

* Add ShardingSphereDatabaseDataTest
  • Loading branch information
terrymanu authored Dec 16, 2024
1 parent cbda874 commit 259a5ef
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,42 @@ public final class ShardingSphereDatabaseData {

private final Map<String, ShardingSphereSchemaData> schemaData = new CaseInsensitiveMap<>();

/**
* Judge whether to contains schema.
*
* @param schemaName schema name
* @return contains schema or not
*/
public boolean containsSchema(final String schemaName) {
return schemaData.containsKey(schemaName);
}

/**
* Get ShardingSphere schema data.
*
* @param schemaName schema name
* @return ShardingSphere schema data
* @return schema data
*/
public ShardingSphereSchemaData getSchema(final String schemaName) {
return schemaData.get(schemaName);
}

/**
* Put ShardingSphere schema data.
* Put schema data.
*
* @param schemaName schema name
* @param schema ShardingSphere schema data
* @param schema schema data
*/
public void putSchema(final String schemaName, final ShardingSphereSchemaData schema) {
schemaData.put(schemaName, schema);
}

/**
* Remove ShardingSphere schema data.
* Remove schema data.
*
* @param schemaName schema name
*/
public void removeSchema(final String schemaName) {
schemaData.remove(schemaName);
}

/**
* Judge contains ShardingSphere schema from ShardingSphere database or not.
*
* @param schemaName schema name
* @return Contains schema from database or not
*/
public boolean containsSchema(final String schemaName) {
return schemaData.containsKey(schemaName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.infra.metadata.statistics;

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

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ShardingSphereDatabaseDataTest {

private final ShardingSphereDatabaseData databaseData = new ShardingSphereDatabaseData();

@BeforeEach
void setUp() {
databaseData.putSchema("foo_schema", new ShardingSphereSchemaData());
}

@Test
void assertContainsSchema() {
assertTrue(databaseData.containsSchema("foo_schema"));
assertFalse(databaseData.containsSchema("bar_schema"));
}

@Test
void assertGetSchema() {
assertTrue(databaseData.getSchema("foo_schema").getTableData().isEmpty());
assertNull(databaseData.getSchema("bar_schema"));
}

@Test
void assertPutSchema() {
databaseData.putSchema("bar_schema", new ShardingSphereSchemaData());
assertTrue(databaseData.containsSchema("bar_schema"));
}

@Test
void assertRemoveSchema() {
databaseData.removeSchema("foo_schema");
assertFalse(databaseData.containsSchema("foo_schema"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Optional<ShardingSphereStatistics> load(final ShardingSphereMetaData meta
private ShardingSphereDatabaseData load(final String databaseName, final ShardingSphereDatabase database) {
ShardingSphereDatabaseData result = new ShardingSphereDatabaseData();
for (String each : repository.getChildrenKeys(ShardingSphereDataNode.getSchemasPath(databaseName)).stream().filter(database::containsSchema).collect(Collectors.toList())) {
result.getSchemaData().put(each, load(databaseName, each, database.getSchema(each)));
result.putSchema(each, load(databaseName, each, database.getSchema(each)));
}
return result;
}
Expand Down

0 comments on commit 259a5ef

Please sign in to comment.