mirror of
https://github.com/JockeTF/fimfarchive.git
synced 2024-11-25 06:37:58 +01:00
Cache len in directory fetcher
This commit is contained in:
parent
cdb968bbf0
commit
6dd650f302
2 changed files with 15 additions and 1 deletions
|
@ -62,6 +62,7 @@ class DirectoryFetcher(Fetcher):
|
|||
"""
|
||||
self.meta_path = meta_path
|
||||
self.data_path = data_path
|
||||
self.length: Optional[int] = None
|
||||
self.flavors = frozenset(flavors)
|
||||
|
||||
def iter_path_keys(self, path: Optional[Path]) -> Iterator[int]:
|
||||
|
@ -111,7 +112,10 @@ class DirectoryFetcher(Fetcher):
|
|||
"""
|
||||
Returns the total number of stories in the directories.
|
||||
"""
|
||||
return len(self.list_keys())
|
||||
if self.length is None:
|
||||
self.length = len(self.list_keys())
|
||||
|
||||
return self.length
|
||||
|
||||
def __iter__(self) -> Iterator[Story]:
|
||||
"""
|
||||
|
|
|
@ -26,6 +26,7 @@ import json
|
|||
import pytest
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict
|
||||
from unittest.mock import patch
|
||||
|
||||
from fimfarchive.exceptions import InvalidStoryError
|
||||
from fimfarchive.fetchers import DirectoryFetcher
|
||||
|
@ -155,6 +156,15 @@ class TestDirectoryFetcher:
|
|||
"""
|
||||
assert 3 == len(fetcher)
|
||||
|
||||
def test_len_caching(test, fetcher):
|
||||
"""
|
||||
Tests len is only calculated once.
|
||||
"""
|
||||
with patch.object(fetcher, 'list_keys', wraps=fetcher.list_keys) as m:
|
||||
assert 3 == len(fetcher)
|
||||
assert 3 == len(fetcher)
|
||||
assert 1 == len(m.mock_calls)
|
||||
|
||||
def test_iter(self, fetcher):
|
||||
"""
|
||||
Tests iter yields all available stories, ordered by key.
|
||||
|
|
Loading…
Reference in a new issue