def testDigitSimpleElement(self): """test SimpleElement(check='digit')""" elem = SimpleElement('digit') self.common_checking(elem, 45, 45, 54) self.assertRaises(ModelFileValueError, elem.add, 'notadigit') elem = SimpleElement('digit') self.common_checking(elem, "0xe", 0xe, 0x4) self.assertRaises(ModelFileValueError, elem.add, 'notadigit')
def testBooleanSimpleElement(self): """test SimpleElement(check='boolean')""" elem = SimpleElement('boolean') self.common_checking(elem, 'no', False, 'yes', True) elem = SimpleElement('boolean') self.common_checking(elem, 'True', True, 'False', False) elem = SimpleElement('boolean') self.common_checking(elem, '0', False, '1', True) self.assertRaises(ModelFileValueError, elem.add, 'wrong')
def testGenericTestSimpleElement(self): """test common SimpleElement methods""" elem = SimpleElement('string', default='foo') self.assertEqual(elem.get(), 'foo') # Could not add twice elem.add('foo') self.assertRaises(KeyError, elem.add, 'bar') elem.clear() elem.add('foo')
def testDiff(self): """MultipleElement diff method""" elem = MultipleElement(SimpleElement(check='digit')) # (empty) <=> (empty) added, changed, removed = elem.diff(elem) self.assertTrue(len(added) == len(removed) == len(changed) == 0) elem.add(3) elem.add(9) elem.add(4) # itself added, changed, removed = elem.diff(elem) self.assertTrue(len(added) == len(removed) == len(changed) == 0) # elem <=> (empty) added, changed, removed = elem.diff(elem.emptycopy()) self.assertTrue(len(added) == len(changed) == 0) self.assertEqual(removed.get(), [3, 9, 4]) # (empty) <=> elem added, changed, removed = elem.emptycopy().diff(elem) self.assertTrue(len(removed) == len(changed) == 0) self.assertEqual(added.get(), [3, 9, 4]) # Mixed other = elem.copy() other.remove(4) other.add(5) added, changed, removed = elem.diff(other) self.assertEqual(changed.get(), None) self.assertEqual(added.get(), [5]) self.assertEqual(removed.get(), [4])
def _validate(self, value): """Call SimpleElement validate method and also check value length.""" value = SimpleElement._validate(self, value) if len(value) > 8: raise ModelFileValueError( "Name '%s' should be 8-character long max" % value) return value
def testComplexPathSimpleElement(self): """test SimpleElement(check='path') (complex)""" elem = SimpleElement('path') self.common_checking( elem, '/dev/disk/by-path/pci-0000:00:04.0-virtio-pci-virtio1-part3', '/dev/disk/by-path/pci-0000:00:04.0-virtio-pci-virtio1-part3', '/dev/disk/by-path/pci-0000:00:14.0-virtio-pci-virtio9-part8') self.assertRaises(ModelFileValueError, elem.add, 'not a path')
def testBase(self): """MultipleElement base methods""" elem = MultipleElement(SimpleElement(check='string')) # Default self.assertEqual(elem.get([]), []) # First add() elem.add("3") self.assertEqual(elem.get(), ["3"]) self.assertEqual(elem.content(), ["3"]) self.assertEqual(elem[0], "3") self.assertEqual(str(elem), "3") self.assertEqual(len(elem), 1) # Second add() elem.add("5") elem.add("7") self.assertEqual(elem.get(), ["3", "5", "7"]) self.assertEqual(elem.content(), ["3", "5", "7"]) self.assertEqual(elem[1], "5") self.assertEqual(str(elem), "3 5 7") self.assertEqual(len(elem), 3) # Remove an item del elem[1] self.assertEqual(elem.get(), ["3", "7"]) self.assertEqual(len(elem), 2) elem.remove("3") self.assertEqual(elem.get(), ["7"]) self.assertEqual(len(elem), 1) self.assertRaises(KeyError, elem.remove, "6") # replace() elem.replace("12") self.assertEqual(elem.get(), ["12"]) # clear() elem.clear() self.assertEqual(len(elem), 0) # parse() elem.parse("4") self.assertEqual(elem.get(), ["4"]) self.assertEqual(str(elem), "4") self.assertEqual(len(elem), 1) # __eq__ other = elem.emptycopy() self.assertNotEqual(elem, other) other.add("4") self.assertEqual(elem, other)
def testCopy(self): """MultipleElement copy methods""" elem = MultipleElement(SimpleElement(check='digit')) # Empty self.assertEqual(len(elem.emptycopy()), 0) self.assertEqual(elem.copy().as_dict(), elem.as_dict()) # With some content elem.add(3) elem.add(5) self.assertEqual(len(elem.emptycopy()), 0) self.assertEqual(elem.copy().as_dict(), elem.as_dict())
def test_str_fold(self): """MultipleElement uses range-based folding when format to str""" elem = MultipleElement(SimpleElement(check='string')) # By default, no folding is done when cast to str elem.add('3') elem.add('4') elem.add('5') self.assertEqual(str(elem), "3\n4\n5") # With enabled folding elem.fold = True self.assertEqual(str(elem), '[3-5]') # Folding keeps order elem.clear() elem.add('3') elem.add('5') elem.add('4') self.assertEqual(str(elem), "[3,5]\n4")
def __init__(self, check='string', default=None, values=None): SimpleElement.__init__(self, 'string', default, values)
def __init__(self, check='digit', default=None, value=None): SimpleElement.__init__(self, check, default, value)
def testEnumSimpleElement(self): """test SimpleElement(check='enum')""" elem = SimpleElement('enum', values=[.25, .50, .75]) self.common_checking(elem, .25, .25, .50) self.assertRaises(ModelFileValueError, elem.add, 'notinlist')
def testPathSimpleElement(self): """test SimpleElement(check='path')""" elem = SimpleElement('path') self.common_checking(elem, '/mnt/lustre', '/mnt/lustre', '/mnt/foo') self.assertRaises(ModelFileValueError, elem.add, 'not a path')
def __init__(self, check='string', default=None, values=None): SimpleElement.__init__(self, check, default, values)
def testWrongSimpleElement(self): """test SimpleElement with a wrong check""" elem = SimpleElement('wrong') self.assertRaises(TypeError, elem.add, 1)
def testStringSimpleElement(self): """test SimpleElement(check='string')""" elem = SimpleElement('string') self.common_checking(elem, 'ok', 'ok', 'ko') self.assertRaises(ModelFileValueError, elem.add, 45)
def test_bad_eq(self): """test SimpleElement equality with another type.""" elem = SimpleElement('wrong') self.assertNotEqual(elem, 'not a SimpleElement')