test_algorithm_dependencies.py 5.77 KB
Newer Older
André Anjos's avatar
André Anjos committed
1 2 3
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

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
###################################################################################
#                                                                                 #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/               #
# Contact: beat.support@idiap.ch                                                  #
#                                                                                 #
# Redistribution and use in source and binary forms, with or without              #
# modification, are permitted provided that the following conditions are met:     #
#                                                                                 #
# 1. Redistributions of source code must retain the above copyright notice, this  #
# list of conditions and the following disclaimer.                                #
#                                                                                 #
# 2. Redistributions in binary form must reproduce the above copyright notice,    #
# this list of conditions and the following disclaimer in the documentation       #
# and/or other materials provided with the distribution.                          #
#                                                                                 #
# 3. Neither the name of the copyright holder nor the names of its contributors   #
# may be used to endorse or promote products derived from this software without   #
# specific prior written permission.                                              #
#                                                                                 #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   #
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE          #
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE    #
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL      #
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR      #
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER      #
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,   #
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   #
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.            #
#                                                                                 #
###################################################################################
André Anjos's avatar
André Anjos committed
35 36 37 38 39 40


import nose.tools

from ..algorithm import Algorithm, Storage
from ..library import Library, Storage as LibraryStorage
41
from ..dataformat import Storage as DataFormatStorage
André Anjos's avatar
André Anjos committed
42 43 44 45

from . import prefix, tmp_prefix
from .utils import cleanup

46

47
def copy_objects(algorithm, library):
André Anjos's avatar
André Anjos committed
48

49 50 51
    alg = Algorithm(prefix, algorithm)
    storage = Storage(tmp_prefix, alg.name)
    alg.write(storage)
André Anjos's avatar
André Anjos committed
52

53 54 55
    for dataformat in alg.dataformats:
        storage = DataFormatStorage(tmp_prefix, dataformat)
        alg.dataformats[dataformat].write(storage)
André Anjos's avatar
André Anjos committed
56

57 58 59
    lib = Library(prefix, library)
    storage = LibraryStorage(tmp_prefix, lib.name)
    lib.write(storage)
André Anjos's avatar
André Anjos committed
60 61 62 63


@nose.tools.with_setup(teardown=cleanup)
def test_dependencies():
64 65
    name = "user/for_dep/1"
    dep_name = "user/dep/1"
André Anjos's avatar
André Anjos committed
66

Philip ABBET's avatar
Philip ABBET committed
67
    copy_objects(name, dep_name)
André Anjos's avatar
André Anjos committed
68

69 70
    alg = Algorithm(tmp_prefix, name)
    nose.tools.assert_true(alg.valid, "\n  * %s" % "\n  * ".join(alg.errors))
71
    nose.tools.assert_is_none(alg.uses)
72
    nose.tools.eq_(len(alg.libraries), 0)
André Anjos's avatar
André Anjos committed
73

Philip ABBET's avatar
Philip ABBET committed
74
    l_dep = Library(tmp_prefix, dep_name)
75
    nose.tools.assert_true(l_dep.valid, "\n  * %s" % "\n  * ".join(l_dep.errors))
André Anjos's avatar
André Anjos committed
76

Philip ABBET's avatar
Philip ABBET committed
77
    # check modification
78
    alg.uses = {"dep1": dep_name}
79 80 81
    alg.write()
    alg = Algorithm(tmp_prefix, name)
    nose.tools.assert_true(alg.valid, "\n  * %s" % "\n  * ".join(alg.errors))
André Anjos's avatar
André Anjos committed
82

83 84 85 86
    nose.tools.eq_(len(alg.uses), 1)
    nose.tools.eq_(len(alg.libraries), 1)
    nose.tools.eq_(list(alg.uses.keys())[0], "dep1")
    nose.tools.eq_(list(alg.uses.values())[0], "user/dep/1")
André Anjos's avatar
André Anjos committed
87

88 89 90 91 92
    alg.uses = {}
    alg.uses["mod1"] = dep_name
    alg.write()
    alg = Algorithm(tmp_prefix, name)
    nose.tools.assert_true(alg.valid, "\n  * %s" % "\n  * ".join(alg.errors))
André Anjos's avatar
André Anjos committed
93

94 95 96 97
    nose.tools.eq_(len(alg.uses), 1)
    nose.tools.eq_(len(alg.libraries), 1)
    nose.tools.eq_(list(alg.uses.keys())[0], "mod1")
    nose.tools.eq_(list(alg.uses.values())[0], "user/dep/1")
André Anjos's avatar
André Anjos committed
98

99 100 101 102
    alg.uses = {}
    alg.write()
    alg = Algorithm(tmp_prefix, name)
    nose.tools.assert_true(alg.valid, "\n  * %s" % "\n  * ".join(alg.errors))
André Anjos's avatar
André Anjos committed
103

104 105
    nose.tools.eq_(len(alg.uses), 0)
    nose.tools.eq_(len(alg.libraries), 0)
André Anjos's avatar
André Anjos committed
106 107 108 109 110


@nose.tools.with_setup(teardown=cleanup)
def test_invalid_dependencies():

111
    name = "user/for_dep/1"
112
    dep_name = "errors/invalid_dep/1"
André Anjos's avatar
André Anjos committed
113

Philip ABBET's avatar
Philip ABBET committed
114
    copy_objects(name, dep_name)
André Anjos's avatar
André Anjos committed
115

116 117
    alg = Algorithm(tmp_prefix, name)
    nose.tools.assert_true(alg.valid, "\n  * %s" % "\n  * ".join(alg.errors))
118
    nose.tools.assert_is_none(alg.uses)
119
    nose.tools.eq_(len(alg.libraries), 0)
André Anjos's avatar
André Anjos committed
120

121
    l_dep = Library(tmp_prefix, "errors/invalid_dep/1")
122
    nose.tools.assert_true(l_dep.valid, "\n  * %s" % "\n  * ".join(l_dep.errors))
André Anjos's avatar
André Anjos committed
123

124
    alg.uses = {"dep": dep_name}
125 126 127 128
    alg.write()
    alg = Algorithm(tmp_prefix, name)
    nose.tools.assert_false(alg.valid)
    nose.tools.assert_not_equal(alg.errors[0].find("differs from current language"), -1)
129 130 131 132 133 134 135 136 137 138 139 140 141 142


@nose.tools.with_setup(teardown=cleanup)
def test_invalid_dependency_setup():
    name = "user/for_dep/1"
    dep_name = "user/dep/1"

    copy_objects(name, dep_name)

    alg = Algorithm(tmp_prefix, name)
    nose.tools.assert_true(alg.valid, "\n  * %s" % "\n  * ".join(alg.errors))
    nose.tools.assert_is_none(alg.uses)
    with nose.tools.assert_raises(RuntimeError):
        alg.uses = "dummy"