def test_help(self): p_dict = ParameterDictionary( Integer(name='answer', help='just some value', default=42, print_format='%sm'), Float(name='other', help='just some super float', default=71.7, print_format='%.2f')) print p_dict.getHelpString()
def test_parameters_dictionary(self): p1 = String(name='param1', default='default default 1') p2 = String(name='param2', default='default default 2', max_items=3, item_separator=':') self.assertEquals(p2.parse('a:b:C'), ['a', 'b', 'C']) p_dict = ParameterDictionary(p1, p2) self.assertTrue(len(p_dict) == 2) self.assertTrue(p1.name in p_dict and p_dict[p1.name] == p1.default) self.assertTrue(p2.name in p_dict and p_dict[p2.name] == p2.default) self.assertEquals(len(p_dict.parameters()), 2) # TODO: Fix error # self.assertEquals(p_dict.get_parameter(p1.name), p1) # self.assertEquals(p_dict.get_parameter(p2.name), p2) # Check parameters remain in the order we put them params = [] for i in range(1000): params.append(String(name=str(i), default=i)) p_dict = ParameterDictionary(*params) self.assertEqual(p_dict.keys(), [p.name for p in params]) self.assertEqual(p_dict.values(), [p.default for p in params]) self.assertEqual(p_dict.parameters(), params)
def test_defaults(self): #All these should cause no exception Bool(name='a', default=True) Bool(name='a', default="True") Bool(name='a', default="0") Float(name='a', default=1.2e-2) Float(name='a', default="1.2e-2") Integer(name='a', default='1232') Range(name='a', default='1:5:100') p_dict = ParameterDictionary( File(name='file', default='/tmp/file1', mandatory=True, max_items=2, item_separator=':'), Date(name='date', item_separator='/')) self.failUnlessRaises(ValidationError, p_dict.parseArguments, ["date=2"], use_defaults=False) self.assertEquals(p_dict.parseArguments(["date=2"], use_defaults=True), { 'date': '2', 'file': ['/tmp/file1'] })
class DummyPlugin(PluginAbstract): """Stub class for implementing the abstract one""" __short_description__ = 'A dummy plugin' __version__ = (0, 0, 0) __parameters__ = ParameterDictionary( Integer(name='number', help='This is just a number, not really important'), Integer(name='the_number', mandatory=True, help='This is *THE* number. Please provide it'), String(name='something', default='test'), Float(name='other', default=1.4), Directory(name='input')) _runs = [] _template = "${number} - $something - $other" def runTool(self, config_dict=None): DummyPlugin._runs.append(config_dict) print "Dummy tool was run with: %s" % config_dict return { '/tmp/dummyfile1': dict(type='plot'), '/tmp/dummyfile2': dict(type='data') }
def test_validate_errors(self): p_dict = ParameterDictionary( Integer(name='int', mandatory=True), File(name='file', max_items=2, item_separator=':'), Float(name='float', mandatory=True, max_items=2)) self.assertTrue(p_dict.validate_errors(dict(int=1, float=2.0)) is None) self.assertEquals(p_dict.validate_errors({'int': None}), { 'too_many_items': [], 'missing': ['int', 'float'] }) self.assertEquals(p_dict.validate_errors({'int': [1, 2, 3, 4, 5]}), { 'too_many_items': [('int', 1)], 'missing': ['float'] }) self.assertEquals(p_dict.validate_errors({'int': [1, 2, 3, 4, 5]}), { 'too_many_items': [('int', 1)], 'missing': ['float'] })
def test_complete(self): p_dict = ParameterDictionary(Integer(name='int'), File(name='file', default='/tmp/file1'), Date(name='date')) conf = dict(int=1) p_dict.complete(conf) self.assertEquals(conf, {'int': 1, 'file': '/tmp/file1'}) p_dict.complete(conf, add_missing_defaults=True) self.assertEquals(conf, {'int': 1, 'date': None, 'file': '/tmp/file1'}) self.assertEquals(p_dict.complete(), {'file': '/tmp/file1'}) self.assertEquals(p_dict.complete(add_missing_defaults=True), { 'int': None, 'date': None, 'file': '/tmp/file1' }) #assure default value gets parsed (i.e. validated) when creating Parameter p = ParameterDictionary(Integer(name='a', default='0')) self.assertEquals(p.complete(add_missing_defaults=True), {'a': 0})
def test_parse_arguments(self): p_dict = ParameterDictionary(String(name='a'), String(name='b')) res = p_dict.parseArguments("a=1 b=2".split()) self.assertEqual(res, dict(a='1', b='2')) p_dict = ParameterDictionary(Integer(name='a'), Integer(name='b')) res = p_dict.parseArguments("a=1 b=2".split()) self.assertEqual(res, dict(a=1, b=2)) # more arguments than those expected p_dict = ParameterDictionary(Integer(name='a')) self.failUnlessRaises(ValidationError, p_dict.parseArguments, "a=1 b=2".split()) p_dict = ParameterDictionary(Integer(name='int'), Long(name='long'), Float(name='float'), Bool(name='bool'), String(name='string'), File(name='file', default='/tmp/file1'), Date(name='date'), Range(name='range')) res = p_dict.parseArguments("int=1 long=1 date=1 bool=1".split()) self.assertEqual(res, dict(int=1, date='1', bool=True, long=1L)) res = p_dict.parseArguments("int=1 long=1 date=1 bool=1".split(), use_defaults=True) self.assertEqual( res, dict(int=1, date='1', bool=True, long=1L, file='/tmp/file1'))
("float=1.2", 1.2), ("float=-1e-1", -0.1), ("float=2E2", 200.0), ("long=0", 0L), ("long=-42", -42L), ("string=1", "1"), ("string=ä", "ä") ]: res = p_dict.parseArguments(arg.split()) self.assertEqual(res, {arg.split('=')[0]: parsed_val}, 'Error when parsing %s, got %s' % (arg, res)) # multiple arguments p_dict = ParameterDictionary( File(name='file', default='/tmp/file1', max_items=2, item_separator=':'), Date(name='date', item_separator='/')) self.assertEquals(p_dict.parseArguments(["file=a:b"]), dict(file=['a', 'b'])) self.failUnlessRaises(ValidationError, p_dict.parseArguments, ["file=a:b:c"]) self.failUnlessRaises(ValidationError, p_dict.parseArguments, ["file=a", "file=b", "file=c"]) # this should still work since max_items defaults to 1 # and in that case no splitting happens self.assertEquals(p_dict.parseArguments(["date=a/b"]), dict(date="a/b")) self.assertEquals(p_dict.parseArguments(["file=a", "file=b"]), dict(file=['a', 'b']))