test_algorithm_dependencies.py 5.09 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 41 42 43 44 45


import nose.tools

from ..algorithm import Algorithm, Storage
from ..library import Library, Storage as LibraryStorage
from ..dataformat import DataFormat, Storage as DataFormatStorage

from . import prefix, tmp_prefix
from .utils import cleanup

46

André Anjos's avatar
André Anjos committed
47 48
def copy_objects(algo, lib):

Philip ABBET's avatar
Philip ABBET committed
49 50 51
    a = Algorithm(prefix, algo)
    storage = Storage(tmp_prefix, a.name)
    a.write(storage)
André Anjos's avatar
André Anjos committed
52

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

Philip ABBET's avatar
Philip ABBET committed
57 58 59
    l = Library(prefix, lib)
    storage = LibraryStorage(tmp_prefix, l.name)
    l.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

Philip ABBET's avatar
Philip ABBET committed
69
    a = Algorithm(tmp_prefix, name)
70
    assert a.valid, "\n  * %s" % "\n  * ".join(a.errors)
Philip ABBET's avatar
Philip ABBET committed
71 72
    nose.tools.eq_(len(a.uses), 0)
    nose.tools.eq_(len(a.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
    assert 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
    a.uses["dep1"] = dep_name
Philip ABBET's avatar
Philip ABBET committed
79 80
    a.write()
    a = Algorithm(tmp_prefix, name)
81
    assert a.valid, "\n  * %s" % "\n  * ".join(a.errors)
André Anjos's avatar
André Anjos committed
82

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

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

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

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

Philip ABBET's avatar
Philip ABBET committed
104 105
    nose.tools.eq_(len(a.uses), 0)
    nose.tools.eq_(len(a.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 112
    name = "user/for_dep/1"
    dep_name = "user/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

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

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

124
    a.uses["dep"] = dep_name
Philip ABBET's avatar
Philip ABBET committed
125 126 127
    a.write()
    a = Algorithm(tmp_prefix, name)
    assert not a.valid
128
    assert a.errors[0].find("differs from current language") != -1