Spaces:
Build error
Build error
File size: 12,707 Bytes
0827183 |
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
Base tests that all storage providers should implement in their own tests.
They handle the storage-based assertions, internally.
All tests return true if assertions pass to indicate that the code ran to completion, passing internal assertions.
Therefore, all tests using theses static tests should strictly check that the method returns true.
Note: Python cannot have dicts with properties with a None value like other SDKs can have properties with null values.
Because of this, StoreItem tests have "e_tag: *" where the tests in the other SDKs do not.
This has also caused us to comment out some parts of these tests where we assert that "e_tag"
is None for the same reason. A null e_tag should work just like a * e_tag when writing,
as far as the storage adapters are concerened, so this shouldn't cause issues.
:Example:
async def test_handle_null_keys_when_reading(self):
await reset()
test_ran = await StorageBaseTests.handle_null_keys_when_reading(get_storage())
assert test_ran
"""
import pytest
from botbuilder.azure import CosmosDbStorage
from botbuilder.core import (
ConversationState,
TurnContext,
MessageFactory,
MemoryStorage,
)
from botbuilder.core.adapters import TestAdapter
from botbuilder.dialogs import (
DialogSet,
DialogTurnStatus,
TextPrompt,
PromptValidatorContext,
WaterfallStepContext,
Dialog,
WaterfallDialog,
PromptOptions,
)
class StorageBaseTests:
# pylint: disable=pointless-string-statement
@staticmethod
async def return_empty_object_when_reading_unknown_key(storage) -> bool:
result = await storage.read(["unknown"])
assert result is not None
assert len(result) == 0
return True
@staticmethod
async def handle_null_keys_when_reading(storage) -> bool:
if isinstance(storage, (CosmosDbStorage, MemoryStorage)):
result = await storage.read(None)
assert len(result.keys()) == 0
# Catch-all
else:
with pytest.raises(Exception) as err:
await storage.read(None)
assert err.value.args[0] == "Keys are required when reading"
return True
@staticmethod
async def handle_null_keys_when_writing(storage) -> bool:
with pytest.raises(Exception) as err:
await storage.write(None)
assert err.value.args[0] == "Changes are required when writing"
return True
@staticmethod
async def does_not_raise_when_writing_no_items(storage) -> bool:
# noinspection PyBroadException
try:
await storage.write([])
except:
pytest.fail("Should not raise")
return True
@staticmethod
async def create_object(storage) -> bool:
store_items = {
"createPoco": {"id": 1},
"createPocoStoreItem": {"id": 2, "e_tag": "*"},
}
await storage.write(store_items)
read_store_items = await storage.read(store_items.keys())
assert store_items["createPoco"]["id"] == read_store_items["createPoco"]["id"]
assert (
store_items["createPocoStoreItem"]["id"]
== read_store_items["createPocoStoreItem"]["id"]
)
# If decided to validate e_tag integrity again, uncomment this code
# assert read_store_items["createPoco"]["e_tag"] is not None
assert read_store_items["createPocoStoreItem"]["e_tag"] is not None
return True
@staticmethod
async def handle_crazy_keys(storage) -> bool:
key = '!@#$%^&*()_+??><":QASD~`'
store_item = {"id": 1}
store_items = {key: store_item}
await storage.write(store_items)
read_store_items = await storage.read(store_items.keys())
assert read_store_items[key] is not None
assert read_store_items[key]["id"] == 1
return True
@staticmethod
async def update_object(storage) -> bool:
original_store_items = {
"pocoItem": {"id": 1, "count": 1},
"pocoStoreItem": {"id": 1, "count": 1, "e_tag": "*"},
}
# 1st write should work
await storage.write(original_store_items)
loaded_store_items = await storage.read(["pocoItem", "pocoStoreItem"])
update_poco_item = loaded_store_items["pocoItem"]
update_poco_item["e_tag"] = None
update_poco_store_item = loaded_store_items["pocoStoreItem"]
assert update_poco_store_item["e_tag"] is not None
# 2nd write should work
update_poco_item["count"] += 1
update_poco_store_item["count"] += 1
await storage.write(loaded_store_items)
reloaded_store_items = await storage.read(loaded_store_items.keys())
reloaded_update_poco_item = reloaded_store_items["pocoItem"]
reloaded_update_poco_store_item = reloaded_store_items["pocoStoreItem"]
assert reloaded_update_poco_item["count"] == 2
assert reloaded_update_poco_store_item["count"] == 2
# Write with old e_tag should succeed for non-storeItem
update_poco_item["count"] = 123
await storage.write({"pocoItem": update_poco_item})
# Write with old eTag should FAIL for storeItem
update_poco_store_item["count"] = 123
"""
This assert exists in the other SDKs but can't in python, currently
due to using "e_tag: *" above (see comment near the top of this file for details).
with pytest.raises(Exception) as err:
await storage.write({"pocoStoreItem": update_poco_store_item})
assert err.value is not None
"""
reloaded_store_items2 = await storage.read(["pocoItem", "pocoStoreItem"])
reloaded_poco_item2 = reloaded_store_items2["pocoItem"]
reloaded_poco_item2["e_tag"] = None
reloaded_poco_store_item2 = reloaded_store_items2["pocoStoreItem"]
assert reloaded_poco_item2["count"] == 123
assert reloaded_poco_store_item2["count"] == 2
# write with wildcard etag should work
reloaded_poco_item2["count"] = 100
reloaded_poco_store_item2["count"] = 100
reloaded_poco_store_item2["e_tag"] = "*"
wildcard_etag_dict = {
"pocoItem": reloaded_poco_item2,
"pocoStoreItem": reloaded_poco_store_item2,
}
await storage.write(wildcard_etag_dict)
reloaded_store_items3 = await storage.read(["pocoItem", "pocoStoreItem"])
assert reloaded_store_items3["pocoItem"]["count"] == 100
assert reloaded_store_items3["pocoStoreItem"]["count"] == 100
# Write with empty etag should not work
reloaded_store_items4 = await storage.read(["pocoStoreItem"])
reloaded_store_item4 = reloaded_store_items4["pocoStoreItem"]
assert reloaded_store_item4 is not None
reloaded_store_item4["e_tag"] = ""
dict2 = {"pocoStoreItem": reloaded_store_item4}
with pytest.raises(Exception) as err:
await storage.write(dict2)
assert err.value is not None
final_store_items = await storage.read(["pocoItem", "pocoStoreItem"])
assert final_store_items["pocoItem"]["count"] == 100
assert final_store_items["pocoStoreItem"]["count"] == 100
return True
@staticmethod
async def delete_object(storage) -> bool:
store_items = {"delete1": {"id": 1, "count": 1, "e_tag": "*"}}
await storage.write(store_items)
read_store_items = await storage.read(["delete1"])
assert read_store_items["delete1"]["e_tag"]
assert read_store_items["delete1"]["count"] == 1
await storage.delete(["delete1"])
reloaded_store_items = await storage.read(["delete1"])
assert reloaded_store_items.get("delete1", None) is None
return True
@staticmethod
async def delete_unknown_object(storage) -> bool:
# noinspection PyBroadException
try:
await storage.delete(["unknown_key"])
except:
pytest.fail("Should not raise")
return True
@staticmethod
async def perform_batch_operations(storage) -> bool:
await storage.write(
{
"batch1": {"count": 10},
"batch2": {"count": 20},
"batch3": {"count": 30},
}
)
result = await storage.read(["batch1", "batch2", "batch3"])
assert result.get("batch1", None) is not None
assert result.get("batch2", None) is not None
assert result.get("batch3", None) is not None
assert result["batch1"]["count"] == 10
assert result["batch2"]["count"] == 20
assert result["batch3"]["count"] == 30
"""
If decided to validate e_tag integrity aagain, uncomment this code
assert result["batch1"].get("e_tag", None) is not None
assert result["batch2"].get("e_tag", None) is not None
assert result["batch3"].get("e_tag", None) is not None
"""
await storage.delete(["batch1", "batch2", "batch3"])
result = await storage.read(["batch1", "batch2", "batch3"])
assert result.get("batch1", None) is None
assert result.get("batch2", None) is None
assert result.get("batch3", None) is None
return True
@staticmethod
async def proceeds_through_waterfall(storage) -> bool:
convo_state = ConversationState(storage)
dialog_state = convo_state.create_property("dialogState")
dialogs = DialogSet(dialog_state)
async def exec_test(turn_context: TurnContext) -> None:
dialog_context = await dialogs.create_context(turn_context)
await dialog_context.continue_dialog()
if not turn_context.responded:
await dialog_context.begin_dialog(WaterfallDialog.__name__)
await convo_state.save_changes(turn_context)
adapter = TestAdapter(exec_test)
async def prompt_validator(prompt_context: PromptValidatorContext):
result = prompt_context.recognized.value
if len(result) > 3:
succeeded_message = MessageFactory.text(
f"You got it at the {prompt_context.options.number_of_attempts}rd try!"
)
await prompt_context.context.send_activity(succeeded_message)
return True
reply = MessageFactory.text(
f"Please send a name that is longer than 3 characters. {prompt_context.options.number_of_attempts}"
)
await prompt_context.context.send_activity(reply)
return False
async def step_1(step_context: WaterfallStepContext) -> DialogTurnStatus:
assert isinstance(step_context.active_dialog.state["stepIndex"], int)
await step_context.context.send_activity("step1")
return Dialog.end_of_turn
async def step_2(step_context: WaterfallStepContext) -> None:
assert isinstance(step_context.active_dialog.state["stepIndex"], int)
await step_context.prompt(
TextPrompt.__name__,
PromptOptions(prompt=MessageFactory.text("Please type your name")),
)
async def step_3(step_context: WaterfallStepContext) -> DialogTurnStatus:
assert isinstance(step_context.active_dialog.state["stepIndex"], int)
await step_context.context.send_activity("step3")
return Dialog.end_of_turn
steps = [step_1, step_2, step_3]
dialogs.add(WaterfallDialog(WaterfallDialog.__name__, steps))
dialogs.add(TextPrompt(TextPrompt.__name__, prompt_validator))
step1 = await adapter.send("hello")
step2 = await step1.assert_reply("step1")
step3 = await step2.send("hello")
step4 = await step3.assert_reply("Please type your name") # None
step5 = await step4.send("hi")
step6 = await step5.assert_reply(
"Please send a name that is longer than 3 characters. 0"
)
step7 = await step6.send("hi")
step8 = await step7.assert_reply(
"Please send a name that is longer than 3 characters. 1"
)
step9 = await step8.send("hi")
step10 = await step9.assert_reply(
"Please send a name that is longer than 3 characters. 2"
)
step11 = await step10.send("Kyle")
step12 = await step11.assert_reply("You got it at the 3rd try!")
await step12.assert_reply("step3")
return True
|