Skip to content

Commit 5f80209

Browse files
[Tables] test cleanup (#18676)
* moving helper methods to base class, reducing code * more duplicates * consolidating teardown method, new recordings to keep everything green * service properties files * insert and create query table methods * set up for entity tests * finished all refactor * reducing refactored code * calling all methods _set_up * more cleanup and sleeps for cosmos tests * corrections * removing prints * undoing all my changes to recording files now * adding _ to get_table_reference * removed commented out code
1 parent c9ebd98 commit 5f80209

24 files changed

+684
-2455
lines changed
Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1-
21
# coding: utf-8
32
# -------------------------------------------------------------------------
43
# Copyright (c) Microsoft Corporation. All rights reserved.
54
# Licensed under the MIT License. See License.txt in the project root for
65
# license information.
76
# --------------------------------------------------------------------------
7+
from __future__ import division
8+
from datetime import datetime
9+
from dateutil.tz import tzutc
10+
import uuid
11+
812
from azure.core.credentials import AccessToken
13+
from azure.core.exceptions import ResourceExistsError
14+
from azure.data.tables import (
15+
EntityProperty,
16+
EdmType,
17+
TableServiceClient,
18+
)
19+
20+
from devtools_testutils import is_live
21+
22+
from .testcase import TableTestCase, SLEEP_DELAY
23+
24+
25+
TEST_TABLE_PREFIX = "pytableasync"
926

10-
from .testcase import TableTestCase
1127

1228
class AsyncFakeTokenCredential(object):
1329
"""Protocol for classes able to provide OAuth tokens.
1430
:param str scopes: Lets you specify the type of access needed.
1531
"""
32+
1633
def __init__(self):
1734
self.token = AccessToken("YOU SHALL NOT PASS", 0)
1835

@@ -21,6 +38,95 @@ async def get_token(self, *args):
2138

2239

2340
class AsyncTableTestCase(TableTestCase):
24-
2541
def generate_fake_token(self):
2642
return AsyncFakeTokenCredential()
43+
44+
def _get_table_reference(self, prefix=TEST_TABLE_PREFIX):
45+
table_name = self.get_resource_name(prefix)
46+
return table_name
47+
48+
async def _create_table(self, ts, prefix=TEST_TABLE_PREFIX, table_list=None):
49+
table_name = self._get_table_reference(prefix)
50+
try:
51+
table = await ts.create_table(table_name)
52+
if table_list is not None:
53+
table_list.append(table)
54+
except ResourceExistsError:
55+
table = ts.get_table_client(table_name)
56+
return table
57+
58+
async def _delete_all_tables(self, account_name, key):
59+
client = TableServiceClient(self.account_url(account_name, "cosmos"), key)
60+
async for table in client.list_tables():
61+
await client.delete_table(table.name)
62+
63+
if self.is_live:
64+
self.sleep(10)
65+
66+
async def _tear_down(self):
67+
if is_live():
68+
async for table in self.ts.list_tables():
69+
await self.ts.delete_table(table.name)
70+
if self.ts._cosmos_endpoint:
71+
self.sleep(SLEEP_DELAY)
72+
self.test_tables = []
73+
await self.ts.close()
74+
75+
async def _create_query_table(self, entity_count):
76+
"""
77+
Creates a table with the specified name and adds entities with the
78+
default set of values. PartitionKey is set to 'MyPartition' and RowKey
79+
is set to a unique counter value starting at 1 (as a string).
80+
"""
81+
table_name = self.get_resource_name("querytable")
82+
table = await self.ts.create_table(table_name)
83+
self.query_tables.append(table_name)
84+
client = self.ts.get_table_client(table_name)
85+
entity = self._create_random_entity_dict()
86+
for i in range(1, entity_count + 1):
87+
entity["RowKey"] = entity["RowKey"] + str(i)
88+
await client.create_entity(entity=entity)
89+
return client
90+
91+
async def _insert_two_opposite_entities(self, pk=None, rk=None):
92+
entity1 = self._create_random_entity_dict()
93+
resp = await self.table.create_entity(entity1)
94+
95+
partition, row = self._create_pk_rk(pk, rk)
96+
properties = {
97+
"PartitionKey": partition + u"1",
98+
"RowKey": row + u"1",
99+
"age": 49,
100+
"sex": u"female",
101+
"married": False,
102+
"deceased": True,
103+
"optional": None,
104+
"ratio": 5.2,
105+
"evenratio": 6.0,
106+
"large": 39999011,
107+
"Birthday": datetime(1993, 4, 1, tzinfo=tzutc()),
108+
"birthday": datetime(1990, 4, 1, tzinfo=tzutc()),
109+
"binary": b"binary-binary",
110+
"other": EntityProperty(40, EdmType.INT32),
111+
"clsid": uuid.UUID("c8da6455-213e-42d9-9b79-3f9149a57833"),
112+
}
113+
await self.table.create_entity(properties)
114+
return entity1, resp
115+
116+
async def _insert_random_entity(self, pk=None, rk=None):
117+
entity = self._create_random_entity_dict(pk, rk)
118+
metadata = await self.table.create_entity(entity=entity)
119+
return entity, metadata["etag"]
120+
121+
async def _set_up(self, account_name, account_key, url="table"):
122+
account_url = self.account_url(account_name, url)
123+
self.ts = TableServiceClient(account_url, account_key)
124+
self.table_name = self.get_resource_name("uttable")
125+
self.table = self.ts.get_table_client(self.table_name)
126+
if self.is_live:
127+
try:
128+
await self.ts.create_table(table_name=self.table_name)
129+
except ResourceExistsError:
130+
pass
131+
132+
self.query_tables = []

0 commit comments

Comments
 (0)