AMashenkov commented on a change in pull request #326: URL: https://github.com/apache/ignite-3/pull/326#discussion_r754251147
########## File path: modules/sql/src/test/java/IgniteSqlTest.java ########## @@ -0,0 +1,384 @@ +/* + * 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. + */ + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Flow; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; +import java.util.function.Function; +import org.apache.ignite.internal.schema.Column; +import org.apache.ignite.internal.schema.NativeTypes; +import org.apache.ignite.internal.schema.SchemaDescriptor; +import org.apache.ignite.internal.schema.SchemaRegistry; +import org.apache.ignite.internal.util.Constants; +import org.apache.ignite.schema.definition.ColumnType; +import org.apache.ignite.sql.IgniteSql; +import org.apache.ignite.sql.MultiResultSet; +import org.apache.ignite.sql.ResultSet; +import org.apache.ignite.sql.ResultSetMetadata; +import org.apache.ignite.sql.Session; +import org.apache.ignite.sql.SqlRow; +import org.apache.ignite.sql.async.AsyncResultSet; +import org.apache.ignite.sql.reactive.ReactiveResultSet; +import org.apache.ignite.table.RecordView; +import org.apache.ignite.table.Tuple; +import org.apache.ignite.tx.IgniteTransactions; +import org.apache.ignite.tx.Transaction; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +/** + * Tests IgniteSQL facade API. + */ +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +public class IgniteSqlTest { + @Mock + IgniteSql queryMgr; + + @Mock + private IgniteTransactions igniteTx; + + @Mock + private Transaction tx; + + @BeforeEach + void setUp() { + initMock(); + } + + @Test + public void testSyncSql() { + igniteTx.runInTransaction(tx -> { + Session sess = queryMgr.newSession(); + + sess.defaultTimeout(10_000, TimeUnit.MILLISECONDS); // Set default timeout. + sess.property("memoryQuota", 10 * Constants.MiB); // Set default quota. + + // Execute outside TX. + ResultSet rs = sess.execute("INSERT INTO tbl VALUES (?, ?)", 10, "str"); + + assertEquals(1, rs.updateCount()); + + // Execute in TX. + Session txSession = tx.wrap(queryMgr.newSession()); + + rs = txSession.execute("SELECT id, val FROM tbl WHERE id < {};", 10); + + for (SqlRow r : rs) { + assertTrue(10 > r.longValue("id")); + assertTrue((r.stringValue("val")).startsWith("str")); + } + + tx.commit(); + }); + + Mockito.verify(tx).commit(); + } + + @Test + public void testSyncSql2() { + RecordView<Tuple> tbl = getTable(); + + // Starts new TX. + Session txSession = tx.wrap(queryMgr.newSession()); + + ResultSet rs = txSession.execute("SELECT id, val FROM tbl WHERE id < {};", 10); + SqlRow row = rs.iterator().next(); + + tbl.withTransaction(tx) + .insertAsync(Tuple.create().set("val", "NewValue")) + .thenAccept(r -> txSession.transaction().rollback()); + + Mockito.verify(tx, Mockito.times(1)).rollback(); + } + + @Test + public void testSyncMultiStatementSql() { + Session sess = tx.wrap(queryMgr.newSession()); + + MultiResultSet multiRs = sess.executeScript( + "CREATE TABLE tbl(id INTEGER PRIMARY KEY, val VARCHAR);" + + "INSERT INTO tbl VALUES (1, 2);" + + "SELECT id, val FROM tbl WHERE id == {};" + + "DROP TABLE tbl", 10); + + Iterator<ResultSet> iterator = multiRs.iterator(); + //TODO: Can iterator return null?? Review comment: We thought multi-statement queries are non-transactional (or each statement executes in implicit tx), and we agree about non-transactional DDL. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
