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#
23
24from tests.unit import unittest
25from mock import Mock
26
27from boto.dynamodb.layer2 import Layer2
28from boto.dynamodb.table import Table, Schema
29
30
31DESCRIBE_TABLE = {
32    "Table": {
33        "CreationDateTime": 1.353526122785E9, "ItemCount": 1,
34        "KeySchema": {
35            "HashKeyElement": {"AttributeName": "foo", "AttributeType": "N"}},
36        "ProvisionedThroughput": {
37            "NumberOfDecreasesToday": 0,
38            "ReadCapacityUnits": 5,
39            "WriteCapacityUnits": 5},
40        "TableName": "footest",
41        "TableSizeBytes": 21,
42        "TableStatus": "ACTIVE"}
43}
44
45
46class TestTableConstruction(unittest.TestCase):
47    def setUp(self):
48        self.layer2 = Layer2('access_key', 'secret_key')
49        self.api = Mock()
50        self.layer2.layer1 = self.api
51
52    def test_get_table(self):
53        self.api.describe_table.return_value = DESCRIBE_TABLE
54        table = self.layer2.get_table('footest')
55        self.assertEqual(table.name, 'footest')
56        self.assertEqual(table.create_time, 1353526122.785)
57        self.assertEqual(table.status, 'ACTIVE')
58        self.assertEqual(table.item_count, 1)
59        self.assertEqual(table.size_bytes, 21)
60        self.assertEqual(table.read_units, 5)
61        self.assertEqual(table.write_units, 5)
62        self.assertEqual(table.schema, Schema.create(hash_key=('foo', 'N')))
63
64    def test_create_table_without_api_call(self):
65        table = self.layer2.table_from_schema(
66            name='footest',
67            schema=Schema.create(hash_key=('foo', 'N')))
68        self.assertEqual(table.name, 'footest')
69        self.assertEqual(table.schema, Schema.create(hash_key=('foo', 'N')))
70        # describe_table is never called.
71        self.assertEqual(self.api.describe_table.call_count, 0)
72
73    def test_create_schema_with_hash_and_range(self):
74        schema = self.layer2.create_schema('foo', int, 'bar', str)
75        self.assertEqual(schema.hash_key_name, 'foo')
76        self.assertEqual(schema.hash_key_type, 'N')
77        self.assertEqual(schema.range_key_name, 'bar')
78        self.assertEqual(schema.range_key_type, 'S')
79
80    def test_create_schema_with_hash(self):
81        schema = self.layer2.create_schema('foo', str)
82        self.assertEqual(schema.hash_key_name, 'foo')
83        self.assertEqual(schema.hash_key_type, 'S')
84        self.assertIsNone(schema.range_key_name)
85        self.assertIsNone(schema.range_key_type)
86
87
88class TestSchemaEquality(unittest.TestCase):
89    def test_schema_equal(self):
90        s1 = Schema.create(hash_key=('foo', 'N'))
91        s2 = Schema.create(hash_key=('foo', 'N'))
92        self.assertEqual(s1, s2)
93
94    def test_schema_not_equal(self):
95        s1 = Schema.create(hash_key=('foo', 'N'))
96        s2 = Schema.create(hash_key=('bar', 'N'))
97        s3 = Schema.create(hash_key=('foo', 'S'))
98        self.assertNotEqual(s1, s2)
99        self.assertNotEqual(s1, s3)
100
101    def test_equal_with_hash_and_range(self):
102        s1 = Schema.create(hash_key=('foo', 'N'), range_key=('bar', 'S'))
103        s2 = Schema.create(hash_key=('foo', 'N'), range_key=('bar', 'S'))
104        self.assertEqual(s1, s2)
105
106    def test_schema_with_hash_and_range_not_equal(self):
107        s1 = Schema.create(hash_key=('foo', 'N'), range_key=('bar', 'S'))
108        s2 = Schema.create(hash_key=('foo', 'N'), range_key=('bar', 'N'))
109        s3 = Schema.create(hash_key=('foo', 'S'), range_key=('baz', 'N'))
110        s4 = Schema.create(hash_key=('bar', 'N'), range_key=('baz', 'N'))
111        self.assertNotEqual(s1, s2)
112        self.assertNotEqual(s1, s3)
113        self.assertNotEqual(s1, s4)
114        self.assertNotEqual(s2, s4)
115        self.assertNotEqual(s3, s4)
116
117
118if __name__ == '__main__':
119    unittest.main()
120