Commit 997cf059 authored by Samuel GAIST's avatar Samuel GAIST

[plotterparameter] Fix writing

The write method was wrong as well as there were missing
pieces in the class for it to work properly.
parent 8a79b938
Pipeline #29919 passed with stage
in 15 minutes and 57 seconds
......@@ -42,6 +42,8 @@ plotterparameter
Validation for plotterparameters
"""
import simplejson as json
from . import schema
from . import prototypes
from . import utils
......@@ -241,6 +243,16 @@ class Plotterparameter(object):
self._name = value
self.storage = Storage(self.prefix, value)
@property
def description(self):
"""The short description for this object"""
return self.data.get("description", None)
@description.setter
def description(self, value):
"""Sets the short description for this object"""
self.data["description"] = value
@property
def documentation(self):
"""The full-length description for this object"""
......@@ -272,6 +284,27 @@ class Plotterparameter(object):
return self.storage.hash()
def json_dumps(self, indent=4):
"""Dumps the JSON declaration of this object in a string
Parameters:
indent (int): The number of indentation spaces at every indentation
level
Returns:
str: The JSON representation for this object
"""
return json.dumps(self.data, indent=indent, cls=utils.NumpyJSONEncoder)
def __str__(self):
return self.json_dumps()
def write(self, storage=None):
"""Writes contents to prefix location
......@@ -287,4 +320,4 @@ class Plotterparameter(object):
raise RuntimeError("plotterparameter has no name")
storage = self.storage # overwrite
storage.save(str(self), self.code, self.description)
storage.save(str(self), self.description)
{
"plotter": "plot/unknown/1",
"description": "",
"data": {}
"plotter": "plot/unknown/1",
"description": "",
"data": {}
}
......@@ -35,27 +35,52 @@
import nose.tools
from . import prefix
from ..plotterparameter import Plotterparameter
from ..plotterparameter import Storage
from . import prefix
def test_default():
# test for the "dummy" plotterparameter
p = Plotterparameter(prefix, data=None)
nose.tools.assert_false(p.valid)
def test_plot_config_1():
# test for a simple plotterparameter for a simple plotter
p = Plotterparameter(prefix, "plot/config/1")
nose.tools.assert_true(p.valid, "\n * %s" % "\n * ".join(p.errors))
def test_plot_invalid_1():
# test for invalid parameter name
p = Plotterparameter(prefix, "plot/invalid/1")
nose.tools.assert_false(p.valid)
nose.tools.assert_true(p.errors[0] == "'not_an_option' isn't a parameter for plotter user/scatter/1")
nose.tools.assert_true(
p.errors[0] == "'not_an_option' isn't a parameter for plotter user/scatter/1"
)
def test_plot_invalid_2():
# test for invalid "plotter" field
p = Plotterparameter(prefix, "plot/invalid/2")
nose.tools.assert_false(p.valid)
nose.tools.assert_true(p.errors[0] == "Plotter declaration file not found: user/not_a_plotter/1")
nose.tools.assert_true(
p.errors[0] == "Plotter declaration file not found: user/not_a_plotter/1"
)
def test_export():
plotterparameter_name = "plot/config/1"
target_name = "plot/generated/1"
obj = Plotterparameter(prefix, plotterparameter_name)
nose.tools.assert_true(obj.valid, "\n * %s" % "\n * ".join(obj.errors))
pp_storage = Storage(prefix, target_name)
obj.write(pp_storage)
# load from tmp_prefix and validates
exported = Plotterparameter(prefix, target_name)
nose.tools.assert_true(exported.valid, "\n * %s" % "\n * ".join(exported.errors))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment