Skip to content

Commit 6451e4d

Browse files
Take byte overhead into account for the max sizes in the tests
1 parent e869fce commit 6451e4d

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/test/java/org/javawebstack/orm/test/automigrate/DefaultSizeTest.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,57 @@
1616

1717
public class DefaultSizeTest extends ORMTestCase {
1818

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+
1935
final static String tableName = "just_strings";
2036

37+
@Test
38+
public void testStringUsesDefaultSizeChar() throws ORMConfigurationException, SQLException {
39+
setUpWithDefaultSize(JustString.class, 1);
40+
(new Field("just_strings", "string")).assertType("char(1)");
41+
}
2142

2243
@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};
2546
for( int singleParameter : parameters) {
2647
setUpWithDefaultSize(JustString.class, singleParameter);
2748
(new Field("just_strings", "string")).assertType(String.format("varchar(%s)", singleParameter));
2849
}
2950
}
3051

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+
3170
private void setUpWithDefaultSize(Class<? extends Model> clazz, int defaultSize) throws ORMConfigurationException {
3271
ORMConfig config = new ORMConfig()
3372
.setDefaultSize(defaultSize);

0 commit comments

Comments
 (0)