You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
6.0 KiB
184 lines
6.0 KiB
package de.superx.bianalysis; |
|
|
|
import java.util.Arrays; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
import org.junit.Assert; |
|
import org.junit.Test; |
|
|
|
import de.superx.bianalysis.bin.ColumnCollector; |
|
|
|
public class ColumnCollectorTest { |
|
|
|
@Test |
|
public void testJoins() { |
|
String sql = "SELECT t1.a," |
|
+ " t2.b," |
|
+ " t3.c," |
|
+ " t.d" |
|
+ " FROM table t" |
|
+ " JOIN table1 AS t1" |
|
+ " ON t1.id = t.id" |
|
+ " JOIN table2 AS t2" |
|
+ " ON t2.id = t1.id" |
|
+ " JOIN table3 AS t3" |
|
+ " ON t3.id = t.id"; |
|
|
|
Map<String, List<String>> expected = new HashMap<>(); |
|
expected.put("null.table1", Arrays.asList("id", "a")); |
|
expected.put("null.table2", Arrays.asList("id", "b")); |
|
expected.put("null.table3", Arrays.asList("id", "c")); |
|
expected.put("null.table", Arrays.asList("id", "d")); |
|
|
|
ColumnCollector collector = new ColumnCollector(); |
|
collector.parseSql(sql); |
|
|
|
Assert.assertEquals(expected, collector.getColumns()); |
|
} |
|
|
|
@Test |
|
public void testCTE() { |
|
String sql = "WITH mycte as (" |
|
+ "SELECT t.a," |
|
+ " t.b," |
|
+ " tk.c," |
|
+ " tk.b," |
|
+ " t.d" |
|
+ " FROM firsttable t" |
|
+ " JOIN firsttable1 AS tk" |
|
+ " ON tk.id = t.id" |
|
+ ")," |
|
+ "myothercte as (" |
|
+ "SELECT " |
|
+ " t3.c," |
|
+ " t1.d," |
|
+ " t1.x" |
|
+ " FROM table t3" |
|
+ " JOIN tableother AS t1" |
|
+ " ON t1.id = t3.id" |
|
+ ")" |
|
+ "select t1, t2.f from mycte " |
|
+ "join tabnew t2 on t2.id = mycte.id"; |
|
|
|
Map<String, List<String>> expected = new HashMap<>(); |
|
expected.put("null.firsttable", Arrays.asList("id", "a", "b", "d")); |
|
expected.put("null.firsttable1", Arrays.asList("id", "c", "b")); |
|
expected.put("null.table", Arrays.asList("id", "c")); |
|
expected.put("null.tableother", Arrays.asList("id", "d","x")); |
|
expected.put("null.tabnew", Arrays.asList("id", "f")); |
|
|
|
ColumnCollector collector = new ColumnCollector(); |
|
collector.parseSql(sql); |
|
|
|
Assert.assertEquals(expected, collector.getColumns()); |
|
} |
|
|
|
@Test |
|
public void testFunction() { |
|
String sql = "WITH mycte as (" |
|
+ "SELECT count(distinct t.a)," |
|
+ " max(min(t.b))," |
|
+ " coalesce(t.c, t.b)," |
|
+ " count(t.d)" |
|
+ " FROM firsttable t" |
|
+ ")" |
|
+ "select * from mycte "; |
|
|
|
Map<String, List<String>> expected = new HashMap<>(); |
|
expected.put("null.firsttable", Arrays.asList("a", "b", "c", "d")); |
|
|
|
ColumnCollector collector = new ColumnCollector(); |
|
collector.parseSql(sql); |
|
|
|
Assert.assertEquals(expected, collector.getColumns()); |
|
} |
|
|
|
@Test |
|
public void testUnion() { |
|
String sql = "SELECT count(distinct t.a)," |
|
+ " max(min(t.b))," |
|
+ " coalesce(t.c, t.b)," |
|
+ " count(t.d)" |
|
+ " FROM firsttable t" |
|
+ " UNION " |
|
+ "select a,c,x from tab "; |
|
|
|
Map<String, List<String>> expected = new HashMap<>(); |
|
expected.put("null.firsttable", Arrays.asList("a", "b", "c", "d")); |
|
expected.put("null.tab", Arrays.asList("a", "c", "x")); |
|
|
|
ColumnCollector collector = new ColumnCollector(); |
|
collector.parseSql(sql); |
|
|
|
Assert.assertEquals(expected, collector.getColumns()); |
|
} |
|
|
|
@Test |
|
public void testSubSelectInWhere() { |
|
String sql = "SELECT t1.a," |
|
+ " t.d" |
|
+ " FROM table t" |
|
+ " JOIN table1 AS t1" |
|
+ " ON t1.id = t.id" |
|
+ " WHERE" |
|
+ " t.s = 1 " |
|
+ " AND t1.x NOT IN (SELECT kpp.ll FROM person AS kpp) "; |
|
|
|
Map<String, List<String>> expected = new HashMap<>(); |
|
expected.put("null.table1", Arrays.asList("id", "a", "x")); |
|
expected.put("null.table", Arrays.asList("id", "d", "s")); |
|
expected.put("null.person", Arrays.asList("ll")); |
|
|
|
ColumnCollector collector = new ColumnCollector(); |
|
collector.parseSql(sql); |
|
|
|
Assert.assertEquals(expected, collector.getColumns()); |
|
} |
|
|
|
@Test |
|
public void testWhere() { |
|
String sql = "SELECT" |
|
+ " id" |
|
+ " , defaulttext" |
|
+ " , uniquename" |
|
+ " FROM k_research_area" |
|
+ " WHERE research_area_type = 'hochschuleigen'"; |
|
|
|
Map<String, List<String>> expected = new HashMap<>(); |
|
expected.put("null.k_research_area", Arrays.asList("id", "defaulttext", "uniquename", "research_area_type")); |
|
|
|
ColumnCollector collector = new ColumnCollector(); |
|
collector.parseSql(sql); |
|
|
|
Assert.assertEquals(expected, collector.getColumns()); |
|
} |
|
|
|
|
|
@Test |
|
public void testUnionCase() { |
|
String sql3 = "WITH outer_cte AS (" |
|
+ " WITH inner_cte AS (" |
|
+ " SELECT id FROM table1" |
|
+ " UNION" |
|
+ " SELECT bla FROM table2" |
|
+ " )" |
|
+ " SELECT ha FROM inner_cte" |
|
+ ")" |
|
+ "SELECT bu FROM outer_cte;" |
|
+ ""; |
|
|
|
Map<String, List<String>> expected = new HashMap<>(); |
|
expected.put("null.table2", Arrays.asList("bla")); |
|
expected.put("null.table1", Arrays.asList("id")); |
|
|
|
ColumnCollector collector = new ColumnCollector(); |
|
collector.parseSql(sql3); |
|
|
|
Assert.assertEquals(expected, collector.getColumns()); |
|
} |
|
|
|
|
|
|
|
}
|
|
|