Skip to content
Snippets Groups Projects
Select Git revision
  • 59ddf680a79e6d5481a218be4d15bb89c1908759
  • main default protected
  • f-xlsx-list-file
  • f-yaml-parser-enums
  • dev protected
  • f-fix-paths
  • f-fix-validate-to-dict
  • f-labfolder-converter
  • f-state-machine-script
  • f-xlsx-converter-warnings-errors
  • f-rename
  • f-extra-deps
  • f-more-jsonschema-export
  • f-henrik
  • f-fix-89
  • f-trigger-advanced-user-tools
  • f-real-rename-test
  • f-linkahead-rename
  • f-register-integrationtests
  • f-fix-id
  • f-h5-files
  • v0.14.0
  • v0.13.0
  • v0.12.0
  • v0.11.0
  • v0.10.0-numpy2
  • v0.10.0
  • v0.9.0
  • v0.8.0
  • v0.7.0
  • v0.6.1
  • v0.6.0
  • v0.5.0
  • v0.4.1
  • v0.4.0
  • v0.3.1
  • v0.3.0
37 results

test_utils.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    test_file_identifiables.py 2.50 KiB
    #!/bin/python
    # Tests for file identifiables
    # A. Schlemmer, 06/2021
    
    import caosdb as db
    
    import pytest
    from pytest import raises
    from unittest.mock import patch, Mock
    
    from caoscrawler.identifiable_adapters import LocalStorageIdentifiableAdapter
    from caosdb.cached import cache_clear
    from caoscrawler.identifiable import Identifiable
    
    from caosdb.exceptions import EmptyUniqueQueryError
    
    
    @pytest.fixture(autouse=True)
    def clear_cache():
        cache_clear()
    
    
    def mock_get_entity_by(eid=None, name=None, path=None):
        if eid is not None:
            candidates = [el for el in EXAMPLE_SERVER_STATE if el.id == eid]
            if len(candidates) > 0:
                return candidates[0]
            else:
                raise EmptyUniqueQueryError("")
        raise EmptyUniqueQueryError("")
    
    
    @patch("caoscrawler.identifiable_adapters.cached_get_entity_by",
           new=Mock(side_effect=mock_get_entity_by))
    def test_file_identifiable():
        ident = LocalStorageIdentifiableAdapter()
    
        # Without a path there is no identifying information
        with raises(ValueError):
            ident.get_identifiable(db.File(), [])
    
        fp = "/test/bla/bla.txt"
        file_obj = db.File(path=fp)
        identifiable = ident.get_identifiable(file_obj)
    
        # the path is copied to the identifiable
        assert fp == identifiable.path
        assert isinstance(identifiable, Identifiable)
    
        # __eq__ function is only defined for Identifiable objects
        with raises(ValueError):
            file_obj != identifiable
    
        # since the path does not exist in the data in ident, the follwoing functions return None
        with raises(EmptyUniqueQueryError):
            ident.retrieve_identified_record_for_record(file_obj)
        assert ident.get_file(identifiable) is None
    
        # Try again with actual files in the store:
        records = ident.get_records()
        test_record_wrong_path = db.File(path="/bla/bla/test.txt")
        test_record_correct_path = db.File(path="/test/bla/bla.txt")
        test_record_alsocorrect_path = db.File(path="/test/bla/bla.txt")
        records.append(test_record_wrong_path)
        # Now, there is a file, but still wrong path -> result is still None
        identified_file = ident.get_file(file_obj)
        assert identified_file is None
    
        records.append(test_record_correct_path)
        # now there is a match
        identified_file = ident.get_file(file_obj)
        assert identified_file is not None
        assert identified_file.path == file_obj.path
    
        with raises(RuntimeError, match=".*unambigiously.*"):
            records.append(test_record_alsocorrect_path)
            identified_file = ident.get_file(file_obj)