|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
5 | 6 | import static org.junit.jupiter.api.Assertions.assertTrue; |
6 | 7 |
|
7 | 8 | import java.util.Optional; |
@@ -106,4 +107,152 @@ void testOfBytesArrayInequality() { |
106 | 107 |
|
107 | 108 | assertNotEquals(expr1, expr2); |
108 | 109 | } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testOfStringsCreatesInstanceWithTextType() { |
| 113 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofStrings("tags"); |
| 114 | + |
| 115 | + assertEquals("tags", expression.getName()); |
| 116 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 117 | + assertEquals(PostgresDataType.TEXT, expression.getArrayElementType().get()); |
| 118 | + assertEquals("text[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void testOfIntsCreatesInstanceWithIntegerType() { |
| 123 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofInts("ids"); |
| 124 | + |
| 125 | + assertEquals("ids", expression.getName()); |
| 126 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 127 | + assertEquals(PostgresDataType.INTEGER, expression.getArrayElementType().get()); |
| 128 | + assertEquals("integer[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void testOfLongsCreatesInstanceWithBigintType() { |
| 133 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofLongs("timestamps"); |
| 134 | + |
| 135 | + assertEquals("timestamps", expression.getName()); |
| 136 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 137 | + assertEquals(PostgresDataType.BIGINT, expression.getArrayElementType().get()); |
| 138 | + assertEquals("bigint[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void testOfShortsCreatesInstanceWithSmallintType() { |
| 143 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofShorts("counts"); |
| 144 | + |
| 145 | + assertEquals("counts", expression.getName()); |
| 146 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 147 | + assertEquals(PostgresDataType.SMALLINT, expression.getArrayElementType().get()); |
| 148 | + assertEquals("smallint[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void testOfFloatsCreatesInstanceWithFloatType() { |
| 153 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofFloats("temperatures"); |
| 154 | + |
| 155 | + assertEquals("temperatures", expression.getName()); |
| 156 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 157 | + assertEquals(PostgresDataType.FLOAT, expression.getArrayElementType().get()); |
| 158 | + assertEquals("real[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void testOfDoublesCreatesInstanceWithDoubleType() { |
| 163 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofDoubles("coordinates"); |
| 164 | + |
| 165 | + assertEquals("coordinates", expression.getName()); |
| 166 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 167 | + assertEquals(PostgresDataType.DOUBLE, expression.getArrayElementType().get()); |
| 168 | + assertEquals("double precision[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + void testOfDecimalsCreatesInstanceWithNumericType() { |
| 173 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofDecimals("prices"); |
| 174 | + |
| 175 | + assertEquals("prices", expression.getName()); |
| 176 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 177 | + assertEquals(PostgresDataType.NUMERIC, expression.getArrayElementType().get()); |
| 178 | + assertEquals("numeric[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void testOfBooleansCreatesInstanceWithBooleanType() { |
| 183 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofBooleans("flags"); |
| 184 | + |
| 185 | + assertEquals("flags", expression.getName()); |
| 186 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 187 | + assertEquals(PostgresDataType.BOOLEAN, expression.getArrayElementType().get()); |
| 188 | + assertEquals("boolean[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + void testOfTimestampsCreatesInstanceWithTimestampType() { |
| 193 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofTimestamps("events"); |
| 194 | + |
| 195 | + assertEquals("events", expression.getName()); |
| 196 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 197 | + assertEquals(PostgresDataType.TIMESTAMP, expression.getArrayElementType().get()); |
| 198 | + assertEquals("timestamp[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + void testOfTimestampsTzCreatesInstanceWithTimestampTzType() { |
| 203 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofTimestampsTz("eventsTz"); |
| 204 | + |
| 205 | + assertEquals("eventsTz", expression.getName()); |
| 206 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 207 | + assertEquals(PostgresDataType.TIMESTAMPTZ, expression.getArrayElementType().get()); |
| 208 | + assertEquals("timestamptz[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + void testOfDatesCreatesInstanceWithDateType() { |
| 213 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofDates("dates"); |
| 214 | + |
| 215 | + assertEquals("dates", expression.getName()); |
| 216 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 217 | + assertEquals(PostgresDataType.DATE, expression.getArrayElementType().get()); |
| 218 | + assertEquals("date[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + void testOfUuidsCreatesInstanceWithUuidType() { |
| 223 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.ofUuids("uuids"); |
| 224 | + |
| 225 | + assertEquals("uuids", expression.getName()); |
| 226 | + assertTrue(expression.getArrayElementType().isPresent()); |
| 227 | + assertEquals(PostgresDataType.UUID, expression.getArrayElementType().get()); |
| 228 | + assertEquals("uuid[]", expression.getPostgresArrayTypeString().orElse(null)); |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + void testToPostgresArrayTypeThrowsExceptionForNonPostgresDataType() throws Exception { |
| 233 | + // Create an expression with a custom FlatCollectionDataType that is not PostgresDataType |
| 234 | + FlatCollectionDataType customType = |
| 235 | + new FlatCollectionDataType() { |
| 236 | + @Override |
| 237 | + public String getType() { |
| 238 | + return "custom_type"; |
| 239 | + } |
| 240 | + }; |
| 241 | + |
| 242 | + ArrayIdentifierExpression expression = new ArrayIdentifierExpression("test", customType); |
| 243 | + |
| 244 | + // Calling getPostgresArrayTypeString should throw IllegalArgumentException |
| 245 | + IllegalArgumentException exception = |
| 246 | + assertThrows(IllegalArgumentException.class, () -> expression.getPostgresArrayTypeString()); |
| 247 | + |
| 248 | + assertTrue(exception.getMessage().contains("Only PostgresDataType is currently supported")); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + void testGetPostgresArrayTypeStringReturnsEmptyForNullType() { |
| 253 | + ArrayIdentifierExpression expression = ArrayIdentifierExpression.of("untyped"); |
| 254 | + |
| 255 | + Optional<String> arrayType = expression.getPostgresArrayTypeString(); |
| 256 | + assertTrue(arrayType.isEmpty()); |
| 257 | + } |
109 | 258 | } |
0 commit comments