#766 - RawSql should automatically map column alias with underscore to camelCase ... so item_total should automatically map to bean property itemTotal

This commit is contained in:
Robin Bygrave
2016-07-08 23:44:11 +12:00
parent c7c6410cd9
commit aaf256b9be
6 changed files with 75 additions and 13 deletions
@@ -13,6 +13,16 @@ import static org.junit.Assert.assertNull;
public class TestRawSqlBuilder extends BaseTestCase {
@Test
public void testDeriveProperty() {
assertThat(RawSql.ColumnMapping.Column.derivePropertyName("item_total", "some_other")).isEqualTo("itemTotal");
assertThat(RawSql.ColumnMapping.Column.derivePropertyName(null, "some_other")).isEqualTo("someOther");
assertThat(RawSql.ColumnMapping.Column.derivePropertyName(null, "alias.some_other")).isEqualTo("someOther");
assertThat(RawSql.ColumnMapping.Column.derivePropertyName(null, "alias.someOther")).isEqualTo("someOther");
assertThat(RawSql.ColumnMapping.Column.derivePropertyName(null, "some")).isEqualTo("some");
assertThat(RawSql.ColumnMapping.Column.derivePropertyName(null, "someOther")).isEqualTo("someOther");
}
@Test
public void testSimple() {
@@ -192,9 +202,9 @@ public class TestRawSqlBuilder extends BaseTestCase {
assertEquals(0, columnMapping.getIndexPosition("id"));
assertEquals(1, columnMapping.getIndexPosition("status"));
assertEquals(2, columnMapping.getIndexPosition("budget"));
assertEquals(3, columnMapping.getIndexPosition("transaction_sum"));
assertEquals(3, columnMapping.getIndexPosition("transactionSum"));
assertEquals(4, columnMapping.getIndexPosition("balance"));
assertEquals(5, columnMapping.getIndexPosition("data_month"));
assertEquals(5, columnMapping.getIndexPosition("dataMonth"));
}
@Test
@@ -0,0 +1,31 @@
package com.avaje.ebean.util;
import org.junit.Test;
import static org.junit.Assert.*;
public class CamelCaseHelperTest {
@Test
public void when_underscore() throws Exception {
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there"), "helloThere");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there_jim"), "helloThereJim");
}
@Test
public void when_trailing_numbers() throws Exception {
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_1"), "hello1");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there_2"), "helloThere2");
}
@Test
public void when_already_camel() throws Exception {
assertEquals(CamelCaseHelper.toCamelFromUnderscore("helloThere"), "helloThere");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("helloThereJim"), "helloThereJim");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello"), "hello");
assertEquals(CamelCaseHelper.toCamelFromUnderscore("HELLO"), "HELLO");
}
}
@@ -51,7 +51,7 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
}
@Test
public void test_when_explicitMapping() {
public void test_when_aliasInUnderscore() {
ResetBasicData.reset();
@@ -62,8 +62,6 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
RawSql rawSql = RawSqlBuilder.parse(sql)
.columnMapping("order_id", "order.id")
.columnMapping("total_items", "totalItems")
.columnMapping("total_amount", "totalAmount")
.create();
Query<OrderAggregate> query = Ebean.find(OrderAggregate.class)
@@ -74,6 +72,28 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
assertThat(query.getGeneratedSql()).contains("count(*) as total_items, sum(order_qty*unit_price) as total_amount");
}
@Test
public void test_when_aliasInCamelCase() {
ResetBasicData.reset();
String sql =
"select order_id, count(*) as totalItems, sum(order_qty*unit_price) as totalAmount \n" +
"from o_order_detail \n" +
"group by order_id";
RawSql rawSql = RawSqlBuilder.parse(sql)
.columnMapping("order_id", "order.id")
.create();
Query<OrderAggregate> query = Ebean.find(OrderAggregate.class)
.setRawSql(rawSql);
query.findList();
assertThat(query.getGeneratedSql()).contains("count(*) as totalItems, sum(order_qty*unit_price) as totalAmount");
}
@Test
public void testDefaultNamedRawSql() {
@@ -92,7 +112,7 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
Query<OrderAggregate> query = Ebean.getDefaultServer().createNamedQuery(OrderAggregate.class, "withMax");
List<OrderAggregate> list = query.findList();
assertThat(query.getGeneratedSql()).contains("count(*) as totalItems, sum(order_qty*unit_price) as totalAmount, max(order_qty*unit_price) as maxAmount from o_order_detail");
assertThat(query.getGeneratedSql()).contains("count(*) as total_items, sum(order_qty*unit_price) as total_amount, max(order_qty*unit_price) as maxAmount from o_order_detail");
assertNotNull(list);
}
@@ -108,7 +128,7 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
.order().desc("totalAmount")
.findList();
assertThat(query.getGeneratedSql()).contains("count(*) as totalItems, sum(order_qty*unit_price) as totalAmount, max(order_qty*unit_price) as maxAmount from o_order_detail");
assertThat(query.getGeneratedSql()).contains("count(*) as total_items, sum(order_qty*unit_price) as total_amount, max(order_qty*unit_price) as maxAmount from o_order_detail");
assertThat(query.getGeneratedSql()).contains("from o_order_detail where order_id > ? group by order_id having count(*) > ? order by sum(order_qty*unit_price) desc");
assertNotNull(list);
}