OLD | NEW |
(Empty) | |
| 1 # |
| 2 # Copyright 2015 Google Inc. |
| 3 # |
| 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 # you may not use this file except in compliance with the License. |
| 6 # You may obtain a copy of the License at |
| 7 # |
| 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 # |
| 10 # Unless required by applicable law or agreed to in writing, software |
| 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 # See the License for the specific language governing permissions and |
| 14 # limitations under the License. |
| 15 |
| 16 """Tests for list_pager.""" |
| 17 |
| 18 import unittest2 |
| 19 |
| 20 from apitools.base.py import list_pager |
| 21 from apitools.base.py.testing import mock |
| 22 from apitools.base.py.testing import testclient as fusiontables |
| 23 |
| 24 |
| 25 class ListPagerTest(unittest2.TestCase): |
| 26 |
| 27 def _AssertInstanceSequence(self, results, n): |
| 28 counter = 0 |
| 29 for instance in results: |
| 30 self.assertEqual(instance.name, 'c' + str(counter)) |
| 31 counter += 1 |
| 32 |
| 33 self.assertEqual(counter, n) |
| 34 |
| 35 def setUp(self): |
| 36 self.mocked_client = mock.Client(fusiontables.FusiontablesV1) |
| 37 self.mocked_client.Mock() |
| 38 self.addCleanup(self.mocked_client.Unmock) |
| 39 |
| 40 def testYieldFromList(self): |
| 41 self.mocked_client.column.List.Expect( |
| 42 fusiontables.FusiontablesColumnListRequest( |
| 43 maxResults=100, |
| 44 pageToken=None, |
| 45 tableId='mytable', |
| 46 ), |
| 47 fusiontables.ColumnList( |
| 48 items=[ |
| 49 fusiontables.Column(name='c0'), |
| 50 fusiontables.Column(name='c1'), |
| 51 fusiontables.Column(name='c2'), |
| 52 fusiontables.Column(name='c3'), |
| 53 ], |
| 54 nextPageToken='x', |
| 55 )) |
| 56 self.mocked_client.column.List.Expect( |
| 57 fusiontables.FusiontablesColumnListRequest( |
| 58 maxResults=100, |
| 59 pageToken='x', |
| 60 tableId='mytable', |
| 61 ), |
| 62 fusiontables.ColumnList( |
| 63 items=[ |
| 64 fusiontables.Column(name='c4'), |
| 65 fusiontables.Column(name='c5'), |
| 66 fusiontables.Column(name='c6'), |
| 67 fusiontables.Column(name='c7'), |
| 68 ], |
| 69 )) |
| 70 |
| 71 client = fusiontables.FusiontablesV1(get_credentials=False) |
| 72 request = fusiontables.FusiontablesColumnListRequest(tableId='mytable') |
| 73 results = list_pager.YieldFromList(client.column, request) |
| 74 |
| 75 self._AssertInstanceSequence(results, 8) |
| 76 |
| 77 def testYieldNoRecords(self): |
| 78 client = fusiontables.FusiontablesV1(get_credentials=False) |
| 79 request = fusiontables.FusiontablesColumnListRequest(tableId='mytable') |
| 80 results = list_pager.YieldFromList(client.column, request, limit=False) |
| 81 self.assertEqual(0, len(list(results))) |
| 82 |
| 83 def testYieldFromListPartial(self): |
| 84 self.mocked_client.column.List.Expect( |
| 85 fusiontables.FusiontablesColumnListRequest( |
| 86 maxResults=100, |
| 87 pageToken=None, |
| 88 tableId='mytable', |
| 89 ), |
| 90 fusiontables.ColumnList( |
| 91 items=[ |
| 92 fusiontables.Column(name='c0'), |
| 93 fusiontables.Column(name='c1'), |
| 94 fusiontables.Column(name='c2'), |
| 95 fusiontables.Column(name='c3'), |
| 96 ], |
| 97 nextPageToken='x', |
| 98 )) |
| 99 self.mocked_client.column.List.Expect( |
| 100 fusiontables.FusiontablesColumnListRequest( |
| 101 maxResults=100, |
| 102 pageToken='x', |
| 103 tableId='mytable', |
| 104 ), |
| 105 fusiontables.ColumnList( |
| 106 items=[ |
| 107 fusiontables.Column(name='c4'), |
| 108 fusiontables.Column(name='c5'), |
| 109 fusiontables.Column(name='c6'), |
| 110 fusiontables.Column(name='c7'), |
| 111 ], |
| 112 )) |
| 113 |
| 114 client = fusiontables.FusiontablesV1(get_credentials=False) |
| 115 request = fusiontables.FusiontablesColumnListRequest(tableId='mytable') |
| 116 results = list_pager.YieldFromList(client.column, request, limit=6) |
| 117 |
| 118 self._AssertInstanceSequence(results, 6) |
| 119 |
| 120 def testYieldFromListEmpty(self): |
| 121 self.mocked_client.column.List.Expect( |
| 122 fusiontables.FusiontablesColumnListRequest( |
| 123 maxResults=100, |
| 124 pageToken=None, |
| 125 tableId='mytable', |
| 126 ), |
| 127 fusiontables.ColumnList()) |
| 128 |
| 129 client = fusiontables.FusiontablesV1(get_credentials=False) |
| 130 request = fusiontables.FusiontablesColumnListRequest(tableId='mytable') |
| 131 results = list_pager.YieldFromList(client.column, request, limit=6) |
| 132 |
| 133 self._AssertInstanceSequence(results, 0) |
| 134 |
| 135 def testYieldFromListWithPredicate(self): |
| 136 self.mocked_client.column.List.Expect( |
| 137 fusiontables.FusiontablesColumnListRequest( |
| 138 maxResults=100, |
| 139 pageToken=None, |
| 140 tableId='mytable', |
| 141 ), |
| 142 fusiontables.ColumnList( |
| 143 items=[ |
| 144 fusiontables.Column(name='c0'), |
| 145 fusiontables.Column(name='bad0'), |
| 146 fusiontables.Column(name='c1'), |
| 147 fusiontables.Column(name='bad1'), |
| 148 ], |
| 149 nextPageToken='x', |
| 150 )) |
| 151 self.mocked_client.column.List.Expect( |
| 152 fusiontables.FusiontablesColumnListRequest( |
| 153 maxResults=100, |
| 154 pageToken='x', |
| 155 tableId='mytable', |
| 156 ), |
| 157 fusiontables.ColumnList( |
| 158 items=[ |
| 159 fusiontables.Column(name='c2'), |
| 160 ], |
| 161 )) |
| 162 |
| 163 client = fusiontables.FusiontablesV1(get_credentials=False) |
| 164 request = fusiontables.FusiontablesColumnListRequest(tableId='mytable') |
| 165 results = list_pager.YieldFromList( |
| 166 client.column, request, predicate=lambda x: 'c' in x.name) |
| 167 |
| 168 self._AssertInstanceSequence(results, 3) |
| 169 |
| 170 def testYieldFromListWithAttributes(self): |
| 171 self.mocked_client.columnalternate.List.Expect( |
| 172 fusiontables.FusiontablesColumnListAlternateRequest( |
| 173 pageSize=100, |
| 174 pageToken=None, |
| 175 tableId='mytable', |
| 176 ), |
| 177 fusiontables.ColumnListAlternate( |
| 178 columns=[ |
| 179 fusiontables.Column(name='c0'), |
| 180 fusiontables.Column(name='c1'), |
| 181 ], |
| 182 nextPageToken='x', |
| 183 )) |
| 184 self.mocked_client.columnalternate.List.Expect( |
| 185 fusiontables.FusiontablesColumnListAlternateRequest( |
| 186 pageSize=100, |
| 187 pageToken='x', |
| 188 tableId='mytable', |
| 189 ), |
| 190 fusiontables.ColumnListAlternate( |
| 191 columns=[ |
| 192 fusiontables.Column(name='c2'), |
| 193 ], |
| 194 )) |
| 195 |
| 196 client = fusiontables.FusiontablesV1(get_credentials=False) |
| 197 request = fusiontables.FusiontablesColumnListAlternateRequest( |
| 198 tableId='mytable') |
| 199 results = list_pager.YieldFromList( |
| 200 client.columnalternate, request, |
| 201 batch_size_attribute='pageSize', field='columns') |
| 202 |
| 203 self._AssertInstanceSequence(results, 3) |
OLD | NEW |