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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest
from devtools_testutils import AzureRecordedTestCase
from devtools_testutils.aio import recorded_by_proxy_async
from azure.core.exceptions import ResourceExistsError, HttpResponseError
from azure.data.tables.aio import TableServiceClient, TableClient
from _shared.asynctestcase import AsyncTableTestCase
from async_preparers import cosmos_decorator_async
class TestTableCosmosAsync(AzureRecordedTestCase, AsyncTableTestCase):
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_create_table(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
# Arrange
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
table_name = self._get_table_reference()
# Act
table = ts.get_table_client(table_name)
created = await table.create_table()
# Assert
assert created.name == table_name
await ts.delete_table(table_name=table_name)
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_create_table_fail_on_exist(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
# Arrange
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
table_name = self._get_table_reference()
# Act
created = await ts.create_table(table_name=table_name)
with pytest.raises(ResourceExistsError):
await ts.create_table(table_name=table_name)
# Assert
assert created
await ts.delete_table(table_name=table_name)
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_query_tables_per_page(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
# Arrange
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
table_name = "myasynctable"
for i in range(5):
await ts.create_table(table_name + str(i))
query_filter = "TableName eq 'myasynctable0' or TableName eq 'myasynctable1' or TableName eq 'myasynctable2'"
table_count = 0
page_count = 0
async for table_page in ts.query_tables(query_filter, results_per_page=2).by_page():
temp_count = 0
async for table in table_page:
temp_count += 1
assert temp_count <= 2
page_count += 1
table_count += temp_count
assert page_count == 2
assert table_count == 3
await self._delete_all_tables(tables_cosmos_account_name, tables_primary_cosmos_account_key)
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_list_tables(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
# Arrange
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
table = await self._create_table(ts)
# Act
tables = []
async for t in ts.list_tables():
tables.append(t)
# Assert
assert tables is not None
assert len(tables) >= 1
assert tables[0] is not None
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_query_tables_with_filter(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
# Arrange
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
table = await self._create_table(ts)
# Act
name_filter = "TableName eq '{}'".format(table.table_name)
tables = []
async for t in ts.query_tables(name_filter):
tables.append(t)
# Assert
assert tables is not None
assert len(tables) == 1
await ts.delete_table(table.table_name)
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_list_tables_with_num_results(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
# Arrange
await self._delete_all_tables(tables_cosmos_account_name, tables_primary_cosmos_account_key)
prefix = "listtable"
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
table_list = []
for i in range(0, 4):
await self._create_table(ts, prefix + str(i), table_list)
# Act
all_tables = 0
async for t in ts.list_tables():
all_tables += 1
small_page = 0
async for page in ts.list_tables(results_per_page=3).by_page():
page_size = 0
async for table in page:
page_size += 1
assert page_size <= 3
small_page += 1
assert small_page == 2
assert all_tables == 4
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_list_tables_with_marker(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
# Arrange
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
prefix = "listtable"
table_names = []
for i in range(0, 4):
await self._create_table(ts, prefix + str(i), table_names)
# Act
generator1 = ts.list_tables(results_per_page=2).by_page()
await generator1.__anext__()
generator2 = ts.list_tables(results_per_page=2).by_page(continuation_token=generator1.continuation_token)
await generator2.__anext__()
tables1 = generator1._current_page
tables2 = generator2._current_page
tables1_len = 0
async for _ in tables1:
tables1_len += 1
tables2_len = 0
async for _ in tables2:
tables2_len += 1
# Assert
assert tables1_len == 2
assert tables2_len == 2
assert tables1 != tables2
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_delete_table_with_existing_table(
self, tables_cosmos_account_name, tables_primary_cosmos_account_key
):
# Arrange
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
table = await self._create_table(ts)
# Act
deleted = await ts.delete_table(table_name=table.table_name)
# Assert
assert deleted is None
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_delete_table_with_non_existing_table_fail_not_exist(
self, tables_cosmos_account_name, tables_primary_cosmos_account_key
):
# Arrange
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
table_name = self._get_table_reference()
await ts.delete_table(table_name)
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_create_table_underscore_name(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
# Arrange
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
table_name = "my_table"
client = await ts.create_table(table_name)
assert client.table_name == table_name
await ts.delete_table(table_name)
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_create_table_unicode_name(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
# Arrange
ts = TableServiceClient(
self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key
)
table_name = "啊齄丂狛狜"
client = await ts.create_table(table_name)
assert client.table_name == table_name
await ts.delete_table(table_name)
@cosmos_decorator_async
@recorded_by_proxy_async
async def test_client_with_url_ends_with_table_name(
self, tables_cosmos_account_name, tables_primary_cosmos_account_key
):
url = self.account_url(tables_cosmos_account_name, "cosmos")
table_name = self.get_resource_name("mytable")
invalid_url = url + "/" + table_name
# test table client has the same table name as in url
tc = TableClient(invalid_url, table_name, credential=tables_primary_cosmos_account_key)
with pytest.raises(HttpResponseError) as exc:
await tc.create_table()
assert ("Request url is invalid") in str(exc.value)
assert ("Please check your account URL.") in str(exc.value)
# test table client has a different table name as in url
table_name2 = self.get_resource_name("mytable2")
tc2 = TableClient(invalid_url, table_name2, credential=tables_primary_cosmos_account_key)
with pytest.raises(HttpResponseError) as exc:
await tc2.create_table()
assert ("Request url is invalid") in str(exc.value)
assert ("Please check your account URL.") in str(exc.value)
valid_tc = TableClient(url, table_name, credential=tables_primary_cosmos_account_key)
await valid_tc.create_table()
# test creating a table when it already exists
with pytest.raises(HttpResponseError) as exc:
await tc.create_table()
assert ("Request url is invalid") in str(exc.value)
assert ("Please check your account URL.") in str(exc.value)
# test deleting a table when it already exists
with pytest.raises(HttpResponseError) as exc:
await tc.delete_table()
assert ("Request url is invalid") in str(exc.value)
assert ("Please check your account URL.") in str(exc.value)
await valid_tc.delete_table()
await valid_tc.close()
await tc.close()
await tc2.close()
|