Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand unit tests for ShardingSphereDatabase class #28646

Merged
merged 4 commits into from
Oct 16, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@

package org.apache.shardingsphere.infra.metadata.database;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;

import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;
Expand All @@ -31,51 +43,117 @@
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import java.util.HashMap;
import java.util.Map;

@ExtendWith(AutoMockExtension.class)
@StaticMockSettings(ShardingSphereDatabase.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class ShardingSphereDatabaseTest {


@Test
void assertContainsSchema() {
DatabaseType databaseType = mock(DatabaseType.class);
RuleMetaData ruleMetaData = mock(RuleMetaData.class);
Map<String, ShardingSphereSchema> schemas = new HashMap<>();
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
schemas.put("schema1", schema);
ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", databaseType, mock(ResourceMetaData.class), ruleMetaData, schemas);
assertTrue(database.containsSchema("schema1"));
assertFalse(database.containsSchema("non_existent_schema"));
}

@Test
void assertGetSchema() {
DatabaseType databaseType = mock(DatabaseType.class);
RuleMetaData ruleMetaData = mock(RuleMetaData.class);
Map<String, ShardingSphereSchema> schemas = new HashMap<>();
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
schemas.put("schema1", schema);
ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", databaseType, mock(ResourceMetaData.class), ruleMetaData, schemas);
assertEquals(schema, database.getSchema("schema1"));
Zhenye-Na marked this conversation as resolved.
Show resolved Hide resolved
assertNull(database.getSchema("non_existent_schema"));
}

@Test
void assertAddSchema() {
DatabaseType databaseType = mock(DatabaseType.class);
RuleMetaData ruleMetaData = mock(RuleMetaData.class);
Map<String, ShardingSphereSchema> schemas = new HashMap<>();
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", databaseType, mock(ResourceMetaData.class), ruleMetaData, schemas);
assertFalse(database.containsSchema("new_schema"));
database.addSchema("new_schema", schema);
assertTrue(database.containsSchema("new_schema"));
assertEquals(schema, database.getSchema("new_schema"));
}

@Test
void assertDropSchema() {
DatabaseType databaseType = mock(DatabaseType.class);
RuleMetaData ruleMetaData = mock(RuleMetaData.class);
Map<String, ShardingSphereSchema> schemas = new HashMap<>();
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
schemas.put("schema1", schema);
ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", databaseType, mock(ResourceMetaData.class), ruleMetaData, schemas);
assertTrue(database.containsSchema("schema1"));
database.dropSchema("schema1");
assertFalse(database.containsSchema("schema1"));
}

@Test
void assertIsComplete() {
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource()));
RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class)));
assertTrue(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).isComplete());
}

@Test
void assertIsNotCompleteWithoutRule() {
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource()));
RuleMetaData ruleMetaData = new RuleMetaData(Collections.emptyList());
assertFalse(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).isComplete());
}

@Test
void assertIsNotCompleteWithoutDataSource() {
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap());
RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class)));
assertFalse(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).isComplete());
}


@Test
void assertNotContainsDataSource() {
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap());
RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class)));
assertFalse(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).containsDataSource());
}

@Test
void assertContainsDataSource() {
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource()));
RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class)));
assertTrue(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).containsDataSource());
}

@Test
void assertReloadRules() {
ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource()));
Expand All @@ -88,15 +166,15 @@ void assertReloadRules() {
database.reloadRules(MutableDataNodeRule.class);
assertThat(database.getRuleMetaData().getRules().size(), is(3));
}

@Test
void assertGetPostgreSQLDefaultSchema() throws SQLException {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, Collections.singletonMap("", databaseType),
mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(InstanceContext.class));
assertNotNull(actual.getSchema("public"));
}

@Test
void assertGetMySQLDefaultSchema() throws SQLException {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL");
Expand Down
Loading