1#!/usr/bin/env python
2# Copyright (c) 2012 Amazon.com, Inc. or its affiliates.  All Rights Reserved
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the
6# "Software"), to deal in the Software without restriction, including
7# without limitation the rights to use, copy, modify, merge, publish, dis-
8# tribute, sublicense, and/or sell copies of the Software, and to permit
9# persons to whom the Software is furnished to do so, subject to the fol-
10# lowing conditions:
11#
12# The above copyright notice and this permission notice shall be included
13# in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22#
23from tests.unit import unittest
24
25from boto.dynamodb.batch import Batch
26from boto.dynamodb.table import Table
27from boto.dynamodb.layer2 import Layer2
28from boto.dynamodb.batch import BatchList
29
30
31DESCRIBE_TABLE_1 = {
32    'Table': {
33        'CreationDateTime': 1349910554.478,
34        'ItemCount': 1,
35        'KeySchema': {'HashKeyElement': {'AttributeName': u'foo',
36                                         'AttributeType': u'S'}},
37        'ProvisionedThroughput': {'ReadCapacityUnits': 10,
38                                  'WriteCapacityUnits': 10},
39        'TableName': 'testtable',
40        'TableSizeBytes': 54,
41        'TableStatus': 'ACTIVE'}
42}
43
44DESCRIBE_TABLE_2 = {
45    'Table': {
46        'CreationDateTime': 1349910554.478,
47        'ItemCount': 1,
48        'KeySchema': {'HashKeyElement': {'AttributeName': u'baz',
49                                         'AttributeType': u'S'},
50                      'RangeKeyElement': {'AttributeName': 'myrange',
51                                          'AttributeType': 'N'}},
52        'ProvisionedThroughput': {'ReadCapacityUnits': 10,
53                                  'WriteCapacityUnits': 10},
54        'TableName': 'testtable2',
55        'TableSizeBytes': 54,
56        'TableStatus': 'ACTIVE'}
57}
58
59
60class TestBatchObjects(unittest.TestCase):
61    maxDiff = None
62
63    def setUp(self):
64        self.layer2 = Layer2('access_key', 'secret_key')
65        self.table = Table(self.layer2, DESCRIBE_TABLE_1)
66        self.table2 = Table(self.layer2, DESCRIBE_TABLE_2)
67
68    def test_batch_to_dict(self):
69        b = Batch(self.table, ['k1', 'k2'], attributes_to_get=['foo'],
70                  consistent_read=True)
71        self.assertDictEqual(
72            b.to_dict(),
73            {'AttributesToGet': ['foo'],
74             'Keys': [{'HashKeyElement': {'S': 'k1'}},
75                      {'HashKeyElement': {'S': 'k2'}}],
76             'ConsistentRead': True}
77        )
78
79    def test_batch_consistent_read_defaults_to_false(self):
80        b = Batch(self.table, ['k1'])
81        self.assertDictEqual(
82            b.to_dict(),
83            {'Keys': [{'HashKeyElement': {'S': 'k1'}}],
84             'ConsistentRead': False}
85        )
86
87    def test_batch_list_consistent_read(self):
88        b = BatchList(self.layer2)
89        b.add_batch(self.table, ['k1'], ['foo'], consistent_read=True)
90        b.add_batch(self.table2, [('k2', 54)], ['bar'], consistent_read=False)
91        self.assertDictEqual(
92            b.to_dict(),
93            {'testtable': {'AttributesToGet': ['foo'],
94                           'Keys': [{'HashKeyElement': {'S': 'k1'}}],
95                           'ConsistentRead': True},
96              'testtable2': {'AttributesToGet': ['bar'],
97                             'Keys': [{'HashKeyElement': {'S': 'k2'},
98                                       'RangeKeyElement': {'N': '54'}}],
99                             'ConsistentRead': False}})
100
101
102if __name__ == '__main__':
103    unittest.main()
104