fix matplotlib testcase

This commit is contained in:
astroshim 2017-03-15 00:10:52 +09:00
parent 046db8816a
commit f8e19bef1a
4 changed files with 65 additions and 52 deletions

View file

@ -454,6 +454,7 @@ public class PythonInterpreter extends Interpreter implements ExecuteResultHandl
while ((line = bootstrapReader.readLine()) != null) {
bootstrapCode += line + "\n";
}
interpret(bootstrapCode, context);
}

View file

@ -39,10 +39,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import static org.apache.zeppelin.python.PythonInterpreter.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
public class PythonInterpreterMatplotlibTest implements InterpreterOutputListener {
private InterpreterGroup intpGroup;
@ -61,18 +58,12 @@ public class PythonInterpreterMatplotlibTest implements InterpreterOutputListene
python = new PythonInterpreter(p);
python.setInterpreterGroup(intpGroup);
//python.open();
List<Interpreter> interpreters = new LinkedList<>();
interpreters.add(python);
intpGroup.put("note", interpreters);
out = new InterpreterOutput(this);
/*
context = new InterpreterContext("note", "id", null, "title", "text", new AuthenticationInfo(),
new HashMap<String, Object>(), new GUI(),
new AngularObjectRegistry(intpGroup.getId(), null), null,
new LinkedList<InterpreterContextRunner>(), out);
*/
context = new InterpreterContext("note", "id", null, "title", "text",
new AuthenticationInfo(),
@ -83,7 +74,11 @@ public class PythonInterpreterMatplotlibTest implements InterpreterOutputListene
new LinkedList<InterpreterContextRunner>(),
out);
python.open();
}
@After
public void afterTest() throws IOException {
python.close();
}
@Test
@ -93,8 +88,8 @@ public class PythonInterpreterMatplotlibTest implements InterpreterOutputListene
assertEquals(ret.message().toString(), InterpreterResult.Code.SUCCESS, ret.code());
// inline backend
//ret = python.interpret("import backend_zinline", context);
//assertEquals(ret.message().toString(), InterpreterResult.Code.SUCCESS, ret.code());
ret = python.interpret("import backend_zinline", context);
assertEquals(ret.message().toString(), InterpreterResult.Code.SUCCESS, ret.code());
}
@Test
@ -106,24 +101,16 @@ public class PythonInterpreterMatplotlibTest implements InterpreterOutputListene
ret = python.interpret("plt.plot([1, 2, 3])", context);
ret = python.interpret("plt.show()", context);
System.out.println("===>" + new String(out.getOutputAt(0).toByteArray()));
assertEquals(new String(out.getOutputAt(0).toByteArray()), InterpreterResult.Code.SUCCESS, ret.code());
assertEquals(new String(out.getOutputAt(0).toByteArray()), InterpreterResult.Type.TEXT, out.getOutputAt(0).getType());
assertEquals(new String(out.getOutputAt(1).toByteArray()), InterpreterResult.Type.HTML, out.getOutputAt(1).getType());
assertTrue(new String(out.getOutputAt(1).toByteArray()).contains("data:image/png;base64"));
assertTrue(new String(out.getOutputAt(1).toByteArray()).contains("<div>"));
/*
assertEquals(ret.message().get(0).getData(), InterpreterResult.Code.SUCCESS, ret.code());
assertEquals(ret.message().get(0).getData(), Type.HTML, ret.message().get(0).getType());
assertTrue(ret.message().get(0).getData().contains("data:image/png;base64"));
assertTrue(ret.message().get(0).getData().contains("<div>"));
*/
}
@Test
// Test for when configuration is set to auto-close figures after show().
public void testClose() {
public void testClose() throws IOException {
InterpreterResult ret;
InterpreterResult ret1;
InterpreterResult ret2;
@ -137,19 +124,27 @@ public class PythonInterpreterMatplotlibTest implements InterpreterOutputListene
// of FigureManager, causing show() to return before setting the output
// type to HTML.
ret = python.interpret("plt.show()", context);
assertEquals(new String(out.getOutputAt(0).toByteArray()), InterpreterResult.Code.SUCCESS, ret.code());
assertEquals(0, ret.message().size());
// Now test that new plot is drawn. It should be identical to the
// previous one.
ret = python.interpret("plt.plot([1, 2, 3])", context);
String msg1 = new String(out.getOutputAt(0).toByteArray());
InterpreterResult.Type type1 = out.getOutputAt(0).getType();
ret2 = python.interpret("plt.show()", context);
assertEquals(ret1.message().get(0).getType(), ret2.message().get(0).getType());
assertEquals(ret1.message().get(0).getData(), ret2.message().get(0).getData());
String msg2 = new String(out.getOutputAt(0).toByteArray());
InterpreterResult.Type type2 = out.getOutputAt(0).getType();
assertEquals(msg1, msg2);
assertEquals(type1, type2);
}
@Test
// Test for when configuration is set to not auto-close figures after show().
public void testNoClose() {
public void testNoClose() throws IOException {
InterpreterResult ret;
InterpreterResult ret1;
InterpreterResult ret2;
@ -163,14 +158,18 @@ public class PythonInterpreterMatplotlibTest implements InterpreterOutputListene
// of FigureManager, causing show() to set the output
// type to HTML even though the figure is inactive.
ret = python.interpret("plt.show()", context);
assertEquals("", ret.message().get(0).getData());
String msg1 = new String(out.getOutputAt(0).toByteArray());
assertNotSame("", msg1);
// Now test that plot can be reshown if it is updated. It should be
// different from the previous one because it will plot the same line
// again but in a different color.
ret = python.interpret("plt.plot([1, 2, 3])", context);
msg1 = new String(out.getOutputAt(1).toByteArray());
ret2 = python.interpret("plt.show()", context);
assertNotSame(ret1.message().get(0).getData(), ret2.message().get(0).getData());
String msg2 = new String(out.getOutputAt(1).toByteArray());
assertNotSame(msg1, msg2);
}

View file

@ -21,13 +21,16 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.apache.zeppelin.display.AngularObjectRegistry;
import org.apache.zeppelin.display.GUI;
import org.apache.zeppelin.interpreter.Interpreter;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterContextRunner;
import org.apache.zeppelin.interpreter.InterpreterGroup;
@ -35,7 +38,10 @@ import org.apache.zeppelin.interpreter.InterpreterOutput;
import org.apache.zeppelin.interpreter.InterpreterOutputListener;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.interpreter.InterpreterResult.Type;
import org.apache.zeppelin.interpreter.InterpreterResultMessageOutput;
import org.apache.zeppelin.resource.LocalResourcePool;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -53,13 +59,14 @@ import org.junit.Test;
* mvn -Dpython.test.exclude='' test -pl python -am
* </code>
*/
public class PythonInterpreterPandasSqlTest {
public class PythonInterpreterPandasSqlTest implements InterpreterOutputListener {
private InterpreterGroup intpGroup;
private PythonInterpreterPandasSql sql;
private PythonInterpreter python;
private InterpreterContext context;
private InterpreterOutput out;
@Before
public void setUp() throws Exception {
@ -71,21 +78,29 @@ public class PythonInterpreterPandasSqlTest {
python = new PythonInterpreter(p);
python.setInterpreterGroup(intpGroup);
python.open();
sql = new PythonInterpreterPandasSql(p);
sql.setInterpreterGroup(intpGroup);
intpGroup.put("note", Arrays.asList(python, sql));
context = new InterpreterContext("note", "id", null, "title", "text", new AuthenticationInfo(),
new HashMap<String, Object>(), new GUI(),
new AngularObjectRegistry(intpGroup.getId(), null), null,
new LinkedList<InterpreterContextRunner>(), new InterpreterOutput(null));
out = new InterpreterOutput(this);
//important to be last step
context = new InterpreterContext("note", "id", null, "title", "text",
new AuthenticationInfo(),
new HashMap<String, Object>(),
new GUI(),
new AngularObjectRegistry(intpGroup.getId(), null),
new LocalResourcePool("id"),
new LinkedList<InterpreterContextRunner>(),
out);
python.open();
sql.open();
//it depends on python interpreter presence in the same group
}
@After
public void afterTest() throws IOException {
sql.close();
}
@Test
@ -172,4 +187,19 @@ public class PythonInterpreterPandasSqlTest {
assertTrue(ret.message().get(0).getData().indexOf("nan") > 0);
assertTrue(ret.message().get(0).getData().indexOf("6.7") > 0);
}
@Override
public void onUpdateAll(InterpreterOutput out) {
}
@Override
public void onAppend(int index, InterpreterResultMessageOutput out, byte[] line) {
}
@Override
public void onUpdate(int index, InterpreterResultMessageOutput out) {
}
}

View file

@ -59,7 +59,6 @@ public class PythonInterpreterTest implements InterpreterOutputListener {
Properties p = new Properties();
p.setProperty(ZEPPELIN_PYTHON, DEFAULT_ZEPPELIN_PYTHON);
p.setProperty(MAX_RESULT, "1000");
//p.setProperty("python.path", "/Users/shim/zeppelin/interpreter/lib/python");
return p;
}
@ -107,22 +106,6 @@ public class PythonInterpreterTest implements InterpreterOutputListener {
assertTrue(new String(out.getOutputAt(0).toByteArray()).contains("hi\nhi\nhi"));
}
@Test
public void testMy() throws IOException {
InterpreterResult ret;
ret = pythonInterpreter.interpret("import matplotlib.pyplot as plt", context);
ret = pythonInterpreter.interpret("z.configure_mpl(interactive=False)", context);
ret = pythonInterpreter.interpret("plt.plot([1, 2, 3])", context);
ret = pythonInterpreter.interpret("plt.show()", context);
System.out.println("===>" + new String(out.getOutputAt(0).toByteArray()));
assertEquals(ret.message().get(0).getData(), InterpreterResult.Code.SUCCESS, ret.code());
assertEquals(ret.message().get(0).getData(), InterpreterResult.Type.HTML, ret.message().get(0).getType());
assertTrue(ret.message().get(0).getData().contains("data:image/png;base64"));
assertTrue(ret.message().get(0).getData().contains("<div>"));
}
@Override
public void onUpdateAll(InterpreterOutput out) {