|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.gravitino.cli.output; |
| 21 | + |
| 22 | +import static org.apache.gravitino.rel.expressions.NamedReference.field; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import org.apache.gravitino.cli.outputs.LineUtil; |
| 27 | +import org.apache.gravitino.rel.Column; |
| 28 | +import org.apache.gravitino.rel.expressions.FunctionExpression; |
| 29 | +import org.apache.gravitino.rel.expressions.literals.Literal; |
| 30 | +import org.apache.gravitino.rel.types.Type; |
| 31 | +import org.apache.gravitino.rel.types.Types; |
| 32 | +import org.junit.jupiter.api.Assertions; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +public class TestLineUtil { |
| 36 | + @Test |
| 37 | + void testGetDisplayWidthWithAscii() { |
| 38 | + Assertions.assertEquals(13, LineUtil.getDisplayWidth("Hello, world!")); |
| 39 | + Assertions.assertEquals(5, LineUtil.getDisplayWidth("Hello")); |
| 40 | + Assertions.assertEquals(1, LineUtil.getDisplayWidth("H")); |
| 41 | + } |
| 42 | + |
| 43 | + @SuppressWarnings("DefaultCharset") |
| 44 | + @Test |
| 45 | + void testGetDisplayWidthWithNonAscii() { |
| 46 | + Assertions.assertEquals(8, LineUtil.getDisplayWidth("、世界!")); |
| 47 | + Assertions.assertEquals(10, LineUtil.getDisplayWidth("こんにちは")); |
| 48 | + Assertions.assertEquals(2, LineUtil.getDisplayWidth("こ")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testGetSpaces() { |
| 53 | + Assertions.assertEquals(" ", LineUtil.getSpaces(1)); |
| 54 | + Assertions.assertEquals(" ", LineUtil.getSpaces(2)); |
| 55 | + Assertions.assertEquals(" ", LineUtil.getSpaces(3)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testGetAutoIncrementWithLong() { |
| 60 | + Column mockColumn1 = mock(Column.class); |
| 61 | + when(mockColumn1.dataType()).thenReturn(Types.LongType.get()); |
| 62 | + when(mockColumn1.autoIncrement()).thenReturn(true); |
| 63 | + |
| 64 | + Assertions.assertEquals("true", LineUtil.getAutoIncrement(mockColumn1)); |
| 65 | + |
| 66 | + Column mockColumn2 = mock(Column.class); |
| 67 | + when(mockColumn2.dataType()).thenReturn(Types.LongType.get()); |
| 68 | + when(mockColumn2.autoIncrement()).thenReturn(false); |
| 69 | + |
| 70 | + Assertions.assertEquals("false", LineUtil.getAutoIncrement(mockColumn2)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testGetAutoIncrementWithInteger() { |
| 75 | + Column mockColumn1 = mock(Column.class); |
| 76 | + when(mockColumn1.dataType()).thenReturn(Types.IntegerType.get()); |
| 77 | + when(mockColumn1.autoIncrement()).thenReturn(true); |
| 78 | + |
| 79 | + Assertions.assertEquals("true", LineUtil.getAutoIncrement(mockColumn1)); |
| 80 | + |
| 81 | + Column mockColumn2 = mock(Column.class); |
| 82 | + when(mockColumn2.dataType()).thenReturn(Types.IntegerType.get()); |
| 83 | + when(mockColumn2.autoIncrement()).thenReturn(false); |
| 84 | + |
| 85 | + Assertions.assertEquals("false", LineUtil.getAutoIncrement(mockColumn2)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testGetAutoIncrementWithShort() { |
| 90 | + Column mockColumn1 = mock(Column.class); |
| 91 | + when(mockColumn1.dataType()).thenReturn(Types.LongType.get()); |
| 92 | + when(mockColumn1.autoIncrement()).thenReturn(true); |
| 93 | + |
| 94 | + Assertions.assertEquals("true", LineUtil.getAutoIncrement(mockColumn1)); |
| 95 | + |
| 96 | + Column mockColumn2 = mock(Column.class); |
| 97 | + when(mockColumn2.dataType()).thenReturn(Types.LongType.get()); |
| 98 | + when(mockColumn2.autoIncrement()).thenReturn(false); |
| 99 | + |
| 100 | + Assertions.assertEquals("false", LineUtil.getAutoIncrement(mockColumn2)); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void testGetAutoIncrementWithByte() { |
| 105 | + Column mockColumn1 = mock(Column.class); |
| 106 | + when(mockColumn1.dataType()).thenReturn(Types.ByteType.get()); |
| 107 | + when(mockColumn1.autoIncrement()).thenReturn(true); |
| 108 | + |
| 109 | + Assertions.assertEquals("true", LineUtil.getAutoIncrement(mockColumn1)); |
| 110 | + |
| 111 | + Column mockColumn2 = mock(Column.class); |
| 112 | + when(mockColumn2.dataType()).thenReturn(Types.ByteType.get()); |
| 113 | + when(mockColumn2.autoIncrement()).thenReturn(false); |
| 114 | + |
| 115 | + Assertions.assertEquals("false", LineUtil.getAutoIncrement(mockColumn2)); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void testGetAutoIncrementWithNonInteger() { |
| 120 | + Column mockColumn1 = mock(Column.class); |
| 121 | + when(mockColumn1.dataType()).thenReturn(Types.StringType.get()); |
| 122 | + when(mockColumn1.autoIncrement()).thenReturn(true); |
| 123 | + |
| 124 | + Assertions.assertEquals("", LineUtil.getAutoIncrement(mockColumn1)); |
| 125 | + |
| 126 | + Column mockColumn2 = mock(Column.class); |
| 127 | + when(mockColumn2.dataType()).thenReturn(Types.BooleanType.get()); |
| 128 | + when(mockColumn2.autoIncrement()).thenReturn(false); |
| 129 | + |
| 130 | + Assertions.assertEquals("", LineUtil.getAutoIncrement(mockColumn2)); |
| 131 | + |
| 132 | + Column mockColumn3 = mock(Column.class); |
| 133 | + when(mockColumn3.dataType()).thenReturn(Types.TimeType.get()); |
| 134 | + when(mockColumn3.autoIncrement()).thenReturn(false); |
| 135 | + |
| 136 | + Assertions.assertEquals("", LineUtil.getAutoIncrement(mockColumn3)); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void testGetComment() { |
| 141 | + Column mockColumn1 = mock(Column.class); |
| 142 | + when(mockColumn1.comment()).thenReturn("This is a comment"); |
| 143 | + |
| 144 | + Assertions.assertEquals("This is a comment", LineUtil.getComment(mockColumn1)); |
| 145 | + |
| 146 | + Column mockColumn2 = mock(Column.class); |
| 147 | + when(mockColumn2.comment()).thenReturn(null); |
| 148 | + |
| 149 | + Assertions.assertEquals("N/A", LineUtil.getComment(mockColumn2)); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void testGetDefaultValueWithInteger() { |
| 154 | + Column mockColumn1 = mock(Column.class); |
| 155 | + when(mockColumn1.dataType()).thenReturn(Types.IntegerType.get()); |
| 156 | + when(mockColumn1.defaultValue()) |
| 157 | + .thenReturn( |
| 158 | + new Literal<Integer>() { |
| 159 | + @Override |
| 160 | + public Integer value() { |
| 161 | + return 1; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public Type dataType() { |
| 166 | + return null; |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + Assertions.assertEquals("1", LineUtil.getDefaultValue(mockColumn1)); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void testGetDefaultValueWithNull() { |
| 175 | + Column mockColumn1 = mock(Column.class); |
| 176 | + when(mockColumn1.dataType()).thenReturn(Types.IntegerType.get()); |
| 177 | + when(mockColumn1.defaultValue()).thenReturn(Column.DEFAULT_VALUE_NOT_SET); |
| 178 | + |
| 179 | + Assertions.assertEquals(LineUtil.EMPTY_DEFAULT_VALUE, LineUtil.getDefaultValue(mockColumn1)); |
| 180 | + |
| 181 | + Column mockColumn2 = mock(Column.class); |
| 182 | + when(mockColumn2.dataType()).thenReturn(Types.StringType.get()); |
| 183 | + when(mockColumn2.defaultValue()).thenReturn(null); |
| 184 | + |
| 185 | + Assertions.assertEquals(LineUtil.EMPTY_DEFAULT_VALUE, LineUtil.getDefaultValue(mockColumn2)); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + void testGetDefaultValueWithString() { |
| 190 | + Column mockColumn1 = mock(Column.class); |
| 191 | + when(mockColumn1.dataType()).thenReturn(Types.StringType.get()); |
| 192 | + when(mockColumn1.defaultValue()) |
| 193 | + .thenReturn( |
| 194 | + new Literal<String>() { |
| 195 | + @Override |
| 196 | + public String value() { |
| 197 | + return ""; |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public Type dataType() { |
| 202 | + return null; |
| 203 | + } |
| 204 | + }); |
| 205 | + |
| 206 | + Assertions.assertEquals("''", LineUtil.getDefaultValue(mockColumn1)); |
| 207 | + |
| 208 | + Column mockColumn2 = mock(Column.class); |
| 209 | + when(mockColumn2.dataType()).thenReturn(Types.StringType.get()); |
| 210 | + when(mockColumn2.defaultValue()) |
| 211 | + .thenReturn( |
| 212 | + new Literal<String>() { |
| 213 | + @Override |
| 214 | + public String value() { |
| 215 | + return "Hello, world!"; |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public Type dataType() { |
| 220 | + return null; |
| 221 | + } |
| 222 | + }); |
| 223 | + |
| 224 | + Assertions.assertEquals("Hello, world!", LineUtil.getDefaultValue(mockColumn2)); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + void testGetDefaultValueWithFunctionAndEmptyArgs() { |
| 229 | + Column mockColumn1 = mock(Column.class); |
| 230 | + when(mockColumn1.dataType()).thenReturn(Types.StringType.get()); |
| 231 | + when(mockColumn1.defaultValue()).thenReturn(FunctionExpression.of("current_timestamp")); |
| 232 | + |
| 233 | + Assertions.assertEquals("current_timestamp()", LineUtil.getDefaultValue(mockColumn1)); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + void testGetDefaultValueWithFunctionAndArgs() { |
| 238 | + Column mockColumn1 = mock(Column.class); |
| 239 | + when(mockColumn1.dataType()).thenReturn(Types.StringType.get()); |
| 240 | + when(mockColumn1.defaultValue()).thenReturn(FunctionExpression.of("date", field("b"))); |
| 241 | + |
| 242 | + Assertions.assertEquals("date([b])", LineUtil.getDefaultValue(mockColumn1)); |
| 243 | + } |
| 244 | +} |
0 commit comments