]> gerrit.simantics Code Review - simantics/python.git/blobdiff - 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
diff --git a/org.simantics.pythonlink/test/org/simantics/pythonlink/test/TestPythonVariable.java b/org.simantics.pythonlink/test/org/simantics/pythonlink/test/TestPythonVariable.java
new file mode 100644 (file)
index 0000000..56ff8e8
--- /dev/null
@@ -0,0 +1,139 @@
+package org.simantics.pythonlink.test;\r
+\r
+import static org.junit.Assert.assertArrayEquals;\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.assertTrue;\r
+\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.NullProgressMonitor;\r
+import org.junit.After;\r
+import org.junit.AfterClass;\r
+import org.junit.Before;\r
+import org.junit.BeforeClass;\r
+import org.junit.Test;\r
+import org.simantics.PlatformException;\r
+import org.simantics.Simantics;\r
+import org.simantics.application.arguments.Arguments;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.type.MapType;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.pythonlink.Python;\r
+import org.simantics.pythonlink.PythonContext;\r
+\r
+public class TestPythonVariable {\r
+    static IProgressMonitor progress = new NullProgressMonitor();\r
+    \r
+    PythonContext python;\r
+\r
+    @BeforeClass\r
+    public static void SetUpClass() throws PlatformException {\r
+        Simantics.startUpHeadless(Arguments.parse(new String[] {}), progress);        \r
+    }\r
+    \r
+    @AfterClass\r
+    public static void TearDownClass() throws PlatformException {\r
+        Simantics.shutdown(progress);        \r
+    }\r
+    \r
+    @Before\r
+    public void setUp() throws Exception {\r
+        python = Python.openPythonContext();\r
+    }\r
+\r
+    @After\r
+    public void tearDown() throws Exception {\r
+        python.close();\r
+    }\r
+    \r
+    @Test\r
+    public void test() throws DatabaseException {\r
+        Simantics.getSession().syncRequest(new Read<Void>() {\r
+            @Override\r
+            public Void perform(ReadGraph graph) throws DatabaseException {\r
+                python.setPythonDoubleArrayVariable("foo", new double[] { 1, 2, 3 });\r
+                \r
+                Variable var = Python.getPythonContextVariable(python);\r
+                \r
+                Object value = var.getPossiblePropertyValue(graph, "foo");\r
+                \r
+                assertNotNull(value);\r
+                assertTrue(value instanceof Object[]);\r
+                assertArrayEquals(new Object[] {1.0, 2.0, 3.0}, (Object[])value);\r
+                \r
+                return null;\r
+            }\r
+        });\r
+    }\r
+    \r
+    @Test\r
+    public void test2() throws DatabaseException {\r
+        Simantics.getSession().syncRequest(new Read<Void>() {\r
+            @Override\r
+            public Void perform(ReadGraph graph) throws DatabaseException {\r
+                python.setPythonDoubleArrayVariable("foo", new double[] { 1, 2, 3 });\r
+                \r
+                Variable var = Python.getPythonContextVariable(python);\r
+                \r
+                Object value = var.getPossiblePropertyValue(graph, "foo", Bindings.DOUBLE_ARRAY);\r
+                \r
+                assertNotNull(value);\r
+                assertTrue(value instanceof double[]);\r
+                assertArrayEquals(new double[] {1.0, 2.0, 3.0}, (double[])value, 0.0);\r
+                \r
+                return null;\r
+            }\r
+        });\r
+    }\r
+    \r
+    @Test\r
+    public void test3() throws DatabaseException {\r
+        Simantics.getSession().syncRequest(new Read<Void>() {\r
+            @Override\r
+            public Void perform(ReadGraph graph) throws DatabaseException {\r
+                python.executePythonStatement("foo = (1, 2, 3)");\r
+                \r
+                Variable var = Python.getPythonContextVariable(python);\r
+                Variable foo = var.browse(graph, "#foo");\r
+        \r
+                Object value = foo.getPossibleValue(graph, Bindings.LONG_ARRAY);\r
+                \r
+                assertNotNull(value);\r
+                assertTrue(value instanceof long[]);\r
+                assertArrayEquals(new long[] {1,2,3}, (long[])value);\r
+                \r
+                return null;\r
+            }\r
+        });\r
+    }\r
+    \r
+    @Test\r
+    public void test4() throws DatabaseException {\r
+        Simantics.getSession().syncRequest(new Read<Void>() {\r
+            @Override\r
+            public Void perform(ReadGraph graph) throws DatabaseException {\r
+                python.executePythonStatement("foo = {1:'foo', 2:'bar'}");\r
+                \r
+                Variable var = Python.getPythonContextVariable(python);\r
+                Variable foo = var.getPossibleProperty(graph, "foo");\r
+                \r
+                Object value = foo.getPossibleValue(graph, Bindings.getBinding(new MapType(Datatypes.LONG, Datatypes.STRING)));\r
+                \r
+                assertNotNull(value);\r
+                assertTrue(value instanceof Map<?,?>);\r
+                assertEquals(2, ((Map<?,?>)value).size());\r
+                assertEquals("foo", ((Map<?,?>)value).get(1L));\r
+                assertEquals("bar", ((Map<?,?>)value).get(2L));\r
+                \r
+                return null;\r
+            }\r
+        });\r
+    }\r
+    \r
+}\r