test_vtk_surface_model_directory_loader.py 2.69 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3

import os
4
import platform
5
import six
6 7
import pytest

8
from sksurgeryvtk.models.vtk_surface_model_directory_loader import VTKSurfaceModelDirectoryLoader
9

10

11 12
@pytest.fixture(scope="function")
def valid_vtk_model():
13
    input_file = 'tests/data/models/tests/Prostate.vtk'
14 15 16 17 18 19 20 21 22 23 24 25 26
    model = VTKModel(input_file, colors.red)
    return model


def test_invalid_because_null_directory_name():
    with pytest.raises(ValueError):
        VTKSurfaceModelDirectoryLoader(None)


def test_invalid_because_empty_directory_name():
    with pytest.raises(ValueError):
        VTKSurfaceModelDirectoryLoader("")

27 28
def print_dir_permissions(dir_name):
        print("R: {} W: {} X: {}".format(os.access(dir_name, os.R_OK),
29 30
                                     os.access(dir_name, os.W_OK),
                                     os.access(dir_name, os.X_OK)))
Thomas Dowrick's avatar
Thomas Dowrick committed
31
    
32 33

def test_invalid_because_directory_not_readable():
34 35

    if platform.system() == 'Windows' or not platform.system():
36
        six.print_('Not running test as Windows doesnt do permissions.')
37 38
        return
    
39 40 41 42
    output_name = 'tests/output/'
    if not os.path.exists(output_name):
        os.mkdir(output_name)
    dir_name = 'tests/output/unreadable'
43 44
    if not os.path.exists(dir_name):
        os.mkdir(dir_name)
45 46
    print_dir_permissions(dir_name)

47
    os.chmod(dir_name, 0o000)
48
    print_dir_permissions(dir_name)
49 50
    with pytest.raises(ValueError):
        VTKSurfaceModelDirectoryLoader(dir_name)
51
    os.chmod(dir_name, 0o100)
52
    print_dir_permissions(dir_name)
53 54
    with pytest.raises(ValueError):
        VTKSurfaceModelDirectoryLoader(dir_name)
55
    os.chmod(dir_name, 0o400)
56
    print_dir_permissions(dir_name)
57 58
    with pytest.raises(ValueError):
        VTKSurfaceModelDirectoryLoader(dir_name)
59
    os.chmod(dir_name, 0o500)
60
    print_dir_permissions(dir_name)
61
    VTKSurfaceModelDirectoryLoader(dir_name)  # should instantiate, but no data.
62 63 64
    os.rmdir(dir_name)

def test_valid_dir_with_default_colours():
65
    dir_name = 'tests/data/models/Kidney'
66 67 68 69 70 71 72
    loader = VTKSurfaceModelDirectoryLoader(dir_name)
    assert len(loader.models) == 2
    assert loader.models[0].get_colour() == loader.colours[str(0)]
    assert loader.models[1].get_colour() == loader.colours[str(1)]


def test_valid_dir_with_colours_from_file_from_issue_4():
73
    dir_name = 'tests/data/models/Liver'
74 75
    loader = VTKSurfaceModelDirectoryLoader(dir_name)
    assert len(loader.models) == 9
76 77 78 79

def test_consistent_model_order_issue_89():
    dir_name = 'tests/data/models/Liver'
    loader = VTKSurfaceModelDirectoryLoader(dir_name)
80 81 82
    assert loader.models[0].get_name() == "arteries.vtk"
    assert loader.models[3].get_name() == "gastric_vein.vtk"
    assert loader.models[5].get_name() == "liver.vtk"
83