1 package org.simantics.pythonlink.test;
\r
3 import static org.junit.Assert.assertArrayEquals;
\r
4 import static org.junit.Assert.assertEquals;
\r
5 import static org.junit.Assert.assertNotNull;
\r
6 import static org.junit.Assert.assertTrue;
\r
8 import java.util.Map;
\r
10 import org.eclipse.core.runtime.IProgressMonitor;
\r
11 import org.eclipse.core.runtime.NullProgressMonitor;
\r
12 import org.junit.After;
\r
13 import org.junit.AfterClass;
\r
14 import org.junit.Before;
\r
15 import org.junit.BeforeClass;
\r
16 import org.junit.Test;
\r
17 import org.simantics.PlatformException;
\r
18 import org.simantics.Simantics;
\r
19 import org.simantics.application.arguments.Arguments;
\r
20 import org.simantics.databoard.Bindings;
\r
21 import org.simantics.databoard.Datatypes;
\r
22 import org.simantics.databoard.type.MapType;
\r
23 import org.simantics.db.ReadGraph;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.layer0.variable.Variable;
\r
26 import org.simantics.db.request.Read;
\r
27 import org.simantics.pythonlink.Python;
\r
28 import org.simantics.pythonlink.PythonContext;
\r
30 public class TestPythonVariable {
\r
31 static IProgressMonitor progress = new NullProgressMonitor();
\r
33 PythonContext python;
\r
36 public static void SetUpClass() throws PlatformException {
\r
37 Simantics.startUpHeadless(Arguments.parse(new String[] {}), progress);
\r
41 public static void TearDownClass() throws PlatformException {
\r
42 Simantics.shutdown(progress);
\r
46 public void setUp() throws Exception {
\r
47 python = Python.openPythonContext();
\r
51 public void tearDown() throws Exception {
\r
56 public void test() throws DatabaseException {
\r
57 Simantics.getSession().syncRequest(new Read<Void>() {
\r
59 public Void perform(ReadGraph graph) throws DatabaseException {
\r
60 python.setPythonDoubleArrayVariable("foo", new double[] { 1, 2, 3 });
\r
62 Variable var = Python.getPythonContextVariable(python);
\r
64 Object value = var.getPossiblePropertyValue(graph, "foo");
\r
66 assertNotNull(value);
\r
67 assertTrue(value instanceof Object[]);
\r
68 assertArrayEquals(new Object[] {1.0, 2.0, 3.0}, (Object[])value);
\r
76 public void test2() throws DatabaseException {
\r
77 Simantics.getSession().syncRequest(new Read<Void>() {
\r
79 public Void perform(ReadGraph graph) throws DatabaseException {
\r
80 python.setPythonDoubleArrayVariable("foo", new double[] { 1, 2, 3 });
\r
82 Variable var = Python.getPythonContextVariable(python);
\r
84 Object value = var.getPossiblePropertyValue(graph, "foo", Bindings.DOUBLE_ARRAY);
\r
86 assertNotNull(value);
\r
87 assertTrue(value instanceof double[]);
\r
88 assertArrayEquals(new double[] {1.0, 2.0, 3.0}, (double[])value, 0.0);
\r
96 public void test3() throws DatabaseException {
\r
97 Simantics.getSession().syncRequest(new Read<Void>() {
\r
99 public Void perform(ReadGraph graph) throws DatabaseException {
\r
100 python.executePythonStatement("foo = (1, 2, 3)");
\r
102 Variable var = Python.getPythonContextVariable(python);
\r
103 Variable foo = var.browse(graph, "#foo");
\r
105 Object value = foo.getPossibleValue(graph, Bindings.LONG_ARRAY);
\r
107 assertNotNull(value);
\r
108 assertTrue(value instanceof long[]);
\r
109 assertArrayEquals(new long[] {1,2,3}, (long[])value);
\r
117 public void test4() throws DatabaseException {
\r
118 Simantics.getSession().syncRequest(new Read<Void>() {
\r
120 public Void perform(ReadGraph graph) throws DatabaseException {
\r
121 python.executePythonStatement("foo = {1:'foo', 2:'bar'}");
\r
123 Variable var = Python.getPythonContextVariable(python);
\r
124 Variable foo = var.getPossibleProperty(graph, "foo");
\r
126 Object value = foo.getPossibleValue(graph, Bindings.getBinding(new MapType(Datatypes.LONG, Datatypes.STRING)));
\r
128 assertNotNull(value);
\r
129 assertTrue(value instanceof Map<?,?>);
\r
130 assertEquals(2, ((Map<?,?>)value).size());
\r
131 assertEquals("foo", ((Map<?,?>)value).get(1L));
\r
132 assertEquals("bar", ((Map<?,?>)value).get(2L));
\r