test_database_view.py 5.89 KB
Newer Older
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.            #
#                                                                                 #
###################################################################################
35 36


37
import os
Samuel GAIST's avatar
Samuel GAIST committed
38 39 40
import shutil
import tempfile
import unittest
41

42 43 44
from ddt import ddt
from ddt import idata

45 46
from ..database import Database
from . import prefix
Samuel GAIST's avatar
Samuel GAIST committed
47
from .test_database import INTEGERS_DBS
48

49
# ----------------------------------------------------------
50 51 52 53 54 55


class MyExc(Exception):
    pass


56
# ----------------------------------------------------------
57 58


59
@ddt
60 61 62
class TestDatabaseViewRunner(unittest.TestCase):
    def setUp(self):
        self.cache_root = tempfile.mkdtemp(prefix=__name__)
63

64 65
    def tearDown(self):
        shutil.rmtree(self.cache_root)
66

67
    def test_syntax_error(self):
68
        db = Database(prefix, "syntax_error/1")
69
        self.assertTrue(db.valid)
70

71
        with self.assertRaises(SyntaxError):
72
            db.view("protocol", "set")
73

74
    def test_unknown_view(self):
75
        db = Database(prefix, "integers_db/1")
76
        self.assertTrue(db.valid)
77

78
        with self.assertRaises(KeyError):
79
            db.view("protocol", "does_not_exist")
80

81 82 83
    @idata(INTEGERS_DBS)
    def test_valid_view(self, db_name):
        db = Database(prefix, db_name)
84
        self.assertTrue(db.valid)
85

86
        view = db.view("double", "double")
87
        self.assertTrue(view is not None)
88

89
    def test_indexing_crash(self):
90
        db = Database(prefix, "crash/1")
91
        self.assertTrue(db.valid)
92

93
        view = db.view("protocol", "index_crashes", MyExc)
94

95
        with self.assertRaises(MyExc):
96
            view.index(os.path.join(self.cache_root, "data.db"))
97

98
    def test_get_crash(self):
99
        db = Database(prefix, "crash/1")
100
        self.assertTrue(db.valid)
101

102 103 104
        view = db.view("protocol", "get_crashes", MyExc)
        view.index(os.path.join(self.cache_root, "data.db"))
        view.setup(os.path.join(self.cache_root, "data.db"))
105

106
        with self.assertRaises(MyExc):
107
            view.get("a", 0)
108

109
    def test_not_setup(self):
110
        db = Database(prefix, "crash/1")
111
        self.assertTrue(db.valid)
112

113
        view = db.view("protocol", "get_crashes", MyExc)
114

115
        with self.assertRaises(MyExc):
116
            view.get("a", 0)
117

118 119 120
    @idata(INTEGERS_DBS)
    def test_success(self, db_name):
        db = Database(prefix, db_name)
121
        self.assertTrue(db.valid)
122

123 124 125
        view = db.view("double", "double", MyExc)
        view.index(os.path.join(self.cache_root, "data.db"))
        view.setup(os.path.join(self.cache_root, "data.db"))
126

127
        self.assertTrue(view.data_sources is not None)
128
        self.assertEqual(len(view.data_sources), 3)
129

130
        for i in range(0, 9):
131 132 133
            self.assertEqual(view.get("a", i)["value"], i + 1)
            self.assertEqual(view.get("b", i)["value"], (i + 1) * 10)
            self.assertEqual(view.get("sum", i)["value"], (i + 1) * 10 + i + 1)
134 135

    def test_success_using_keywords(self):
136
        db = Database(prefix, "python_keyword/1")
137 138
        self.assertTrue(db.valid)

139 140 141
        view = db.view("keyword", "keyword", MyExc)
        view.index(os.path.join(self.cache_root, "data.db"))
        view.setup(os.path.join(self.cache_root, "data.db"))
142 143 144 145 146

        self.assertTrue(view.data_sources is not None)
        self.assertEqual(len(view.data_sources), 3)

        for i in range(0, 9):
147 148 149
            self.assertEqual(view.get("class", i)["value"], i + 1)
            self.assertEqual(view.get("def", i)["value"], (i + 1) * 10)
            self.assertEqual(view.get("sum", i)["value"], (i + 1) * 10 + i + 1)