Skip to content
Snippets Groups Projects
Select Git revision
  • dec8858e46b4955dea5ad3ae1834b339f6d8c88e
  • main default protected
  • dev
  • f-unmod
  • f-checkidentical
  • f-simple-breakpoint
  • f-new-debug-tree
  • f-existing-file-id
  • f-no-ident
  • f-collect-problems
  • f-refactor-debug-tree
  • v0.13.0
  • v0.12.0
  • v0.11.0
  • v0.10.1
  • v0.10.0
  • v0.9.1
  • v0.9.0
  • v0.8.0
  • v0.7.1
  • v0.7.0
  • v0.6.0
  • v0.5.0
  • v0.4.0
  • v0.3.0
  • v0.2.0
  • v0.1.0
27 results

test_tool.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    test_h5_converter.py 3.13 KiB
    #
    # This file is a part of the CaosDB Project.
    #
    # Copyright (C) 2023 IndiScale GmbH <info@indiscale.com>
    # Copyright (C) 2023 Florian Spreckelsen <f.spreckelsen@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/>.
    #
    import numpy as np
    
    from pathlib import Path
    from pytest import fixture, importorskip
    
    from caoscrawler.debug_tree import DebugTree
    from caoscrawler.hdf5_converter import (convert_basic_element_with_nd_array,
                                            convert_h5_element, H5GroupElement,
                                            H5DatasetElement, H5NdarrayElement)
    from caoscrawler.scanner import scan_directory
    from caoscrawler.structure_elements import (FloatElement, ListElement,
                                                TextElement)
    
    # Skip the whole module if h5py hasn't been installed
    h5py = importorskip("h5py")
    
    
    UNITTESTDIR = Path(__file__).parent
    
    
    @fixture
    def h5_dummy_file():
    
        path = UNITTESTDIR / "hdf5_dummy_file.hdf5"
    
        return h5py.File(path, 'r')
    
    
    def test_h5_elements(h5_dummy_file):
    
        elt = convert_h5_element(h5_dummy_file["group_level1_a"], "test")
        assert isinstance(elt, H5GroupElement)
    
        elt = convert_h5_element(h5_dummy_file["root_integers"], "test")
        assert isinstance(elt, H5DatasetElement)
    
    
    def test_nd_array_conversion():
    
        # Only test array handling here, `convert_basic_element` is tested
        # elsewhere.
        arr = np.array([[["something"]]])
        elt = convert_basic_element_with_nd_array(arr)
        assert isinstance(elt, TextElement)
        assert elt.value == "something"
    
        arr = np.zeros((1, 1))
        elt = convert_basic_element_with_nd_array(arr)
        assert isinstance(elt, FloatElement)
        assert elt.value == 0
    
        arr = np.zeros((1, 3, 1))
        elt = convert_basic_element_with_nd_array(arr)
        assert isinstance(elt, ListElement)
        assert elt.value == [0, 0, 0]
    
        arr = np.array([[1, 2, 3], [4, 5, 6]])
        elt = convert_basic_element_with_nd_array(arr, internal_path="some/path")
        assert isinstance(elt, H5NdarrayElement)
        assert elt.internal_path == "some/path"
    
        # Non-arrays should be forwarded correctly
        elt = convert_basic_element_with_nd_array("something")
        assert isinstance(elt, TextElement)
        assert elt.value == "something"
    
        elt = convert_basic_element_with_nd_array([0, 0, 0])
        assert isinstance(elt, ListElement)
        assert elt.value == [0, 0, 0]
    
    def test_record_creation():
    
        dbtr = DebugTree()
        records = scan_directory(UNITTESTDIR, UNITTESTDIR / "h5_cfood.yml", debug_tree=dbtr)
        print(dbtr.debug_tree)
        print(dbtr.debug_metadata)
        assert False