Skip to content
Snippets Groups Projects
Select Git revision
  • 5634580792a3603e2f294aec196595e81d02b428
  • main default protected
  • dev protected
  • f-fix-accent-sensitivity
  • f-filesystem-import
  • f-update-acl
  • f-filesystem-link
  • f-filesystem-directory
  • f-filesystem-core
  • f-filesystem-cleanup
  • f-string-ids
  • f-filesystem-main
  • f-multipart-encoding
  • f-trigger-advanced-user-tools
  • f-real-rename-test-pylibsolo2
  • f-real-rename-test-pylibsolo
  • f-real-rename-test
  • f-linkahead-rename
  • f-reference-record
  • f-xml-serialization
  • f-xfail-server-181
  • linkahead-pylib-v0.18.0
  • linkahead-control-v0.16.0
  • linkahead-pylib-v0.17.0
  • linkahead-mariadbbackend-v8.0.0
  • linkahead-server-v0.13.0
  • caosdb-pylib-v0.15.0
  • caosdb-pylib-v0.14.0
  • caosdb-pylib-v0.13.2
  • caosdb-server-v0.12.1
  • caosdb-pylib-v0.13.1
  • caosdb-pylib-v0.12.0
  • caosdb-server-v0.10.0
  • caosdb-pylib-v0.11.1
  • caosdb-pylib-v0.11.0
  • caosdb-server-v0.9.0
  • caosdb-pylib-v0.10.0
  • caosdb-server-v0.8.1
  • caosdb-pylib-v0.8.0
  • caosdb-server-v0.8.0
  • caosdb-pylib-v0.7.2
41 results

test_update.py

Blame
  • 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