def testSetWithSubOption(self): andrew = InputParameters() andrew.add('year') people = InputParameters() people.add('andrew', default=andrew) people.set('andrew', 'year', 1949) self.assertEqual(andrew.get('year'), 1949) people.set('andrew', {'year': 1954}) self.assertEqual(andrew.get('year'), 1954) people.set('andrew_year', 1977) self.assertEqual(andrew.get('year'), 1977) yo_dawg = InputParameters() yo_dawg.add('nunchuck') andrew.add('skills', yo_dawg) self.assertEqual(yo_dawg.get('nunchuck'), None) people.set('andrew_skills', 'nunchuck', True) self.assertEqual(yo_dawg.get('nunchuck'), True) with self.assertLogs(level='WARNING') as log: people.set('andrew_day', 'python', False) self.assertEqual(len(log.output), 1) self.assertIn("The parameter 'day' does not exist", log.output[0]) with self.assertLogs(level='WARNING') as log: people.set('andrew_skills', 'python', False) self.assertEqual(len(log.output), 1) self.assertIn("The parameter 'python' does not exist.", log.output[0])
def testErrorMode(self): params = InputParameters(InputParameters.ErrorMode.WARNING) with self.assertLogs(level='WARNING') as log: self.assertIsNone(params.isValid('bar')) self.assertEqual(len(log.output), 1) self.assertIn( "Cannot determine if the parameters is valid, the parameter 'bar' does not exist", log.output[0]) params = InputParameters(InputParameters.ErrorMode.ERROR) with self.assertLogs(level='ERROR') as log: self.assertIsNone(params.isValid('bar')) self.assertEqual(len(log.output), 1) self.assertIn( "Cannot determine if the parameters is valid, the parameter 'bar' does not exist", log.output[0]) params = InputParameters(InputParameters.ErrorMode.EXCEPTION) with self.assertRaises(InputParameters.InputParameterException): with self.assertLogs(level='CRITICAL') as log: self.assertIsNone(params.isValid('bar')) self.assertEqual(len(log.output), 1) self.assertIn( "Cannot determine if the parameters is valid, the parameter 'bar' does not exist", log.output[0])
def testIAdd(self): params = InputParameters() params.add('foo') params2 = InputParameters() params2.add('bar') params += params2 self.assertIn('foo', params) self.assertIn('bar', params)
def testToString(self): opt = Parameter('year') s = str(opt) self.assertIn('Value: None', s) self.assertNotIn('Default', s) self.assertNotIn('Type', s) self.assertNotIn('Allow', s) opt = Parameter('year', default=1980, vtype=int, allow=(1949, 1954, 1977, 1980)) ret, err = opt.setValue(1954) self.assertEqual(ret, 0) self.assertEqual(err, None) s = str(opt) self.assertIn('Value: 1954', s) self.assertIn('Default: 1980', s) self.assertIn("Type(s): ('int',)", s) self.assertIn('Allow: (1949, 1954, 1977, 1980)', s) opt = Parameter('year', default=1980, doc="The best year.") s = str(opt) self.assertIn("best", s) opt = Parameter('date') sub = InputParameters() sub.add('year') opt.setValue(sub) s = str(opt) self.assertIn("date\n", s) self.assertIn("date_year\n", s)
def testGetWithSubOption(self): unit = InputParameters() unit.add('name', 'pts') font = InputParameters() font.add('size', default=24) font.add('unit', unit) text = InputParameters() text.add('font', font) self.assertEqual(text.get('font', 'size'), 24) self.assertEqual(text.get('font', 'unit', 'name'), 'pts') self.assertEqual(text.get('font_size'), 24) self.assertEqual(text.get('font_unit_name'), 'pts')
def testUpdate(self): params = InputParameters() params.add('foo') params.update(foo=1980) self.assertEqual(params.get('foo'), 1980) params2 = InputParameters() params2.add('foo', 2013) params.update(params2) self.assertEqual(params.get('foo'), 2013) with self.assertLogs(level='WARNING') as log: params.update(foo=2011, bar=2013) self.assertEqual(len(log.output), 1) self.assertIn("The parameter 'bar' does not exist.", log.output[0])
def testSet(self): params = InputParameters() params.add('foo') params.set('foo', 42) self.assertEqual(list(params.keys()), ['foo']) self.assertTrue(params.isValid('foo')) self.assertIn('foo', params) self.assertIsNotNone(params.get('foo')) self.assertEqual(params.get('foo'), 42) self.assertTrue(params.hasParameter('foo')) with self.assertLogs(level='WARNING') as log: params.set('bar', 1980) self.assertEqual(len(log.output), 1) self.assertIn("The parameter 'bar' does not exist", log.output[0]) # Sub-options params2 = InputParameters() params2.add('bar') params.add('sub', params2) params.set('sub', {'bar': 2013}) self.assertEqual(params2.get('bar'), 2013) self.assertEqual(params.get('sub').get('bar'), 2013) params2.set('bar', 1954) self.assertEqual(params2.get('bar'), 1954) self.assertEqual(params.get('sub').get('bar'), 1954) params3 = InputParameters() params3.add('bar', default=2011) params.set('sub', params3) self.assertEqual(params2.get('bar'), 1954) self.assertEqual(params3.get('bar'), 2011) self.assertEqual(params.get('sub').get('bar'), 2011) params.set('sub', 'bar', 1944) self.assertEqual(params2.get('bar'), 1954) self.assertEqual(params3.get('bar'), 1944) self.assertEqual(params.get('sub').get('bar'), 1944) with self.assertLogs(level='WARNING') as log: params.set('foo', 1980, 2011) params.set('foo') self.assertEqual(len(log.output), 2) self.assertIn("Extra argument(s) found: 1980", log.output[0]) self.assertIn("One or more names must be supplied.", log.output[1])
def testValues(self): params = InputParameters() params.add('foo', 1949) params.add('bar', 1980) gold = [1949, 1980] for i, v in enumerate(params.values()): self.assertEqual(v, gold[i])
def testKeys(self): params = InputParameters() params.add('foo', 1949) params.add('bar', 1980) gold = ['foo', 'bar'] for i, v in enumerate(params.keys()): self.assertEqual(v, gold[i])
def testItems(self): params = InputParameters() params.add('foo', 1949) params.add('bar', 1980) gold = [('foo', 1949), ('bar', 1980)] for i, (k, v) in enumerate(params.items()): self.assertEqual(k, gold[i][0]) self.assertEqual(v, gold[i][1])
def testGet(self): params = InputParameters() params.add('foo', default=1980) self.assertEqual(params.get('foo'), 1980) with self.assertLogs(level='WARNING') as log: self.assertIsNone(params.get('bar')) self.assertEqual(len(log.output), 1) self.assertIn("The parameter 'bar' does not exist", log.output[0])
def testRemove(self): params = InputParameters() params.add('foo') self.assertTrue(params.hasParameter('foo')) params.remove('foo') self.assertFalse(params.hasParameter('foo')) with self.assertLogs(level='WARNING') as log: params.remove('bar') self.assertEqual(len(log.output), 1) self.assertIn("The parameter 'bar' does not exist", log.output[0])
def testIsValid(self): params = InputParameters() params.add('foo') self.assertFalse(params.isValid('foo')) params.set('foo', 1980) self.assertTrue(params.isValid('foo')) with self.assertLogs(level='WARNING') as log: self.assertIsNone(params.isValid('bar')) self.assertEqual(len(log.output), 1) self.assertIn("The parameter 'bar' does not exist", log.output[0])
def testValidate(self): sub = InputParameters() sub.add('year') opt = Parameter('date', default=sub) self.assertFalse(opt.is_validated) self.assertFalse(sub.parameter('year').is_validated) opt.validate() self.assertTrue(opt.is_validated) self.assertTrue(sub.parameter('year').is_validated)
def testIsDefault(self): params = InputParameters() params.add('foo', default=1949) self.assertTrue(params.isDefault('foo')) params.set('foo', 1980) self.assertFalse(params.isDefault('foo')) with self.assertLogs(level='WARNING') as log: self.assertIsNone(params.isDefault('bar')) self.assertEqual(len(log.output), 1) self.assertIn( "Cannot determine if the parameter is default, the parameter 'bar' does not exist", log.output[0])
def testAdd(self): params = InputParameters() params.add('foo') self.assertEqual(list(params.keys()), ['foo']) self.assertFalse(params.isValid('foo')) self.assertIn('foo', params) self.assertIsNone(params.get('foo')) self.assertTrue(params.hasParameter('foo')) with self.assertLogs(level='WARNING') as log: params.add('foo') self.assertEqual(len(log.output), 1) self.assertIn( "Cannot add parameter, the parameter 'foo' already exists.", log.output[0])
def testSetDefault(self): params = InputParameters() params.add('foo') self.assertIsNone(params.get('foo')) params.setDefault('foo', 1980) self.assertEqual(params.get('foo'), 1980) params.add('bar', default=1980) params.setDefault('bar', 1949) self.assertEqual(params.get('bar'), 1980) self.assertEqual(params.getDefault('bar'), 1949) with self.assertLogs(level='WARNING') as log: params.setDefault('other', 1980) self.assertEqual(len(log.output), 1) self.assertIn( "Cannot set default, the parameter 'other' does not exist", log.output[0])
def testSet(self): params = InputParameters() params.add('foo') params.set('foo', 42) self.assertEqual(list(params.keys()), ['foo']) self.assertTrue(params.isValid('foo')) self.assertIn('foo', params) self.assertIsNotNone(params.get('foo')) self.assertEqual(params.get('foo'), 42) self.assertTrue(params.hasParameter('foo')) with self.assertLogs(level='WARNING') as log: params.set('bar', 1980) self.assertEqual(len(log.output), 1) self.assertIn("Cannot set value, the parameter 'bar' does not exist", log.output[0]) # Sub-options params2 = InputParameters() params2.add('bar') params.add('sub', params2) params.set('sub', {'bar': 2013}) self.assertEqual(params2.get('bar'), 2013) self.assertEqual(params.get('sub').get('bar'), 2013) params2.set('bar', 1954) self.assertEqual(params2.get('bar'), 1954) self.assertEqual(params.get('sub').get('bar'), 1954) params3 = InputParameters() params3.add('bar', default=2011) params.set('sub', params3) self.assertEqual(params2.get('bar'), 1954) self.assertEqual(params3.get('bar'), 2011) self.assertEqual(params.get('sub').get('bar'), 2011) params.set('sub', bar=1944) self.assertEqual(params2.get('bar'), 1954) self.assertEqual(params3.get('bar'), 1944) self.assertEqual(params.get('sub').get('bar'), 1944) # More errors with self.assertLogs(level='WARNING') as log: params.set('foo', 1980, this='that') self.assertEqual(len(log.output), 1) self.assertIn( "Key, value pairs are not allowed when setting the 'foo' parameter.", log.output[0]) with self.assertLogs(level='WARNING') as log: params.set('foo', 1980, 2011) params.set('foo') self.assertEqual(len(log.output), 2) self.assertIn( "A single second argument is required for the 'foo' parameter.", log.output[0]) self.assertIn( "A single second argument is required for the 'foo' parameter.", log.output[1]) with self.assertLogs(level='WARNING') as log: params.set('sub', 1980, bar=1948) self.assertEqual(len(log.output), 1) self.assertIn( "Key, value pairs are not allowed when setting the 'sub' parameter with a supplied dict argument.", log.output[0]) with self.assertLogs(level='WARNING') as log: params.set('sub', 1980) self.assertEqual(len(log.output), 1) self.assertIn( "The second argument for the 'sub' parameter must be a dict() or InputParametrs object", log.output[0])
def testContains(self): params = InputParameters() params.add('foo') self.assertIn('foo', params) self.assertNotIn('bar', params)
def testHasParameter(self): params = InputParameters() params.add('foo') self.assertTrue(params.hasParameter('foo')) self.assertFalse(params.hasParameter('bar'))