4444 * <ol>
4545 * - <li>Python</li>
4646 * - <li>NumPy</li>
47- * - <li>Pandas DataFrame</li>
47+ * - <li>Pandas</li>
48+ * - <li>PandaSql</li>
4849 * <ol>
4950 *
5051 * To run manually on such environment, use:
5152 * <code>
5253 * mvn -Dpython.test.exclude='' test -pl python -am
5354 * </code>
5455 */
55- public class PythonPandasSqlInterpreterTest {
56+ public class PythonInterpreterPandasSqlTest {
5657
5758 private InterpreterGroup intpGroup ;
58- private PythonPandasSqlInterpreter sql ;
59+ private PythonInterpreterPandasSql sql ;
5960 private PythonInterpreter python ;
6061
6162 private InterpreterContext context ;
@@ -72,7 +73,7 @@ public void setUp() throws Exception {
7273 python .setInterpreterGroup (intpGroup );
7374 python .open ();
7475
75- sql = new PythonPandasSqlInterpreter (p );
76+ sql = new PythonInterpreterPandasSql (p );
7677 sql .setInterpreterGroup (intpGroup );
7778
7879 intpGroup .put ("note" , Arrays .asList (python , sql ));
@@ -92,23 +93,47 @@ public void setUp() throws Exception {
9293 }
9394
9495 @ Test
95- public void sqlOverTestDataPrintsTable () {
96- //given
97- // `import pandas as pd` and `import numpy as np` done
98- // DataFrame \w test data
99- String expectedTable = "name\t age\n \n moon\t 33\n \n park\t 34" ;
100- python .interpret ("df2 = pd.DataFrame({ 'age' : np.array([33, 51, 51, 34]), " +
101- "'name' : pd.Categorical(['moon','jobs','gates','park'])})" , context );
96+ public void dependenciesAreInstalled () {
97+ InterpreterResult ret = python .interpret ("import pandas\n import pandasql\n import numpy\n " , context );
98+ assertEquals (ret .message (), InterpreterResult .Code .SUCCESS , ret .code ());
99+ }
100+
101+ @ Test
102+ public void errorMessageIfDependenciesNotInstalled () {
103+ InterpreterResult ret ;
104+ // given
105+ ret = python .interpret (
106+ "pysqldf = lambda q: print('Can not execute SQL as Python dependency is not installed')" ,
107+ context );
108+ assertEquals (ret .message (), InterpreterResult .Code .SUCCESS , ret .code ());
109+
110+ // when
111+ ret = sql .interpret ("SELECT * from something" , context );
112+
113+ // then
114+ assertNotNull (ret );
115+ assertEquals (ret .message (), InterpreterResult .Code .SUCCESS , ret .code ());
116+ assertTrue (ret .message ().contains ("dependency is not installed" ));
117+ }
102118
119+ @ Test
120+ public void sqlOverTestDataPrintsTable () {
121+ InterpreterResult ret ;
122+ // given
123+ //String expectedTable = "name\tage\n\nmoon\t33\n\npark\t34";
124+ ret = python .interpret ("import pandas as pd" , context );
125+ ret = python .interpret ("import numpy as np" , context );
126+ // DataFrame df2 \w test data
127+ ret = python .interpret ("df2 = pd.DataFrame({ 'age' : np.array([33, 51, 51, 34]), " +
128+ "'name' : pd.Categorical(['moon','jobs','gates','park'])})" , context );
129+ assertEquals (ret .message (), InterpreterResult .Code .SUCCESS , ret .code ());
103130
104131 //when
105- InterpreterResult ret = sql .interpret ("select name, age from df2 where age < 40" , context );
132+ ret = sql .interpret ("select name, age from df2 where age < 40" , context );
106133
107134 //then
108- assertEquals (InterpreterResult .Code .SUCCESS , ret .code ());
109- assertEquals (Type .TABLE , ret .type ());
110- //System.out.println(ret.message());
111- //System.out.println(expectedTable);
135+ assertEquals (ret .message (), InterpreterResult .Code .SUCCESS , ret .code ());
136+ assertEquals (ret .message (), Type .TABLE , ret .type ());
112137 //assertEquals(expectedTable, ret.message()); //somehow it's same but not equal
113138 assertTrue (ret .message ().indexOf ("moon\t 33" ) > 0 );
114139 assertTrue (ret .message ().indexOf ("park\t 34" ) > 0 );
@@ -124,9 +149,8 @@ public void badSqlSyntaxFails() {
124149 //then
125150 assertNotNull ("Interpreter returned 'null'" , ret );
126151 //System.out.println("\nInterpreter response: \n" + ret.message());
127- assertEquals (InterpreterResult .Code .ERROR , ret .code ());
152+ assertEquals (ret . toString (), InterpreterResult .Code .ERROR , ret .code ());
128153 assertTrue (ret .message ().length () > 0 );
129154 }
130155
131-
132156}
0 commit comments