]> gerrit.simantics Code Review - simantics/python.git/blob - org.simantics.pythonlink/test/org/simantics/pythonlink/test/TestPythonVariable.java
Added support for dynamically typed data access and a Variable interface.
[simantics/python.git] / org.simantics.pythonlink / test / org / simantics / pythonlink / test / TestPythonVariable.java
1 package org.simantics.pythonlink.test;\r
2 \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
7 \r
8 import java.util.Map;\r
9 \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
29 \r
30 public class TestPythonVariable {\r
31     static IProgressMonitor progress = new NullProgressMonitor();\r
32     \r
33     PythonContext python;\r
34 \r
35     @BeforeClass\r
36     public static void SetUpClass() throws PlatformException {\r
37         Simantics.startUpHeadless(Arguments.parse(new String[] {}), progress);        \r
38     }\r
39     \r
40     @AfterClass\r
41     public static void TearDownClass() throws PlatformException {\r
42         Simantics.shutdown(progress);        \r
43     }\r
44     \r
45     @Before\r
46     public void setUp() throws Exception {\r
47         python = Python.openPythonContext();\r
48     }\r
49 \r
50     @After\r
51     public void tearDown() throws Exception {\r
52         python.close();\r
53     }\r
54     \r
55     @Test\r
56     public void test() throws DatabaseException {\r
57         Simantics.getSession().syncRequest(new Read<Void>() {\r
58             @Override\r
59             public Void perform(ReadGraph graph) throws DatabaseException {\r
60                 python.setPythonDoubleArrayVariable("foo", new double[] { 1, 2, 3 });\r
61                 \r
62                 Variable var = Python.getPythonContextVariable(python);\r
63                 \r
64                 Object value = var.getPossiblePropertyValue(graph, "foo");\r
65                 \r
66                 assertNotNull(value);\r
67                 assertTrue(value instanceof Object[]);\r
68                 assertArrayEquals(new Object[] {1.0, 2.0, 3.0}, (Object[])value);\r
69                 \r
70                 return null;\r
71             }\r
72         });\r
73     }\r
74     \r
75     @Test\r
76     public void test2() throws DatabaseException {\r
77         Simantics.getSession().syncRequest(new Read<Void>() {\r
78             @Override\r
79             public Void perform(ReadGraph graph) throws DatabaseException {\r
80                 python.setPythonDoubleArrayVariable("foo", new double[] { 1, 2, 3 });\r
81                 \r
82                 Variable var = Python.getPythonContextVariable(python);\r
83                 \r
84                 Object value = var.getPossiblePropertyValue(graph, "foo", Bindings.DOUBLE_ARRAY);\r
85                 \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
89                 \r
90                 return null;\r
91             }\r
92         });\r
93     }\r
94     \r
95     @Test\r
96     public void test3() throws DatabaseException {\r
97         Simantics.getSession().syncRequest(new Read<Void>() {\r
98             @Override\r
99             public Void perform(ReadGraph graph) throws DatabaseException {\r
100                 python.executePythonStatement("foo = (1, 2, 3)");\r
101                 \r
102                 Variable var = Python.getPythonContextVariable(python);\r
103                 Variable foo = var.browse(graph, "#foo");\r
104         \r
105                 Object value = foo.getPossibleValue(graph, Bindings.LONG_ARRAY);\r
106                 \r
107                 assertNotNull(value);\r
108                 assertTrue(value instanceof long[]);\r
109                 assertArrayEquals(new long[] {1,2,3}, (long[])value);\r
110                 \r
111                 return null;\r
112             }\r
113         });\r
114     }\r
115     \r
116     @Test\r
117     public void test4() throws DatabaseException {\r
118         Simantics.getSession().syncRequest(new Read<Void>() {\r
119             @Override\r
120             public Void perform(ReadGraph graph) throws DatabaseException {\r
121                 python.executePythonStatement("foo = {1:'foo', 2:'bar'}");\r
122                 \r
123                 Variable var = Python.getPythonContextVariable(python);\r
124                 Variable foo = var.getPossibleProperty(graph, "foo");\r
125                 \r
126                 Object value = foo.getPossibleValue(graph, Bindings.getBinding(new MapType(Datatypes.LONG, Datatypes.STRING)));\r
127                 \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
133                 \r
134                 return null;\r
135             }\r
136         });\r
137     }\r
138     \r
139 }\r