|
| 1 | +package tech.ydb.table.integration; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.ClassRule; |
| 5 | + |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import tech.ydb.core.Result; |
| 9 | +import tech.ydb.core.Status; |
| 10 | +import tech.ydb.table.SessionRetryContext; |
| 11 | +import tech.ydb.table.description.SequenceDescription; |
| 12 | +import tech.ydb.table.description.TableColumn; |
| 13 | +import tech.ydb.table.description.TableDescription; |
| 14 | +import tech.ydb.table.impl.SimpleTableClient; |
| 15 | +import tech.ydb.table.rpc.grpc.GrpcTableRpc; |
| 16 | +import tech.ydb.table.settings.CreateTableSettings; |
| 17 | +import tech.ydb.table.values.PrimitiveType; |
| 18 | +import tech.ydb.table.values.PrimitiveValue; |
| 19 | +import tech.ydb.test.junit4.GrpcTransportRule; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Kirill Kurdyukov |
| 23 | + */ |
| 24 | +public class CreateTableTest { |
| 25 | + |
| 26 | + @ClassRule |
| 27 | + public final static GrpcTransportRule ydbTransport = new GrpcTransportRule(); |
| 28 | + |
| 29 | + private final SimpleTableClient tableClient = SimpleTableClient.newClient( |
| 30 | + GrpcTableRpc.useTransport(ydbTransport) |
| 31 | + ).build(); |
| 32 | + |
| 33 | + private final SessionRetryContext ctx = SessionRetryContext.create(tableClient).build(); |
| 34 | + |
| 35 | + @Test |
| 36 | + public void smallSerialTest() { |
| 37 | + String tablePath = ydbTransport.getDatabase() + "/small_serial_table"; |
| 38 | + |
| 39 | + TableDescription tableDescription = TableDescription.newBuilder() |
| 40 | + .addSmallSerialColumn("id") |
| 41 | + .setPrimaryKey("id") |
| 42 | + .build(); |
| 43 | + |
| 44 | + Status createStatus = ctx.supplyStatus(session -> |
| 45 | + session.createTable(tablePath, tableDescription, new CreateTableSettings())).join(); |
| 46 | + Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess()); |
| 47 | + |
| 48 | + Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join(); |
| 49 | + Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess()); |
| 50 | + TableColumn tableColumn = describeResult.getValue().getColumns().get(0); |
| 51 | + Assert.assertNotNull(tableColumn.getSequenceDescription()); |
| 52 | + Assert.assertEquals("sequence_default", tableColumn.getSequenceDescription().getName()); |
| 53 | + Assert.assertEquals(PrimitiveType.Int16, tableColumn.getType()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void serialTest() { |
| 58 | + String tablePath = ydbTransport.getDatabase() + "/serial_table"; |
| 59 | + |
| 60 | + TableDescription tableDescription = TableDescription.newBuilder() |
| 61 | + .addSerialColumn("id") |
| 62 | + .setPrimaryKey("id") |
| 63 | + .build(); |
| 64 | + |
| 65 | + Status createStatus = ctx.supplyStatus(session -> |
| 66 | + session.createTable(tablePath, tableDescription, new CreateTableSettings())).join(); |
| 67 | + Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess()); |
| 68 | + |
| 69 | + Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join(); |
| 70 | + Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess()); |
| 71 | + TableColumn tableColumn = describeResult.getValue().getColumns().get(0); |
| 72 | + Assert.assertNotNull(tableColumn.getSequenceDescription()); |
| 73 | + Assert.assertEquals("sequence_default", tableColumn.getSequenceDescription().getName()); |
| 74 | + Assert.assertEquals(PrimitiveType.Int32, tableColumn.getType()); |
| 75 | + |
| 76 | + ctx.supplyStatus(session -> session.dropTable(tablePath)).join(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void bigSerialTest() { |
| 81 | + String tablePath = ydbTransport.getDatabase() + "/big_serial_table"; |
| 82 | + |
| 83 | + TableDescription tableDescription = TableDescription.newBuilder() |
| 84 | + .addBigSerialColumn("id") |
| 85 | + .setPrimaryKey("id") |
| 86 | + .build(); |
| 87 | + |
| 88 | + Status createStatus = ctx.supplyStatus(session -> |
| 89 | + session.createTable(tablePath, tableDescription, new CreateTableSettings())).join(); |
| 90 | + Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess()); |
| 91 | + |
| 92 | + Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join(); |
| 93 | + Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess()); |
| 94 | + TableDescription description = describeResult.getValue(); |
| 95 | + TableColumn tableColumn = description.getColumns().get(0); |
| 96 | + Assert.assertNotNull(tableColumn.getSequenceDescription()); |
| 97 | + Assert.assertEquals("sequence_default", tableColumn.getSequenceDescription().getName()); |
| 98 | + Assert.assertEquals(PrimitiveType.Int64, tableColumn.getType()); |
| 99 | + |
| 100 | + ctx.supplyStatus(session -> session.dropTable(tablePath)).join(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void defaultValueTest() { |
| 105 | + String tablePath = ydbTransport.getDatabase() + "/default_value_table"; |
| 106 | + |
| 107 | + TableDescription tableDescription = TableDescription.newBuilder() |
| 108 | + .addColumn("id", PrimitiveType.Text, PrimitiveValue.newText("text")) |
| 109 | + .setPrimaryKey("id") |
| 110 | + .build(); |
| 111 | + |
| 112 | + Status createStatus = ctx.supplyStatus(session -> |
| 113 | + session.createTable(tablePath, tableDescription, new CreateTableSettings())).join(); |
| 114 | + Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess()); |
| 115 | + |
| 116 | + Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join(); |
| 117 | + Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess()); |
| 118 | + TableColumn tableColumn = describeResult.getValue().getColumns().get(0); |
| 119 | + Assert.assertNull(tableColumn.getSequenceDescription()); |
| 120 | + Assert.assertEquals("text", tableColumn.getLiteralDefaultValue().getText()); |
| 121 | + Assert.assertEquals(PrimitiveType.Text, tableColumn.getType()); |
| 122 | + |
| 123 | + ctx.supplyStatus(session -> session.dropTable(tablePath)).join(); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void customSequenceDescriptionTest() { |
| 128 | + String tablePath = ydbTransport.getDatabase() + "/default_value_table"; |
| 129 | + |
| 130 | + TableDescription tableDescription = TableDescription.newBuilder() |
| 131 | + .addBigSerialColumn("id", SequenceDescription.newBuilder() |
| 132 | + .setCache(5L) |
| 133 | + .setMaxValue((long) Integer.MAX_VALUE) |
| 134 | + .setMinValue(10L) |
| 135 | + .setName("custom_sequence_description") |
| 136 | + .setCycle(true) |
| 137 | + .setSetVal(11L, true) |
| 138 | + .setIncrement(2L) |
| 139 | + .setStartValue(12L) |
| 140 | + .build() |
| 141 | + ) |
| 142 | + .setPrimaryKey("id") |
| 143 | + .build(); |
| 144 | + |
| 145 | + Status createStatus = ctx.supplyStatus(session -> |
| 146 | + session.createTable(tablePath, tableDescription, new CreateTableSettings())).join(); |
| 147 | + Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess()); |
| 148 | + |
| 149 | + Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join(); |
| 150 | + Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess()); |
| 151 | + TableDescription description = describeResult.getValue(); |
| 152 | + SequenceDescription sequenceDescription = description.getColumns().get(0).getSequenceDescription(); |
| 153 | + Assert.assertNotNull(sequenceDescription); |
| 154 | + Assert.assertEquals("custom_sequence_description", sequenceDescription.getName()); |
| 155 | + Assert.assertEquals(Integer.MAX_VALUE, sequenceDescription.getMaxValue().intValue()); |
| 156 | + Assert.assertEquals(10L, (long) sequenceDescription.getMinValue()); |
| 157 | + Assert.assertEquals(5L, (long) sequenceDescription.getCache()); |
| 158 | + Assert.assertEquals(true, sequenceDescription.getCycle()); |
| 159 | + Assert.assertEquals(2L, (long) sequenceDescription.getIncrement()); |
| 160 | + Assert.assertEquals(12L, (long) sequenceDescription.getStartValue()); |
| 161 | + |
| 162 | + ctx.supplyStatus(session -> session.dropTable(tablePath)).join(); |
| 163 | + } |
| 164 | +} |
0 commit comments