JdbcMapper/test/src/test/java/com/moparisthebest/jdbc/codegen/SimpleSQLParserTest.java

54 lines
1.4 KiB
Java
Raw Normal View History

2017-05-27 23:54:22 -04:00
package com.moparisthebest.jdbc.codegen;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Created by mopar on 5/30/17.
*/
public class SimpleSQLParserTest {
2017-05-27 23:54:22 -04:00
private final SQLParser factory = new SimpleSQLParser();
public SQLParser getFactory() {
return factory;
}
2017-05-27 23:54:22 -04:00
@Test
public void testSingleSelect() {
final SQLParser ret = getFactory().parse("select bob from tom");
2017-05-27 23:54:22 -04:00
assertTrue(ret.isSelect());
assertArrayEquals(new String[]{null, "BOB"}, ret.columnNames());
}
@Test
public void testMultiSelect() {
final String[] expected = new String[]{null, "BOB", "TOM"};
for (final String sql : new String[]{
"select bob, tom from tom"
, "select some_bob bob, some_tom as tom from tom"
, "select tom.bob, some_tom as tom from tom"
, "select tom.bob, COALESCE(some_tom, 'UNKNOWN') as tom from tom"
, "select tom.bob, (SELECT some_column from some_table where other_column = 'YAY') as tom from tom"
2017-05-27 23:54:22 -04:00
}) {
final SQLParser ret = getFactory().parse(sql);
2017-05-27 23:54:22 -04:00
assertTrue(ret.isSelect());
assertArrayEquals(expected, ret.columnNames());
}
}
@Test
public void testNotSelect() {
for (final String sql : new String[]{
"UPDATE bob SET bob = 'bob' WHERE bob_no = 1"
, "INSERT INTO bob (bob_no, bob) VALUES (1, 'bob')"
, "MERGE INTO bob bla bla bla"
}) {
final SQLParser ret = getFactory().parse(sql);
2017-05-27 23:54:22 -04:00
assertFalse(ret.isSelect());
}
}
}