test_database.py 6.06 KB
Newer Older
1
2
3
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

Samuel GAIST's avatar
Samuel GAIST committed
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.            #
#                                                                                 #
###################################################################################
35
36
37
38
39
40
41
42


import nose.tools
from ..database import Database

from . import prefix


Samuel GAIST's avatar
Samuel GAIST committed
43
44
45
46
INTEGERS_DBS = ["integers_db/{}".format(i) for i in range(1, 3)]


# ----------------------------------------------------------
47
48
49
50
51


def load(database_name):

    database = Database(prefix, database_name)
Samuel GAIST's avatar
Samuel GAIST committed
52
    nose.tools.assert_true(database.valid, "\n  * %s" % "\n  * ".join(database.errors))
53
54
55
    return database


Samuel GAIST's avatar
Samuel GAIST committed
56
# ----------------------------------------------------------
57
58
59
60


def test_load_valid_database():

Samuel GAIST's avatar
Samuel GAIST committed
61
62
63
    for db_name in INTEGERS_DBS:
        yield load_valid_database, db_name

64

Samuel GAIST's avatar
Samuel GAIST committed
65
66
def load_valid_database(db_name):
    database = load(db_name)
67
68
69
70
71
    nose.tools.eq_(len(database.sets("double")), 1)
    nose.tools.eq_(len(database.sets("triple")), 1)
    nose.tools.eq_(len(database.sets("two_sets")), 2)


Samuel GAIST's avatar
Samuel GAIST committed
72
# ----------------------------------------------------------
73
74
75
76


def test_load_protocol_with_one_set():

Samuel GAIST's avatar
Samuel GAIST committed
77
78
79
80
81
82
83
    for db_name in INTEGERS_DBS:
        yield load_valid_database, db_name


def load_protocol_with_one_set(db_name):

    database = load(db_name)
84
85

    protocol = database.protocol("double")
Samuel GAIST's avatar
Samuel GAIST committed
86
    nose.tools.eq_(len(protocol["sets"]), 1)
87

Samuel GAIST's avatar
Samuel GAIST committed
88
    set_ = database.set("double", "double")
89

Samuel GAIST's avatar
Samuel GAIST committed
90
91
    nose.tools.eq_(set_["name"], "double")
    nose.tools.eq_(len(set_["outputs"]), 3)
92

Samuel GAIST's avatar
Samuel GAIST committed
93
94
95
    nose.tools.assert_is_not_none(set_["outputs"]["a"])
    nose.tools.assert_is_not_none(set_["outputs"]["b"])
    nose.tools.assert_is_not_none(set_["outputs"]["sum"])
96
97


Samuel GAIST's avatar
Samuel GAIST committed
98
# ----------------------------------------------------------
99
100
101
102


def test_load_protocol_with_two_sets():

Samuel GAIST's avatar
Samuel GAIST committed
103
104
105
106
107
108
109
    for db_name in INTEGERS_DBS:
        yield load_valid_database, db_name


def load_protocol_with_two_sets(db_name):

    database = load(db_name)
110
111

    protocol = database.protocol("two_sets")
Samuel GAIST's avatar
Samuel GAIST committed
112
    nose.tools.eq_(len(protocol["sets"]), 2)
113

Samuel GAIST's avatar
Samuel GAIST committed
114
    set_ = database.set("two_sets", "double")
115

Samuel GAIST's avatar
Samuel GAIST committed
116
117
    nose.tools.eq_(set["name"], "double")
    nose.tools.eq_(len(set["outputs"]), 3)
118

Samuel GAIST's avatar
Samuel GAIST committed
119
120
121
    nose.tools.assert_is_not_none(set_["outputs"]["a"])
    nose.tools.assert_is_not_none(set_["outputs"]["b"])
    nose.tools.assert_is_not_none(set_["outputs"]["sum"])
122

Samuel GAIST's avatar
Samuel GAIST committed
123
    set_ = database.set("two_sets", "triple")
124

Samuel GAIST's avatar
Samuel GAIST committed
125
126
    nose.tools.eq_(set_["name"], "triple")
    nose.tools.eq_(len(set_["outputs"]), 4)
127

Samuel GAIST's avatar
Samuel GAIST committed
128
129
130
131
    nose.tools.assert_is_not_none(set_["outputs"]["a"])
    nose.tools.assert_is_not_none(set_["outputs"]["b"])
    nose.tools.assert_is_not_none(set_["outputs"]["c"])
    nose.tools.assert_is_not_none(set_["outputs"]["sum"])
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151


# ----------------------------------------------------------


def test_view_definitions():
    yield compare_definitions, "integers_db", "double", "double"
    yield compare_definitions, "with_parameters", "test_with_parameters", "double"
    yield compare_definitions, "with_parameters", "test_with_empty_parameters", "double"


def compare_definitions(db_name, protocol_name, view_name):

    db_1 = load("{}/1".format(db_name))
    db_1_view_definition = db_1.view_definition(protocol_name, view_name)
    db_1_view_definition.pop("template")  # Unused property

    db_2 = load("{}/2".format(db_name))
    db_2_view_definition = db_2.view_definition(protocol_name, view_name)

152
153
154
155
156
157
158
159
160
161
162
163
    db_1_sorted = sorted(db_1_view_definition)
    db_2_sorted = sorted(db_2_view_definition)
    nose.tools.eq_(db_1_sorted, db_2_sorted)


# ----------------------------------------------------------


def test_duplicate_key_error():
    database = Database(prefix, "duplicate_key_error/1")
    nose.tools.assert_false(database.valid)
    nose.tools.assert_true("Database declaration file invalid" in database.errors[0])