Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def global_test_2():
assert global_test_2.cache_dpath() is not None


@pytest.mark.mongo
def test_mongetter_default_param():

cachier.set_default_params(mongetter=_test_mongetter)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ def _calls_wait_for_calc_timeout_slow(res_queue):
@pytest.mark.parametrize(
'mongetter,backend',
[
(_test_mongetter, 'mongo'),
# (_test_mongetter, 'mongo'), # Please do NOT test the mongo backend
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not rather mark it with

pytest.param(_test_mongetter, 'mongo', marks=pytest.mark.mongo)

instead of duplicating whole test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. I didn't know you're going to review in time. :P
Anyway, I didn't know you could do that with pytest!
Let's try that later. You can work it into your suite separation PR, or later; I'm not sure I understand the exact syntax you refer to.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems that this parameter marking was already used in the tests, see: #145

# here. It is tested in test_mongo_core.py
(None, 'memory'),
(None, 'pickle'),
]
Expand Down Expand Up @@ -182,7 +183,8 @@ def func(arg_1, arg_2):
@pytest.mark.parametrize(
'mongetter,backend',
[
(_test_mongetter, 'mongo'),
# (_test_mongetter, 'mongo'), # Please do NOT test the mongo backend
# here. It is tested in test_mongo_core.py
(None, 'memory'),
(None, 'pickle'),
]
Expand Down
56 changes: 53 additions & 3 deletions tests/test_mongo_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import threading
from datetime import timedelta
from random import random
from time import sleep
from time import sleep, time
from urllib.parse import quote_plus

from birch import Birch # type: ignore[import-not-found]
Expand All @@ -25,8 +25,6 @@


# === Enables testing vs a real MongoDB instance ===
CFG = Birch("cachier")


class CfgKey():
HOST = "TEST_HOST"
Expand All @@ -36,6 +34,14 @@ class CfgKey():
TEST_VS_LIVE_MONGO = "TEST_VS_LIVE_MONGO"


CFG = Birch(
namespace="cachier",
defaults={
CfgKey.TEST_VS_LIVE_MONGO: False,
},
)


URI_TEMPLATE = (
"mongodb+srv://{uname}:{pwd}@{host}/{db}?retrywrites=true&w=majority")

Expand Down Expand Up @@ -114,6 +120,50 @@ def _test_mongo_caching(arg_1, arg_2):
assert val6 == val5


@pytest.mark.mongo
def test_mongo_precache_value():

@cachier(backend='mongo', mongetter=_test_mongetter)
def func(arg_1, arg_2):
"""Some function."""
return arg_1 + arg_2

result = func.precache_value(2, 2, value_to_cache=5)
assert result == 5
result = func(2, 2)
assert result == 5
func.clear_cache()
result = func(2, 2)
assert result == 4
result = func.precache_value(2, arg_2=2, value_to_cache=5)
assert result == 5
result = func(2, arg_2=2)
assert result == 5


@pytest.mark.mongo
def test_mongo_ignore_self_in_methods():

class TestClass():
@cachier(backend='mongo', mongetter=_test_mongetter)
def takes_2_seconds(self, arg_1, arg_2):
"""Some function."""
sleep(2)
return arg_1 + arg_2

test_object_1 = TestClass()
test_object_2 = TestClass()
test_object_1.takes_2_seconds.clear_cache()
test_object_2.takes_2_seconds.clear_cache()
result_1 = test_object_1.takes_2_seconds(1, 2)
assert result_1 == 3
start = time()
result_2 = test_object_2.takes_2_seconds(1, 2)
end = time()
assert result_2 == 3
assert end - start < 1


@pytest.mark.mongo
def test_mongo_core_keywords():
"""Basic Mongo core functionality with keyword arguments."""
Expand Down