| Index: gslib/tests/test_plurality_checkable_iterator.py
|
| ===================================================================
|
| --- gslib/tests/test_plurality_checkable_iterator.py (revision 33376)
|
| +++ gslib/tests/test_plurality_checkable_iterator.py (working copy)
|
| @@ -1,3 +1,4 @@
|
| +# -*- coding: utf-8 -*-
|
| # Copyright 2012 Google Inc. All Rights Reserved.
|
| #
|
| # Permission is hereby granted, free of charge, to any person obtaining a
|
| @@ -18,21 +19,30 @@
|
| # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
| # IN THE SOFTWARE.
|
| +"""Unit tests for PluralityCheckableIterator."""
|
|
|
| -"""Unit tests for PluralityCheckableIterator"""
|
| +from __future__ import absolute_import
|
|
|
| +import sys
|
| +
|
| from gslib.plurality_checkable_iterator import PluralityCheckableIterator
|
| import gslib.tests.testcase as testcase
|
|
|
| +
|
| +class CustomTestException(Exception):
|
| + pass
|
| +
|
| +
|
| class PluralityCheckableIteratorTests(testcase.GsUtilUnitTestCase):
|
| + """Unit tests for PluralityCheckableIterator."""
|
|
|
| def testPluralityCheckableIteratorWith0Elems(self):
|
| """Tests empty PluralityCheckableIterator."""
|
| input_list = range(0)
|
| it = iter(input_list)
|
| pcit = PluralityCheckableIterator(it)
|
| - self.assertTrue(pcit.is_empty())
|
| - self.assertFalse(pcit.has_plurality())
|
| + self.assertTrue(pcit.IsEmpty())
|
| + self.assertFalse(pcit.HasPlurality())
|
| output_list = list(pcit)
|
| self.assertEqual(input_list, output_list)
|
|
|
| @@ -41,8 +51,8 @@
|
| input_list = range(1)
|
| it = iter(input_list)
|
| pcit = PluralityCheckableIterator(it)
|
| - self.assertFalse(pcit.is_empty())
|
| - self.assertFalse(pcit.has_plurality())
|
| + self.assertFalse(pcit.IsEmpty())
|
| + self.assertFalse(pcit.HasPlurality())
|
| output_list = list(pcit)
|
| self.assertEqual(input_list, output_list)
|
|
|
| @@ -51,8 +61,8 @@
|
| input_list = range(2)
|
| it = iter(input_list)
|
| pcit = PluralityCheckableIterator(it)
|
| - self.assertFalse(pcit.is_empty())
|
| - self.assertTrue(pcit.has_plurality())
|
| + self.assertFalse(pcit.IsEmpty())
|
| + self.assertTrue(pcit.HasPlurality())
|
| output_list = list(pcit)
|
| self.assertEqual(input_list, output_list)
|
|
|
| @@ -61,7 +71,116 @@
|
| input_list = range(3)
|
| it = iter(input_list)
|
| pcit = PluralityCheckableIterator(it)
|
| - self.assertFalse(pcit.is_empty())
|
| - self.assertTrue(pcit.has_plurality())
|
| + self.assertFalse(pcit.IsEmpty())
|
| + self.assertTrue(pcit.HasPlurality())
|
| output_list = list(pcit)
|
| self.assertEqual(input_list, output_list)
|
| +
|
| + def testPluralityCheckableIteratorWith1Elem1Exception(self):
|
| + """Tests PluralityCheckableIterator with 2 elements.
|
| +
|
| + The second element raises an exception.
|
| + """
|
| +
|
| + class IterTest(object):
|
| +
|
| + def __init__(self):
|
| + self.position = 0
|
| +
|
| + def __iter__(self):
|
| + return self
|
| +
|
| + def next(self):
|
| + if self.position == 0:
|
| + self.position += 1
|
| + return 1
|
| + elif self.position == 1:
|
| + self.position += 1
|
| + raise CustomTestException('Test exception')
|
| + else:
|
| + raise StopIteration()
|
| +
|
| + pcit = PluralityCheckableIterator(IterTest())
|
| + self.assertFalse(pcit.IsEmpty())
|
| + self.assertTrue(pcit.HasPlurality())
|
| + iterated_value = None
|
| + try:
|
| + for value in pcit:
|
| + iterated_value = value
|
| + self.fail('Expected exception from iterator')
|
| + except CustomTestException:
|
| + pass
|
| + self.assertEqual(iterated_value, 1)
|
| +
|
| + def testPluralityCheckableIteratorWith2Exceptions(self):
|
| + """Tests PluralityCheckableIterator with 2 elements that both raise."""
|
| +
|
| + class IterTest(object):
|
| +
|
| + def __init__(self):
|
| + self.position = 0
|
| +
|
| + def __iter__(self):
|
| + return self
|
| +
|
| + def next(self):
|
| + if self.position < 2:
|
| + self.position += 1
|
| + raise CustomTestException('Test exception %s' % self.position)
|
| + else:
|
| + raise StopIteration()
|
| +
|
| + pcit = PluralityCheckableIterator(IterTest())
|
| + try:
|
| + for _ in pcit:
|
| + pass
|
| + self.fail('Expected exception 1 from iterator')
|
| + except CustomTestException, e:
|
| + self.assertIn(e.message, 'Test exception 1')
|
| + try:
|
| + for _ in pcit:
|
| + pass
|
| + self.fail('Expected exception 2 from iterator')
|
| + except CustomTestException, e:
|
| + self.assertIn(e.message, 'Test exception 2')
|
| + for _ in pcit:
|
| + self.fail('Expected StopIteration')
|
| +
|
| + def testPluralityCheckableIteratorWithYieldedException(self):
|
| + """Tests PluralityCheckableIterator an iterator that yields an exception.
|
| +
|
| + The yielded exception is in the form of a tuple and must also contain a
|
| + stack trace.
|
| + """
|
| +
|
| + class IterTest(object):
|
| +
|
| + def __init__(self):
|
| + self.position = 0
|
| +
|
| + def __iter__(self):
|
| + return self
|
| +
|
| + def next(self):
|
| + if self.position == 0:
|
| + try:
|
| + self.position += 1
|
| + raise CustomTestException('Test exception 0')
|
| + except CustomTestException, e:
|
| + return (e, sys.exc_info()[2])
|
| + elif self.position == 1:
|
| + self.position += 1
|
| + return 1
|
| + else:
|
| + raise StopIteration()
|
| +
|
| + pcit = PluralityCheckableIterator(IterTest())
|
| + try:
|
| + for _ in pcit:
|
| + pass
|
| + self.fail('Expected exception 0 from iterator')
|
| + except CustomTestException, e:
|
| + self.assertIn(e.message, 'Test exception 0')
|
| + for value in pcit:
|
| + iterated_value = value
|
| + self.assertEqual(iterated_value, 1)
|
|
|