Select Git revision
test_update.py
-
Timm Fitschen authored
Squashed commit of the following: commit feea533048a28badbdc09be9dfcf6faf7a00c394 Author: Timm Fitschen <t.fitschen@indiscale.com> Date: Tue Sep 3 10:28:20 2019 +0200 TST: skip empty text value (until fixed) commit 9ece19b1d30547bf62e48d79a21e1a148ff48ece Merge: 6290274 1c81f890 Author: Timm Fitschen <t.fitschen@indiscale.com> Date: Tue Sep 3 09:30:21 2019 +0200 Merge branch 'dev' into f-server-side-scripting commit 6290274d119a284b0cf957daa7755a4d01e4bdbd Merge: 6c40bb1 fc5977f6 Author: Henrik tom Wörden <henrik@trineo.org> Date: Wed Aug 28 15:21:08 2019 +0200 Merge branch 'dev' into f-server-side-scripting commit fc5977f6 Author: Henrik tom Wörden <henrik@trineo.org> Date: Wed Aug 28 15:10:40 2019 +0200 DEV: added test for usage of empty strings commit 6c40bb1f78df44af855f93d29495e4b139a4948b Author: Henrik tom Wörden <henrik@trineo.org> Date: Sat Aug 17 15:44:35 2019 +0200 cosmetcis commit 5daf49823ff4dd2273df786e6f88c38575a81b26 Author: Henrik tom Wörden <henrik@trineo.org> Date: Fri Aug 16 21:49:44 2019 +0200 MAINT: remove depricated assert_equals commit e8ca7fc0849fbc4fff43abbb22a340e78ca04c32 Merge: 483b08a 5789272a Author: Henrik tom Wörden <henrik@trineo.org> Date: Fri Aug 16 21:48:59 2019 +0200 Merge branch 'dev' into f-server-side-scripting commit 483b08a11e72cd5d0c1039f7ca425c4824b946c5 Author: Henrik tom Wörden <henrik@trineo.org> Date: Thu Aug 15 17:58:09 2019 +0200 FIX: removed assertion that was uncomplete commit dafc7886cc15110ca28c626d32be27204d106fe3 Merge: 2e732d6 e1971635 Author: Henrik tom Wörden <henrik@trineo.org> Date: Thu Aug 15 14:10:52 2019 +0200 Merge branch 'dev' into f-server-side-scripting commit 2e732d63bc71f61edcbb7e31169b3ec9217583d5 Merge: ac9af7e 7b876f97 Author: Henrik tom Wörden <henrik@trineo.org> Date: Thu Aug 15 14:06:17 2019 +0200 Merge branch 'dev' into f-server-side-scripting commit ac9af7e3883770602c305e09ea543d00aa7fb170 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Thu Mar 28 10:24:52 2019 +0100 TST: fix tests for mariadb commit 156b77d7b925fc4e225de9dfcb4224f4b7dc4540 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Mar 27 15:13:10 2019 +0100 TST: add another test for like queries commit 156a8a717c0c42a7534a86de2c7843aa50e8aab0 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Mar 27 11:09:50 2019 +0100 TST: test like queries commit dcd2181155b256b7800aa312d6f38e726f1ef17b Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Fri Mar 22 14:53:23 2019 +0100 TEST: add tests for CHECK_ENTITY_ACL_ROLES_MODE commit ac56d505385690da962efe9d40df2c71c4d42f55 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Dec 18 22:50:11 2018 +0100 TST: changes due to changed std. config of server commit 994201dab6e483c05bed8500806585c0e8f42590 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Dec 11 23:34:13 2018 +0100 TST: Test AccessControl Bug commit fa3426cae23939ab7c8a370b5230910331b89ddc Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Dec 10 16:09:10 2018 +0100 TST: fix configuration without password_method commit c353ea395aa4737290605e42a24ae6983e7edb76 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Dec 5 01:45:04 2018 +0100 TST: make tests compatible with password_method = pass commit f09809f47587d50ec8c4e68da5db526d14aa2222 Merge: d66940c 44390a1c Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Dec 5 00:56:14 2018 +0100 Merge branch 'dev' into f-server-side-scripting commit d66940cf4ead03ae7f64fba66adde26f24722ab6 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Dec 5 00:36:28 2018 +0100 TST: fix test_benchmark for userInfo commit 6da4a48878cc55a8c0527d91286340dd561ab5e7 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Dec 4 23:40:02 2018 +0100 TST: fix test_query for userInfo commit 157b90571485fec89b4de3900aef32c4c79d9dad Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Dec 4 16:30:23 2018 +0100 TST: fix test_files for userInfo commit 909f1cd4010a528934bb41baa978b035288cf5e6 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Nov 20 00:03:39 2018 +0100 TST: reset password method for dummy user commit 6b904b83b44a40f7d3dd0a7646f1eb5c662b2164 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Nov 19 21:44:21 2018 +0100 update tests for mysql 5.7 commit 6dd4c04482000de60439fd9e56ad5dd67f907257 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Oct 17 13:51:58 2018 +0200 TST: added setup for installing test scripts commit b090913f630d4f8c304bf9b71196f0e9c2ae102c Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Oct 16 00:06:20 2018 +0200 TST: update tox.ini commit 661a915a617033afdb105257911c9a4082daa6ce Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Oct 15 23:51:14 2018 +0200 TST: pytest instead of py.test commit 7129bce97f1c5e2145465396c944a7b71b13bc70 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Oct 15 23:39:59 2018 +0200 BUG: added encoding to test_server_side_scripting commit b1289861a2b5c92d2e0ac0d5b3eed4ea3b899dfe Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Oct 15 21:04:28 2018 +0200 TST: skip test which needs a script to be installed commit 1bc9aa1979cb9e7ebeaf5648ad5a87231dd2bd1e Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Oct 15 20:48:34 2018 +0200 DOC: Added license header commit f7407d725cab7c2b632797d30c1985ac73749153 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Oct 15 20:44:10 2018 +0200 TST: Test for server-side scripting
Timm Fitschen authoredSquashed commit of the following: commit feea533048a28badbdc09be9dfcf6faf7a00c394 Author: Timm Fitschen <t.fitschen@indiscale.com> Date: Tue Sep 3 10:28:20 2019 +0200 TST: skip empty text value (until fixed) commit 9ece19b1d30547bf62e48d79a21e1a148ff48ece Merge: 6290274 1c81f890 Author: Timm Fitschen <t.fitschen@indiscale.com> Date: Tue Sep 3 09:30:21 2019 +0200 Merge branch 'dev' into f-server-side-scripting commit 6290274d119a284b0cf957daa7755a4d01e4bdbd Merge: 6c40bb1 fc5977f6 Author: Henrik tom Wörden <henrik@trineo.org> Date: Wed Aug 28 15:21:08 2019 +0200 Merge branch 'dev' into f-server-side-scripting commit fc5977f6 Author: Henrik tom Wörden <henrik@trineo.org> Date: Wed Aug 28 15:10:40 2019 +0200 DEV: added test for usage of empty strings commit 6c40bb1f78df44af855f93d29495e4b139a4948b Author: Henrik tom Wörden <henrik@trineo.org> Date: Sat Aug 17 15:44:35 2019 +0200 cosmetcis commit 5daf49823ff4dd2273df786e6f88c38575a81b26 Author: Henrik tom Wörden <henrik@trineo.org> Date: Fri Aug 16 21:49:44 2019 +0200 MAINT: remove depricated assert_equals commit e8ca7fc0849fbc4fff43abbb22a340e78ca04c32 Merge: 483b08a 5789272a Author: Henrik tom Wörden <henrik@trineo.org> Date: Fri Aug 16 21:48:59 2019 +0200 Merge branch 'dev' into f-server-side-scripting commit 483b08a11e72cd5d0c1039f7ca425c4824b946c5 Author: Henrik tom Wörden <henrik@trineo.org> Date: Thu Aug 15 17:58:09 2019 +0200 FIX: removed assertion that was uncomplete commit dafc7886cc15110ca28c626d32be27204d106fe3 Merge: 2e732d6 e1971635 Author: Henrik tom Wörden <henrik@trineo.org> Date: Thu Aug 15 14:10:52 2019 +0200 Merge branch 'dev' into f-server-side-scripting commit 2e732d63bc71f61edcbb7e31169b3ec9217583d5 Merge: ac9af7e 7b876f97 Author: Henrik tom Wörden <henrik@trineo.org> Date: Thu Aug 15 14:06:17 2019 +0200 Merge branch 'dev' into f-server-side-scripting commit ac9af7e3883770602c305e09ea543d00aa7fb170 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Thu Mar 28 10:24:52 2019 +0100 TST: fix tests for mariadb commit 156b77d7b925fc4e225de9dfcb4224f4b7dc4540 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Mar 27 15:13:10 2019 +0100 TST: add another test for like queries commit 156a8a717c0c42a7534a86de2c7843aa50e8aab0 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Mar 27 11:09:50 2019 +0100 TST: test like queries commit dcd2181155b256b7800aa312d6f38e726f1ef17b Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Fri Mar 22 14:53:23 2019 +0100 TEST: add tests for CHECK_ENTITY_ACL_ROLES_MODE commit ac56d505385690da962efe9d40df2c71c4d42f55 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Dec 18 22:50:11 2018 +0100 TST: changes due to changed std. config of server commit 994201dab6e483c05bed8500806585c0e8f42590 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Dec 11 23:34:13 2018 +0100 TST: Test AccessControl Bug commit fa3426cae23939ab7c8a370b5230910331b89ddc Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Dec 10 16:09:10 2018 +0100 TST: fix configuration without password_method commit c353ea395aa4737290605e42a24ae6983e7edb76 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Dec 5 01:45:04 2018 +0100 TST: make tests compatible with password_method = pass commit f09809f47587d50ec8c4e68da5db526d14aa2222 Merge: d66940c 44390a1c Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Dec 5 00:56:14 2018 +0100 Merge branch 'dev' into f-server-side-scripting commit d66940cf4ead03ae7f64fba66adde26f24722ab6 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Dec 5 00:36:28 2018 +0100 TST: fix test_benchmark for userInfo commit 6da4a48878cc55a8c0527d91286340dd561ab5e7 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Dec 4 23:40:02 2018 +0100 TST: fix test_query for userInfo commit 157b90571485fec89b4de3900aef32c4c79d9dad Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Dec 4 16:30:23 2018 +0100 TST: fix test_files for userInfo commit 909f1cd4010a528934bb41baa978b035288cf5e6 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Nov 20 00:03:39 2018 +0100 TST: reset password method for dummy user commit 6b904b83b44a40f7d3dd0a7646f1eb5c662b2164 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Nov 19 21:44:21 2018 +0100 update tests for mysql 5.7 commit 6dd4c04482000de60439fd9e56ad5dd67f907257 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed Oct 17 13:51:58 2018 +0200 TST: added setup for installing test scripts commit b090913f630d4f8c304bf9b71196f0e9c2ae102c Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Tue Oct 16 00:06:20 2018 +0200 TST: update tox.ini commit 661a915a617033afdb105257911c9a4082daa6ce Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Oct 15 23:51:14 2018 +0200 TST: pytest instead of py.test commit 7129bce97f1c5e2145465396c944a7b71b13bc70 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Oct 15 23:39:59 2018 +0200 BUG: added encoding to test_server_side_scripting commit b1289861a2b5c92d2e0ac0d5b3eed4ea3b899dfe Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Oct 15 21:04:28 2018 +0200 TST: skip test which needs a script to be installed commit 1bc9aa1979cb9e7ebeaf5648ad5a87231dd2bd1e Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Oct 15 20:48:34 2018 +0200 DOC: Added license header commit f7407d725cab7c2b632797d30c1985ac73749153 Author: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Mon Oct 15 20:44:10 2018 +0200 TST: Test for server-side scripting
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
test_cached.py 7.71 KiB
# -*- coding: utf-8 -*-
#
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2023 Henrik tom Wörden <h.tomwoerden@indiscale.com>
# Copyright (C) 2023 IndiScale GmbH <info@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
""" Test the caosdb.cached module """
from caosdb.cached import (cached_get_entity_by, cache_clear, cache_info, fill_cache,
AccessType, cache_initialize, cached_query)
from unittest.mock import patch
import caosdb as db
from copy import deepcopy
import pytest
DUMMY_SERVER_CONTENT = [
db.Record(name='a', id=101),
db.Record(name='b', id=102),
db.Record(name='c', id=103),
db.File(path='p', id=104),
db.File(path='pp', id=105),
]
@pytest.fixture(autouse=True)
def cache_clean_up():
cache_clear()
yield
cache_clear()
def mocked_name_query(name):
# copy the object, because Entities would normally be created from XML response
return deepcopy([el for el in DUMMY_SERVER_CONTENT if el.name == name][0])
def mocked_id_query(eid):
# copy the object, because Entities would normally be created from XML response
return deepcopy([el for el in DUMMY_SERVER_CONTENT if el.id == eid][0])
def mocked_path_query(path):
# copy the object, because Entities would normally be created from XML response
return deepcopy([el for el in DUMMY_SERVER_CONTENT if el.path == path][0])
def mocked_gen_query(q):
if q == 'a':
return db.Container().extend([DUMMY_SERVER_CONTENT[0]])
else:
return db.Container().extend(DUMMY_SERVER_CONTENT)
@patch("caosdb.utils.get_entity.get_entity_by_name")
def test_get_by_name(mocked_get_by_name):
mocked_get_by_name.side_effect = mocked_name_query
# first call; not in cache -> mocked_execute is touched
a = cached_get_entity_by(name='a')
assert a.id == 101
assert mocked_get_by_name.call_count == 1
# second call; in cache -> mocked_execute is NOT touched (count is still 1)
b = cached_get_entity_by(name='a')
assert mocked_get_by_name.call_count == 1
# the cache returned the same object
assert a is b
# check the info
assert cache_info().hits == 1
assert cache_info().currsize == 1
# after clearing the test, the mock is used again
cache_clear()
cached_get_entity_by(name='a')
assert mocked_get_by_name.call_count == 2
# we fill the cache manually and make sure the element is used
fill_cache({'lol': db.Entity(id=10001, name='lol')}, AccessType.NAME, unique=True)
# there are now two elements in the cache: a and lol
assert cache_info().currsize == 2
# we can retrieve the inserted element
lol = cached_get_entity_by(name='lol')
assert lol.id == 10001
# this did not touch the mocked function
assert mocked_get_by_name.call_count == 2
# make sure normal retrieval still works (count +1)
c = cached_get_entity_by(name='c')
assert mocked_get_by_name.call_count == 3
assert c.id == 103
@patch("caosdb.utils.get_entity.get_entity_by_id")
def test_get_by_id(mocked_get_by_id):
mocked_get_by_id.side_effect = mocked_id_query
# first call; not in cache -> mocked_execute is touched
b = cached_get_entity_by(eid=102)
assert b.id == 102
assert b.name == 'b'
assert mocked_get_by_id.call_count == 1
# second call; in cache -> mocked_execute is NOT touched (count is still 1)
a = cached_get_entity_by(eid=102)
assert mocked_get_by_id.call_count == 1
# the cache returned the same object
assert a is b
# check the info
assert cache_info().hits == 1
assert cache_info().currsize == 1
# after clearing the test, the mock is used again
cache_clear()
cached_get_entity_by(eid=102)
assert mocked_get_by_id.call_count == 2
# we fill the cache manually and make sure the element is used
fill_cache({10001: db.Entity(id=10001, name='lol')}, AccessType.EID, unique=True)
# there are now two elements in the cache: a and lol
assert cache_info().currsize == 2
# we can retrieve the inserted element
lol = cached_get_entity_by(eid=10001)
assert lol.name == 'lol'
# this did not touch the mocked function
assert mocked_get_by_id.call_count == 2
# make sure normal retrieval still works (count +1)
c = cached_get_entity_by(eid=103)
assert mocked_get_by_id.call_count == 3
assert c.name == 'c'
@patch("caosdb.cached.get_entity.get_entity_by_path")
def test_get_by_path(mocked_get_by_path):
mocked_get_by_path.side_effect = mocked_path_query
# first call; not in cache -> mocked_execute is touched
b = cached_get_entity_by(path='p')
assert b.id == 104
assert mocked_get_by_path.call_count == 1
# second call; in cache -> mocked_execute is NOT touched (count is still 1)
a = cached_get_entity_by(path='p')
assert mocked_get_by_path.call_count == 1
# the cache returned the same object
assert a is b
# check the info
assert cache_info().hits == 1
assert cache_info().currsize == 1
# after clearing the test, the mock is used again
cache_clear()
cached_get_entity_by(path='p')
assert mocked_get_by_path.call_count == 2
# we fill the cache manually and make sure the element is used
fill_cache({'lol': db.File(id=10001, path='lol')}, AccessType.PATH, unique=True)
# there are now two elements in the cache: a and lol
assert cache_info().currsize == 2
# we can retrieve the inserted element
lol = cached_get_entity_by(path='lol')
assert lol.id == 10001
# this did not touch the mocked function
assert mocked_get_by_path.call_count == 2
# make sure normal retrieval still works (count +1)
c = cached_get_entity_by(path='pp')
assert mocked_get_by_path.call_count == 3
assert c.id == 105
@patch("caosdb.cached.execute_query")
def test_cached_query(mocked_query):
mocked_query.side_effect = mocked_gen_query
# test cache initialization
cache_initialize(maxsize=10)
assert cache_info().maxsize == 10
# first call; not in cache -> mocked_execute is touched
res = cached_query('stuff')
assert len(res) == len(DUMMY_SERVER_CONTENT)
assert mocked_query.call_count == 1
# second call; in cache -> mocked_execute is NOT touched (count is still 1)
a = cached_query('stuff')
assert mocked_query.call_count == 1
# the cache returned the same object
assert a is res
# check the info
assert cache_info().hits == 1
assert cache_info().currsize == 1
# after clearing the test, the mock is used again
cache_clear()
cached_query('stuff')
assert mocked_query.call_count == 2
# we fill the cache manually and make sure the element is used
cache_fill({'lol': db.Container().extend([db.Entity(id=10001, name='lol')])},
AccessType.QUERY, unique=False)
# there are now two elements in the cache: a and lol
assert cache_info().currsize == 2
# we can retrieve the inserted element
lol = cached_query('lol')
assert lol[0].id == 10001
# this did not touch the mocked function
assert mocked_query.call_count == 2
# make sure normal retrieval still works (count +1)
c = cached_query('a')
assert mocked_query.call_count == 3
assert c[0].id == 101