xtern commented on code in PR #4972:
URL: https://github.com/apache/ignite-3/pull/4972#discussion_r1898869860


##########
modules/catalog/src/test/java/org/apache/ignite/internal/catalog/CatalogSchemaTest.java:
##########
@@ -17,33 +17,160 @@
 
 package org.apache.ignite.internal.catalog;
 
+import static org.apache.ignite.internal.catalog.CatalogTestUtils.columnParams;
+import static 
org.apache.ignite.internal.catalog.descriptors.CatalogColumnCollation.ASC_NULLS_LAST;
+import static org.apache.ignite.internal.lang.IgniteStringFormatter.format;
 import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrowFast;
 import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.apache.ignite.sql.ColumnType.INT32;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 
+import java.util.List;
 import org.apache.ignite.internal.catalog.commands.CreateSchemaCommand;
+import org.apache.ignite.internal.catalog.commands.DropSchemaCommand;
+import org.apache.ignite.internal.catalog.descriptors.CatalogSchemaDescriptor;
 import org.apache.ignite.internal.sql.SqlCommon;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 /** Tests for schema related commands. */
 public class CatalogSchemaTest extends BaseCatalogManagerTest {
+    private static final String TEST_SCHEMA = "S1";
 
     @Test
     public void testCreateSchema() {
-        String schemaName = "S1";
+        
assertThat(manager.execute(CreateSchemaCommand.builder().name(TEST_SCHEMA).build()),
 willCompleteSuccessfully());
 
-        
assertThat(manager.execute(CreateSchemaCommand.builder().name(schemaName).build()),
 willCompleteSuccessfully());
+        Catalog latestCatalog = latestCatalog();
 
-        Catalog latestCatalog = 
manager.catalog(manager.activeCatalogVersion(clock.nowLong()));
-
-        assertNotNull(latestCatalog);
-        assertNotNull(latestCatalog.schema(schemaName));
+        assertNotNull(latestCatalog.schema(TEST_SCHEMA));
         assertNotNull(latestCatalog.schema(SqlCommon.DEFAULT_SCHEMA_NAME));
 
         assertThat(
-                
manager.execute(CreateSchemaCommand.builder().name(schemaName).build()),
+                
manager.execute(CreateSchemaCommand.builder().name(TEST_SCHEMA).build()),
                 willThrowFast(CatalogValidationException.class, "Schema with 
name 'S1' already exists")
         );
     }
+
+    @Test
+    public void testDropEmpty() {
+        int initialSchemasCount = latestCatalog().schemas().size();
+
+        
assertThat(manager.execute(CreateSchemaCommand.builder().name(TEST_SCHEMA).build()),
 willCompleteSuccessfully());
+
+        assertThat(latestCatalog().schemas(), hasSize(initialSchemasCount + 
1));
+
+        CatalogCommand cmd = 
DropSchemaCommand.builder().name(TEST_SCHEMA).build();
+
+        assertThat(manager.execute(cmd), willCompleteSuccessfully());
+        assertThat(latestCatalog().schema(TEST_SCHEMA), nullValue());
+        assertThat(latestCatalog().schemas(), hasSize(initialSchemasCount));
+
+        assertThat(
+                
manager.execute(DropSchemaCommand.builder().name(TEST_SCHEMA).build()),
+                willThrowFast(CatalogValidationException.class, "Schema with 
name 'S1' not found")
+        );
+    }
+
+    @Test
+    public void testDropDefaultSchemaIsAllowed() {
+        CatalogCommand cmd = 
DropSchemaCommand.builder().name(SqlCommon.DEFAULT_SCHEMA_NAME).build();
+
+        assertThat(manager.execute(cmd), willCompleteSuccessfully());
+        assertThat(latestCatalog().schema(SqlCommon.DEFAULT_SCHEMA_NAME), 
nullValue());
+
+        assertThat(
+                manager.execute(simpleTable("test")),
+                willThrowFast(CatalogValidationException.class, "Schema with 
name 'PUBLIC' not found")
+        );
+    }
+
+    @Test
+    public void testDropNonEmpty() {
+        CatalogCommand newSchemaCmd = 
CreateSchemaCommand.builder().name(TEST_SCHEMA).build();
+        CatalogCommand newTableCmd = newTableCommand("T1");
+        CatalogCommand idxCmd = newIndexCommand("T1", "I1");
+
+        assertThat(
+                manager.execute(List.of(newSchemaCmd, newTableCmd, idxCmd)),
+                willCompleteSuccessfully()
+        );
+
+        // RESTRICT
+        {
+            assertThat(
+                    
manager.execute(DropSchemaCommand.builder().name(TEST_SCHEMA).build()),
+                    willThrowFast(CatalogValidationException.class, "Schema 
'S1' is not empty. Use CASCADE to drop it anyway.")
+            );
+
+            CatalogSchemaDescriptor schemaDescriptor = 
latestCatalog().schema(TEST_SCHEMA);
+
+            assertThat(schemaDescriptor, is(notNullValue()));
+            assertThat(schemaDescriptor.tables().length, is(1));
+        }
+
+        // CASCADE
+        {
+            assertThat(latestCatalog().tables(), hasSize(1));
+            assertThat(latestCatalog().indexes(), hasSize(2));
+
+            CatalogCommand dropCmd = 
DropSchemaCommand.builder().name(TEST_SCHEMA).cascade(true).build();
+
+            assertThat(manager.execute(dropCmd), willCompleteSuccessfully());
+
+            assertThat(latestCatalog().schema(TEST_SCHEMA), nullValue());
+            assertThat(latestCatalog().tables(), hasSize(0));
+            assertThat(latestCatalog().indexes(), hasSize(0));
+        }
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {
+            CatalogService.SYSTEM_SCHEMA_NAME,
+            CatalogService.INFORMATION_SCHEMA,
+            CatalogService.DEFINITION_SCHEMA
+    })
+    public void testDropSystemSchemaIsForbidden(String schemaName) {

Review Comment:
   Done, added `CatalogSchemaValidationTest` (for create/drop commands)



-- 
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: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to