class TestCaseQueryFail(unittest.TestCase): def setUp(self): qs = QuerySpecification("*") self.querymock = mock.MagicMock() self.query = Query(qs, self.querymock, page_size=10) def test_unexpected_fail(self): mock_ex = create_PSED_Exception(UNEXPECTED_FAIL, ERROR_REASON) self.querymock.side_effect = mock_ex with self.assertRaises(ProvisioningServiceError) as cm: self.query.next() e = cm.exception self.assertEqual( str(e), self.query.err_msg.format(UNEXPECTED_FAIL, ERROR_REASON)) self.assertIs(e.cause, mock_ex)
class TestCaseQueryFail(unittest.TestCase): def setUp(self): qs = QuerySpecification("*") self.querymock = mock.MagicMock() sasfactory = SasTokenFactory("dummy", "values", "only") self.query = Query(qs, self.querymock, sasfactory, page_size=10) def test_exepcted_fail(self): self.querymock.return_value = query_custom_response(FAIL, MESSAGE) with self.assertRaises(ProvisioningServiceError) as cm: self.query.next() e = cm.exception self.assertEqual(str(e), MESSAGE) self.assertIsNone(e.cause) def test_unexpected_fail(self): mock_ex = create_PSED_Exception(UNEXPECTED_FAIL, MESSAGE) self.querymock.side_effect = mock_ex with self.assertRaises(ProvisioningServiceError) as cm: self.query.next() e = cm.exception self.assertEqual(str(e), self.query.err_msg_unexpected.format(UNEXPECTED_FAIL)) self.assertIs(e.cause, mock_ex)
class TestCaseQueryFail(unittest.TestCase): def setUp(self): qs = QuerySpecification("*") self.querymock = mock.MagicMock() sasfactory = SasTokenFactory("dummy", "values", "only") self.query = Query(qs, self.querymock, sasfactory, page_size=10) def test_exepcted_fail(self): self.querymock.return_value = query_custom_response(FAIL, MESSAGE) with self.assertRaises(ProvisioningServiceError) as cm: self.query.next() e = cm.exception self.assertEqual(str(e), MESSAGE) self.assertIsNone(e.cause) def test_unexpected_fail(self): mock_ex = create_PSED_Exception(UNEXPECTED_FAIL, MESSAGE) self.querymock.side_effect = mock_ex with self.assertRaises(ProvisioningServiceError) as cm: self.query.next() e = cm.exception self.assertEqual(str(e), self.query.err_msg_unexpected.format(UNEXPECTED_FAIL)) self.assertIs(e.cause, mock_ex)
class TestCaseQueryNoPageSize(unittest.TestCase): """ No page size indicates that a full list of results should be expected. There will be no paging. All further calls to "next" after the first should raise exceptions """ def setUp(self): qs = QuerySpecification("*") self.querymock = mock.MagicMock(side_effect=mock_query_op) self.query = Query(qs, self.querymock) global RESULTS RESULTS = setup_results() #reset results to not have wrappers @mock.patch.object(SasToken, '__str__', return_value=SAS) def test_next_no_page_no_token(self, mock_sas): page_size = self.query.page_size cont_token = self.query.continuation_token expected_result = get_result(cont_token, page_size) res = self.query.next() check_results(self, res, expected_result) self.querymock.assert_called_with(self.query._query_spec_or_id, page_size, cont_token, raw=True) with self.assertRaises(StopIteration): self.query.next() @mock.patch.object(SasToken, '__str__', return_value=SAS) def test_next_no_page_w_token(self, mock_sas): page_size = self.query.page_size cont_token = "10" expected_result = get_result(cont_token, page_size) res = self.query.next(cont_token) check_results(self, res, expected_result) self.querymock.assert_called_with(self.query._query_spec_or_id, page_size, cont_token, raw=True) with self.assertRaises(StopIteration): self.query.next() def test_no_page_for_loop(self): full_results = [] count = 0 for page in self.query: count += 1 full_results += page check_results(self, full_results, RESULTS) self.assertEqual(count, 1)
class TestCaseQueryNoPageSize(unittest.TestCase): """ No page size indicates that a full list of results should be expected. There will be no paging. All further calls to "next" after the first should raise exceptions """ def setUp(self): qs = QuerySpecification("*") self.querymock = mock.MagicMock(side_effect=mock_query_op) sasfactory = SasTokenFactory("dummy", "values", "only") self.query = Query(qs, self.querymock, sasfactory) @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_no_page_no_token(self, mock_sas): expected_headers = get_expected_headers(self.query, self.query.continuation_token) expected_result = get_result(self.query.continuation_token, self.query.page_size) res = self.query.next() check_internals(self, res, expected_result) self.querymock.assert_called_with(self.query._query_spec_or_id, expected_headers, True) with self.assertRaises(StopIteration): self.query.next() @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_no_page_w_token(self, mock_sas): custom_token = "10" expected_headers = get_expected_headers(self.query, custom_token) expected_result = get_result(custom_token, self.query.page_size) res = self.query.next(custom_token) check_internals(self, res, expected_result) self.querymock.assert_called_with(self.query._query_spec_or_id, expected_headers, True) with self.assertRaises(StopIteration): self.query.next() def test_no_page_for_loop(self): full_results = [] count = 0 for page in self.query: count += 1 full_results += page check_internals(self, full_results, RESULTS) self.assertEqual(count, 1)
class TestCaseQueryNoPageSize(unittest.TestCase): """ No page size indicates that a full list of results should be expected. There will be no paging. All further calls to "next" after the first should raise exceptions """ def setUp(self): qs = QuerySpecification("*") self.querymock = mock.MagicMock(side_effect=mock_query_op) sasfactory = SasTokenFactory("dummy", "values", "only") self.query = Query(qs, self.querymock, sasfactory) @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_no_page_no_token(self, mock_sas): expected_headers = get_expected_headers(self.query, self.query.continuation_token) expected_result = get_result(self.query.continuation_token, self.query.page_size) res = self.query.next() check_internals(self, res, expected_result) self.querymock.assert_called_with( self.query._query_spec_or_id, expected_headers, True) with self.assertRaises(StopIteration): self.query.next() @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_no_page_w_token(self, mock_sas): custom_token = "10" expected_headers = get_expected_headers(self.query, custom_token) expected_result = get_result(custom_token, self.query.page_size) res = self.query.next(custom_token) check_internals(self, res, expected_result) self.querymock.assert_called_with( self.query._query_spec_or_id, expected_headers, True) with self.assertRaises(StopIteration): self.query.next() def test_no_page_for_loop(self): full_results = [] count = 0 for page in self.query: count += 1 full_results += page check_internals(self, full_results, RESULTS) self.assertEqual(count, 1)
class TestCaseQueryCustomPageSize(unittest.TestCase): def setUp(self): qs = QuerySpecification("*") self.querymock = mock.MagicMock(side_effect=mock_query_op) sasfactory = SasTokenFactory("dummy", "values", "only") self.query = Query(qs, self.querymock, sasfactory, page_size=10) @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_no_token(self, mock_sas): expected_headers = get_expected_headers(self.query, self.query.continuation_token) expected_result = get_result(self.query.continuation_token, self.query.page_size) res = self.query.next() check_internals(self, res, expected_result) self.querymock.assert_called_with(self.query._query_spec_or_id, expected_headers, True) @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_w_token(self, mock_sas): custom_token = "10" expected_headers = get_expected_headers(self.query, custom_token) expected_result = get_result(custom_token, self.query.page_size) res = self.query.next(custom_token) check_internals(self, res, expected_result) self.querymock.assert_called_with(self.query._query_spec_or_id, expected_headers, True) @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_page_not_full(self, mock_sas): custom_token = "20" #although the page size is 10, from this token there are only 5 results expected_headers = get_expected_headers(self.query, custom_token) expected_result = get_result(custom_token, self.query.page_size) res = self.query.next(custom_token) check_internals(self, res, expected_result) self.querymock.assert_called_with(self.query._query_spec_or_id, expected_headers, True) @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_custom_page_size(self, mock_sas): self.query.page_size = 3 expected_headers = get_expected_headers(self.query, self.query.continuation_token) expected_result = get_result(self.query.continuation_token, self.query.page_size) res = self.query.next() check_internals(self, res, expected_result) self.querymock.assert_called_with(self.query._query_spec_or_id, expected_headers, True) def test_next_has_next_false(self): self.query.has_next = False with self.assertRaises(StopIteration): res = self.query.next() self.querymock.assert_not_called() @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_no_more_results_but_has_next_true(self, mock_sas): """has_next = True does not guarantee there are actually more results. It just means there is more to check for, i.e. has not looked past the end of the data to know that it is the end """ custom_token = "25" #there are only 25 results, i.e. start from last index expected_headers = get_expected_headers(self.query, custom_token) with self.assertRaises(StopIteration): res = self.query.next(custom_token) self.querymock.assert_called_with(self.query._query_spec_or_id, expected_headers, True) def test___iter__(self): res = self.query.__iter__() self.assertEqual(res, self.query) self.assertIsNone(self.query.continuation_token) @mock.patch.object(Query, 'next', return_value=DUMMY_LIST) def test___next__(self, mock_next): res = self.query.__next__() mock_next.assert_called_with() self.assertEqual(res, DUMMY_LIST) def test_iterate_for_loop(self): full_results = [] for page in self.query: full_results += page check_internals(self, full_results, RESULTS) def test_set_page_size_negative(self): with self.assertRaises(ValueError): self.query.page_size = -1 def test_set_page_size_zero(self): with self.assertRaises(ValueError): self.query.page_size = 0 def test_set_page_size_positive(self): self.query.page_size = 1 self.assertEqual(self.query.page_size, 1) def test_set_page_size_none(self): self.query.page_size = None self.assertEqual(self.query.page_size, None)
class TestCaseQueryCustomPageSize(unittest.TestCase): def setUp(self): qs = QuerySpecification("*") self.querymock = mock.MagicMock(side_effect=mock_query_op) sasfactory = SasTokenFactory("dummy", "values", "only") self.query = Query(qs, self.querymock, sasfactory, page_size=10) @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_no_token(self, mock_sas): expected_headers = get_expected_headers(self.query, self.query.continuation_token) expected_result = get_result(self.query.continuation_token, self.query.page_size) res = self.query.next() check_internals(self, res, expected_result) self.querymock.assert_called_with( self.query._query_spec_or_id, expected_headers, True) @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_w_token(self, mock_sas): custom_token = "10" expected_headers = get_expected_headers(self.query, custom_token) expected_result = get_result(custom_token, self.query.page_size) res = self.query.next(custom_token) check_internals(self, res, expected_result) self.querymock.assert_called_with( self.query._query_spec_or_id, expected_headers, True) @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_page_not_full(self, mock_sas): custom_token = "20" #although the page size is 10, from this token there are only 5 results expected_headers = get_expected_headers(self.query, custom_token) expected_result = get_result(custom_token, self.query.page_size) res = self.query.next(custom_token) check_internals(self, res, expected_result) self.querymock.assert_called_with( self.query._query_spec_or_id, expected_headers, True) @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_custom_page_size(self, mock_sas): self.query.page_size = 3 expected_headers = get_expected_headers(self.query, self.query.continuation_token) expected_result = get_result(self.query.continuation_token, self.query.page_size) res = self.query.next() check_internals(self, res, expected_result) self.querymock.assert_called_with( self.query._query_spec_or_id, expected_headers, True) def test_next_has_next_false(self): self.query.has_next = False with self.assertRaises(StopIteration): res = self.query.next() self.querymock.assert_not_called() @mock.patch.object(SasTokenFactory, 'generate_sastoken', return_value=SAS) def test_next_no_more_results_but_has_next_true(self, mock_sas): """has_next = True does not guarantee there are actually more results. It just means there is more to check for, i.e. has not looked past the end of the data to know that it is the end """ custom_token = "25" #there are only 25 results, i.e. start from last index expected_headers = get_expected_headers(self.query, custom_token) with self.assertRaises(StopIteration): res = self.query.next(custom_token) self.querymock.assert_called_with( self.query._query_spec_or_id, expected_headers, True) def test___iter__(self): res = self.query.__iter__() self.assertEqual(res, self.query) self.assertIsNone(self.query.continuation_token) @mock.patch.object(Query, 'next', return_value=DUMMY_LIST) def test___next__(self, mock_next): res = self.query.__next__() mock_next.assert_called_with() self.assertEqual(res, DUMMY_LIST) def test_iterate_for_loop(self): full_results = [] for page in self.query: full_results += page check_internals(self, full_results, RESULTS) def test_set_page_size_negative(self): with self.assertRaises(ValueError): self.query.page_size = -1 def test_set_page_size_zero(self): with self.assertRaises(ValueError): self.query.page_size = 0 def test_set_page_size_positive(self): self.query.page_size = 1 self.assertEqual(self.query.page_size, 1) def test_set_page_size_none(self): self.query.page_size = None self.assertEqual(self.query.page_size, None)