Skip to content

Commit

Permalink
Add test to check mapper caches do not pollute one another
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Dec 20, 2022
1 parent 5584cab commit 0d4b678
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
5 changes: 0 additions & 5 deletions optimade/server/mappers/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ class BaseResourceMapper:
RELATIONSHIP_ENTRY_TYPES: Set[str] = {"references", "structures"}
TOP_LEVEL_NON_ATTRIBUTES_FIELDS: Set[str] = {"id", "type", "relationships", "links"}

def __init__(self):
raise RuntimeError(
f"{self.__class__.__name__!r} should not be instantiated directly, instead use its functionality via the `@classmethods`"
)

@classmethod
@lru_cache(maxsize=NUM_ENTRY_TYPES)
def all_aliases(cls) -> Iterable[Tuple[str, str]]:
Expand Down
2 changes: 1 addition & 1 deletion tests/filtertransformers/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def parser():
def transformer():
from optimade.server.mappers import StructureMapper

return ElasticTransformer(mapper=StructureMapper())
return ElasticTransformer(mapper=StructureMapper)


test_queries = [
Expand Down
34 changes: 32 additions & 2 deletions tests/server/test_mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_disallowed_aliases(mapper):
class MyMapper(mapper(MAPPER)):
ALIASES = (("$and", "my_special_and"), ("not", "$not"))

mapper = MyMapper()
mapper = MyMapper
with pytest.raises(RuntimeError):
MongoCollection("fake", StructureResource, mapper, database="fake")

Expand All @@ -27,7 +27,7 @@ class MyMapper(mapper(MAPPER)):
LENGTH_ALIASES = (("_exmpl_test_field", "test_field_len"),)
ALIASES = (("field", "completely_different_field"),)

mapper = MyMapper()
mapper = MyMapper
assert mapper.get_backend_field("_exmpl_dft_parameters") == "dft_parameters"
assert mapper.get_backend_field("_exmpl_test_field") == "test_field"
assert mapper.get_backend_field("field") == "completely_different_field"
Expand Down Expand Up @@ -62,3 +62,33 @@ class MyMapper(mapper(MAPPER)):
mapper.get_backend_field("_exmpl_dft_parameters_dft_parameters.nested.property")
== "_exmpl_dft_parameters_dft_parameters.nested.property"
)


def test_cached_mapper_properties(mapper):
"""Tests that alias caching both occurs, and is not effected
by the presence of other mapper caches.
"""

class MyMapper(mapper(MAPPER)):
ALIASES = (("field", "completely_different_field"),)

class MyOtherMapper(mapper(MAPPER)):
ALIASES = (
("field", "completely_different_field2"),
("a", "b"),
)

assert MyMapper.get_backend_field("field") == "completely_different_field"
hits = MyMapper.get_backend_field.cache_info().hits
assert MyMapper.get_backend_field("field") == "completely_different_field"
assert MyMapper.get_backend_field.cache_info().hits == hits + 1
assert MyOtherMapper.get_backend_field("field") == "completely_different_field2"
assert MyOtherMapper.get_backend_field.cache_info().hits == hits + 1
assert MyOtherMapper.get_backend_field("field") == "completely_different_field2"
assert MyOtherMapper.get_backend_field.cache_info().hits == hits + 2

assert MyOtherMapper.get_backend_field("a") == "b"
assert MyOtherMapper.get_backend_field.cache_info().hits == hits + 2
assert MyOtherMapper.get_backend_field("a") == "b"
assert MyOtherMapper.get_backend_field.cache_info().hits == hits + 3
assert MyMapper.get_backend_field("a") == "a"

0 comments on commit 0d4b678

Please sign in to comment.