| OLD | NEW |
| 1 import mock | 1 import mock |
| 2 import unittest | 2 from tests.unit import unittest |
| 3 from boto.dynamodb2 import exceptions | 3 from boto.dynamodb2 import exceptions |
| 4 from boto.dynamodb2.fields import (HashKey, RangeKey, | 4 from boto.dynamodb2.fields import (HashKey, RangeKey, |
| 5 AllIndex, KeysOnlyIndex, IncludeIndex, | 5 AllIndex, KeysOnlyIndex, IncludeIndex, |
| 6 GlobalAllIndex, GlobalKeysOnlyIndex, | 6 GlobalAllIndex, GlobalKeysOnlyIndex, |
| 7 GlobalIncludeIndex) | 7 GlobalIncludeIndex) |
| 8 from boto.dynamodb2.items import Item | 8 from boto.dynamodb2.items import Item |
| 9 from boto.dynamodb2.layer1 import DynamoDBConnection | 9 from boto.dynamodb2.layer1 import DynamoDBConnection |
| 10 from boto.dynamodb2.results import ResultSet, BatchGetResultSet | 10 from boto.dynamodb2.results import ResultSet, BatchGetResultSet |
| 11 from boto.dynamodb2.table import Table | 11 from boto.dynamodb2.table import Table |
| 12 from boto.dynamodb2.types import (STRING, NUMBER, | 12 from boto.dynamodb2.types import (STRING, NUMBER, BINARY, |
| 13 FILTER_OPERATORS, QUERY_OPERATORS) | 13 FILTER_OPERATORS, QUERY_OPERATORS) |
| 14 from boto.exception import JSONResponseError | 14 from boto.exception import JSONResponseError |
| 15 | 15 |
| 16 | 16 |
| 17 FakeDynamoDBConnection = mock.create_autospec(DynamoDBConnection) | 17 FakeDynamoDBConnection = mock.create_autospec(DynamoDBConnection) |
| 18 | 18 |
| 19 | 19 |
| 20 | |
| 21 class SchemaFieldsTestCase(unittest.TestCase): | 20 class SchemaFieldsTestCase(unittest.TestCase): |
| 22 def test_hash_key(self): | 21 def test_hash_key(self): |
| 23 hash_key = HashKey('hello') | 22 hash_key = HashKey('hello') |
| 24 self.assertEqual(hash_key.name, 'hello') | 23 self.assertEqual(hash_key.name, 'hello') |
| 25 self.assertEqual(hash_key.data_type, STRING) | 24 self.assertEqual(hash_key.data_type, STRING) |
| 26 self.assertEqual(hash_key.attr_type, 'HASH') | 25 self.assertEqual(hash_key.attr_type, 'HASH') |
| 27 | 26 |
| 28 self.assertEqual(hash_key.definition(), { | 27 self.assertEqual(hash_key.definition(), { |
| 29 'AttributeName': 'hello', | 28 'AttributeName': 'hello', |
| 30 'AttributeType': 'S' | 29 'AttributeType': 'S' |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 'gender', | 291 'gender', |
| 293 'friend_count', | 292 'friend_count', |
| 294 ] | 293 ] |
| 295 }, | 294 }, |
| 296 'ProvisionedThroughput': { | 295 'ProvisionedThroughput': { |
| 297 'ReadCapacityUnits': 5, | 296 'ReadCapacityUnits': 5, |
| 298 'WriteCapacityUnits': 5 | 297 'WriteCapacityUnits': 5 |
| 299 } | 298 } |
| 300 }) | 299 }) |
| 301 | 300 |
| 301 def test_global_include_index_throughput(self): |
| 302 include_index = GlobalIncludeIndex('IncludeKeys', parts=[ |
| 303 HashKey('username'), |
| 304 RangeKey('date_joined') |
| 305 ], includes=[ |
| 306 'gender', |
| 307 'friend_count' |
| 308 ], throughput={ |
| 309 'read': 10, |
| 310 'write': 8 |
| 311 }) |
| 312 |
| 313 self.assertEqual(include_index.schema(), { |
| 314 'IndexName': 'IncludeKeys', |
| 315 'KeySchema': [ |
| 316 { |
| 317 'AttributeName': 'username', |
| 318 'KeyType': 'HASH' |
| 319 }, |
| 320 { |
| 321 'AttributeName': 'date_joined', |
| 322 'KeyType': 'RANGE' |
| 323 } |
| 324 ], |
| 325 'Projection': { |
| 326 'ProjectionType': 'INCLUDE', |
| 327 'NonKeyAttributes': [ |
| 328 'gender', |
| 329 'friend_count', |
| 330 ] |
| 331 }, |
| 332 'ProvisionedThroughput': { |
| 333 'ReadCapacityUnits': 10, |
| 334 'WriteCapacityUnits': 8 |
| 335 } |
| 336 }) |
| 337 |
| 302 | 338 |
| 303 class ItemTestCase(unittest.TestCase): | 339 class ItemTestCase(unittest.TestCase): |
| 304 def setUp(self): | 340 def setUp(self): |
| 305 super(ItemTestCase, self).setUp() | 341 super(ItemTestCase, self).setUp() |
| 306 self.table = Table('whatever', connection=FakeDynamoDBConnection()) | 342 self.table = Table('whatever', connection=FakeDynamoDBConnection()) |
| 307 self.johndoe = self.create_item({ | 343 self.johndoe = self.create_item({ |
| 308 'username': 'johndoe', | 344 'username': 'johndoe', |
| 309 'first_name': 'John', | 345 'first_name': 'John', |
| 310 'date_joined': 12345, | 346 'date_joined': 12345, |
| 311 }) | 347 }) |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 if exclusive_start_key + 5 < end_cap: | 850 if exclusive_start_key + 5 < end_cap: |
| 815 retval['last_key'] = exclusive_start_key + 5 | 851 retval['last_key'] = exclusive_start_key + 5 |
| 816 | 852 |
| 817 return retval | 853 return retval |
| 818 | 854 |
| 819 | 855 |
| 820 class ResultSetTestCase(unittest.TestCase): | 856 class ResultSetTestCase(unittest.TestCase): |
| 821 def setUp(self): | 857 def setUp(self): |
| 822 super(ResultSetTestCase, self).setUp() | 858 super(ResultSetTestCase, self).setUp() |
| 823 self.results = ResultSet() | 859 self.results = ResultSet() |
| 824 self.results.to_call(fake_results, 'john', greeting='Hello', limit=20) | 860 self.result_function = mock.MagicMock(side_effect=fake_results) |
| 861 self.results.to_call(self.result_function, 'john', greeting='Hello', lim
it=20) |
| 825 | 862 |
| 826 def test_first_key(self): | 863 def test_first_key(self): |
| 827 self.assertEqual(self.results.first_key, 'exclusive_start_key') | 864 self.assertEqual(self.results.first_key, 'exclusive_start_key') |
| 828 | 865 |
| 866 def test_max_page_size_fetch_more(self): |
| 867 self.results = ResultSet(max_page_size=10) |
| 868 self.results.to_call(self.result_function, 'john', greeting='Hello') |
| 869 self.results.fetch_more() |
| 870 self.result_function.assert_called_with('john', greeting='Hello', limit=
10) |
| 871 self.result_function.reset_mock() |
| 872 |
| 873 def test_max_page_size_and_smaller_limit_fetch_more(self): |
| 874 self.results = ResultSet(max_page_size=10) |
| 875 self.results.to_call(self.result_function, 'john', greeting='Hello', lim
it=5) |
| 876 self.results.fetch_more() |
| 877 self.result_function.assert_called_with('john', greeting='Hello', limit=
5) |
| 878 self.result_function.reset_mock() |
| 879 |
| 880 def test_max_page_size_and_bigger_limit_fetch_more(self): |
| 881 self.results = ResultSet(max_page_size=10) |
| 882 self.results.to_call(self.result_function, 'john', greeting='Hello', lim
it=15) |
| 883 self.results.fetch_more() |
| 884 self.result_function.assert_called_with('john', greeting='Hello', limit=
10) |
| 885 self.result_function.reset_mock() |
| 886 |
| 829 def test_fetch_more(self): | 887 def test_fetch_more(self): |
| 830 # First "page". | 888 # First "page". |
| 831 self.results.fetch_more() | 889 self.results.fetch_more() |
| 832 self.assertEqual(self.results._results, [ | 890 self.assertEqual(self.results._results, [ |
| 833 'Hello john #0', | 891 'Hello john #0', |
| 834 'Hello john #1', | 892 'Hello john #1', |
| 835 'Hello john #2', | 893 'Hello john #2', |
| 836 'Hello john #3', | 894 'Hello john #3', |
| 837 'Hello john #4', | 895 'Hello john #4', |
| 838 ]) | 896 ]) |
| 839 | 897 |
| 898 self.result_function.assert_called_with('john', greeting='Hello', limit=
20) |
| 899 self.result_function.reset_mock() |
| 900 |
| 840 # Fake in a last key. | 901 # Fake in a last key. |
| 841 self.results._last_key_seen = 4 | 902 self.results._last_key_seen = 4 |
| 842 # Second "page". | 903 # Second "page". |
| 843 self.results.fetch_more() | 904 self.results.fetch_more() |
| 844 self.assertEqual(self.results._results, [ | 905 self.assertEqual(self.results._results, [ |
| 845 'Hello john #5', | 906 'Hello john #5', |
| 846 'Hello john #6', | 907 'Hello john #6', |
| 847 'Hello john #7', | 908 'Hello john #7', |
| 848 'Hello john #8', | 909 'Hello john #8', |
| 849 'Hello john #9', | 910 'Hello john #9', |
| 850 ]) | 911 ]) |
| 851 | 912 |
| 913 self.result_function.assert_called_with('john', greeting='Hello', limit=
20, exclusive_start_key=4) |
| 914 self.result_function.reset_mock() |
| 915 |
| 852 # Fake in a last key. | 916 # Fake in a last key. |
| 853 self.results._last_key_seen = 9 | 917 self.results._last_key_seen = 9 |
| 854 # Last "page". | 918 # Last "page". |
| 855 self.results.fetch_more() | 919 self.results.fetch_more() |
| 856 self.assertEqual(self.results._results, [ | 920 self.assertEqual(self.results._results, [ |
| 857 'Hello john #10', | 921 'Hello john #10', |
| 858 'Hello john #11', | 922 'Hello john #11', |
| 859 'Hello john #12', | 923 'Hello john #12', |
| 860 ]) | 924 ]) |
| 861 | 925 |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1083 raw_schema_1 = [ | 1147 raw_schema_1 = [ |
| 1084 { | 1148 { |
| 1085 "AttributeName": "username", | 1149 "AttributeName": "username", |
| 1086 "KeyType": "HASH" | 1150 "KeyType": "HASH" |
| 1087 }, | 1151 }, |
| 1088 { | 1152 { |
| 1089 "AttributeName": "date_joined", | 1153 "AttributeName": "date_joined", |
| 1090 "KeyType": "RANGE" | 1154 "KeyType": "RANGE" |
| 1091 } | 1155 } |
| 1092 ] | 1156 ] |
| 1093 schema_1 = self.users._introspect_schema(raw_schema_1) | 1157 raw_attributes_1 = [ |
| 1158 { |
| 1159 'AttributeName': 'username', |
| 1160 'AttributeType': 'S' |
| 1161 }, |
| 1162 { |
| 1163 'AttributeName': 'date_joined', |
| 1164 'AttributeType': 'S' |
| 1165 }, |
| 1166 ] |
| 1167 schema_1 = self.users._introspect_schema(raw_schema_1, raw_attributes_1) |
| 1094 self.assertEqual(len(schema_1), 2) | 1168 self.assertEqual(len(schema_1), 2) |
| 1095 self.assertTrue(isinstance(schema_1[0], HashKey)) | 1169 self.assertTrue(isinstance(schema_1[0], HashKey)) |
| 1096 self.assertEqual(schema_1[0].name, 'username') | 1170 self.assertEqual(schema_1[0].name, 'username') |
| 1097 self.assertTrue(isinstance(schema_1[1], RangeKey)) | 1171 self.assertTrue(isinstance(schema_1[1], RangeKey)) |
| 1098 self.assertEqual(schema_1[1].name, 'date_joined') | 1172 self.assertEqual(schema_1[1].name, 'date_joined') |
| 1099 | 1173 |
| 1100 raw_schema_2 = [ | 1174 raw_schema_2 = [ |
| 1101 { | 1175 { |
| 1102 "AttributeName": "username", | 1176 "AttributeName": "username", |
| 1103 "KeyType": "BTREE" | 1177 "KeyType": "BTREE" |
| 1104 }, | 1178 }, |
| 1105 ] | 1179 ] |
| 1180 raw_attributes_2 = [ |
| 1181 { |
| 1182 'AttributeName': 'username', |
| 1183 'AttributeType': 'S' |
| 1184 }, |
| 1185 ] |
| 1106 self.assertRaises( | 1186 self.assertRaises( |
| 1107 exceptions.UnknownSchemaFieldError, | 1187 exceptions.UnknownSchemaFieldError, |
| 1108 self.users._introspect_schema, | 1188 self.users._introspect_schema, |
| 1109 raw_schema_2 | 1189 raw_schema_2, |
| 1190 raw_attributes_2 |
| 1110 ) | 1191 ) |
| 1111 | 1192 |
| 1193 # Test a complex schema & ensure the types come back correctly. |
| 1194 raw_schema_3 = [ |
| 1195 { |
| 1196 "AttributeName": "user_id", |
| 1197 "KeyType": "HASH" |
| 1198 }, |
| 1199 { |
| 1200 "AttributeName": "junk", |
| 1201 "KeyType": "RANGE" |
| 1202 } |
| 1203 ] |
| 1204 raw_attributes_3 = [ |
| 1205 { |
| 1206 'AttributeName': 'user_id', |
| 1207 'AttributeType': 'N' |
| 1208 }, |
| 1209 { |
| 1210 'AttributeName': 'junk', |
| 1211 'AttributeType': 'B' |
| 1212 }, |
| 1213 ] |
| 1214 schema_3 = self.users._introspect_schema(raw_schema_3, raw_attributes_3) |
| 1215 self.assertEqual(len(schema_3), 2) |
| 1216 self.assertTrue(isinstance(schema_3[0], HashKey)) |
| 1217 self.assertEqual(schema_3[0].name, 'user_id') |
| 1218 self.assertEqual(schema_3[0].data_type, NUMBER) |
| 1219 self.assertTrue(isinstance(schema_3[1], RangeKey)) |
| 1220 self.assertEqual(schema_3[1].name, 'junk') |
| 1221 self.assertEqual(schema_3[1].data_type, BINARY) |
| 1222 |
| 1112 def test__introspect_indexes(self): | 1223 def test__introspect_indexes(self): |
| 1113 raw_indexes_1 = [ | 1224 raw_indexes_1 = [ |
| 1114 { | 1225 { |
| 1115 "IndexName": "MostRecentlyJoinedIndex", | 1226 "IndexName": "MostRecentlyJoinedIndex", |
| 1116 "KeySchema": [ | 1227 "KeySchema": [ |
| 1117 { | 1228 { |
| 1118 "AttributeName": "username", | 1229 "AttributeName": "username", |
| 1119 "KeyType": "HASH" | 1230 "KeyType": "HASH" |
| 1120 }, | 1231 }, |
| 1121 { | 1232 { |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1589 with mock.patch.object( | 1700 with mock.patch.object( |
| 1590 self.users, | 1701 self.users, |
| 1591 'get_item', | 1702 'get_item', |
| 1592 return_value=expected) as mock_get_item: | 1703 return_value=expected) as mock_get_item: |
| 1593 self.users.lookup('johndoe', 1366056668) | 1704 self.users.lookup('johndoe', 1366056668) |
| 1594 | 1705 |
| 1595 mock_get_item.assert_called_once_with( | 1706 mock_get_item.assert_called_once_with( |
| 1596 username= 'johndoe', | 1707 username= 'johndoe', |
| 1597 date_joined= 1366056668) | 1708 date_joined= 1366056668) |
| 1598 | 1709 |
| 1599 | |
| 1600 def test_put_item(self): | 1710 def test_put_item(self): |
| 1601 with mock.patch.object( | 1711 with mock.patch.object( |
| 1602 self.users.connection, | 1712 self.users.connection, |
| 1603 'put_item', | 1713 'put_item', |
| 1604 return_value={}) as mock_put_item: | 1714 return_value={}) as mock_put_item: |
| 1605 self.users.put_item(data={ | 1715 self.users.put_item(data={ |
| 1606 'username': 'johndoe', | 1716 'username': 'johndoe', |
| 1607 'last_name': 'Doe', | 1717 'last_name': 'Doe', |
| 1608 'date_joined': 12345, | 1718 'date_joined': 12345, |
| 1609 }) | 1719 }) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1657 return_value={}) as mock_delete_item: | 1767 return_value={}) as mock_delete_item: |
| 1658 self.assertTrue(self.users.delete_item(username='johndoe', date_join
ed=23456)) | 1768 self.assertTrue(self.users.delete_item(username='johndoe', date_join
ed=23456)) |
| 1659 | 1769 |
| 1660 mock_delete_item.assert_called_once_with('users', { | 1770 mock_delete_item.assert_called_once_with('users', { |
| 1661 'username': { | 1771 'username': { |
| 1662 'S': 'johndoe' | 1772 'S': 'johndoe' |
| 1663 }, | 1773 }, |
| 1664 'date_joined': { | 1774 'date_joined': { |
| 1665 'N': '23456' | 1775 'N': '23456' |
| 1666 } | 1776 } |
| 1667 }) | 1777 }, expected=None, conditional_operator=None) |
| 1778 |
| 1779 def test_delete_item_conditionally(self): |
| 1780 with mock.patch.object( |
| 1781 self.users.connection, |
| 1782 'delete_item', |
| 1783 return_value={}) as mock_delete_item: |
| 1784 self.assertTrue(self.users.delete_item(expected={'balance__eq': 0}, |
| 1785 username='johndoe', date_join
ed=23456)) |
| 1786 |
| 1787 mock_delete_item.assert_called_once_with('users', { |
| 1788 'username': { |
| 1789 'S': 'johndoe' |
| 1790 }, |
| 1791 'date_joined': { |
| 1792 'N': '23456' |
| 1793 } |
| 1794 }, |
| 1795 expected={ |
| 1796 'balance': { |
| 1797 'ComparisonOperator': 'EQ', 'AttributeValueList': [{'N': '0'}] |
| 1798 }, |
| 1799 }, |
| 1800 conditional_operator=None) |
| 1801 |
| 1802 def side_effect(*args, **kwargs): |
| 1803 raise exceptions.ConditionalCheckFailedException(400, '', {}) |
| 1804 |
| 1805 with mock.patch.object( |
| 1806 self.users.connection, |
| 1807 'delete_item', |
| 1808 side_effect=side_effect) as mock_delete_item: |
| 1809 self.assertFalse(self.users.delete_item(expected={'balance__eq': 0}, |
| 1810 username='johndoe', date_joi
ned=23456)) |
| 1668 | 1811 |
| 1669 def test_get_key_fields_no_schema_populated(self): | 1812 def test_get_key_fields_no_schema_populated(self): |
| 1670 expected = { | 1813 expected = { |
| 1671 "Table": { | 1814 "Table": { |
| 1672 "AttributeDefinitions": [ | 1815 "AttributeDefinitions": [ |
| 1673 { | 1816 { |
| 1674 "AttributeName": "username", | 1817 "AttributeName": "username", |
| 1675 "AttributeType": "S" | 1818 "AttributeType": "S" |
| 1676 }, | 1819 }, |
| 1677 { | 1820 { |
| (...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2079 reverse=True, | 2222 reverse=True, |
| 2080 username__between=['aaa', 'mmm'] | 2223 username__between=['aaa', 'mmm'] |
| 2081 ) | 2224 ) |
| 2082 usernames = [res['username'] for res in results['results']] | 2225 usernames = [res['username'] for res in results['results']] |
| 2083 self.assertEqual(usernames, ['johndoe', 'jane', 'alice', 'bob']) | 2226 self.assertEqual(usernames, ['johndoe', 'jane', 'alice', 'bob']) |
| 2084 self.assertEqual(len(results['results']), 4) | 2227 self.assertEqual(len(results['results']), 4) |
| 2085 self.assertEqual(results['last_key'], None) | 2228 self.assertEqual(results['last_key'], None) |
| 2086 | 2229 |
| 2087 mock_query.assert_called_once_with('users', | 2230 mock_query.assert_called_once_with('users', |
| 2088 consistent_read=False, | 2231 consistent_read=False, |
| 2089 scan_index_forward=True, | 2232 scan_index_forward=False, |
| 2090 index_name=None, | 2233 index_name=None, |
| 2091 attributes_to_get=None, | 2234 attributes_to_get=None, |
| 2092 limit=4, | 2235 limit=4, |
| 2093 key_conditions={ | 2236 key_conditions={ |
| 2094 'username': { | 2237 'username': { |
| 2095 'AttributeValueList': [{'S': 'aaa'}, {'S': 'mmm'}], | 2238 'AttributeValueList': [{'S': 'aaa'}, {'S': 'mmm'}], |
| 2096 'ComparisonOperator': 'BETWEEN', | 2239 'ComparisonOperator': 'BETWEEN', |
| 2097 } | 2240 } |
| 2098 }, | 2241 }, |
| 2099 select=None | 2242 select=None, |
| 2243 query_filter=None, |
| 2244 conditional_operator=None |
| 2100 ) | 2245 ) |
| 2101 | 2246 |
| 2102 # Now alter the expected. | 2247 # Now alter the expected. |
| 2103 expected['LastEvaluatedKey'] = { | 2248 expected['LastEvaluatedKey'] = { |
| 2104 'username': { | 2249 'username': { |
| 2105 'S': 'johndoe', | 2250 'S': 'johndoe', |
| 2106 }, | 2251 }, |
| 2107 } | 2252 } |
| 2108 | 2253 |
| 2109 with mock.patch.object( | 2254 with mock.patch.object( |
| 2110 self.users.connection, | 2255 self.users.connection, |
| 2111 'query', | 2256 'query', |
| 2112 return_value=expected) as mock_query_2: | 2257 return_value=expected) as mock_query_2: |
| 2113 results = self.users._query( | 2258 results = self.users._query( |
| 2114 limit=4, | 2259 limit=4, |
| 2115 reverse=True, | 2260 reverse=True, |
| 2116 username__between=['aaa', 'mmm'], | 2261 username__between=['aaa', 'mmm'], |
| 2117 exclusive_start_key={ | 2262 exclusive_start_key={ |
| 2118 'username': 'adam', | 2263 'username': 'adam', |
| 2119 }, | 2264 }, |
| 2120 consistent=True | 2265 consistent=True, |
| 2266 query_filter=None, |
| 2267 conditional_operator='AND' |
| 2121 ) | 2268 ) |
| 2122 usernames = [res['username'] for res in results['results']] | 2269 usernames = [res['username'] for res in results['results']] |
| 2123 self.assertEqual(usernames, ['johndoe', 'jane', 'alice', 'bob']) | 2270 self.assertEqual(usernames, ['johndoe', 'jane', 'alice', 'bob']) |
| 2124 self.assertEqual(len(results['results']), 4) | 2271 self.assertEqual(len(results['results']), 4) |
| 2125 self.assertEqual(results['last_key'], {'username': 'johndoe'}) | 2272 self.assertEqual(results['last_key'], {'username': 'johndoe'}) |
| 2126 | 2273 |
| 2127 mock_query_2.assert_called_once_with('users', | 2274 mock_query_2.assert_called_once_with('users', |
| 2128 key_conditions={ | 2275 key_conditions={ |
| 2129 'username': { | 2276 'username': { |
| 2130 'AttributeValueList': [{'S': 'aaa'}, {'S': 'mmm'}], | 2277 'AttributeValueList': [{'S': 'aaa'}, {'S': 'mmm'}], |
| 2131 'ComparisonOperator': 'BETWEEN', | 2278 'ComparisonOperator': 'BETWEEN', |
| 2132 } | 2279 } |
| 2133 }, | 2280 }, |
| 2134 index_name=None, | 2281 index_name=None, |
| 2135 attributes_to_get=None, | 2282 attributes_to_get=None, |
| 2136 scan_index_forward=True, | 2283 scan_index_forward=False, |
| 2137 limit=4, | 2284 limit=4, |
| 2138 exclusive_start_key={ | 2285 exclusive_start_key={ |
| 2139 'username': { | 2286 'username': { |
| 2140 'S': 'adam', | 2287 'S': 'adam', |
| 2141 }, | 2288 }, |
| 2142 }, | 2289 }, |
| 2143 consistent_read=True, | 2290 consistent_read=True, |
| 2144 select=None | 2291 select=None, |
| 2292 query_filter=None, |
| 2293 conditional_operator='AND' |
| 2145 ) | 2294 ) |
| 2146 | 2295 |
| 2147 def test_private_scan(self): | 2296 def test_private_scan(self): |
| 2148 expected = { | 2297 expected = { |
| 2149 "ConsumedCapacity": { | 2298 "ConsumedCapacity": { |
| 2150 "CapacityUnits": 0.5, | 2299 "CapacityUnits": 0.5, |
| 2151 "TableName": "users" | 2300 "TableName": "users" |
| 2152 }, | 2301 }, |
| 2153 "Count": 4, | 2302 "Count": 4, |
| 2154 "Items": [ | 2303 "Items": [ |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2196 mock_scan.assert_called_once_with('users', | 2345 mock_scan.assert_called_once_with('users', |
| 2197 scan_filter={ | 2346 scan_filter={ |
| 2198 'friend_count': { | 2347 'friend_count': { |
| 2199 'AttributeValueList': [{'N': '2'}], | 2348 'AttributeValueList': [{'N': '2'}], |
| 2200 'ComparisonOperator': 'LE', | 2349 'ComparisonOperator': 'LE', |
| 2201 } | 2350 } |
| 2202 }, | 2351 }, |
| 2203 limit=2, | 2352 limit=2, |
| 2204 segment=None, | 2353 segment=None, |
| 2205 attributes_to_get=None, | 2354 attributes_to_get=None, |
| 2206 total_segments=None | 2355 total_segments=None, |
| 2356 conditional_operator=None |
| 2207 ) | 2357 ) |
| 2208 | 2358 |
| 2209 # Now alter the expected. | 2359 # Now alter the expected. |
| 2210 expected['LastEvaluatedKey'] = { | 2360 expected['LastEvaluatedKey'] = { |
| 2211 'username': { | 2361 'username': { |
| 2212 'S': 'jane', | 2362 'S': 'jane', |
| 2213 }, | 2363 }, |
| 2214 } | 2364 } |
| 2215 | 2365 |
| 2216 with mock.patch.object( | 2366 with mock.patch.object( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 2239 } | 2389 } |
| 2240 }, | 2390 }, |
| 2241 limit=3, | 2391 limit=3, |
| 2242 exclusive_start_key={ | 2392 exclusive_start_key={ |
| 2243 'username': { | 2393 'username': { |
| 2244 'S': 'adam', | 2394 'S': 'adam', |
| 2245 }, | 2395 }, |
| 2246 }, | 2396 }, |
| 2247 segment=None, | 2397 segment=None, |
| 2248 attributes_to_get=None, | 2398 attributes_to_get=None, |
| 2249 total_segments=None | 2399 total_segments=None, |
| 2400 conditional_operator=None |
| 2250 ) | 2401 ) |
| 2251 | 2402 |
| 2252 def test_query(self): | 2403 def test_query(self): |
| 2253 items_1 = { | 2404 items_1 = { |
| 2254 'results': [ | 2405 'results': [ |
| 2255 Item(self.users, data={ | 2406 Item(self.users, data={ |
| 2256 'username': 'johndoe', | 2407 'username': 'johndoe', |
| 2257 'first_name': 'John', | 2408 'first_name': 'John', |
| 2258 'last_name': 'Doe', | 2409 'last_name': 'Doe', |
| 2259 }), | 2410 }), |
| 2260 Item(self.users, data={ | 2411 Item(self.users, data={ |
| 2261 'username': 'jane', | 2412 'username': 'jane', |
| 2262 'first_name': 'Jane', | 2413 'first_name': 'Jane', |
| 2263 'last_name': 'Doe', | 2414 'last_name': 'Doe', |
| 2264 }), | 2415 }), |
| 2265 ], | 2416 ], |
| 2266 'last_key': 'jane', | 2417 'last_key': 'jane', |
| 2267 } | 2418 } |
| 2268 | 2419 |
| 2269 results = self.users.query(last_name__eq='Doe') | 2420 results = self.users.query_2(last_name__eq='Doe') |
| 2270 self.assertTrue(isinstance(results, ResultSet)) | 2421 self.assertTrue(isinstance(results, ResultSet)) |
| 2271 self.assertEqual(len(results._results), 0) | 2422 self.assertEqual(len(results._results), 0) |
| 2272 self.assertEqual(results.the_callable, self.users._query) | 2423 self.assertEqual(results.the_callable, self.users._query) |
| 2273 | 2424 |
| 2274 with mock.patch.object( | 2425 with mock.patch.object( |
| 2275 results, | 2426 results, |
| 2276 'the_callable', | 2427 'the_callable', |
| 2277 return_value=items_1) as mock_query: | 2428 return_value=items_1) as mock_query: |
| 2278 res_1 = results.next() | 2429 res_1 = results.next() |
| 2279 # Now it should be populated. | 2430 # Now it should be populated. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2313 Item(self.users, data={ | 2464 Item(self.users, data={ |
| 2314 'username': 'johndoe', | 2465 'username': 'johndoe', |
| 2315 }), | 2466 }), |
| 2316 Item(self.users, data={ | 2467 Item(self.users, data={ |
| 2317 'username': 'jane', | 2468 'username': 'jane', |
| 2318 }), | 2469 }), |
| 2319 ], | 2470 ], |
| 2320 'last_key': 'jane', | 2471 'last_key': 'jane', |
| 2321 } | 2472 } |
| 2322 | 2473 |
| 2323 results = self.users.query(last_name__eq='Doe', | 2474 results = self.users.query_2(last_name__eq='Doe', |
| 2324 attributes=['username']) | 2475 attributes=['username']) |
| 2325 self.assertTrue(isinstance(results, ResultSet)) | 2476 self.assertTrue(isinstance(results, ResultSet)) |
| 2326 self.assertEqual(len(results._results), 0) | 2477 self.assertEqual(len(results._results), 0) |
| 2327 self.assertEqual(results.the_callable, self.users._query) | 2478 self.assertEqual(results.the_callable, self.users._query) |
| 2328 | 2479 |
| 2329 with mock.patch.object( | 2480 with mock.patch.object( |
| 2330 results, | 2481 results, |
| 2331 'the_callable', | 2482 'the_callable', |
| 2332 return_value=items_1) as mock_query: | 2483 return_value=items_1) as mock_query: |
| 2333 res_1 = results.next() | 2484 res_1 = results.next() |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2391 return_value=items_2) as mock_scan_2: | 2542 return_value=items_2) as mock_scan_2: |
| 2392 res_3 = results.next() | 2543 res_3 = results.next() |
| 2393 # New results should have been found. | 2544 # New results should have been found. |
| 2394 self.assertEqual(len(results._results), 1) | 2545 self.assertEqual(len(results._results), 1) |
| 2395 self.assertEqual(res_3['username'], 'zoeydoe') | 2546 self.assertEqual(res_3['username'], 'zoeydoe') |
| 2396 | 2547 |
| 2397 self.assertRaises(StopIteration, results.next) | 2548 self.assertRaises(StopIteration, results.next) |
| 2398 | 2549 |
| 2399 self.assertEqual(mock_scan_2.call_count, 1) | 2550 self.assertEqual(mock_scan_2.call_count, 1) |
| 2400 | 2551 |
| 2401 | |
| 2402 def test_scan_with_specific_attributes(self): | 2552 def test_scan_with_specific_attributes(self): |
| 2403 items_1 = { | 2553 items_1 = { |
| 2404 'results': [ | 2554 'results': [ |
| 2405 Item(self.users, data={ | 2555 Item(self.users, data={ |
| 2406 'username': 'johndoe', | 2556 'username': 'johndoe', |
| 2407 }), | 2557 }), |
| 2408 Item(self.users, data={ | 2558 Item(self.users, data={ |
| 2409 'username': 'jane', | 2559 'username': 'jane', |
| 2410 }), | 2560 }), |
| 2411 ], | 2561 ], |
| (...skipping 12 matching lines...) Expand all Loading... |
| 2424 res_1 = results.next() | 2574 res_1 = results.next() |
| 2425 # Now it should be populated. | 2575 # Now it should be populated. |
| 2426 self.assertEqual(len(results._results), 2) | 2576 self.assertEqual(len(results._results), 2) |
| 2427 self.assertEqual(res_1['username'], 'johndoe') | 2577 self.assertEqual(res_1['username'], 'johndoe') |
| 2428 self.assertEqual(res_1.keys(), ['username']) | 2578 self.assertEqual(res_1.keys(), ['username']) |
| 2429 res_2 = results.next() | 2579 res_2 = results.next() |
| 2430 self.assertEqual(res_2['username'], 'jane') | 2580 self.assertEqual(res_2['username'], 'jane') |
| 2431 | 2581 |
| 2432 self.assertEqual(mock_query.call_count, 1) | 2582 self.assertEqual(mock_query.call_count, 1) |
| 2433 | 2583 |
| 2434 | |
| 2435 def test_count(self): | 2584 def test_count(self): |
| 2436 expected = { | 2585 expected = { |
| 2437 "Table": { | 2586 "Table": { |
| 2438 "AttributeDefinitions": [ | 2587 "AttributeDefinitions": [ |
| 2439 { | 2588 { |
| 2440 "AttributeName": "username", | 2589 "AttributeName": "username", |
| 2441 "AttributeType": "S" | 2590 "AttributeType": "S" |
| 2442 } | 2591 } |
| 2443 ], | 2592 ], |
| 2444 "ItemCount": 5, | 2593 "ItemCount": 5, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 2470 "TableStatus": "ACTIVE" | 2619 "TableStatus": "ACTIVE" |
| 2471 } | 2620 } |
| 2472 } | 2621 } |
| 2473 | 2622 |
| 2474 with mock.patch.object( | 2623 with mock.patch.object( |
| 2475 self.users, | 2624 self.users, |
| 2476 'describe', | 2625 'describe', |
| 2477 return_value=expected) as mock_count: | 2626 return_value=expected) as mock_count: |
| 2478 self.assertEqual(self.users.count(), 5) | 2627 self.assertEqual(self.users.count(), 5) |
| 2479 | 2628 |
| 2629 def test_query_count_simple(self): |
| 2630 expected_0 = { |
| 2631 'Count': 0.0, |
| 2632 } |
| 2633 |
| 2634 expected_1 = { |
| 2635 'Count': 10.0, |
| 2636 } |
| 2637 |
| 2638 with mock.patch.object( |
| 2639 self.users.connection, |
| 2640 'query', |
| 2641 return_value=expected_0) as mock_query: |
| 2642 results = self.users.query_count(username__eq='notmyname') |
| 2643 self.assertTrue(isinstance(results, int)) |
| 2644 self.assertEqual(results, 0) |
| 2645 self.assertEqual(mock_query.call_count, 1) |
| 2646 self.assertIn('scan_index_forward', mock_query.call_args[1]) |
| 2647 self.assertEqual(True, mock_query.call_args[1]['scan_index_forward']) |
| 2648 self.assertIn('limit', mock_query.call_args[1]) |
| 2649 self.assertEqual(None, mock_query.call_args[1]['limit']) |
| 2650 |
| 2651 with mock.patch.object( |
| 2652 self.users.connection, |
| 2653 'query', |
| 2654 return_value=expected_1) as mock_query: |
| 2655 results = self.users.query_count(username__gt='somename', consistent
=True, scan_index_forward=False, limit=10) |
| 2656 self.assertTrue(isinstance(results, int)) |
| 2657 self.assertEqual(results, 10) |
| 2658 self.assertEqual(mock_query.call_count, 1) |
| 2659 self.assertIn('scan_index_forward', mock_query.call_args[1]) |
| 2660 self.assertEqual(False, mock_query.call_args[1]['scan_index_forward']) |
| 2661 self.assertIn('limit', mock_query.call_args[1]) |
| 2662 self.assertEqual(10, mock_query.call_args[1]['limit']) |
| 2663 |
| 2480 def test_private_batch_get(self): | 2664 def test_private_batch_get(self): |
| 2481 expected = { | 2665 expected = { |
| 2482 "ConsumedCapacity": { | 2666 "ConsumedCapacity": { |
| 2483 "CapacityUnits": 0.5, | 2667 "CapacityUnits": 0.5, |
| 2484 "TableName": "users" | 2668 "TableName": "users" |
| 2485 }, | 2669 }, |
| 2486 'Responses': { | 2670 'Responses': { |
| 2487 'users': [ | 2671 'users': [ |
| 2488 { | 2672 { |
| 2489 'username': {'S': 'alice'}, | 2673 'username': {'S': 'alice'}, |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2648 return_value=items_2) as mock_batch_get_2: | 2832 return_value=items_2) as mock_batch_get_2: |
| 2649 res_3 = results.next() | 2833 res_3 = results.next() |
| 2650 # New results should have been found. | 2834 # New results should have been found. |
| 2651 self.assertEqual(len(results._results), 1) | 2835 self.assertEqual(len(results._results), 1) |
| 2652 self.assertEqual(res_3['username'], 'zoeydoe') | 2836 self.assertEqual(res_3['username'], 'zoeydoe') |
| 2653 | 2837 |
| 2654 self.assertRaises(StopIteration, results.next) | 2838 self.assertRaises(StopIteration, results.next) |
| 2655 | 2839 |
| 2656 self.assertEqual(mock_batch_get_2.call_count, 1) | 2840 self.assertEqual(mock_batch_get_2.call_count, 1) |
| 2657 self.assertEqual(results._keys_left, []) | 2841 self.assertEqual(results._keys_left, []) |
| OLD | NEW |