Exemple #1
0
    def test_dynamo_model_different_attr_name(self):
        class Model(DynamoModel):
            _table_name_ = "diff_attr_table"
            hash_key = Key("actual_key_name")
            an_attr = Attr("actual_attr_name")

            def __eq__(self, other):
                if isinstance(other, Model):
                    return (self.hash_key == other.hash_key
                            and self.an_attr == other.an_attr)
                return NotImplemented

        with temporary_dynamodb_table(Model, "actual_key_name") as table:
            test_item = Model(
                hash_key="a hash key",
                an_attr=12345,
            )
            table.put_item(test_item)

            # Item is serialized with the defined attribute names
            boto_item = table._boto3_table.get_item(
                Key={"actual_key_name": "a hash key"}, )["Item"]
            self.assertEqual(
                boto_item,
                {
                    "actual_key_name": "a hash key",
                    "actual_attr_name": 12345,
                },
            )

            self.assertEqual(table["a hash key"], test_item)
Exemple #2
0
    def test_dynamodb_table_query_items_generator(self):
        class BigItem(DynamoModel):
            _table_name_ = "test_tab"
            pkey = Key()
            sortkey = Key()
            bigstring = Attr()

            def __eq__(self, other):
                if isinstance(other, BigItem):
                    return self.pkey == other.pkey and self.bigstring == other.bigstring
                return NotImplemented

        # Page size of DynamoDB item returns is 1MB. Add ~2.2MB of items
        add_items = [
            BigItem(pkey="a", sortkey="item " + str(i), bigstring="a" * 100000)
            for i in range(22)
        ]
        with temporary_dynamodb_table(BigItem, "pkey",
                                      sortkey_name="sortkey") as table:
            for item in add_items:
                table.put_item(item)

            result_items = list(table.query(BigItem.pkey == "a"))

        self.assertEqual(len(result_items), 22)
        self.assertCountEqual(result_items, add_items)
Exemple #3
0
    def setUp(self):
        self.table_without_sortkey = temporary_dynamodb_table(
            ModelWithoutSortkey,
            "hashkey",
        )

        self.table_with_sortkey = temporary_dynamodb_table(
            ModelWithSortkey,
            "hashkey",
            sortkey_name="sortkey",
            sortkey_type="N")

        self.table_with_str_sortkey = temporary_dynamodb_table(
            ModelWithSortkey,
            "hashkey",
            sortkey_name="sortkey",
            sortkey_type="S")
Exemple #4
0
    def test_query_and_filter(self):
        class Student(DynamoModel):
            _table_name_ = "Students"

            # Hash key
            name = Key()

            # Sort key
            year = Key()

            homeroom = Attr()

            def __eq__(self, other):
                if isinstance(other, Student):
                    return ((self.name == other.name)
                            & (self.year == other.year)
                            & (self.homeroom == other.homeroom))
                return NotImplemented

        with temporary_dynamodb_table(
                Student,
                "name",
                hashkey_type="S",
                sortkey_name="year",
                sortkey_type="N",
        ) as students_table:

            item1 = Student(name="Cecil", year=10, homeroom="Faba")
            item2 = Student(name="Cecil", year=11, homeroom="Aaa")
            item3 = Student(name="Cecil", year=12, homeroom="Faba")
            item4 = Student(name="Cloud", year=12, homeroom="Bbb")

            students_table.put_item(item1)
            students_table.put_item(item2)
            students_table.put_item(item3)
            students_table.put_item(item4)

            query_expression = (Student.name == "Cecil") & (Student.year > 10)
            filter_expression = Student.homeroom == "Faba"

            results = list(
                students_table.query(
                    query_expression=query_expression,
                    filter_expression=filter_expression,
                ))

            self.assertEqual(len(results), 1)
            (result, ) = results
            self.assertEqual(result, item3)
Exemple #5
0
    def test_dynamodb_list_tables_long(self):
        # Reduce page size so test doesn't have to make over 100 tables
        ddb_table._PAGE_SIZE = 5

        dynamodb = DynamoDB()

        with ExitStack() as stack:
            for i in range(11):

                class Cls(DynamoModel):
                    _table_name_ = "abc" + str(i)

                table_ctx = temporary_dynamodb_table(Cls, "def" + str(i))
                stack.enter_context(table_ctx)

            self.assertEqual(len(dynamodb.list_tables()), 11)
Exemple #6
0
    def test_dynamo_model_floats(self):
        temp_table = temporary_dynamodb_table(
            ModelWithoutSortkey,
            "hashkey",
            hashkey_type="N",
        )
        with temp_table as table:
            new_item = ModelWithoutSortkey(
                hashkey=9.99,
                another_attr=55.1,
            )
            table.put_item(new_item)

            retrieved_item = table[9.99]
            self.assertEqual(retrieved_item, new_item)
            self.assertIsInstance(retrieved_item.hashkey, float)
            self.assertIsInstance(retrieved_item.another_attr, float)
Exemple #7
0
    def test_dynamodb_indexes(self):
        class Student(DynamoModel):
            _table_name_ = "Students"

            # Hash key
            name = Key()

            # Sort key
            year = Key()

            homeroom = Attr()

        class ByHomeroomIndex(DynamoModel):
            _table_name_ = "Students"
            _index_name_ = "ByHomeroom"

            homeroom = Key()

            name = Key()

            year = Attr()

            def __eq__(self, other):
                if isinstance(other, ByHomeroomIndex):
                    return ((self.name == other.name)
                            & (self.homeroom == other.homeroom)
                            & (self.year == other.year))
                return NotImplemented

        with temporary_dynamodb_table(
                Student,
                "name",
                hashkey_type="S",
                sortkey_name="year",
                sortkey_type="N",
                extra_attributes=[
                    {
                        "AttributeName": "homeroom",
                        "AttributeType": "S",
                    },
                ],
                GlobalSecondaryIndexes=[{
                    "IndexName":
                    "ByHomeroom",
                    "KeySchema": [
                        {
                            "AttributeName": "homeroom",
                            "KeyType": "HASH",
                        },
                        {
                            "AttributeName": "name",
                            "KeyType": "RANGE",
                        },
                    ],
                    "Projection": {
                        "ProjectionType": "ALL",
                    },
                    "ProvisionedThroughput": {
                        "ReadCapacityUnits": 123,
                        "WriteCapacityUnits": 123,
                    },
                }],
        ) as students_table:
            item1 = Student(name="Cecil", year=10, homeroom="Faba")
            item2 = Student(name="Cecil", year=11, homeroom="Aaa")
            item3 = Student(name="Cloud", year=12, homeroom="Faba")
            item4 = Student(name="Aerith", year=12, homeroom="Faba")
            students_table.put_item(item1)
            students_table.put_item(item2)
            students_table.put_item(item3)
            students_table.put_item(item4)

            homeroom_index = DynamoDB()[ByHomeroomIndex]

            query_filter = (ByHomeroomIndex.homeroom
                            == "Faba") & (ByHomeroomIndex.name > "B")
            scan_filter = ByHomeroomIndex.year > 11

            items = list(homeroom_index.query(query_filter, scan_filter))
            self.assertEqual(len(items), 1)
            (item, ) = items

            expected = ByHomeroomIndex(name="Cloud", year=12, homeroom="Faba")
            self.assertEqual(item, expected)
Exemple #8
0
 def setUp(self):
     self.temp_table = temporary_dynamodb_table(MyModel, "pkey")