Skip to content

Commit a6f4738

Browse files
committed
more tests
1 parent da9b902 commit a6f4738

File tree

5 files changed

+219
-2
lines changed

5 files changed

+219
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [unreleased]
1010

11+
## [0.22.0] - 2024-06-05
1112
- Adds caching per API based on user context.
13+
14+
### Breaking change:
1215
- Changes general error in querier to normal python error.
1316

1417
## [0.21.0] - 2024-05-23

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383

8484
setup(
8585
name="supertokens_python",
86-
version="0.21.0",
86+
version="0.22.0",
8787
author="SuperTokens",
8888
license="Apache 2.0",
8989
author_email="[email protected]",

supertokens_python/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from __future__ import annotations
1515

1616
SUPPORTED_CDI_VERSIONS = ["3.0"]
17-
VERSION = "0.21.0"
17+
VERSION = "0.22.0"
1818
TELEMETRY = "/telemetry"
1919
USER_COUNT = "/users/count"
2020
USER_DELETE = "/user/remove"

supertokens_python/querier.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,15 @@ def invalidate_core_call_cache(
400400
upd_global_cache_tag_if_necessary: bool = True,
401401
):
402402
if user_context is None:
403+
# this is done so that the code below runs as expected.
404+
# It will reset the __global_cache_tag if needed, and the
405+
# stuff we assign to the user_context will just be ignored (as expected)
403406
user_context = {}
404407

405408
if upd_global_cache_tag_if_necessary and (
406409
user_context.get("_default", {}).get("keep_cache_alive", False) is not True
407410
):
411+
# there can be race conditions here, but i think we can ignore them.
408412
self.__global_cache_tag = get_timestamp_ms()
409413

410414
user_context["_default"] = {

tests/test_querier.py

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,213 @@ def intercept(
473473
user = await get_user_by_id("random", user_context)
474474
assert user is None
475475
assert called_core
476+
477+
478+
async def test_caching_does_not_get_clear_with_non_get_if_keep_alive():
479+
480+
called_core = False
481+
482+
def intercept(
483+
url: str,
484+
method: str,
485+
headers: Dict[str, Any],
486+
params: Optional[Dict[str, Any]],
487+
body: Optional[Dict[str, Any]],
488+
_: Optional[Dict[str, Any]],
489+
):
490+
nonlocal called_core
491+
called_core = True
492+
return url, method, headers, params, body
493+
494+
init(
495+
supertokens_config=SupertokensConfig(
496+
connection_uri="http://localhost:3567", network_interceptor=intercept
497+
),
498+
app_info=InputAppInfo(
499+
app_name="ST",
500+
api_domain="http://api.supertokens.io",
501+
website_domain="http://supertokens.io",
502+
api_base_path="/auth",
503+
),
504+
framework="fastapi",
505+
mode="asgi",
506+
recipe_list=[
507+
session.init(),
508+
emailpassword.init(),
509+
dashboard.init(),
510+
],
511+
) # type: ignore
512+
start_st()
513+
user_context: Dict[str, Any] = {"_default": {"keep_cache_alive": True}}
514+
user_context_2: Dict[str, Any] = {}
515+
516+
user = await get_user_by_id("random", user_context)
517+
518+
assert user is None
519+
assert called_core
520+
521+
called_core = False
522+
523+
user = await get_user_by_id("random", user_context_2)
524+
525+
assert user is None
526+
assert called_core
527+
528+
await sign_up("public", "[email protected]", "abcd1234", user_context)
529+
530+
called_core = False
531+
532+
user = await get_user_by_id("random", user_context)
533+
assert user is None
534+
assert called_core
535+
536+
called_core = False
537+
538+
user = await get_user_by_id("random", user_context)
539+
assert user is None
540+
assert not called_core
541+
542+
user = await get_user_by_id("random", user_context_2)
543+
544+
assert user is None
545+
assert not called_core
546+
547+
548+
async def test_caching_gets_clear_with_non_get_if_keep_alive_is_false():
549+
550+
called_core = False
551+
552+
def intercept(
553+
url: str,
554+
method: str,
555+
headers: Dict[str, Any],
556+
params: Optional[Dict[str, Any]],
557+
body: Optional[Dict[str, Any]],
558+
_: Optional[Dict[str, Any]],
559+
):
560+
nonlocal called_core
561+
called_core = True
562+
return url, method, headers, params, body
563+
564+
init(
565+
supertokens_config=SupertokensConfig(
566+
connection_uri="http://localhost:3567", network_interceptor=intercept
567+
),
568+
app_info=InputAppInfo(
569+
app_name="ST",
570+
api_domain="http://api.supertokens.io",
571+
website_domain="http://supertokens.io",
572+
api_base_path="/auth",
573+
),
574+
framework="fastapi",
575+
mode="asgi",
576+
recipe_list=[
577+
session.init(),
578+
emailpassword.init(),
579+
dashboard.init(),
580+
],
581+
) # type: ignore
582+
start_st()
583+
user_context: Dict[str, Any] = {"_default": {"keep_cache_alive": False}}
584+
user_context_2: Dict[str, Any] = {}
585+
586+
user = await get_user_by_id("random", user_context)
587+
588+
assert user is None
589+
assert called_core
590+
591+
called_core = False
592+
593+
user = await get_user_by_id("random", user_context_2)
594+
595+
assert user is None
596+
assert called_core
597+
598+
await sign_up("public", "[email protected]", "abcd1234", user_context)
599+
600+
called_core = False
601+
602+
user = await get_user_by_id("random", user_context)
603+
assert user is None
604+
assert called_core
605+
606+
called_core = False
607+
608+
user = await get_user_by_id("random", user_context)
609+
assert user is None
610+
assert not called_core
611+
612+
user = await get_user_by_id("random", user_context_2)
613+
614+
assert user is None
615+
assert called_core
616+
617+
618+
async def test_caching_gets_clear_with_non_get_if_keep_alive_is_not_set():
619+
620+
called_core = False
621+
622+
def intercept(
623+
url: str,
624+
method: str,
625+
headers: Dict[str, Any],
626+
params: Optional[Dict[str, Any]],
627+
body: Optional[Dict[str, Any]],
628+
_: Optional[Dict[str, Any]],
629+
):
630+
nonlocal called_core
631+
called_core = True
632+
return url, method, headers, params, body
633+
634+
init(
635+
supertokens_config=SupertokensConfig(
636+
connection_uri="http://localhost:3567", network_interceptor=intercept
637+
),
638+
app_info=InputAppInfo(
639+
app_name="ST",
640+
api_domain="http://api.supertokens.io",
641+
website_domain="http://supertokens.io",
642+
api_base_path="/auth",
643+
),
644+
framework="fastapi",
645+
mode="asgi",
646+
recipe_list=[
647+
session.init(),
648+
emailpassword.init(),
649+
dashboard.init(),
650+
],
651+
) # type: ignore
652+
start_st()
653+
user_context: Dict[str, Any] = {}
654+
user_context_2: Dict[str, Any] = {}
655+
656+
user = await get_user_by_id("random", user_context)
657+
658+
assert user is None
659+
assert called_core
660+
661+
called_core = False
662+
663+
user = await get_user_by_id("random", user_context_2)
664+
665+
assert user is None
666+
assert called_core
667+
668+
await sign_up("public", "[email protected]", "abcd1234", user_context)
669+
670+
called_core = False
671+
672+
user = await get_user_by_id("random", user_context)
673+
assert user is None
674+
assert called_core
675+
676+
called_core = False
677+
678+
user = await get_user_by_id("random", user_context)
679+
assert user is None
680+
assert not called_core
681+
682+
user = await get_user_by_id("random", user_context_2)
683+
684+
assert user is None
685+
assert called_core

0 commit comments

Comments
 (0)