|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.relational.core.sql.render; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import org.junit.Test; |
| 21 | +import org.springframework.data.relational.core.sql.Column; |
| 22 | +import org.springframework.data.relational.core.sql.StatementBuilder; |
| 23 | +import org.springframework.data.relational.core.sql.Table; |
| 24 | + |
| 25 | +/** |
| 26 | + * Unit tests for rendered {@link org.springframework.data.relational.core.sql.Conditions}. |
| 27 | + * |
| 28 | + * @author Mark Paluch |
| 29 | + */ |
| 30 | +public class ConditionRendererUnitTests { |
| 31 | + |
| 32 | + Table table = Table.create("my_table"); |
| 33 | + Column left = table.column("left"); |
| 34 | + Column right = table.column("right"); |
| 35 | + |
| 36 | + @Test // DATAJDBC-309 |
| 37 | + public void shouldRenderEquals() { |
| 38 | + |
| 39 | + String sql = NaiveSqlRenderer |
| 40 | + .render(StatementBuilder.select(left).from(table).where(left.isEqualTo(right)).build()); |
| 41 | + |
| 42 | + assertThat(sql).endsWith("WHERE my_table.left = my_table.right"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test // DATAJDBC-309 |
| 46 | + public void shouldRenderNotEquals() { |
| 47 | + |
| 48 | + String sql = NaiveSqlRenderer |
| 49 | + .render(StatementBuilder.select(left).from(table).where(left.isNotEqualTo(right)).build()); |
| 50 | + |
| 51 | + assertThat(sql).endsWith("WHERE my_table.left != my_table.right"); |
| 52 | + |
| 53 | + sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.isEqualTo(right).not()).build()); |
| 54 | + |
| 55 | + assertThat(sql).endsWith("WHERE my_table.left != my_table.right"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test // DATAJDBC-309 |
| 59 | + public void shouldRenderIsLess() { |
| 60 | + |
| 61 | + String sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.isLess(right)).build()); |
| 62 | + |
| 63 | + assertThat(sql).endsWith("WHERE my_table.left < my_table.right"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test // DATAJDBC-309 |
| 67 | + public void shouldRenderIsLessOrEqualTo() { |
| 68 | + |
| 69 | + String sql = NaiveSqlRenderer |
| 70 | + .render(StatementBuilder.select(left).from(table).where(left.isLessOrEqualTo(right)).build()); |
| 71 | + |
| 72 | + assertThat(sql).endsWith("WHERE my_table.left <= my_table.right"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test // DATAJDBC-309 |
| 76 | + public void shouldRenderIsGreater() { |
| 77 | + |
| 78 | + String sql = NaiveSqlRenderer |
| 79 | + .render(StatementBuilder.select(left).from(table).where(left.isGreater(right)).build()); |
| 80 | + |
| 81 | + assertThat(sql).endsWith("WHERE my_table.left > my_table.right"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test // DATAJDBC-309 |
| 85 | + public void shouldRenderIsGreaterOrEqualTo() { |
| 86 | + |
| 87 | + String sql = NaiveSqlRenderer |
| 88 | + .render(StatementBuilder.select(left).from(table).where(left.isGreaterOrEqualTo(right)).build()); |
| 89 | + |
| 90 | + assertThat(sql).endsWith("WHERE my_table.left >= my_table.right"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test // DATAJDBC-309 |
| 94 | + public void shouldRenderIn() { |
| 95 | + |
| 96 | + String sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.in(right)).build()); |
| 97 | + |
| 98 | + assertThat(sql).endsWith("WHERE my_table.left IN (my_table.right)"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test // DATAJDBC-309 |
| 102 | + public void shouldRenderLike() { |
| 103 | + |
| 104 | + String sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.like(right)).build()); |
| 105 | + |
| 106 | + assertThat(sql).endsWith("WHERE my_table.left LIKE my_table.right"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test // DATAJDBC-309 |
| 110 | + public void shouldRenderIsNull() { |
| 111 | + |
| 112 | + String sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.isNull()).build()); |
| 113 | + |
| 114 | + assertThat(sql).endsWith("WHERE my_table.left IS NULL"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test // DATAJDBC-309 |
| 118 | + public void shouldRenderIsNotNull() { |
| 119 | + |
| 120 | + String sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.isNotNull()).build()); |
| 121 | + |
| 122 | + assertThat(sql).endsWith("WHERE my_table.left IS NOT NULL"); |
| 123 | + |
| 124 | + sql = NaiveSqlRenderer.render(StatementBuilder.select(left).from(table).where(left.isNull().not()).build()); |
| 125 | + |
| 126 | + assertThat(sql).endsWith("WHERE my_table.left IS NOT NULL"); |
| 127 | + } |
| 128 | +} |
0 commit comments