On 11/10/2014 06:23 PM, Aaron Karper wrote:
> On Thu, Nov 06, 2014 at 12:30:48AM +0200, Alex Pyrgiotis wrote:
>> Test if the GetIndexFromIdentifier and InsertItemToIndex functions work
>> properly.
>>
>> Signed-off-by: Alex Pyrgiotis <[email protected] <mailto:[email protected]>>
>>
>> diff --git a/test/py/cmdlib/instance___unittest.py
> b/test/py/cmdlib/instance___unittest.py
>> index 32c7496..9ff2cde 100644
>> --- a/test/py/cmdlib/instance___unittest.py
>> +++ b/test/py/cmdlib/instance___unittest.py
>> @@ -958,6 +958,60 @@ class
> TestCheckTargetNodeIPolicy(__TestLUInstanceCreate):
>>      self.mcpu.__assertLogContainsRegex(msg)
>>
>>
>> +class TestIndexOperations(unittest.__TestCase):
>> +
>> +  """Test if index operations on containers work as expected."""
>> +
> 
> Please split into separate test methods.
> 

ACK

>> +  def testGetIndexFromIdentifier(__self):
>> +    """Test if an identifier is correctly translated to a container
> index."""
>> +    container = []
>> +
>> +    # Check if -1 is translated to tail index
>> +    idx = instance_utils.__GetIndexFromIdentifier("-1", "test",
> container)
>> +    self.assertEqual(0, idx)
>> +
>> +    idx = instance_utils.__GetIndexFromIdentifier("0", "test", container)
>> +    self.assertEqual(0, idx)
>> +
>> +    # Check if wrong input raises an exception
>> +    with self.assertRaises(errors.__OpPrereqError) as cm:
>> +      instance_utils.__GetIndexFromIdentifier("lala", "test", container)
> 
> Python 2.7 feature: assertRaises as a context manager.
> 

ACK. I'll change every instance of this test with this:

    self.assertRaises(exception, callable, *args, **kwargs)

>> +    self.assertEqual(cm.exception[__0], "Only positive integer or -1 is"
>> +                     " accepted as identifier for add/attach")
>> +    self.assertEqual(cm.exception[__1], "wrong_input")
>> +
>> +    # Check for off-by-one errors
>> +    with self.assertRaises(IndexError) as cm:
>> +      instance_utils.__GetIndexFromIdentifier("1", "test", container)
> 
> same
> 
>> +    self.assertEqual(cm.exception.__message, "Got test index 1, but
> there are"
>> +                     " only 0")
>> +
>> +    # Check if large negative numbers raise exception
>> +    with self.assertRaises(IndexError) as cm:
>> +      instance_utils.__GetIndexFromIdentifier("-1134"__, "test",
> container)
>> +    self.assertEqual(cm.exception.__message, "Not accepting negative
> indices"
>> +                     " other than -1")
> 
> same
> 
>> +
>> +  def testInsertItemtoIndex(self):
>> +    """Test if we can insert an item to a container at a specified
> index."""
>> +    container = []
>> +
>> +    instance_utils.__InsertItemToIndex(0, 2, container)
>> +    self.assertEqual([2], container)
>> +
>> +    instance_utils.__InsertItemToIndex(0, 1, container)
>> +    self.assertEqual([1, 2], container)
>> +
>> +    instance_utils.__InsertItemToIndex(-1, 3, container)
>> +    self.assertEqual([1, 2, 3], container)
>> +
>> +    self.assertRaises(__AssertionError,
> instance_utils.__InsertItemToIndex, -2,
>> +                      1134, container)
>> +
>> +    self.assertRaises(__AssertionError,
> instance_utils.__InsertItemToIndex, 4, 1134,
>> +                      container)
>> +
>> +
>>  class TestApplyContainerMods(__unittest.TestCase):
>>    def testEmptyContainer(self):
>>      container = []
>> --
>> 1.7.10.4
>>
> 
> --
> Google Germany GmbH
> Dienerstr. 12
> 80331 München
> 
> Registergericht und -nummer: Hamburg, HRB 86891
> Sitz der Gesellschaft: Hamburg
> Geschäftsführer: Graham Law, Christine Elizabeth Flores


-- 
Alex | [email protected]

Reply via email to