|
16 | 16 |
|
17 | 17 | public class DefaultSizeTest extends ORMTestCase { |
18 | 18 |
|
| 19 | + // Copy from DefaultMapper: |
| 20 | + // SOURCES: |
| 21 | + // TEXT Datatypes: https://www.mysqltutorial.org/mysql-text/ |
| 22 | + private static final int BYTES_OVERHEAD_VARCHAR = 4; |
| 23 | + private static final int BYTES_OVERHEAD_TINYTEXT = 1; |
| 24 | + private static final int BYTES_OVERHEAD_TEXT = 2; |
| 25 | + private static final int BYTES_OVERHEAD_MEDIUMTEXT = 3; |
| 26 | + private static final int BYTES_OVERHEAD_LONGTEXT = 4; |
| 27 | + |
| 28 | + // The max sizes given in the manual are in bytes. There are overheads which need to be subtracted. |
| 29 | + // The following values assume utf8mb4 encoding which uses 4 bytes per character and |
| 30 | + // further quarters the maximum column length accordingly. |
| 31 | + private static final long MAX_SIZE_VARCHAR = (long) Math.floor((65535 - BYTES_OVERHEAD_VARCHAR) / 4); |
| 32 | + private static final long MAX_SIZE_MEDIUMTEXT = (long) Math.floor((16777215 - BYTES_OVERHEAD_MEDIUMTEXT) / 4); |
| 33 | + private static final long MAX_SIZE_LONGTEXT = (long) Math.floor((4294967295L - BYTES_OVERHEAD_LONGTEXT) / 4); |
| 34 | + |
19 | 35 | final static String tableName = "just_strings"; |
20 | 36 |
|
| 37 | + @Test |
| 38 | + public void testStringUsesDefaultSizeChar() throws ORMConfigurationException, SQLException { |
| 39 | + setUpWithDefaultSize(JustString.class, 1); |
| 40 | + (new Field("just_strings", "string")).assertType("char(1)"); |
| 41 | + } |
21 | 42 |
|
22 | 43 | @Test |
23 | | - public void testStringUsesDefaultSize() throws ORMConfigurationException, SQLException { |
24 | | - int[] parameters = {2, 3, 100, 254, 255, 256, 65534, 65535}; |
| 44 | + public void testStringUsesDefaultSizeVarchar() throws ORMConfigurationException, SQLException { |
| 45 | + int[] parameters = {2, 3, 123, 1234, 12345, (int) MAX_SIZE_VARCHAR - 1, (int) MAX_SIZE_VARCHAR}; |
25 | 46 | for( int singleParameter : parameters) { |
26 | 47 | setUpWithDefaultSize(JustString.class, singleParameter); |
27 | 48 | (new Field("just_strings", "string")).assertType(String.format("varchar(%s)", singleParameter)); |
28 | 49 | } |
29 | 50 | } |
30 | 51 |
|
| 52 | + @Test |
| 53 | + public void testStringUsesDefaultSizeMediumText() throws ORMConfigurationException, SQLException { |
| 54 | + int[] parameters = {(int) MAX_SIZE_VARCHAR + 1, 123456, 1234567, (int) MAX_SIZE_MEDIUMTEXT - 1}; |
| 55 | + for( int singleParameter : parameters) { |
| 56 | + setUpWithDefaultSize(JustString.class, singleParameter); |
| 57 | + (new Field("just_strings", "string")).assertType("mediumtext"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testStringUsesDefaultSizeLongText() throws ORMConfigurationException, SQLException { |
| 63 | + int[] parameters = {(int) MAX_SIZE_MEDIUMTEXT + 1, 123456789, 123467890, Integer.MAX_VALUE}; |
| 64 | + for( int singleParameter : parameters) { |
| 65 | + setUpWithDefaultSize(JustString.class, singleParameter); |
| 66 | + (new Field("just_strings", "string")).assertType("longtext"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
31 | 70 | private void setUpWithDefaultSize(Class<? extends Model> clazz, int defaultSize) throws ORMConfigurationException { |
32 | 71 | ORMConfig config = new ORMConfig() |
33 | 72 | .setDefaultSize(defaultSize); |
|
0 commit comments