def test_filter_all_recursive_yields(self, mock_filter_one): # Test filter_all() allows generators from previous filter_all()s. # filter_all() yields results. We want to make sure that we can # call filter_all() with generators returned from previous calls # to filter_all(). filter_obj_list = ['obj1', 'obj2', 'obj3'] spec_obj = objects.RequestSpec() base_filter = filters.BaseFilter() # The order that _filter_one is going to get called gets # confusing because we will be recursively yielding things.. # We are going to simulate the first call to filter_all() # returning False for 'obj2'. So, 'obj1' will get yielded # 'total_iterations' number of times before the first filter_all() # call gets to processing 'obj2'. We then return 'False' for it. # After that, 'obj3' gets yielded 'total_iterations' number of # times. mock_results = [] total_iterations = 200 for x in range(total_iterations): mock_results.append(True) mock_results.append(False) for x in range(total_iterations): mock_results.append(True) mock_filter_one.side_effect = mock_results objs = iter(filter_obj_list) for x in range(total_iterations): # Pass in generators returned from previous calls. objs = base_filter.filter_all(objs, spec_obj) self.assertTrue(inspect.isgenerator(objs)) self.assertEqual(['obj1', 'obj3'], list(objs))
def test_filter_all_recursive_yields(self): # Test filter_all() allows generators from previous filter_all()s. # filter_all() yields results. We want to make sure that we can # call filter_all() with generators returned from previous calls # to filter_all(). filter_obj_list = ['obj1', 'obj2', 'obj3'] filter_properties = 'fake_filter_properties' base_filter = filters.BaseFilter() self.mox.StubOutWithMock(base_filter, '_filter_one') total_iterations = 200 # The order that _filter_one is going to get called gets # confusing because we will be recursively yielding things.. # We are going to simulate the first call to filter_all() # returning False for 'obj2'. So, 'obj1' will get yielded # 'total_iterations' number of times before the first filter_all() # call gets to processing 'obj2'. We then return 'False' for it. # After that, 'obj3' gets yielded 'total_iterations' number of # times. for x in xrange(total_iterations): base_filter._filter_one('obj1', filter_properties).AndReturn(True) base_filter._filter_one('obj2', filter_properties).AndReturn(False) for x in xrange(total_iterations): base_filter._filter_one('obj3', filter_properties).AndReturn(True) self.mox.ReplayAll() objs = iter(filter_obj_list) for x in xrange(total_iterations): # Pass in generators returned from previous calls. objs = base_filter.filter_all(objs, filter_properties) self.assertTrue(inspect.isgenerator(objs)) self.assertEqual(['obj1', 'obj3'], list(objs))
def test_filter_all(self, mock_filter_one): mock_filter_one.side_effect = [True, False, True] filter_obj_list = ['obj1', 'obj2', 'obj3'] spec_obj = objects.RequestSpec() base_filter = filters.BaseFilter() result = base_filter.filter_all(filter_obj_list, spec_obj) self.assertTrue(inspect.isgenerator(result)) self.assertEqual(['obj1', 'obj3'], list(result))
def test_filter_all(self): filter_obj_list = ['obj1', 'obj2', 'obj3'] filter_properties = 'fake_filter_properties' base_filter = filters.BaseFilter() self.mox.StubOutWithMock(base_filter, '_filter_one') base_filter._filter_one('obj1', filter_properties).AndReturn(True) base_filter._filter_one('obj2', filter_properties).AndReturn(False) base_filter._filter_one('obj3', filter_properties).AndReturn(True) self.mox.ReplayAll() result = base_filter.filter_all(filter_obj_list, filter_properties) self.assertTrue(inspect.isgenerator(result)) self.assertEqual(['obj1', 'obj3'], list(result))
def test_filter_all(self): filter_obj_list = ['obj1', 'obj2', 'obj3'] spec_obj = objects.RequestSpec() base_filter = filters.BaseFilter() self.mox.StubOutWithMock(base_filter, '_filter_one') base_filter._filter_one('obj1', spec_obj).AndReturn(True) base_filter._filter_one('obj2', spec_obj).AndReturn(False) base_filter._filter_one('obj3', spec_obj).AndReturn(True) self.mox.ReplayAll() result = base_filter.filter_all(filter_obj_list, spec_obj) self.assertTrue(inspect.isgenerator(result)) self.assertEqual(['obj1', 'obj3'], list(result))