This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new dc9cb2c Added unit test for
TablesContext#findTableNamesByColumnProjection (#16063)
dc9cb2c is described below
commit dc9cb2cf125980b61e03b4269edd2034c9f03100
Author: Abhijeet Jejurkar <[email protected]>
AuthorDate: Tue Mar 15 06:41:14 2022 +0530
Added unit test for TablesContext#findTableNamesByColumnProjection (#16063)
* Added unit test for TablesContext#findTableNamesByColumnProjection
Fixes: #15815
Signed-off-by: Abhijeet Jejurkar <[email protected]>
* Removed Extra Line at Line Number 176
---
.../binder/segment/table/TablesContextTest.java | 67 +++++++++++++++++++++-
1 file changed, 66 insertions(+), 1 deletion(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContextTest.java
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContextTest.java
index cbd1a62..7d5b744 100644
---
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContextTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContextTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.infra.binder.segment.table;
import com.google.common.collect.Sets;
+import
org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
@@ -113,7 +114,66 @@ public final class TablesContextTest {
assertFalse(actual.isEmpty());
assertThat(actual.get("col"), is("TABLE_1"));
}
-
+
+ @Test
+ public void assertFindTableNameWhenColumnProjectionWhenSingleTable() {
+ SimpleTableSegment tableSegment = createTableSegment("table_1",
"tbl_1");
+ ColumnProjection columnProjection = createColumnProjection(null,
"col", "cl");
+ Map<String, String> actual = new
TablesContext(Collections.singletonList(tableSegment))
+
.findTableNamesByColumnProjection(Collections.singletonList(columnProjection),
mock(ShardingSphereSchema.class));
+ assertFalse(actual.isEmpty());
+ assertThat(actual.get("col"), is("table_1"));
+ }
+
+ @Test
+ public void assertFindTableNameWhenColumnProjectionOwnerPresent() {
+ SimpleTableSegment tableSegment1 = createTableSegment("table_1",
"tbl_1");
+ SimpleTableSegment tableSegment2 = createTableSegment("table_2",
"tbl_2");
+ ColumnProjection columnProjection = createColumnProjection("table_1",
"col", "cl");
+ Map<String, String> actual = new
TablesContext(Arrays.asList(tableSegment1, tableSegment2))
+
.findTableNamesByColumnProjection(Collections.singletonList(columnProjection),
mock(ShardingSphereSchema.class));
+ assertFalse(actual.isEmpty());
+ assertThat(actual.get("table_1.col"), is("table_1"));
+ }
+
+ @Test
+ public void assertFindTableNameWhenColumnProjectionOwnerAbsent() {
+ SimpleTableSegment tableSegment1 = createTableSegment("table_1",
"tbl_1");
+ SimpleTableSegment tableSegment2 = createTableSegment("table_2",
"tbl_2");
+ ColumnProjection columnProjection = createColumnProjection(null,
"col", "cl");
+ Map<String, String> actual = new
TablesContext(Arrays.asList(tableSegment1, tableSegment2))
+
.findTableNamesByColumnProjection(Collections.singletonList(columnProjection),
mock(ShardingSphereSchema.class));
+ assertTrue(actual.isEmpty());
+ }
+
+ @Test
+ public void
assertFindTableNameWhenColumnProjectionOwnerAbsentAndSchemaMetaDataContainsColumn()
{
+ SimpleTableSegment tableSegment1 = createTableSegment("table_1",
"tbl_1");
+ SimpleTableSegment tableSegment2 = createTableSegment("table_2",
"tbl_2");
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+
when(schema.getAllColumnNames("table_1")).thenReturn(Collections.singletonList("col"));
+ ColumnProjection columnProjection = createColumnProjection(null,
"col", "cl");
+ Map<String, String> actual = new
TablesContext(Arrays.asList(tableSegment1, tableSegment2))
+
.findTableNamesByColumnProjection(Collections.singletonList(columnProjection),
schema);
+ assertFalse(actual.isEmpty());
+ assertThat(actual.get("col"), is("table_1"));
+ }
+
+ @Test
+ public void
assertFindTableNameWhenColumnProjectionOwnerAbsentAndSchemaMetaDataContainsColumnInUpperCase()
{
+ SimpleTableSegment tableSegment1 = createTableSegment("TABLE_1",
"TBL_1");
+ SimpleTableSegment tableSegment2 = createTableSegment("TABLE_2",
"TBL_2");
+ TableMetaData tableMetaData = new TableMetaData("TABLE_1",
+ Arrays.asList(new ColumnMetaData("COL", 0, false, false,
true)),
+ Collections.EMPTY_LIST);
+ ShardingSphereSchema schema = new
ShardingSphereSchema(Arrays.asList(tableMetaData).stream().collect(Collectors.toMap(TableMetaData::getName,
v -> v)));
+ ColumnProjection columnProjection = createColumnProjection(null,
"COL", "CL");
+ Map<String, String> actual = new
TablesContext(Arrays.asList(tableSegment1, tableSegment2))
+
.findTableNamesByColumnProjection(Collections.singletonList(columnProjection),
schema);
+ assertFalse(actual.isEmpty());
+ assertThat(actual.get("col"), is("TABLE_1"));
+ }
+
private SimpleTableSegment createTableSegment(final String tableName,
final String alias) {
SimpleTableSegment result = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue(tableName)));
AliasSegment aliasSegment = new AliasSegment(0, 0, new
IdentifierValue(alias));
@@ -128,6 +188,11 @@ public final class TablesContextTest {
}
return result;
}
+
+ private ColumnProjection createColumnProjection(final String owner, final
String name, final String alias) {
+ ColumnProjection result = new ColumnProjection(owner, name, alias);
+ return result;
+ }
@Test
public void assertGetSchemaNameWithSameSchemaAndSameTable() {