File: ru_per_min_tests.py

package info (click to toggle)
azure-cosmos-python 3.1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,280 kB
  • sloc: python: 11,653; makefile: 155
file content (143 lines) | stat: -rw-r--r-- 5,658 bytes parent folder | download | duplicates (2)
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
# The MIT License (MIT)
# Copyright (c) 2017 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
import uuid
import pytest
import azure.cosmos.documents as documents
import azure.cosmos.cosmos_client as cosmos_client
from azure.cosmos import query_iterable
import azure.cosmos.base as base
import test.test_config as test_config

# IMPORTANT NOTES:
  
#      Most test cases in this file create collections in your Azure Cosmos
#      account.
#      Collections are billing entities.  By running these test cases, you may
#      incur monetary costs on your account.
  
#      To Run the test, replace the two member fields (masterKey and host) with
#      values
#   associated with your Azure Cosmos account.

@pytest.mark.usefixtures("teardown")
class RuPerMinTests(unittest.TestCase):
    """RuPerMinTests Tests.
    """
    
    host = test_config._test_config.host
    masterKey = test_config._test_config.masterKey
    connectionPolicy = test_config._test_config.connectionPolicy
    client = cosmos_client.CosmosClient(host, {'masterKey': masterKey}, connectionPolicy)
    created_db = test_config._test_config.create_database_if_not_exist(client)

    @classmethod
    def setUpClass(cls):
        # creates the database, collection, and insert all the documents
        # we will gain some speed up in running the tests by creating the
        # database, collection and inserting all the docs only once

        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.")

    def _query_offers(self, collection_self_link):
        offers = list(self.client.ReadOffers())
        for o in offers:
            if o['resource'] == collection_self_link:
                return o
        return None

    def test_create_collection_with_ru_pm(self):
        # create an ru pm collection
        collection_definition = {
            'id' : "test_create_collection_with_ru_pm collection" + str(uuid.uuid4())
        }

        options = {
            'offerEnableRUPerMinuteThroughput': True,
            'offerVersion': "V2",
            'offerThroughput': 400
        }

        created_collection = self.client.CreateContainer(self.created_db['_self'], collection_definition, options)

        offer = self._query_offers(created_collection['_self'])
        self.assertIsNotNone(offer)
        self.assertEqual(offer['offerType'], "Invalid")
        self.assertIsNotNone(offer['content'])
        self.client.DeleteContainer(created_collection['_self'])

    def test_create_collection_without_ru_pm(self):        
        # create a non ru pm collection
        collection_definition = {
            'id' : "test_create_collection_without_ru_pm collection" + str(uuid.uuid4())
        }

        options = {
            'offerEnableRUPerMinuteThroughput': False,
            'offerVersion': "V2",
            'offerThroughput': 400
        }

        created_collection = self.client.CreateContainer(self.created_db['_self'], collection_definition, options)

        offer = self._query_offers(created_collection['_self'])
        self.assertIsNotNone(offer)
        self.assertEqual(offer['offerType'], "Invalid")
        self.assertIsNotNone(offer['content'])
        self.client.DeleteContainer(created_collection['_self'])

    def test_create_collection_disable_ru_pm_on_request(self):        
        # create a non ru pm collection
        collection_definition = {
            'id' : "test_create_collection_disable_ru_pm_on_request collection" + str(uuid.uuid4())
        }

        options = {
            'offerVersion': "V2",
            'offerThroughput': 400
        }

        created_collection = self.client.CreateContainer(self.created_db['_self'], collection_definition, options)

        offer = self._query_offers(created_collection['_self'])
        self.assertIsNotNone(offer)
        self.assertEqual(offer['offerType'], "Invalid")
        self.assertIsNotNone(offer['content'])
        self.assertEqual(offer['content']['offerIsRUPerMinuteThroughputEnabled'], False)

        request_options = {
            'disableRUPerMinuteUsage': True
        }

        doc = {
            'id' : 'test_doc' + str(uuid.uuid4())
        }
        self.client.CreateItem(created_collection['_self'], doc, request_options)
        self.client.DeleteContainer(created_collection['_self'])

if __name__ == "__main__":

    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()