File: test_auto_scale_async.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (147 lines) | stat: -rw-r--r-- 7,579 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# The MIT License (MIT)
# Copyright (c) 2022 Microsoft Corporation

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import unittest
from azure.cosmos.aio import CosmosClient
import azure.cosmos.exceptions as exceptions
from azure.cosmos import ThroughputProperties, PartitionKey, http_constants
import pytest
import test_config

pytestmark = pytest.mark.cosmosEmulator


@pytest.mark.usefixtures("teardown")
class AutoScaleTest(unittest.TestCase):
    host = test_config._test_config.host
    masterKey = test_config._test_config.masterKey
    connectionPolicy = test_config._test_config.connectionPolicy

    @classmethod
    async def setUpClass(cls):
        if (cls.masterKey == '[YOUR_KEY_HERE]' or
                cls.host == '[YOUR_ENDPOINT_HERE]'):
            raise Exception(
                "You must specify your Azure Cosmos account values for "
                "'masterKey' and 'host' at the top of this class to run the "
                "tests.")

        cls.client = CosmosClient(cls.host, cls.masterKey, consistency_level="Session",
                                  connection_policy=cls.connectionPolicy)
        cls.created_database = await cls.client.create_database_if_not_exists(test_config._test_config.TEST_DATABASE_ID)

    async def test_auto_scale(self):
        created_container = await self.created_database.create_container(
            id='container_with_auto_scale_settings',
            partition_key=PartitionKey(path="/id"),
            offer_throughput=ThroughputProperties(auto_scale_max_throughput=5000, auto_scale_increment_percent=0)

        )
        created_container_properties = await created_container.get_throughput()
        # Testing the input value of the max_throughput
        self.assertEqual(
            created_container_properties.auto_scale_max_throughput, 5000)
        self.assertEqual(created_container_properties.auto_scale_increment_percent, 0)
        self.assertEqual(created_container_properties.offer_throughput, None)

        await self.created_database.delete_container(created_container)

        # Testing the incorrect passing of an input value of the max_throughput to verify negative behavior
        with pytest.raises(exceptions.CosmosHttpResponseError) as e:
            created_container = await self.created_database.create_container(
                id='container_with_wrong_auto_scale_settings',
                partition_key=PartitionKey(path="/id"),
                offer_throughput=ThroughputProperties(auto_scale_max_throughput=-200, auto_scale_increment_percent=0))
        assert "Requested throughput -200 is less than required minimum throughput 1000" in str(e.value)

    async def test_create_container_if_not_exist(self):
        # Testing auto_scale_settings for the create_container_if_not_exists method
        created_container = await self.created_database.create_container_if_not_exists(
            id='container_with_auto_scale_settings',
            partition_key=PartitionKey(path="/id"),
            offer_throughput=ThroughputProperties(auto_scale_max_throughput=1000, auto_scale_increment_percent=0)
        )
        created_container_properties = await created_container.get_throughput()
        # Testing the incorrect input value of the max_throughput
        self.assertNotEqual(
            created_container_properties.auto_scale_max_throughput, 2000)

        await self.created_database.delete_container(created_container)

        created_container = await self.created_database.create_container_if_not_exists(
            id='container_with_auto_scale_settings',
            partition_key=PartitionKey(path="/id"),
            offer_throughput=ThroughputProperties(auto_scale_max_throughput=5000, auto_scale_increment_percent=2)

        )
        created_container_properties = await created_container.get_throughput()
        # Testing the incorrect input value of the max_increment_percentage
        self.assertNotEqual(
            created_container_properties.auto_scale_increment_percent, 3)

        await self.created_database.delete_container(created_container)

    async def test_create_database(self):
        # Testing auto_scale_settings for the create_database method
        created_database = await self.client.create_database("db1", offer_throughput=ThroughputProperties(
            auto_scale_max_throughput=5000,
            auto_scale_increment_percent=0))
        created_db_properties = await created_database.get_throughput()
        # Testing the input value of the max_throughput
        self.assertEqual(
            created_db_properties.auto_scale_max_throughput, 5000)
        # Testing the input value of the increment_percentage
        self.assertEqual(
            created_db_properties.auto_scale_increment_percent, 0)

        await self.client.delete_database("db1")

    async def test_create_database_if_not_exists(self):
        # Testing auto_scale_settings for the create_database_if_not_exists method
        created_database = await self.client.create_database_if_not_exists("db2", offer_throughput=ThroughputProperties(
            auto_scale_max_throughput=9000,
            auto_scale_increment_percent=11))
        created_db_properties = await created_database.get_throughput()
        # Testing the input value of the max_throughput
        self.assertNotEqual(
            created_db_properties.auto_scale_max_throughput, 8000)
        # Testing the input value of the increment_percentage
        self.assertEqual(
            created_db_properties.auto_scale_increment_percent, 11)

        await self.client.delete_database("db2")

    async def test_replace_throughput(self):
        created_container = await self.created_database.create_container(
            id='container_with_auto_scale_settings',
            partition_key=PartitionKey(path="/id"),
            offer_throughput=ThroughputProperties(auto_scale_max_throughput=5000, auto_scale_increment_percent=0)

        )
        new_throughput = await created_container.replace_throughput(
            throughput=ThroughputProperties(auto_scale_max_throughput=7000, auto_scale_increment_percent=20))
        created_container_properties = await created_container.get_throughput()
        # Testing the input value of the replaced auto_scale settings
        self.assertEqual(
            created_container_properties.auto_scale_max_throughput, 7000)
        self.assertEqual(
            created_container_properties.auto_scale_increment_percent, 20)

        await self.client.delete_database(test_config._test_config.TEST_DATABASE_ID)