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 afce58917e3 Add missing unit tests for DistSQL context and transaction 
utils (#37490)
afce58917e3 is described below

commit afce58917e3c229cce961e746be287198d25f419
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Dec 24 13:40:29 2025 +0800

    Add missing unit tests for DistSQL context and transaction utils (#37490)
---
 .../distsql/DistSQLStatementContextTest.java       | 31 ++++++++
 .../distsql/ral/common/DistSQLVariableTest.java    | 38 ++++++++++
 .../proxy/backend/util/TransactionUtilsTest.java   | 85 ++++++++++++++++++++++
 3 files changed, 154 insertions(+)

diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/distsql/DistSQLStatementContextTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/distsql/DistSQLStatementContextTest.java
new file mode 100644
index 00000000000..9a94ee250e7
--- /dev/null
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/distsql/DistSQLStatementContextTest.java
@@ -0,0 +1,31 @@
+/*
+ * 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.proxy.backend.distsql;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+class DistSQLStatementContextTest {
+    
+    @Test
+    void assertGetTablesContext() {
+        assertTrue(new 
DistSQLStatementContext(mock()).getTablesContext().getSimpleTables().isEmpty());
+    }
+}
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/DistSQLVariableTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/DistSQLVariableTest.java
new file mode 100644
index 00000000000..9f6d2d6410a
--- /dev/null
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/DistSQLVariableTest.java
@@ -0,0 +1,38 @@
+/*
+ * 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.proxy.backend.handler.distsql.ral.common;
+
+import 
org.apache.shardingsphere.infra.exception.kernel.syntax.UnsupportedVariableException;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class DistSQLVariableTest {
+    
+    @Test
+    void assertGetValueOfExistingVariable() {
+        assertThat(DistSQLVariable.getValueOf("cached_connections"), 
is(DistSQLVariable.CACHED_CONNECTIONS));
+    }
+    
+    @Test
+    void assertGetValueOfUnsupportedVariable() {
+        assertThrows(UnsupportedVariableException.class, () -> 
DistSQLVariable.getValueOf("unsupported"));
+    }
+}
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/TransactionUtilsTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/TransactionUtilsTest.java
new file mode 100644
index 00000000000..9b3c1017f21
--- /dev/null
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/TransactionUtilsTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.proxy.backend.util;
+
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import 
org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext;
+import 
org.apache.shardingsphere.infra.session.connection.transaction.TransactionManager;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import 
org.apache.shardingsphere.sql.parser.statement.core.enums.TransactionIsolationLevel;
+import 
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import 
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.apache.shardingsphere.transaction.api.TransactionType;
+import org.apache.shardingsphere.transaction.rule.TransactionRule;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+
+import java.sql.Connection;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(ProxyContext.class)
+class TransactionUtilsTest {
+    
+    @Mock
+    private TransactionManager transactionManager;
+    
+    @Test
+    void assertGetTransactionIsolationLevelByEnum() {
+        
assertThat(TransactionUtils.getTransactionIsolationLevel(TransactionIsolationLevel.READ_UNCOMMITTED),
 is(Connection.TRANSACTION_READ_UNCOMMITTED));
+        
assertThat(TransactionUtils.getTransactionIsolationLevel(TransactionIsolationLevel.READ_COMMITTED),
 is(Connection.TRANSACTION_READ_COMMITTED));
+        
assertThat(TransactionUtils.getTransactionIsolationLevel(TransactionIsolationLevel.REPEATABLE_READ),
 is(Connection.TRANSACTION_REPEATABLE_READ));
+        
assertThat(TransactionUtils.getTransactionIsolationLevel(TransactionIsolationLevel.SERIALIZABLE),
 is(Connection.TRANSACTION_SERIALIZABLE));
+        
assertThat(TransactionUtils.getTransactionIsolationLevel(TransactionIsolationLevel.NONE),
 is(Connection.TRANSACTION_NONE));
+    }
+    
+    @Test
+    void assertGetTransactionIsolationLevelByInt() {
+        
assertThat(TransactionUtils.getTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED),
 is(TransactionIsolationLevel.READ_UNCOMMITTED));
+        
assertThat(TransactionUtils.getTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED),
 is(TransactionIsolationLevel.READ_COMMITTED));
+        
assertThat(TransactionUtils.getTransactionIsolationLevel(Connection.TRANSACTION_REPEATABLE_READ),
 is(TransactionIsolationLevel.REPEATABLE_READ));
+        
assertThat(TransactionUtils.getTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE),
 is(TransactionIsolationLevel.SERIALIZABLE));
+        assertThat(TransactionUtils.getTransactionIsolationLevel(-1), 
is(TransactionIsolationLevel.NONE));
+    }
+    
+    @Test
+    void assertGetTransactionTypeFromContext() {
+        TransactionConnectionContext transactionContext = new 
TransactionConnectionContext();
+        transactionContext.beginTransaction(TransactionType.XA.name(), 
transactionManager);
+        assertThat(TransactionUtils.getTransactionType(transactionContext), 
is(TransactionType.XA));
+    }
+    
+    @Test
+    void assertGetTransactionTypeFromDefaultRule() {
+        TransactionRule transactionRule = mock(TransactionRule.class);
+        
when(transactionRule.getDefaultType()).thenReturn(TransactionType.BASE);
+        RuleMetaData globalRuleMetaData = mock(RuleMetaData.class);
+        
when(globalRuleMetaData.getSingleRule(TransactionRule.class)).thenReturn(transactionRule);
+        ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
+        
when(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
+        
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
+        assertThat(TransactionUtils.getTransactionType(new 
TransactionConnectionContext()), is(TransactionType.BASE));
+    }
+}

Reply via email to