Refactor tests, move some unit tests to ebean-api

This commit is contained in:
Rob Bygrave
2022-03-27 20:31:17 +13:00
parent 426d314e63
commit b85bb5f459
10 changed files with 4 additions and 8 deletions
@@ -0,0 +1,63 @@
package io.ebean.config;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class UnderscoreNamingConventionTest {
private UnderscoreNamingConvention namingConvention = new UnderscoreNamingConvention();
@Test
public void simple() {
String col = namingConvention.getColumnFromProperty(null, "helloThere");
assertThat(col).isEqualTo("hello_there");
assertThat(namingConvention.toCamelFromUnderscore(col)).isEqualTo("helloThere");
}
@Test
public void with_suffix_Id() {
String col = namingConvention.getColumnFromProperty(null, "helloId");
assertThat(col).isEqualTo("hello_id");
assertThat(namingConvention.toCamelFromUnderscore(col)).isEqualTo("helloId");
col = namingConvention.getColumnFromProperty(null, "helloThereId");
assertThat(col).isEqualTo("hello_there_id");
assertThat(namingConvention.toCamelFromUnderscore(col)).isEqualTo("helloThereId");
}
@Test
public void with_suffix_ID() {
String col = namingConvention.getColumnFromProperty(null, "helloID");
assertThat(col).isEqualTo("hello_id");
assertThat(namingConvention.toCamelFromUnderscore(col)).isEqualTo("helloId");
col = namingConvention.getColumnFromProperty(null, "helloThereID");
assertThat(col).isEqualTo("hello_there_id");
assertThat(namingConvention.toCamelFromUnderscore(col)).isEqualTo("helloThereId");
}
@Test
public void getColumnFromProperty() {
String fkCol = "bridgetab_user_id";
String col = namingConvention.getColumnFromProperty(null, fkCol);
assertThat(col).isEqualTo(fkCol);
}
@Test
public void getForeignKey() {
String fk = namingConvention.getForeignKey("billing_address", "id");
assertThat(fk).isEqualTo("billing_address_id");
fk = namingConvention.getForeignKey("billing_address", "remoteIdProperty");
assertThat(fk).isEqualTo("billing_address_remote_id_property");
}
}
@@ -0,0 +1,82 @@
package io.ebean.config.dbplatform;
import io.ebean.annotation.Platform;
import io.ebean.config.DatabaseConfig;
import io.ebean.config.MatchingNamingConvention;
import io.ebean.config.PlatformConfig;
import io.ebean.config.dbplatform.h2.H2Platform;
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DatabasePlatformTest {
@Test
public void convertQuotedIdentifiers_when_allQuotedIdentifier_sqlServer() {
DatabaseConfig config = new DatabaseConfig();
config.setAllQuotedIdentifiers(true);
config.setNamingConvention(new MatchingNamingConvention());
DatabasePlatform dbPlatform = new SqlServer17Platform();
dbPlatform.configure(config.getPlatformConfig(), config.isAllQuotedIdentifiers());
assertEquals(dbPlatform.convertQuotedIdentifiers("order"), "[order]");
assertEquals(dbPlatform.convertQuotedIdentifiers("`order`"), "[order]");
assertEquals(dbPlatform.convertQuotedIdentifiers("firstName"), "[firstName]");
}
@Test
public void convertQuotedIdentifiers() {
DatabaseConfig config = new DatabaseConfig();
DatabasePlatform dbPlatform = new SqlServer17Platform();
dbPlatform.configure(config.getPlatformConfig(), config.isAllQuotedIdentifiers());
assertEquals(dbPlatform.convertQuotedIdentifiers("order"), "order");
assertEquals(dbPlatform.convertQuotedIdentifiers("`order`"), "[order]");
assertEquals(dbPlatform.convertQuotedIdentifiers("firstName"), "firstName");
assertEquals(dbPlatform.unQuote("order"), "order");
assertEquals(dbPlatform.unQuote("[order]"), "order");
assertEquals(dbPlatform.unQuote("[firstName]"), "firstName");
}
@Test
public void defaultTypesForDecimalAndVarchar() {
DatabasePlatform dbPlatform = new DatabasePlatform();
assertEquals(defaultDecimalDefn(dbPlatform), "decimal(16,3)");
assertEquals(defaultDefn(DbType.VARCHAR, dbPlatform), "varchar(255)");
}
@Test
public void configure_customType() {
PlatformConfig config = new PlatformConfig();
config.addCustomMapping(DbType.VARCHAR, "text", Platform.POSTGRES);
config.addCustomMapping(DbType.DECIMAL, "decimal(24,4)");
// PG renders custom decimal and varchar
PostgresPlatform pgPlatform = new PostgresPlatform();
pgPlatform.configure(config, false);
assertEquals(defaultDecimalDefn(pgPlatform), "decimal(24,4)");
assertEquals(defaultDefn(DbType.VARCHAR, pgPlatform), "text");
// H2 only renders custom decimal
H2Platform h2Platform = new H2Platform();
h2Platform.configure(config, false);
assertEquals(defaultDecimalDefn(h2Platform), "decimal(24,4)");
assertEquals(defaultDefn(DbType.VARCHAR, h2Platform), "varchar(255)");
}
private String defaultDecimalDefn(DatabasePlatform dbPlatform) {
return defaultDefn(DbType.DECIMAL, dbPlatform);
}
private String defaultDefn(DbType type, DatabasePlatform dbPlatform) {
return dbPlatform.getDbTypeMap().get(type).renderType(0, 0);
}
}
@@ -0,0 +1,54 @@
package io.ebean.config.dbplatform;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.sql.Types;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DbPlatformTypeLookupTest {
DbPlatformTypeLookup lookup = new DbPlatformTypeLookup();
@Test
public void byName() throws Exception {
Assertions.assertEquals(lookup.byName("DECIMAL"), DbType.DECIMAL);
assertEquals(lookup.byName("Decimal"), DbType.DECIMAL);
assertEquals(lookup.byName("decimal"), DbType.DECIMAL);
assertEquals(lookup.byName("varchar"), DbType.VARCHAR);
assertEquals(lookup.byName("varchar2"), DbType.VARCHAR);
assertEquals(lookup.byName("float"), DbType.REAL);
assertEquals(lookup.byName("real"), DbType.REAL);
assertEquals(lookup.byName("uuid"), DbType.UUID);
assertEquals(lookup.byName("hstore"), DbType.HSTORE);
assertEquals(lookup.byName("json"), DbType.JSON);
assertEquals(lookup.byName("jsonb"), DbType.JSONB);
assertEquals(lookup.byName("jsonclob"), DbType.JSONCLOB);
assertEquals(lookup.byName("jsonblob"), DbType.JSONBLOB);
assertEquals(lookup.byName("jsonVarchar"), DbType.JSONVARCHAR);
}
@Test
public void byId() throws Exception {
assertEquals(lookup.byId(Types.ARRAY), DbType.ARRAY);
assertEquals(lookup.byId(Types.BIGINT), DbType.BIGINT);
assertEquals(lookup.byId(ExtraDbTypes.UUID), DbType.UUID);
assertEquals(lookup.byId(ExtraDbTypes.HSTORE), DbType.HSTORE);
assertEquals(lookup.byId(ExtraDbTypes.JSON), DbType.JSON);
assertEquals(lookup.byId(ExtraDbTypes.JSONB), DbType.JSONB);
assertEquals(lookup.byId(ExtraDbTypes.JSONClob), DbType.JSONCLOB);
assertEquals(lookup.byId(ExtraDbTypes.JSONBlob), DbType.JSONBLOB);
assertEquals(lookup.byId(ExtraDbTypes.JSONVarchar), DbType.JSONVARCHAR);
}
}
@@ -0,0 +1,67 @@
package io.ebean.config.dbplatform;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DbPlatformTypeParserTest {
@Test
public void parse_text() throws Exception {
DbPlatformType type = DbPlatformTypeParser.parse("text");
assertEquals(type.getName(), "text");
assertEquals(type.getDefaultLength(), 0);
assertEquals(type.getDefaultScale(), 0);
assertEquals(type.renderType(0, 0), "text");
assertEquals(type.renderType(40, 0), "text");
}
@Test
public void parse_varchar20() throws Exception {
DbPlatformType type = DbPlatformTypeParser.parse("varchar(20)");
assertEquals(type.getName(), "varchar");
assertEquals(type.getDefaultLength(), 20);
assertEquals(type.getDefaultScale(), 0);
assertEquals(type.renderType(0, 0), "varchar(20)");
assertEquals(type.renderType(40, 0), "varchar(40)");
}
@Test
public void parse_decimal_18_6() throws Exception {
DbPlatformType type = DbPlatformTypeParser.parse("decimal(18,6)");
assertEquals(type.getName(), "decimal");
assertEquals(type.getDefaultLength(), 18);
assertEquals(type.getDefaultScale(), 6);
assertEquals(type.renderType(0, 0), "decimal(18,6)");
}
@Test
public void parse_something() throws Exception {
DbPlatformType type = DbPlatformTypeParser.parse("something(asd,6)");
assertEquals(type.getName(), "something(asd,6)");
assertEquals(type.getDefaultLength(), 0);
assertEquals(type.getDefaultScale(), 0);
assertEquals(type.renderType(0, 0), "something(asd,6)");
}
@Test
public void parse_invalid() throws Exception {
DbPlatformType type = DbPlatformTypeParser.parse("something(asd");
assertEquals(type.getName(), "something(asd");
assertEquals(type.getDefaultLength(), 0);
assertEquals(type.getDefaultScale(), 0);
assertEquals(type.renderType(0, 0), "something(asd");
}
}
@@ -0,0 +1,34 @@
package io.ebean.config.dbplatform;
import io.ebean.config.PlatformConfig;
import io.ebean.config.dbplatform.mysql.MySqlPlatform;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class MySqlPlatformTest {
@Test
public void uuid_default() {
MySqlPlatform platform = new MySqlPlatform();
platform.configure(new PlatformConfig(), false);
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
assertThat(dbType.renderType(0, 0)).isEqualTo("varchar(40)");
}
@Test
public void uuid_as_binary() {
MySqlPlatform platform = new MySqlPlatform();
PlatformConfig config = new PlatformConfig();
config.setDbUuid(PlatformConfig.DbUuid.AUTO_BINARY);
platform.configure(config, false);
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
assertThat(dbType.renderType(0, 0)).isEqualTo("binary(16)");
}
}
@@ -0,0 +1,50 @@
package io.ebean.config.dbplatform;
import io.ebean.config.PlatformConfig;
import io.ebean.config.dbplatform.oracle.Oracle11Platform;
import io.ebean.config.dbplatform.oracle.Oracle12Platform;
import io.ebean.config.dbplatform.oracle.OraclePlatform;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class OraclePlatformTest {
@Test
void columnAliasPrefix_Oracle11Platform() {
Oracle11Platform platform11 = new Oracle11Platform();
assertThat(platform11.columnAliasPrefix).isEqualTo("c");
}
@Test
void columnAliasPrefix_Oracle12Platform() {
Oracle12Platform platform12 = new Oracle12Platform();
assertThat(platform12.columnAliasPrefix).isEqualTo("c");
}
@Test
void columnAliasPrefix_OraclePlatform() {
OraclePlatform platform = new OraclePlatform();
assertThat(platform.columnAliasPrefix).isNull();
}
@Test
void uuid_default() {
OraclePlatform platform = new OraclePlatform();
platform.configure(new PlatformConfig(), false);
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
assertThat(dbType.renderType(0, 0)).isEqualTo("varchar2(40)");
}
@Test
void uuid_as_binary() {
OraclePlatform platform = new OraclePlatform();
PlatformConfig config = new PlatformConfig();
config.setDbUuid(PlatformConfig.DbUuid.AUTO_BINARY);
platform.configure(config, false);
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
assertThat(dbType.renderType(0, 0)).isEqualTo("raw(16)");
}
}
@@ -0,0 +1,68 @@
package io.ebean.config.dbplatform;
import io.ebean.Query;
import io.ebean.config.PlatformConfig;
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class PostgresPlatformTest {
@Test
public void testUuidType() {
PostgresPlatform platform = new PostgresPlatform();
platform.configure(new PlatformConfig(), false);
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
String columnDefn = dbType.renderType(0, 0);
assertThat(columnDefn).isEqualTo("uuid");
}
@Test
public void default_forUpdate_expect_noKeyUsed() {
DatabasePlatform platform = new PostgresPlatform();
PlatformConfig config = new PlatformConfig();
platform.configure(config);
assertThat(config.isForUpdateNoKey()).isFalse();
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.DEFAULT)).isEqualTo("X for update skip locked");
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.DEFAULT)).isEqualTo("X for update nowait");
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.DEFAULT)).isEqualTo("X for update");
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.UPDATE)).isEqualTo("X for update skip locked");
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.NO_KEY_UPDATE)).isEqualTo("X for no key update skip locked");
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.SHARE)).isEqualTo("X for share skip locked");
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.KEY_SHARE)).isEqualTo("X for key share skip locked");
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.UPDATE)).isEqualTo("X for update nowait");
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.NO_KEY_UPDATE)).isEqualTo("X for no key update nowait");
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.SHARE)).isEqualTo("X for share nowait");
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.KEY_SHARE)).isEqualTo("X for key share nowait");
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.UPDATE)).isEqualTo("X for update");
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.NO_KEY_UPDATE)).isEqualTo("X for no key update");
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.SHARE)).isEqualTo("X for share");
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.KEY_SHARE)).isEqualTo("X for key share");
}
@Test
public void lockWithKey_forUpdate() {
DatabasePlatform platform = new PostgresPlatform();
PlatformConfig config = new PlatformConfig();
config.setForUpdateNoKey(true);
platform.configure(config);
assertThat(config.isForUpdateNoKey()).isTrue();
assertThat(platform.withForUpdate("X", Query.LockWait.SKIPLOCKED, Query.LockType.DEFAULT)).isEqualTo("X for no key update skip locked");
assertThat(platform.withForUpdate("X", Query.LockWait.NOWAIT, Query.LockType.DEFAULT)).isEqualTo("X for no key update nowait");
assertThat(platform.withForUpdate("X", Query.LockWait.WAIT, Query.LockType.DEFAULT)).isEqualTo("X for no key update");
}
}
@@ -0,0 +1,46 @@
package io.ebean.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CamelCaseHelperTest {
@Test
public void when_underscore() {
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there"), "helloThere");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there_jim"), "helloThereJim");
}
@Test
public void when_trailing_numbers() {
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_1"), "hello1");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there_2"), "helloThere2");
}
@Test
public void when_numbers() {
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello1_id"), "hello1Id");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there2_foo"), "helloThere2Foo");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello1id"), "hello1id");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there2foo"), "helloThere2foo");
}
@Test
public void when_numbersProceedUppercase() {
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello1_id"), "hello1Id");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there2_foo"), "helloThere2Foo");
assertEquals(CamelCaseHelper.toUnderscoreFromCamel("hello1Id"), "hello1_id");
assertEquals(CamelCaseHelper.toUnderscoreFromCamel("helloThere2Foo"), "hello_there2_foo");
}
@Test
public void when_already_camel() {
assertEquals(CamelCaseHelper.toCamelFromUnderscore("helloThere"), "helloThere");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("helloThereJim"), "helloThereJim");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello"), "hello");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("HELLO"), "hello");
}
}
@@ -0,0 +1,29 @@
package io.ebean.util;
import org.junit.jupiter.api.Test;
import static io.ebean.util.EncodeB64.enc;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestEncodeB64 {
@Test
public void test() {
assertEquals("A", enc(0));
assertEquals("B", enc(1));
assertEquals("Z", enc(25));
assertEquals("a", enc(26));
assertEquals("z", enc(51));
assertEquals("0", enc(52));
assertEquals("9", enc(61));
assertEquals("-", enc(62));
assertEquals("_", enc(63));
assertEquals("BA", enc(64));
assertEquals("Bk", enc(100));
assertEquals("B9", enc(125));
assertEquals("B-", enc(126));
assertEquals("B_", enc(127));
assertEquals("CA", enc(128));
}
}