|
3 | 3 | import orjson |
4 | 4 | import shortuuid |
5 | 5 |
|
| 6 | +from django_unicorn.components import UnicornView, unicorn_view |
6 | 7 | from django_unicorn.utils import generate_checksum |
| 8 | +from tests.views.message.utils import post_and_get_response |
7 | 9 |
|
8 | 10 |
|
9 | 11 | def test_message_call_method(client): |
10 | 12 | data = {"method_count": 0} |
11 | | - message = { |
12 | | - "actionQueue": [{"payload": {"name": "test_method"}, "type": "callMethod",}], |
13 | | - "data": data, |
14 | | - "checksum": generate_checksum(orjson.dumps(data)), |
15 | | - "id": shortuuid.uuid()[:8], |
16 | | - "epoch": time.time(), |
17 | | - } |
18 | | - |
19 | | - response = client.post( |
20 | | - "/message/tests.views.fake_components.FakeComponent", |
21 | | - message, |
22 | | - content_type="application/json", |
| 13 | + response = post_and_get_response( |
| 14 | + client, |
| 15 | + url="/message/tests.views.fake_components.FakeComponent", |
| 16 | + data=data, |
| 17 | + action_queue=[{"payload": {"name": "test_method"}, "type": "callMethod",}], |
23 | 18 | ) |
24 | 19 |
|
25 | | - body = orjson.loads(response.content) |
26 | | - |
27 | | - assert body["data"].get("method_count") == 1 |
| 20 | + assert response["data"].get("method_count") == 1 |
28 | 21 |
|
29 | 22 |
|
30 | 23 | def test_message_call_method_redirect(client): |
@@ -414,3 +407,94 @@ def test_message_call_method_refresh(client): |
414 | 407 | # `data` should contain all data (not just the diffs) for refreshes |
415 | 408 | assert body["data"].get("check") is not None |
416 | 409 | assert body["data"].get("dictionary") is not None |
| 410 | + |
| 411 | + |
| 412 | +def test_message_call_method_caches_disabled(client, monkeypatch, settings): |
| 413 | + monkeypatch.setattr(unicorn_view, "COMPONENTS_MODULE_CACHE_ENABLED", False) |
| 414 | + settings.CACHES["default"][ |
| 415 | + "BACKEND" |
| 416 | + ] = "django.core.cache.backends.dummy.DummyCache" |
| 417 | + |
| 418 | + component_id = shortuuid.uuid()[:8] |
| 419 | + response = post_and_get_response( |
| 420 | + client, |
| 421 | + url="/message/tests.views.fake_components.FakeComponent", |
| 422 | + data={"method_count": 0}, |
| 423 | + action_queue=[{"payload": {"name": "test_method"}, "type": "callMethod",}], |
| 424 | + component_id=component_id, |
| 425 | + ) |
| 426 | + |
| 427 | + method_count = response["data"].get("method_count") |
| 428 | + |
| 429 | + assert method_count == 1 |
| 430 | + |
| 431 | + # Get the component again |
| 432 | + view = UnicornView.create( |
| 433 | + component_name="tests.views.fake_components.FakeComponent", |
| 434 | + component_id=component_id, |
| 435 | + use_cache=True, |
| 436 | + ) |
| 437 | + |
| 438 | + # Component is not retrieved from any caches |
| 439 | + assert view.method_count == 0 |
| 440 | + |
| 441 | + |
| 442 | +def test_message_call_method_module_cache_disabled(client, monkeypatch, settings): |
| 443 | + monkeypatch.setattr(unicorn_view, "COMPONENTS_MODULE_CACHE_ENABLED", False) |
| 444 | + settings.UNICORN["CACHE_ALIAS"] = "default" |
| 445 | + settings.CACHES["default"][ |
| 446 | + "BACKEND" |
| 447 | + ] = "django.core.cache.backends.locmem.LocMemCache" |
| 448 | + |
| 449 | + component_id = shortuuid.uuid()[:8] |
| 450 | + response = post_and_get_response( |
| 451 | + client, |
| 452 | + url="/message/tests.views.fake_components.FakeComponent", |
| 453 | + data={"method_count": 0}, |
| 454 | + action_queue=[{"payload": {"name": "test_method"}, "type": "callMethod",}], |
| 455 | + component_id=component_id, |
| 456 | + ) |
| 457 | + |
| 458 | + method_count = response["data"].get("method_count") |
| 459 | + |
| 460 | + assert method_count == 1 |
| 461 | + |
| 462 | + # Get the component again and it should be found in local memory cache |
| 463 | + view = UnicornView.create( |
| 464 | + component_name="tests.views.fake_components.FakeComponent", |
| 465 | + component_id=component_id, |
| 466 | + use_cache=True, |
| 467 | + ) |
| 468 | + |
| 469 | + # Component is retrieved from the local memory cache |
| 470 | + assert view.method_count == method_count |
| 471 | + |
| 472 | + |
| 473 | +def test_message_call_method_cache_backend_dummy(client, monkeypatch, settings): |
| 474 | + monkeypatch.setattr(unicorn_view, "COMPONENTS_MODULE_CACHE_ENABLED", True) |
| 475 | + settings.CACHES["default"][ |
| 476 | + "BACKEND" |
| 477 | + ] = "django.core.cache.backends.dummy.DummyCache" |
| 478 | + |
| 479 | + component_id = shortuuid.uuid()[:8] |
| 480 | + response = post_and_get_response( |
| 481 | + client, |
| 482 | + url="/message/tests.views.fake_components.FakeComponent", |
| 483 | + data={"method_count": 0}, |
| 484 | + action_queue=[{"payload": {"name": "test_method"}, "type": "callMethod",}], |
| 485 | + component_id=component_id, |
| 486 | + ) |
| 487 | + |
| 488 | + method_count = response["data"].get("method_count") |
| 489 | + |
| 490 | + assert method_count == 1 |
| 491 | + |
| 492 | + # Get the component again and it should be found in local memory cache |
| 493 | + view = UnicornView.create( |
| 494 | + component_name="tests.views.fake_components.FakeComponent", |
| 495 | + component_id=component_id, |
| 496 | + use_cache=True, |
| 497 | + ) |
| 498 | + |
| 499 | + # Component is retrieved from the module cache |
| 500 | + assert view.method_count == method_count |
0 commit comments