|
4 | 4 | import pytest
|
5 | 5 | from packaging import version
|
6 | 6 |
|
7 |
| -from arangoasync.errno import FORBIDDEN, QUERY_PARSE |
| 7 | +from arangoasync.errno import ( |
| 8 | + FORBIDDEN, |
| 9 | + QUERY_FUNCTION_INVALID_CODE, |
| 10 | + QUERY_FUNCTION_NOT_FOUND, |
| 11 | + QUERY_PARSE, |
| 12 | +) |
8 | 13 | from arangoasync.exceptions import (
|
9 | 14 | AQLCacheClearError,
|
10 | 15 | AQLCacheConfigureError,
|
11 | 16 | AQLCacheEntriesError,
|
12 | 17 | AQLCachePropertiesError,
|
| 18 | + AQLFunctionCreateError, |
| 19 | + AQLFunctionDeleteError, |
| 20 | + AQLFunctionListError, |
13 | 21 | AQLQueryClearError,
|
14 | 22 | AQLQueryExecuteError,
|
15 | 23 | AQLQueryExplainError,
|
@@ -276,3 +284,82 @@ async def test_cache_plan_management(db, bad_db, doc_col, docs, db_version):
|
276 | 284 | with pytest.raises(AQLCacheClearError) as err:
|
277 | 285 | await bad_db.aql.cache.clear_plan()
|
278 | 286 | assert err.value.error_code == FORBIDDEN
|
| 287 | + |
| 288 | + |
| 289 | +@pytest.mark.asyncio |
| 290 | +async def test_aql_function_management(db, bad_db): |
| 291 | + fn_group = "functions::temperature" |
| 292 | + fn_name_1 = "functions::temperature::celsius_to_fahrenheit" |
| 293 | + fn_body_1 = "function (celsius) { return celsius * 1.8 + 32; }" |
| 294 | + fn_name_2 = "functions::temperature::fahrenheit_to_celsius" |
| 295 | + fn_body_2 = "function (fahrenheit) { return (fahrenheit - 32) / 1.8; }" |
| 296 | + bad_fn_name = "functions::temperature::should_not_exist" |
| 297 | + bad_fn_body = "function (celsius) { invalid syntax }" |
| 298 | + |
| 299 | + aql = db.aql |
| 300 | + # List AQL functions |
| 301 | + assert await aql.functions() == [] |
| 302 | + |
| 303 | + # List AQL functions with bad database |
| 304 | + with pytest.raises(AQLFunctionListError) as err: |
| 305 | + await bad_db.aql.functions() |
| 306 | + assert err.value.error_code == FORBIDDEN |
| 307 | + |
| 308 | + # Create invalid AQL function |
| 309 | + with pytest.raises(AQLFunctionCreateError) as err: |
| 310 | + await aql.create_function(bad_fn_name, bad_fn_body) |
| 311 | + assert err.value.error_code == QUERY_FUNCTION_INVALID_CODE |
| 312 | + |
| 313 | + # Create first AQL function |
| 314 | + result = await aql.create_function(fn_name_1, fn_body_1, is_deterministic=True) |
| 315 | + assert result["isNewlyCreated"] is True |
| 316 | + functions = await aql.functions() |
| 317 | + assert len(functions) == 1 |
| 318 | + assert functions[0]["name"] == fn_name_1 |
| 319 | + assert functions[0]["code"] == fn_body_1 |
| 320 | + assert functions[0]["isDeterministic"] is True |
| 321 | + |
| 322 | + # Create same AQL function again |
| 323 | + result = await aql.create_function(fn_name_1, fn_body_1, is_deterministic=True) |
| 324 | + assert result["isNewlyCreated"] is False |
| 325 | + functions = await aql.functions() |
| 326 | + assert len(functions) == 1 |
| 327 | + assert functions[0]["name"] == fn_name_1 |
| 328 | + assert functions[0]["code"] == fn_body_1 |
| 329 | + assert functions[0]["isDeterministic"] is True |
| 330 | + |
| 331 | + # Create second AQL function |
| 332 | + result = await aql.create_function(fn_name_2, fn_body_2, is_deterministic=False) |
| 333 | + assert result["isNewlyCreated"] is True |
| 334 | + functions = await aql.functions() |
| 335 | + assert len(functions) == 2 |
| 336 | + assert functions[0]["name"] == fn_name_1 |
| 337 | + assert functions[0]["code"] == fn_body_1 |
| 338 | + assert functions[0]["isDeterministic"] is True |
| 339 | + assert functions[1]["name"] == fn_name_2 |
| 340 | + assert functions[1]["code"] == fn_body_2 |
| 341 | + assert functions[1]["isDeterministic"] is False |
| 342 | + |
| 343 | + # Delete first function |
| 344 | + result = await aql.delete_function(fn_name_1) |
| 345 | + assert result["deletedCount"] == 1 |
| 346 | + functions = await aql.functions() |
| 347 | + assert len(functions) == 1 |
| 348 | + |
| 349 | + # Delete missing function |
| 350 | + with pytest.raises(AQLFunctionDeleteError) as err: |
| 351 | + await aql.delete_function(fn_name_1) |
| 352 | + assert err.value.error_code == QUERY_FUNCTION_NOT_FOUND |
| 353 | + result = await aql.delete_function(fn_name_1, ignore_missing=True) |
| 354 | + assert "deletedCount" not in result |
| 355 | + |
| 356 | + # Delete function from bad db |
| 357 | + with pytest.raises(AQLFunctionDeleteError) as err: |
| 358 | + await bad_db.aql.delete_function(fn_name_2) |
| 359 | + assert err.value.error_code == FORBIDDEN |
| 360 | + |
| 361 | + # Delete function group |
| 362 | + result = await aql.delete_function(fn_group, group=True) |
| 363 | + assert result["deletedCount"] == 1 |
| 364 | + functions = await aql.functions() |
| 365 | + assert len(functions) == 0 |
0 commit comments