Skip to content

Commit 09aa511

Browse files
committed
🎲 fix generating random quote/author ids
1 parent 03a3a51 commit 09aa511

File tree

2 files changed

+2
-10
lines changed

2 files changed

+2
-10
lines changed

‎an_website/quotes/utils.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ class UltraDictType[K, V](MutableMapping[K, V], abc.ABC):
8181
UltraDict(buffer_size=1024**2, serializer=dill)
8282
)
8383

84-
MAX_QUOTES_ID = Value("Q", 0)
85-
MAX_AUTHORS_ID = Value("Q", 0)
86-
8784

8885
@dataclass(init=False, slots=True)
8986
class QuotesObjBase(abc.ABC):
@@ -437,7 +434,6 @@ def parse_author(json_data: Mapping[str, Any]) -> Author:
437434
if author is None:
438435
# pylint: disable-next=too-many-function-args
439436
author = Author(id_, name, None)
440-
MAX_AUTHORS_ID.value = max(MAX_AUTHORS_ID.value, id_)
441437
elif author.name != name:
442438
author.name = name
443439
author.info = None # reset info
@@ -474,7 +470,6 @@ def parse_quote(
474470
if quote is None: # new quote
475471
# pylint: disable=too-many-function-args
476472
quote = Quote(quote_id, quote_str, author.id)
477-
MAX_QUOTES_ID.value = max(MAX_QUOTES_ID.value, quote.id)
478473
else: # quote was already saved
479474
quote.quote = quote_str
480475
quote.author_id = author.id
@@ -795,12 +790,12 @@ async def get_rating_by_id(quote_id: int, author_id: int) -> int | None:
795790

796791
def get_random_quote_id() -> int:
797792
"""Get random quote id."""
798-
return random.randint(1, MAX_QUOTES_ID.value) # nosec: B311
793+
return random.choice(tuple(QUOTES_CACHE))
799794

800795

801796
def get_random_author_id() -> int:
802797
"""Get random author id."""
803-
return random.randint(1, MAX_AUTHORS_ID.value) # nosec: B311
798+
return random.choice(tuple(AUTHORS_CACHE))
804799

805800

806801
def get_random_id() -> tuple[int, int]:

‎tests/test_quotes.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ async def test_parsing_wrong_quotes() -> None:
7171
assert await quotes.get_rating_by_id(1, 2) == 4
7272

7373
assert len(quotes.QUOTES_CACHE) == 1
74-
assert quotes.MAX_QUOTES_ID.value == 1
7574
assert len(quotes.AUTHORS_CACHE) == 2
76-
assert quotes.MAX_AUTHORS_ID.value == 2
77-
7875

7976
def test_author_updating() -> None:
8077
"""Test updating the author."""

0 commit comments

Comments
 (0)