test_format_load.py 7.27 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 six
import numpy
import nose.tools

from ..dataformat import DataFormat

from . import prefix, tmp_prefix
from .utils import cleanup

46

André Anjos's avatar
André Anjos committed
47 48
def test_load_default_format():

Philip ABBET's avatar
Philip ABBET committed
49
    df = DataFormat(prefix, data=None)
50
    nose.tools.assert_true(df.valid, "\n  * %s" % "\n  * ".join(df.errors))
51

André Anjos's avatar
André Anjos committed
52 53 54

def test_load_unknown_format():

55
    df = DataFormat(prefix, "user/unknown/1")
56 57
    nose.tools.assert_false(df.valid)
    nose.tools.assert_true(df.errors)
Philip ABBET's avatar
Philip ABBET committed
58
    nose.tools.eq_(len(df.errors), 1)
59
    nose.tools.assert_not_equal(df.errors[0].find("file not found"), -1)
60

André Anjos's avatar
André Anjos committed
61 62 63

def test_load_invalid_format():

64
    df = DataFormat(prefix, "user/invalid/1")
65 66
    nose.tools.assert_false(df.valid)
    nose.tools.assert_true(df.errors)
Philip ABBET's avatar
Philip ABBET committed
67
    nose.tools.eq_(len(df.errors), 1)
68
    nose.tools.assert_not_equal(df.errors[0].find("invalid JSON code"), -1)
69

André Anjos's avatar
André Anjos committed
70 71 72

def test_load_valid_format():

73
    df = DataFormat(prefix, "user/single_integer/1")
74
    nose.tools.assert_true(df.valid, "\n  * %s" % "\n  * ".join(df.errors))
Philip ABBET's avatar
Philip ABBET committed
75
    nose.tools.eq_(len(df.errors), 0)
André Anjos's avatar
André Anjos committed
76

77

André Anjos's avatar
André Anjos committed
78 79 80
@nose.tools.raises(RuntimeError)
def test_fail_to_create_data_of_unknown_format():

81
    df = DataFormat(prefix, "user/unknown/1")
82 83
    nose.tools.assert_false(df.valid)
    nose.tools.assert_true(df.type)
André Anjos's avatar
André Anjos committed
84

85

André Anjos's avatar
André Anjos committed
86 87
def test_fail_to_load_format_with_several_invalid_types():

88
    df = DataFormat(prefix, "user/errors/1")
89
    nose.tools.assert_false(df.valid)
Philip ABBET's avatar
Philip ABBET committed
90
    nose.tools.eq_(len(df.errors), 1)
91 92 93
    nose.tools.assert_not_equal(
        df.errors[0].find("is not valid under any of the given schemas"), -1
    )
94

André Anjos's avatar
André Anjos committed
95 96 97

def test_load_valid_format_from_JSON_declaration():

98
    df = DataFormat(prefix, dict(value="int32"))
99 100
    nose.tools.assert_true(df.valid, "\n  * %s" % "\n  * ".join(df.errors))
    nose.tools.eq_(df.name, "__unnamed_dataformat__")
101

André Anjos's avatar
André Anjos committed
102 103 104

def test_load_versioned_format():

105
    df1 = DataFormat(prefix, "user/versioned/1")
106
    nose.tools.assert_true(df1.valid, "\n  * %s" % "\n  * ".join(df1.errors))
107
    nose.tools.eq_(df1.name, "user/versioned/1")
André Anjos's avatar
André Anjos committed
108

109
    df2 = DataFormat(prefix, "user/versioned/2")
110
    nose.tools.assert_true(df2.valid, "\n  * %s" % "\n  * ".join(df2.errors))
111
    nose.tools.eq_(df2.name, "user/versioned/2")
André Anjos's avatar
André Anjos committed
112

Philip ABBET's avatar
Philip ABBET committed
113 114
    ftype = df2.type
    instance = ftype(value=numpy.float32(32))
115
    nose.tools.assert_true(isinstance(instance.value, numpy.float32))
André Anjos's avatar
André Anjos committed
116

117

André Anjos's avatar
André Anjos committed
118 119
def test_no_description():

