From 4b7d0237bf4a08888ccf2ef058180b7e6dceec3c Mon Sep 17 00:00:00 2001
From: Samuel Gaist <samuel.gaist@idiap.ch>
Date: Thu, 18 Apr 2019 11:43:13 +0200
Subject: [PATCH] [database] Fix protocol view definition generation

The parameter field was missing.
---
 beat/backend/python/database.py               |  3 ++
 .../prefix/databases/with_parameters/1.json   | 42 +++++++++++++++
 .../prefix/databases/with_parameters/1.py     | 53 +++++++++++++++++++
 .../prefix/databases/with_parameters/2.json   | 29 ++++++++++
 .../prefix/databases/with_parameters/2.py     | 53 +++++++++++++++++++
 beat/backend/python/test/test_database.py     | 21 ++++++++
 6 files changed, 201 insertions(+)
 create mode 100644 beat/backend/python/test/prefix/databases/with_parameters/1.json
 create mode 100644 beat/backend/python/test/prefix/databases/with_parameters/1.py
 create mode 100644 beat/backend/python/test/prefix/databases/with_parameters/2.json
 create mode 100644 beat/backend/python/test/prefix/databases/with_parameters/2.py

diff --git a/beat/backend/python/database.py b/beat/backend/python/database.py
index a14c0ba..e2e8711 100644
--- a/beat/backend/python/database.py
+++ b/beat/backend/python/database.py
@@ -455,6 +455,9 @@ class Database(object):
             protocol_template = ProtocolTemplate(self.prefix, template_name)
             view_definition = protocol_template.set(set_name)
             view_definition["view"] = protocol["views"][set_name]["view"]
+            parameters = protocol["views"][set_name].get("parameters")
+            if parameters is not None:
+                view_definition["parameters"] = parameters
 
         return view_definition
 
