Browse Source

Polishing.

Use column aliases in order-by/expressions when column is aliased. Reduce method visibility in tests.

Closes #968
Original pull request: #1080.
pull/1092/head
Mark Paluch 4 years ago
parent
commit
239f8c75bd
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 114
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java
  2. 4
      spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/NameRenderer.java
  3. 4
      spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/ExpressionVisitorUnitTests.java
  4. 18
      spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/NameRendererUnitTests.java
  5. 18
      spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/OrderByClauseVisitorUnitTests.java
  6. 18
      spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java

114
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java

@ -23,9 +23,9 @@ import static org.springframework.data.relational.core.sql.SqlIdentifier.*; @@ -23,9 +23,9 @@ import static org.springframework.data.relational.core.sql.SqlIdentifier.*;
import java.util.Map;
import java.util.Set;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.annotation.Version;
@ -62,19 +62,19 @@ import org.springframework.data.relational.core.sql.Table; @@ -62,19 +62,19 @@ import org.springframework.data.relational.core.sql.Table;
* @author Milan Milanov
* @author Myeonghyeon Lee
*/
public class SqlGeneratorUnitTests {
class SqlGeneratorUnitTests {
static final Identifier BACKREF = Identifier.of(unquoted("backref"), "some-value", String.class);
private static final Identifier BACKREF = Identifier.of(unquoted("backref"), "some-value", String.class);
SqlGenerator sqlGenerator;
NamingStrategy namingStrategy = new PrefixingNamingStrategy();
RelationalMappingContext context = new JdbcMappingContext(namingStrategy);
JdbcConverter converter = new BasicJdbcConverter(context, (identifier, path) -> {
private SqlGenerator sqlGenerator;
private NamingStrategy namingStrategy = new PrefixingNamingStrategy();
private RelationalMappingContext context = new JdbcMappingContext(namingStrategy);
private JdbcConverter converter = new BasicJdbcConverter(context, (identifier, path) -> {
throw new UnsupportedOperationException();
});
@BeforeEach
public void setUp() {
void setUp() {
this.sqlGenerator = createSqlGenerator(DummyEntity.class);
}
@ -91,7 +91,7 @@ public class SqlGeneratorUnitTests { @@ -91,7 +91,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-112
public void findOne() {
void findOne() {
String sql = sqlGenerator.getFindOne();
@ -110,7 +110,7 @@ public class SqlGeneratorUnitTests { @@ -110,7 +110,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-493
public void getAcquireLockById() {
void getAcquireLockById() {
String sql = sqlGenerator.getAcquireLockById(LockMode.PESSIMISTIC_WRITE);
@ -124,7 +124,7 @@ public class SqlGeneratorUnitTests { @@ -124,7 +124,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-493
public void getAcquireLockAll() {
void getAcquireLockAll() {
String sql = sqlGenerator.getAcquireLockAll(LockMode.PESSIMISTIC_WRITE);
@ -137,7 +137,7 @@ public class SqlGeneratorUnitTests { @@ -137,7 +137,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-112
public void cascadingDeleteFirstLevel() {
void cascadingDeleteFirstLevel() {
String sql = sqlGenerator.createDeleteByPath(getPath("ref", DummyEntity.class));
@ -145,7 +145,7 @@ public class SqlGeneratorUnitTests { @@ -145,7 +145,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-112
public void cascadingDeleteByPathSecondLevel() {
void cascadingDeleteByPathSecondLevel() {
String sql = sqlGenerator.createDeleteByPath(getPath("ref.further", DummyEntity.class));
@ -154,7 +154,7 @@ public class SqlGeneratorUnitTests { @@ -154,7 +154,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-112
public void deleteAll() {
void deleteAll() {
String sql = sqlGenerator.createDeleteAllSql(null);
@ -162,7 +162,7 @@ public class SqlGeneratorUnitTests { @@ -162,7 +162,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-112
public void cascadingDeleteAllFirstLevel() {
void cascadingDeleteAllFirstLevel() {
String sql = sqlGenerator.createDeleteAllSql(getPath("ref", DummyEntity.class));
@ -170,7 +170,7 @@ public class SqlGeneratorUnitTests { @@ -170,7 +170,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-112
public void cascadingDeleteAllSecondLevel() {
void cascadingDeleteAllSecondLevel() {
String sql = sqlGenerator.createDeleteAllSql(getPath("ref.further", DummyEntity.class));
@ -179,7 +179,7 @@ public class SqlGeneratorUnitTests { @@ -179,7 +179,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-227
public void deleteAllMap() {
void deleteAllMap() {
String sql = sqlGenerator.createDeleteAllSql(getPath("mappedElements", DummyEntity.class));
@ -187,7 +187,7 @@ public class SqlGeneratorUnitTests { @@ -187,7 +187,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-227
public void deleteMapByPath() {
void deleteMapByPath() {
String sql = sqlGenerator.createDeleteByPath(getPath("mappedElements", DummyEntity.class));
@ -195,7 +195,7 @@ public class SqlGeneratorUnitTests { @@ -195,7 +195,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-101
public void findAllSortedByUnsorted() {
void findAllSortedByUnsorted() {
String sql = sqlGenerator.getFindAll(Sort.unsorted());
@ -203,7 +203,7 @@ public class SqlGeneratorUnitTests { @@ -203,7 +203,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-101
public void findAllSortedBySingleField() {
void findAllSortedBySingleField() {
String sql = sqlGenerator.getFindAll(Sort.by("name"));
@ -222,7 +222,7 @@ public class SqlGeneratorUnitTests { @@ -222,7 +222,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-101
public void findAllSortedByMultipleFields() {
void findAllSortedByMultipleFields() {
String sql = sqlGenerator
.getFindAll(Sort.by(new Sort.Order(Sort.Direction.DESC, "name"), new Sort.Order(Sort.Direction.ASC, "other")));
@ -243,7 +243,7 @@ public class SqlGeneratorUnitTests { @@ -243,7 +243,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-101
public void findAllPagedByUnpaged() {
void findAllPagedByUnpaged() {
String sql = sqlGenerator.getFindAll(Pageable.unpaged());
@ -251,7 +251,7 @@ public class SqlGeneratorUnitTests { @@ -251,7 +251,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-101
public void findAllPaged() {
void findAllPaged() {
String sql = sqlGenerator.getFindAll(PageRequest.of(2, 20));
@ -271,7 +271,7 @@ public class SqlGeneratorUnitTests { @@ -271,7 +271,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-101
public void findAllPagedAndSorted() {
void findAllPagedAndSorted() {
String sql = sqlGenerator.getFindAll(PageRequest.of(3, 10, Sort.by("name")));
@ -292,7 +292,7 @@ public class SqlGeneratorUnitTests { @@ -292,7 +292,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-131, DATAJDBC-111
public void findAllByProperty() {
void findAllByProperty() {
// this would get called when ListParent is the element type of a Set
String sql = sqlGenerator.getFindAllByProperty(BACKREF, null, false);
@ -312,7 +312,7 @@ public class SqlGeneratorUnitTests { @@ -312,7 +312,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-223
public void findAllByPropertyWithMultipartIdentifier() {
void findAllByPropertyWithMultipartIdentifier() {
// this would get called when ListParent is the element type of a Set
Identifier parentIdentifier = Identifier.of(unquoted("backref"), "some-value", String.class) //
@ -335,7 +335,7 @@ public class SqlGeneratorUnitTests { @@ -335,7 +335,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-131, DATAJDBC-111
public void findAllByPropertyWithKey() {
void findAllByPropertyWithKey() {
// this would get called when ListParent is th element type of a Map
String sql = sqlGenerator.getFindAllByProperty(BACKREF, unquoted("key-column"), false);
@ -352,13 +352,13 @@ public class SqlGeneratorUnitTests { @@ -352,13 +352,13 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-130
public void findAllByPropertyOrderedWithoutKey() {
void findAllByPropertyOrderedWithoutKey() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> sqlGenerator.getFindAllByProperty(BACKREF, null, true));
}
@Test // DATAJDBC-131, DATAJDBC-111
public void findAllByPropertyWithKeyOrdered() {
void findAllByPropertyWithKeyOrdered() {
// this would get called when ListParent is th element type of a Map
String sql = sqlGenerator.getFindAllByProperty(BACKREF, unquoted("key-column"), true);
@ -372,11 +372,11 @@ public class SqlGeneratorUnitTests { @@ -372,11 +372,11 @@ public class SqlGeneratorUnitTests {
+ "LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1 " //
+ "LEFT OUTER JOIN second_level_referenced_entity ref_further ON ref_further.referenced_entity = ref.x_l1id " //
+ "WHERE dummy_entity.backref = :backref " //
+ "ORDER BY dummy_entity.key-column");
+ "ORDER BY key-column");
}
@Test // DATAJDBC-219
public void updateWithVersion() {
void updateWithVersion() {
SqlGenerator sqlGenerator = createSqlGenerator(VersionedEntity.class, AnsiDialect.INSTANCE);
@ -391,7 +391,7 @@ public class SqlGeneratorUnitTests { @@ -391,7 +391,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-264
public void getInsertForEmptyColumnList() {
void getInsertForEmptyColumnList() {
SqlGenerator sqlGenerator = createSqlGenerator(IdOnlyEntity.class);
@ -401,7 +401,7 @@ public class SqlGeneratorUnitTests { @@ -401,7 +401,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-334
public void getInsertForQuotedColumnName() {
void getInsertForQuotedColumnName() {
SqlGenerator sqlGenerator = createSqlGenerator(EntityWithQuotedColumnName.class, AnsiDialect.INSTANCE);
@ -412,7 +412,7 @@ public class SqlGeneratorUnitTests { @@ -412,7 +412,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-266
public void joinForOneToOneWithoutIdIncludesTheBackReferenceOfTheOuterJoin() {
void joinForOneToOneWithoutIdIncludesTheBackReferenceOfTheOuterJoin() {
SqlGenerator sqlGenerator = createSqlGenerator(ParentOfNoIdChild.class, AnsiDialect.INSTANCE);
@ -423,7 +423,7 @@ public class SqlGeneratorUnitTests { @@ -423,7 +423,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-262
public void update() {
void update() {
SqlGenerator sqlGenerator = createSqlGenerator(DummyEntity.class, AnsiDialect.INSTANCE);
@ -436,7 +436,7 @@ public class SqlGeneratorUnitTests { @@ -436,7 +436,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-324
public void readOnlyPropertyExcludedFromQuery_when_generateUpdateSql() {
void readOnlyPropertyExcludedFromQuery_when_generateUpdateSql() {
final SqlGenerator sqlGenerator = createSqlGenerator(EntityWithReadOnlyProperty.class, AnsiDialect.INSTANCE);
@ -448,7 +448,7 @@ public class SqlGeneratorUnitTests { @@ -448,7 +448,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-334
public void getUpdateForQuotedColumnName() {
void getUpdateForQuotedColumnName() {
SqlGenerator sqlGenerator = createSqlGenerator(EntityWithQuotedColumnName.class, AnsiDialect.INSTANCE);
@ -460,7 +460,7 @@ public class SqlGeneratorUnitTests { @@ -460,7 +460,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-324
public void readOnlyPropertyExcludedFromQuery_when_generateInsertSql() {
void readOnlyPropertyExcludedFromQuery_when_generateInsertSql() {
final SqlGenerator sqlGenerator = createSqlGenerator(EntityWithReadOnlyProperty.class, AnsiDialect.INSTANCE);
@ -471,7 +471,7 @@ public class SqlGeneratorUnitTests { @@ -471,7 +471,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-324
public void readOnlyPropertyIncludedIntoQuery_when_generateFindAllSql() {
void readOnlyPropertyIncludedIntoQuery_when_generateFindAllSql() {
final SqlGenerator sqlGenerator = createSqlGenerator(EntityWithReadOnlyProperty.class);
@ -482,7 +482,7 @@ public class SqlGeneratorUnitTests { @@ -482,7 +482,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-324
public void readOnlyPropertyIncludedIntoQuery_when_generateFindAllByPropertySql() {
void readOnlyPropertyIncludedIntoQuery_when_generateFindAllByPropertySql() {
final SqlGenerator sqlGenerator = createSqlGenerator(EntityWithReadOnlyProperty.class);
@ -494,12 +494,12 @@ public class SqlGeneratorUnitTests { @@ -494,12 +494,12 @@ public class SqlGeneratorUnitTests {
+ "entity_with_read_only_property.key-column AS key-column " //
+ "FROM entity_with_read_only_property " //
+ "WHERE entity_with_read_only_property.backref = :backref " //
+ "ORDER BY entity_with_read_only_property.key-column" //
+ "ORDER BY key-column" //
);
}
@Test // DATAJDBC-324
public void readOnlyPropertyIncludedIntoQuery_when_generateFindAllInListSql() {
void readOnlyPropertyIncludedIntoQuery_when_generateFindAllInListSql() {
final SqlGenerator sqlGenerator = createSqlGenerator(EntityWithReadOnlyProperty.class);
@ -514,7 +514,7 @@ public class SqlGeneratorUnitTests { @@ -514,7 +514,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-324
public void readOnlyPropertyIncludedIntoQuery_when_generateFindOneSql() {
void readOnlyPropertyIncludedIntoQuery_when_generateFindOneSql() {
final SqlGenerator sqlGenerator = createSqlGenerator(EntityWithReadOnlyProperty.class);
@ -529,7 +529,7 @@ public class SqlGeneratorUnitTests { @@ -529,7 +529,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-340
public void deletingLongChain() {
void deletingLongChain() {
assertThat(
createSqlGenerator(Chain4.class).createDeleteByPath(getPath("chain3.chain2.chain1.chain0", Chain4.class))) //
@ -548,7 +548,7 @@ public class SqlGeneratorUnitTests { @@ -548,7 +548,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-359
public void deletingLongChainNoId() {
void deletingLongChainNoId() {
assertThat(createSqlGenerator(NoIdChain4.class)
.createDeleteByPath(getPath("chain3.chain2.chain1.chain0", NoIdChain4.class))) //
@ -556,7 +556,7 @@ public class SqlGeneratorUnitTests { @@ -556,7 +556,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-359
public void deletingLongChainNoIdWithBackreferenceNotReferencingTheRoot() {
void deletingLongChainNoIdWithBackreferenceNotReferencingTheRoot() {
assertThat(createSqlGenerator(IdIdNoIdChain.class)
.createDeleteByPath(getPath("idNoIdChain.chain4.chain3.chain2.chain1.chain0", IdIdNoIdChain.class))) //
@ -573,12 +573,12 @@ public class SqlGeneratorUnitTests { @@ -573,12 +573,12 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-340
public void noJoinForSimpleColumn() {
void noJoinForSimpleColumn() {
assertThat(generateJoin("id", DummyEntity.class)).isNull();
}
@Test // DATAJDBC-340
public void joinForSimpleReference() {
void joinForSimpleReference() {
SqlGenerator.Join join = generateJoin("ref", DummyEntity.class);
@ -593,7 +593,7 @@ public class SqlGeneratorUnitTests { @@ -593,7 +593,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-340
public void noJoinForCollectionReference() {
void noJoinForCollectionReference() {
SqlGenerator.Join join = generateJoin("elements", DummyEntity.class);
@ -602,7 +602,7 @@ public class SqlGeneratorUnitTests { @@ -602,7 +602,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-340
public void noJoinForMappedReference() {
void noJoinForMappedReference() {
SqlGenerator.Join join = generateJoin("mappedElements", DummyEntity.class);
@ -610,7 +610,7 @@ public class SqlGeneratorUnitTests { @@ -610,7 +610,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-340
public void joinForSecondLevelReference() {
void joinForSecondLevelReference() {
SqlGenerator.Join join = generateJoin("ref.further", DummyEntity.class);
@ -626,7 +626,7 @@ public class SqlGeneratorUnitTests { @@ -626,7 +626,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-340
public void joinForOneToOneWithoutId() {
void joinForOneToOneWithoutId() {
SqlGenerator.Join join = generateJoin("child", ParentOfNoIdChild.class);
Table joinTable = join.getJoinTable();
@ -651,7 +651,7 @@ public class SqlGeneratorUnitTests { @@ -651,7 +651,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-340
public void simpleColumn() {
void simpleColumn() {
assertThat(generatedColumn("id", DummyEntity.class)) //
.extracting(c -> c.getName(), c -> c.getTable().getName(), c -> getAlias(c.getTable()), this::getAlias)
@ -660,7 +660,7 @@ public class SqlGeneratorUnitTests { @@ -660,7 +660,7 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-340
public void columnForIndirectProperty() {
void columnForIndirectProperty() {
assertThat(generatedColumn("ref.l1id", DummyEntity.class)) //
.extracting(c -> c.getName(), c -> c.getTable().getName(), c -> getAlias(c.getTable()), this::getAlias) //
@ -669,13 +669,13 @@ public class SqlGeneratorUnitTests { @@ -669,13 +669,13 @@ public class SqlGeneratorUnitTests {
}
@Test // DATAJDBC-340
public void noColumnForReferencedEntity() {
void noColumnForReferencedEntity() {
assertThat(generatedColumn("ref", DummyEntity.class)).isNull();
}
@Test // DATAJDBC-340
public void columnForReferencedEntityWithoutId() {
void columnForReferencedEntityWithoutId() {
assertThat(generatedColumn("child", ParentOfNoIdChild.class)) //
.extracting(c -> c.getName(), c -> c.getTable().getName(), c -> getAlias(c.getTable()), this::getAlias) //
@ -743,7 +743,7 @@ public class SqlGeneratorUnitTests { @@ -743,7 +743,7 @@ public class SqlGeneratorUnitTests {
NoIdChild child;
}
static class NoIdChild {}
private static class NoIdChild {}
static class OtherAggregate {
@Id Long id;

4
spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/NameRenderer.java

@ -87,6 +87,10 @@ class NameRenderer { @@ -87,6 +87,10 @@ class NameRenderer {
RenderNamingStrategy namingStrategy = context.getNamingStrategy();
if (column instanceof Aliased) {
return render(context, namingStrategy.getReferenceName(column));
}
return render(context, SqlIdentifier.from(namingStrategy.getReferenceName(column.getTable()),
namingStrategy.getReferenceName(column)));
}

4
spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/ExpressionVisitorUnitTests.java

@ -33,7 +33,7 @@ import org.springframework.data.relational.core.sql.Table; @@ -33,7 +33,7 @@ import org.springframework.data.relational.core.sql.Table;
/**
* Tests for the {@link ExpressionVisitor}.
*
*
* @author Jens Schauder
*/
public class ExpressionVisitorUnitTests {
@ -82,7 +82,7 @@ public class ExpressionVisitorUnitTests { @@ -82,7 +82,7 @@ public class ExpressionVisitorUnitTests {
Column expression = Column.aliased("col", Table.create("tab"), "col_alias");
expression.visit(visitor);
assertThat(visitor.getRenderedPart().toString()).isEqualTo("tab.col_alias");
assertThat(visitor.getRenderedPart().toString()).isEqualTo("col_alias");
}
@Test // GH-1003

18
spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/NameRendererUnitTests.java

@ -23,7 +23,7 @@ import org.springframework.data.relational.core.sql.Table; @@ -23,7 +23,7 @@ import org.springframework.data.relational.core.sql.Table;
/**
* Unit tests for the {@link NameRenderer}.
*
*
* @author Jens Schauder
*/
class NameRendererUnitTests {
@ -40,13 +40,23 @@ class NameRendererUnitTests { @@ -40,13 +40,23 @@ class NameRendererUnitTests {
assertThat(rendered).isEqualTo("column");
}
@Test // GH-1003
void fullyQualifiedReference() {
@Test // GH-1003, GH-968
void fullyQualifiedReferenceWithAlias() {
Column column = Column.aliased("col", Table.aliased("table", "tab_alias"), "col_alias");
CharSequence rendered = NameRenderer.fullyQualifiedReference(context, column);
assertThat(rendered).isEqualTo("tab_alias.col_alias");
assertThat(rendered).isEqualTo("col_alias");
}
@Test // GH-1003, GH-968
void fullyQualifiedReference() {
Column column = Table.aliased("table", "tab_alias").column("col");
CharSequence rendered = NameRenderer.fullyQualifiedReference(context, column);
assertThat(rendered).isEqualTo("tab_alias.col");
}
}

18
spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/OrderByClauseVisitorUnitTests.java

@ -28,11 +28,12 @@ import org.springframework.data.relational.core.sql.Table; @@ -28,11 +28,12 @@ import org.springframework.data.relational.core.sql.Table;
* Unit tests for {@link OrderByClauseVisitor}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
public class OrderByClauseVisitorUnitTests {
class OrderByClauseVisitorUnitTests {
@Test // DATAJDBC-309
public void shouldRenderOrderByAlias() {
void shouldRenderOrderByAlias() {
Table employee = SQL.table("employee").as("emp");
Column column = employee.column("name").as("emp_name");
@ -42,10 +43,11 @@ public class OrderByClauseVisitorUnitTests { @@ -42,10 +43,11 @@ public class OrderByClauseVisitorUnitTests {
OrderByClauseVisitor visitor = new OrderByClauseVisitor(new SimpleRenderContext(NamingStrategies.asIs()));
select.visit(visitor);
assertThat(visitor.getRenderedPart().toString()).isEqualTo("emp.emp_name ASC");
assertThat(visitor.getRenderedPart().toString()).isEqualTo("emp_name ASC");
}
@Test // DATAJDBC-309
public void shouldApplyNamingStrategy() {
void shouldApplyNamingStrategy() {
Table employee = SQL.table("employee").as("emp");
Column column = employee.column("name").as("emp_name");
@ -55,11 +57,11 @@ public class OrderByClauseVisitorUnitTests { @@ -55,11 +57,11 @@ public class OrderByClauseVisitorUnitTests {
OrderByClauseVisitor visitor = new OrderByClauseVisitor(new SimpleRenderContext(NamingStrategies.toUpper()));
select.visit(visitor);
assertThat(visitor.getRenderedPart().toString()).isEqualTo("EMP.EMP_NAME ASC");
assertThat(visitor.getRenderedPart().toString()).isEqualTo("EMP_NAME ASC");
}
@Test // GH-968
public void shouldRenderOrderByFullyQualifiedName() {
void shouldRenderOrderByFullyQualifiedName() {
Table employee = SQL.table("employee");
Column column = employee.column("name");
@ -73,7 +75,7 @@ public class OrderByClauseVisitorUnitTests { @@ -73,7 +75,7 @@ public class OrderByClauseVisitorUnitTests {
}
@Test // GH-968
public void shouldRenderOrderByFullyQualifiedNameWithTableAlias() {
void shouldRenderOrderByFullyQualifiedNameWithTableAlias() {
Table employee = SQL.table("employee").as("emp");
Column column = employee.column("name");
@ -86,6 +88,4 @@ public class OrderByClauseVisitorUnitTests { @@ -86,6 +88,4 @@ public class OrderByClauseVisitorUnitTests {
assertThat(visitor.getRenderedPart().toString()).isEqualTo("emp.name ASC");
}
}

18
spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java

@ -261,12 +261,24 @@ class SelectRendererUnitTests { @@ -261,12 +261,24 @@ class SelectRendererUnitTests {
void shouldRenderOrderByName() {
Table employee = SQL.table("employee").as("emp");
Column column = employee.column("name").as("emp_name");
Column column = employee.column("name");
Select select = Select.builder().select(column).from(employee).orderBy(OrderByField.from(column).asc()).build();
assertThat(SqlRenderer.toString(select))
.isEqualTo("SELECT emp.name AS emp_name FROM employee emp ORDER BY emp.emp_name ASC");
.isEqualTo("SELECT emp.name FROM employee emp ORDER BY emp.name ASC");
}
@Test // GH-968
void shouldRenderOrderByAlias() {
Table employee = SQL.table("employee").as("emp");
Column column = employee.column("name").as("my_emp_name");
Select select = Select.builder().select(column).from(employee).orderBy(OrderByField.from(column).asc()).build();
assertThat(SqlRenderer.toString(select))
.isEqualTo("SELECT emp.name AS my_emp_name FROM employee emp ORDER BY my_emp_name ASC");
}
@Test // DATAJDBC-309
@ -485,7 +497,7 @@ class SelectRendererUnitTests { @@ -485,7 +497,7 @@ class SelectRendererUnitTests {
.orderBy(tableAName, tableBName) //
.build();
final String rendered = SqlRenderer.toString(select);
String rendered = SqlRenderer.toString(select);
assertThat(rendered)
.isEqualTo("SELECT * FROM tableA JOIN tableB ON tableA.id = tableB.id ORDER BY tableA.name, tableB.name");
}

Loading…
Cancel
Save