120
    df = DataFormat(prefix, "user/versioned/1")
121 122 123
    nose.tools.assert_true(df.valid, "\n  * %s" % "\n  * ".join(df.errors))
    nose.tools.assert_is_none(df.description)
    nose.tools.assert_is_none(df.documentation)
André Anjos's avatar
André Anjos committed
124

125
    description = "This is my descriptor"
Philip ABBET's avatar
Philip ABBET committed
126
    df.description = description
127
    nose.tools.assert_true(isinstance(df.description, six.string_types))
Philip ABBET's avatar
Philip ABBET committed
128
    nose.tools.eq_(df.description, description)
André Anjos's avatar
André Anjos committed
129

130

André Anjos's avatar
André Anjos committed
131 132
def test_with_description():

133
    df = DataFormat(prefix, "user/versioned/2")
134 135 136 137
    nose.tools.assert_true(df.valid, "\n  * %s" % "\n  * ".join(df.errors))
    nose.tools.assert_true(isinstance(df.description, six.string_types))
    nose.tools.assert_not_equal(len(df.description), 0)
    nose.tools.assert_is_none(df.documentation)
André Anjos's avatar
André Anjos committed
138

139

André Anjos's avatar
André Anjos committed
140 141
def test_description_does_not_affect_hash():

142
    df2 = DataFormat(prefix, "user/versioned/2")
143
    nose.tools.assert_true(df2.valid, "\n  * %s" % "\n  * ".join(df2.errors))
144
    df3 = DataFormat(prefix, "user/versioned/3")  # the same, but no description
145 146
    nose.tools.assert_true(df3.valid, "\n  * %s" % "\n  * ".join(df3.errors))
    nose.tools.eq_(df2.hash(), df3.hash())
André Anjos's avatar
André Anjos committed
147

148

André Anjos's avatar
André Anjos committed
149 150
def test_load_direct_recursion():

151
    df = DataFormat(prefix, "user/direct_recursion/1")
152 153 154 155
    nose.tools.assert_false(df.valid)
    nose.tools.assert_true(df.errors)
    nose.tools.assert_not_equal(df.errors[0].find("recursion for"), -1)
    nose.tools.assert_not_equal(df.errors[0].find("user/direct_recursion/1"), -1)
156

André Anjos's avatar
André Anjos committed
157 158 159

def test_load_indirect_recursion():

160
    df = DataFormat(prefix, "user/indirect_recursion_top/1")
161 162
    nose.tools.assert_false(df.valid)
    nose.tools.assert_true(df.errors)
Philip ABBET's avatar
Philip ABBET committed
163
    nose.tools.eq_(len(df.errors), 1)
164 165 166 167
    nose.tools.assert_not_equal(df.errors[0].find("is invalid"), -1)
    nose.tools.assert_not_equal(
        df.errors[0].find("user/indirect_recursion_bottom/1"), -1
    )
168

André Anjos's avatar
André Anjos committed
169

170 171 172 173 174 175
def test_load_complex():
    """tests that the system accepts complex types in dataformats"""
    df = DataFormat(prefix, "user/complex/1")
    nose.tools.assert_true(df.valid, "\n  * %s" % "\n  * ".join(df.errors))


André Anjos's avatar
André Anjos committed
176 177 178
@nose.tools.with_setup(teardown=cleanup)
def test_export():

179
    name = "user/composed/1"
Philip ABBET's avatar
Philip ABBET committed
180
    obj = DataFormat(prefix, name)
181
    nose.tools.assert_true(obj.valid, "\n  * %s" % "\n  * ".join(obj.errors))
André Anjos's avatar
André Anjos committed
182

Philip ABBET's avatar
Philip ABBET committed
183
    obj.export(tmp_prefix)
André Anjos's avatar
André Anjos committed
184

Philip ABBET's avatar
Philip ABBET committed
185 186
    # load from tmp_prefix and validates
    exported = DataFormat(tmp_prefix, name)
187
    nose.tools.assert_true(exported.valid, "\n  * %s" % "\n  * ".join(exported.errors))