File: readwriteobj_unit_test.py

package info (click to toggle)
python-sigima 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 24,956 kB
  • sloc: python: 33,326; makefile: 3
file content (67 lines) | stat: -rw-r--r-- 2,062 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.

"""
I/O test

Testing `sigima` specific formats.
"""

from __future__ import annotations

import os.path as osp

from sigima.io.image import ImageIORegistry
from sigima.io.signal import SignalIORegistry
from sigima.tests.env import execenv
from sigima.tests.helpers import WorkdirRestoringTempDir, read_test_objects, reduce_path


def __testfunc(
    title: str,
    registry: SignalIORegistry | ImageIORegistry,
    pattern: str = "*.*",
    in_folder: str | None = None,
) -> None:
    """Test I/O features: read and write objects, and check registry functionality

    Args:
        title: Title of the test
        registry: I/O registry to use
        pattern: File name pattern to match
        in_folder: Folder to search for test files

    Raises:
        NotImplementedError: if format is not supported
    """
    execenv.print(f"  {title}:")
    with WorkdirRestoringTempDir() as tmpdir:
        # os.startfile(tmpdir)
        objects = {}
        for fname, obj in read_test_objects(registry, pattern, in_folder):
            label = f"    Opening {reduce_path(fname)}"
            execenv.print(label + ": ", end="")
            if obj is None:
                execenv.print("Skipped (not implemented)")
            else:
                execenv.print("OK")
                objects[fname] = obj
        execenv.print("    Saving:")
        for fname, obj in objects.items():
            path = osp.join(tmpdir, osp.basename(fname))
            try:
                execenv.print(f"      {path}: ", end="")
                registry.write(path, obj)
                execenv.print("OK")
            except NotImplementedError:
                execenv.print("Skipped (not implemented)")


def test_read_write_obj():
    """I/O test: read and write objects, check registry functionality"""
    execenv.print("I/O unit test:")
    __testfunc("Signals", SignalIORegistry)
    __testfunc("Images", ImageIORegistry)


if __name__ == "__main__":
    test_read_write_obj()