diff --git a/beat/backend/python/test/prefix/databases/with_parameters/1.json b/beat/backend/python/test/prefix/databases/with_parameters/1.json
new file mode 100644
index 0000000..c40f678
--- /dev/null
+++ b/beat/backend/python/test/prefix/databases/with_parameters/1.json
@@ -0,0 +1,42 @@
+{
+  "root_folder": "/tmp/foo/bar",
+  "protocols": [
+    {
+      "name": "test_with_parameters",
+      "template": "protocol",
+      "sets": [
+        {
+          "name": "double",
+          "template": "double",
+          "view": "Parametrized",
+          "outputs": {
+              "a": "user/single_integer/1",
+              "b": "user/single_integer/1",
+              "sum": "user/single_integer/1"
+          },
+          "parameters": {
+            "threshold": 3
+          }
+        }
+      ]
+    },
+    {
+      "name": "test_with_empty_parameters",
+      "template": "protocol",
+      "sets": [
+        {
+          "name": "double",
+          "template": "double",
+          "view": "Parametrized",
+          "outputs": {
+              "a": "user/single_integer/1",
+              "b": "user/single_integer/1",
+              "sum": "user/single_integer/1"
+          },
+          "parameters": {
+          }
+        }
+      ]
+    }
+  ]
+}
diff --git a/beat/backend/python/test/prefix/databases/with_parameters/1.py b/beat/backend/python/test/prefix/databases/with_parameters/1.py
new file mode 100644
index 0000000..8e75c18
--- /dev/null
+++ b/beat/backend/python/test/prefix/databases/with_parameters/1.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+# vim: set fileencoding=utf-8 :
+
+###################################################################################
+#                                                                                 #
+# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/               #
+# Contact: beat.support@idiap.ch                                                  #
+#                                                                                 #
+# Redistribution and use in source and binary forms, with or without              #
+# modification, are permitted provided that the following conditions are met:     #
+#                                                                                 #
+# 1. Redistributions of source code must retain the above copyright notice, this  #
+# list of conditions and the following disclaimer.                                #
+#                                                                                 #
+# 2. Redistributions in binary form must reproduce the above copyright notice,    #
+# this list of conditions and the following disclaimer in the documentation       #
+# and/or other materials provided with the distribution.                          #
+#                                                                                 #
+# 3. Neither the name of the copyright holder nor the names of its contributors   #
+# may be used to endorse or promote products derived from this software without   #
+# specific prior written permission.                                              #
+#                                                                                 #
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   #
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE          #
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE    #
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL      #
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR      #
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER      #
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,   #
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   #
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.            #
+#                                                                                 #
+###################################################################################
+
+
+import numpy
+
+from collections import namedtuple
+from beat.backend.python.database import View as BaseView
+
+
+class Parametrized(BaseView):
+    def index(self, root_folder, parameters):
+        Entry = namedtuple("Entry", ["out"])
+
+        return [Entry(42)]
+
+    def get(self, output, index):
+        obj = self.objs[index]
+
+        if output == "out":
+            return {"value": numpy.int32(obj.out)}
diff --git a/beat/backend/python/test/prefix/databases/with_parameters/2.json b/beat/backend/python/test/prefix/databases/with_parameters/2.json
new file mode 100644
index 0000000..9ca285c
--- /dev/null
+++ b/beat/backend/python/test/prefix/databases/with_parameters/2.json
@@ -0,0 +1,29 @@
+{
+    "root_folder": "/tmp/foo/bar",
+    "protocols": [
+        {
+            "name": "test_with_parameters",
+            "template": "double/1",
+            "views": {
+                "double": {
+                    "view": "Parametrized",
+                    "parameters": {
+                        "threshold": 3
+                    }
+                }
+            }
+        },
+        {
+            "name": "test_with_empty_parameters",
+            "template": "double/1",
+            "views": {
+                "double": {
+                    "view": "Parametrized",
+                    "parameters": {
+                    }
+                }
+            }
+        }
+    ],
+    "schema_version": 2
+}
diff --git a/beat/backend/python/test/prefix/databases/with_parameters/2.py b/beat/backend/python/test/prefix/databases/with_parameters/2.py
new file mode 100644
index 0000000..8e75c18
--- /dev/null
+++ b/beat/backend/python/test/prefix/databases/with_parameters/2.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+# vim: set fileencoding=utf-8 :
+
+###################################################################################
+#                                                                                 #
+# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/               #
+# Contact: beat.support@idiap.ch                                                  #
+#                                                                                 #
+# Redistribution and use in source and binary forms, with or without              #
+# modification, are permitted provided that the following conditions are met:     #
+#                                                                                 #
+# 1. Redistributions of source code must retain the above copyright notice, this  #
+# list of conditions and the following disclaimer.                                #
+#                                                                                 #
+# 2. Redistributions in binary form must reproduce the above copyright notice,    #
+# this list of conditions and the following disclaimer in the documentation       #
+# and/or other materials provided with the distribution.                          #
+#                                                                                 #
+# 3. Neither the name of the copyright holder nor the names of its contributors   #
+# may be used to endorse or promote products derived from this software without   #
+# specific prior written permission.                                              #
+#                                                                                 #
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   #
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE          #
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE    #
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL      #
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR      #
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER      #
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,   #
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   #
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.            #
+#                                                                                 #
+###################################################################################
+
+
+import numpy
+
+from collections import namedtuple
+from beat.backend.python.database import View as BaseView
+
+
+class Parametrized(BaseView):
+    def index(self, root_folder, parameters):
+        Entry = namedtuple("Entry", ["out"])
+
+        return [Entry(42)]
+
+    def get(self, output, index):
+        obj = self.objs[index]
+
+        if output == "out":
+            return {"value": numpy.int32(obj.out)}
diff --git a/beat/backend/python/test/test_database.py b/beat/backend/python/test/test_database.py
index 3c87f00..b5e0248 100644
--- a/beat/backend/python/test/test_database.py
+++ b/beat/backend/python/test/test_database.py
@@ -129,3 +129,24 @@ def load_protocol_with_two_sets(db_name):
     nose.tools.assert_is_not_none(set_["outputs"]["b"])
     nose.tools.assert_is_not_none(set_["outputs"]["c"])
     nose.tools.assert_is_not_none(set_["outputs"]["sum"])
+
+
+# ----------------------------------------------------------
+
+
+def test_view_definitions():
+    yield compare_definitions, "integers_db", "double", "double"
+    yield compare_definitions, "with_parameters", "test_with_parameters", "double"
+    yield compare_definitions, "with_parameters", "test_with_empty_parameters", "double"
+
+
+def compare_definitions(db_name, protocol_name, view_name):
+
+    db_1 = load("{}/1".format(db_name))
+    db_1_view_definition = db_1.view_definition(protocol_name, view_name)
+    db_1_view_definition.pop("template")  # Unused property
+
+    db_2 = load("{}/2".format(db_name))
+    db_2_view_definition = db_2.view_definition(protocol_name, view_name)
+
+    nose.tools.eq_(db_1_view_definition, db_2_view_definition)
-- 
GitLab