diff --git a/test/test_list.py b/test/test_list.py new file mode 100644 index 0000000..817b6e7 --- /dev/null +++ b/test/test_list.py @@ -0,0 +1,53 @@ +from json import loads +from tiramisu_json_api import Config + + +def test_list_option(): + filename = 'test/data/boolean1.json' + with open(filename, 'r') as fh: + json = loads(fh.read()) + # + config = Config(json) + opts = [] + for option in config.option.list(): + opts.append(option.option.name()) + assert opts == [] + # + opts = [] + for option in config.option('options').list(): + opts.append(option.option.name()) + assert opts == ['boolean'] + + +def test_list_optiondescription(): + filename = 'test/data/boolean1.json' + with open(filename, 'r') as fh: + json = loads(fh.read()) + # + config = Config(json) + opts = [] + for option in config.option.list('optiondescription'): + opts.append(option.option.name()) + assert opts == ['options'] + # + opts = [] + for option in config.option('options').list('optiondescription'): + opts.append(option.option.name()) + assert opts == [] + + +def test_list_all(): + filename = 'test/data/boolean1.json' + with open(filename, 'r') as fh: + json = loads(fh.read()) + # + config = Config(json) + opts = [] + for option in config.option.list('all'): + opts.append(option.option.name()) + assert opts == ['options'] + # + opts = [] + for option in config.option('options').list('all'): + opts.append(option.option.name()) + assert opts == ['boolean'] diff --git a/tiramisu_json_api/api.py b/tiramisu_json_api/api.py index 67df2dd..40876be 100644 --- a/tiramisu_json_api/api.py +++ b/tiramisu_json_api/api.py @@ -288,18 +288,19 @@ class TiramisuOptionValue(_Value): class _Option: def list(self, type='option'): - if type not in ['all', 'option']: - raise NotImplementedError() + if type not in ['all', 'option', 'optiondescription']: + raise Exception('unknown list type {}'.format(type)) for path, schema in self.schema['properties'].items(): - if type == 'all' or schema['type'] not in ['object', 'array'] and self.config.is_hidden(path, None): + if not self.config.is_hidden(path, None): if schema['type'] in ['object', 'array']: - yield TiramisuOptionDescription(self.config, - schema, - self.model, - self.form, - self.temp, - path) - else: + if type in ['all', 'optiondescription']: + yield TiramisuOptionDescription(self.config, + schema, + self.model, + self.form, + self.temp, + path) + elif type in ['all', 'option']: yield TiramisuOption(self.config, schema, self.model,