test_data.py 15.2 KB
Newer Older
1 2 3 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
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

###############################################################################
#                                                                             #
# Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.backend.python module of the BEAT platform.   #
#                                                                             #
# Commercial License Usage                                                    #
# Licensees holding valid commercial BEAT licenses may use this file in       #
# accordance with the terms contained in a written agreement between you      #
# and Idiap. For further information contact tto@idiap.ch                     #
#                                                                             #
# Alternatively, this file may be used under the terms of the GNU Affero      #
# Public License version 3 as published by the Free Software and appearing    #
# in the file LICENSE.AGPL included in the packaging of this file.            #
# The BEAT platform is distributed in the hope that it will be useful, but    #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
#                                                                             #
# You should have received a copy of the GNU Affero Public License along      #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
#                                                                             #
###############################################################################


import unittest
import os
import glob
import tempfile
33
import shutil
34 35 36

from ..data import mixDataIndices
from ..data import CachedDataSource
37
from ..data import CachedDataSink
38 39 40 41
from ..data import getAllFilenames
from ..data import foundSplitRanges
from ..hash import hashFileContents
from ..dataformat import DataFormat
42
from ..database import Database
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

from . import prefix


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


class TestMixDataIndices(unittest.TestCase):

    def test_one_list(self):
        list_of_data_indices = [
            [(0, 2), (3, 4), (5, 6)]
        ]

        result = mixDataIndices(list_of_data_indices)

        self.assertEqual([(0, 2), (3, 4), (5, 6)], result)


    def test_two_identical_lists(self):
        list_of_data_indices = [
            [(0, 2), (3, 4), (5, 6)],
            [(0, 2), (3, 4), (5, 6)],
        ]

        result = mixDataIndices(list_of_data_indices)

        self.assertEqual([(0, 2), (3, 4), (5, 6)], result)


    def test_two_synchronized_lists(self):
        list_of_data_indices = [
            [(0, 2), (3, 4), (5, 6)],
            [(0, 4), (5, 6)],
        ]

        result = mixDataIndices(list_of_data_indices)

        self.assertEqual([(0, 2), (3, 4), (5, 6)], result)


    def test_two_desynchronized_lists(self):
        list_of_data_indices = [
            [(0, 2), (3, 4), (5, 6)],
            [(0, 1), (2, 4), (5, 6)],
        ]

        result = mixDataIndices(list_of_data_indices)

        self.assertEqual([(0, 1), (2, 2), (3, 4), (5, 6)], result)


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


class TestCachedDataBase(unittest.TestCase):

    def setUp(self):
        testfile = tempfile.NamedTemporaryFile(prefix=__name__, suffix='.data')
        testfile.close() # preserve only the name
        self.filename = testfile.name


    def tearDown(self):
        basename, ext = os.path.splitext(self.filename)
        filenames = [self.filename]
        filenames += glob.glob(basename + '*')
        for filename in filenames:
            if os.path.exists(filename):
                os.unlink(filename)


    def writeData(self, dataformat_name, start_index=0, end_index=10):
        dataformat = DataFormat(prefix, dataformat_name)
        self.assertTrue(dataformat.valid)

        data_sink = CachedDataSink()
120
        self.assertTrue(data_sink.setup(self.filename, dataformat, start_index, end_index))
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

        all_data = []
        for i in range(start_index, end_index + 1):
            data = dataformat.type()
            data_sink.write(data, i, i)
            all_data.append(data)

        (nb_bytes, duration) = data_sink.statistics()
        self.assertTrue(nb_bytes > 0)
        self.assertTrue(duration > 0)

        data_sink.close()
        del data_sink

        return all_data


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


class TestGetAllFilenames(TestCachedDataBase):

    def test_one_complete_data_file(self):
        self.writeData('user/single_integer/1', 0, 9)

        (data_filenames, indices_filenames, data_checksum_filenames, indices_checksum_filenames) = \
            getAllFilenames(self.filename)

        self.assertEqual(1, len(data_filenames))
        self.assertEqual(1, len(indices_filenames))
        self.assertEqual(1, len(data_checksum_filenames))
        self.assertEqual(1, len(indices_checksum_filenames))


    def test_three_complete_data_files(self):
        self.writeData('user/single_integer/1', 0, 9)
        self.writeData('user/single_integer/1', 10, 19)
        self.writeData('user/single_integer/1', 20, 29)

        (data_filenames, indices_filenames, data_checksum_filenames, indices_checksum_filenames) = \
            getAllFilenames(self.filename)

        self.assertEqual(3, len(data_filenames))
        self.assertEqual(3, len(indices_filenames))
        self.assertEqual(3, len(data_checksum_filenames))
        self.assertEqual(3, len(indices_checksum_filenames))


    def test_one_partial_data_file(self):
        self.writeData('user/single_integer/1', 0, 9)

        (data_filenames, indices_filenames, data_checksum_filenames, indices_checksum_filenames) = \
            getAllFilenames(self.filename, 2, 6)

        self.assertEqual(1, len(data_filenames))
        self.assertEqual(1, len(indices_filenames))
        self.assertEqual(1, len(data_checksum_filenames))
        self.assertEqual(1, len(indices_checksum_filenames))


    def test_three_partial_data_files_1(self):
        self.writeData('user/single_integer/1', 0, 9)
        self.writeData('user/single_integer/1', 10, 19)
        self.writeData('user/single_integer/1', 20, 29)

        (data_filenames, indices_filenames, data_checksum_filenames, indices_checksum_filenames) = \
            getAllFilenames(self.filename, 14, 18)

        self.assertEqual(1, len(data_filenames))
        self.assertEqual(1, len(indices_filenames))
        self.assertEqual(1, len(data_checksum_filenames))
        self.assertEqual(1, len(indices_checksum_filenames))


    def test_three_partial_data_files_2(self):
        self.writeData('user/single_integer/1', 0, 9)
        self.writeData('user/single_integer/1', 10, 19)
        self.writeData('user/single_integer/1', 20, 29)

        (data_filenames, indices_filenames, data_checksum_filenames, indices_checksum_filenames) = \
            getAllFilenames(self.filename, 4, 18)

        self.assertEqual(2, len(data_filenames))
        self.assertEqual(2, len(indices_filenames))
        self.assertEqual(2, len(data_checksum_filenames))
        self.assertEqual(2, len(indices_checksum_filenames))


    def test_three_partial_data_files_3(self):
        self.writeData('user/single_integer/1', 0, 9)
        self.writeData('user/single_integer/1', 10, 19)
        self.writeData('user/single_integer/1', 20, 29)

        (data_filenames, indices_filenames, data_checksum_filenames, indices_checksum_filenames) = \
            getAllFilenames(self.filename, 4, 28)

        self.assertEqual(3, len(data_filenames))
        self.assertEqual(3, len(indices_filenames))
        self.assertEqual(3, len(data_checksum_filenames))
        self.assertEqual(3, len(indices_checksum_filenames))


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


226
class TestCachedDataSource(TestCachedDataBase):
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

    def check_valid_indices(self, cached_file):
        for i in range(0, len(cached_file)):
            (data, start_index, end_index) = cached_file[i]
            self.assertTrue(data is not None)
            self.assertEqual(i + cached_file.first_data_index(), start_index)
            self.assertEqual(i + cached_file.first_data_index(), end_index)


    def check_valid_data_indices(self, cached_file):
        for i in range(0, len(cached_file)):
            (data, start_index, end_index) = cached_file.getAtDataIndex(i + cached_file.first_data_index())
            self.assertTrue(data is not None)
            self.assertEqual(i + cached_file.first_data_index(), start_index)
            self.assertEqual(i + cached_file.first_data_index(), end_index)


    def check_invalid_indices(self, cached_file):
        # Invalid indices
        (data, start_index, end_index) = cached_file[-1]
        self.assertTrue(data is None)

        (data, start_index, end_index) = cached_file[len(cached_file)]
        self.assertTrue(data is None)

        # Invalid data indices
        (data, start_index, end_index) = cached_file.getAtDataIndex(cached_file.first_data_index() - 1)
        self.assertTrue(data is None)

        (data, start_index, end_index) = cached_file.getAtDataIndex(cached_file.last_data_index() + 1)
        self.assertTrue(data is None)


    def test_one_complete_data_file(self):
        self.writeData('user/single_integer/1', 0, 9)

263
        cached_file = CachedDataSource()
264 265 266 267 268 269 270 271 272 273 274 275 276 277
        cached_file.setup(self.filename, prefix)

        self.assertEqual(10, len(cached_file))

        self.check_valid_indices(cached_file)
        self.check_valid_data_indices(cached_file)
        self.check_invalid_indices(cached_file)


    def test_three_complete_data_files(self):
        self.writeData('user/single_integer/1', 0, 9)
        self.writeData('user/single_integer/1', 10, 19)
        self.writeData('user/single_integer/1', 20, 29)

278
        cached_file = CachedDataSource()
279 280 281 282 283 284 285 286 287 288 289 290
        cached_file.setup(self.filename, prefix)

        self.assertEqual(30, len(cached_file))

        self.check_valid_indices(cached_file)
        self.check_valid_data_indices(cached_file)
        self.check_invalid_indices(cached_file)


    def test_one_partial_data_file(self):
        self.writeData('user/single_integer/1', 0, 9)

291
        cached_file = CachedDataSource()
292 293 294 295 296 297 298 299 300 301 302 303 304 305
        cached_file.setup(self.filename, prefix, 2, 6)

        self.assertEqual(5, len(cached_file))

        self.check_valid_indices(cached_file)
        self.check_valid_data_indices(cached_file)
        self.check_invalid_indices(cached_file)


    def test_three_partial_data_files_1(self):
        self.writeData('user/single_integer/1', 0, 9)
        self.writeData('user/single_integer/1', 10, 19)
        self.writeData('user/single_integer/1', 20, 29)

306
        cached_file = CachedDataSource()
307 308 309 310 311 312 313 314 315 316 317 318 319 320
        cached_file.setup(self.filename, prefix, 14, 18)

        self.assertEqual(5, len(cached_file))

        self.check_valid_indices(cached_file)
        self.check_valid_data_indices(cached_file)
        self.check_invalid_indices(cached_file)


    def test_three_partial_data_files_2(self):
        self.writeData('user/single_integer/1', 0, 9)
        self.writeData('user/single_integer/1', 10, 19)
        self.writeData('user/single_integer/1', 20, 29)

321
        cached_file = CachedDataSource()
322 323 324 325 326 327 328 329 330 331 332 333 334 335
        cached_file.setup(self.filename, prefix, 4, 18)

        self.assertEqual(15, len(cached_file))

        self.check_valid_indices(cached_file)
        self.check_valid_data_indices(cached_file)
        self.check_invalid_indices(cached_file)


    def test_three_partial_data_files_3(self):
        self.writeData('user/single_integer/1', 0, 9)
        self.writeData('user/single_integer/1', 10, 19)
        self.writeData('user/single_integer/1', 20, 29)

336
        cached_file = CachedDataSource()
337 338 339 340 341 342 343 344 345 346 347 348
        cached_file.setup(self.filename, prefix, 4, 28)

        self.assertEqual(25, len(cached_file))

        self.check_valid_indices(cached_file)
        self.check_valid_data_indices(cached_file)
        self.check_invalid_indices(cached_file)


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


349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
class TestDatabaseOutputDataSource(unittest.TestCase):

    def setUp(self):
        self.cache_root = tempfile.mkdtemp(prefix=__name__)


    def tearDown(self):
        shutil.rmtree(self.cache_root)


    def check_valid_indices(self, data_source):
        for i in range(0, len(data_source)):
            (data, start_index, end_index) = data_source[i]
            self.assertTrue(data is not None)
            self.assertEqual(i + data_source.first_data_index(), start_index)
            self.assertEqual(i + data_source.first_data_index(), end_index)


    def check_valid_data_indices(self, data_source):
        for i in range(0, len(data_source)):
            (data, start_index, end_index) = data_source.getAtDataIndex(i + data_source.first_data_index())
            self.assertTrue(data is not None)
            self.assertEqual(i + data_source.first_data_index(), start_index)
            self.assertEqual(i + data_source.first_data_index(), end_index)


    def check_invalid_indices(self, data_source):
        # Invalid indices
        (data, start_index, end_index) = data_source[-1]
        self.assertTrue(data is None)

        (data, start_index, end_index) = data_source[len(data_source)]
        self.assertTrue(data is None)

        # Invalid data indices
        (data, start_index, end_index) = data_source.getAtDataIndex(data_source.first_data_index() - 1)
        self.assertTrue(data is None)

        (data, start_index, end_index) = data_source.getAtDataIndex(data_source.last_data_index() + 1)
        self.assertTrue(data is None)


    def test(self):
        db = Database(prefix, 'integers_db/1')
        self.assertTrue(db.valid)

        view = db.view('double', 'double')
        view.index(os.path.join(self.cache_root, 'data.db'))
        view.setup(os.path.join(self.cache_root, 'data.db'), pack=False)

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

        for output_name, data_source in view.data_sources.items():
            self.assertEqual(9, len(data_source))

            self.check_valid_indices(data_source)
            self.check_valid_data_indices(data_source)
            self.check_invalid_indices(data_source)


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


413 414 415 416 417 418 419
class TestDataSink(TestCachedDataBase):

    def test_creation(self):
        dataformat = DataFormat(prefix, 'user/single_integer/1')
        self.assertTrue(dataformat.valid)

        data_sink = CachedDataSink()
420
        self.assertTrue(data_sink.setup(self.filename, dataformat, 0, 10))
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441


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


class TestFoundSplitRanges(unittest.TestCase):

    def test_2_splits(self):
        l = [[0,2,4,6,8,10,12], [0,3,6,9,12]]
        n_split = 2
        ref = [(0, 5), (6, 11)]
        res = foundSplitRanges(l, n_split)
        self.assertEqual(res, ref)


    def test_5_splits(self):
        l = [[0,2,4,6,8,10,12,15], [0,3,6,9,12,15]]
        n_split = 5
        ref = [(0, 5), (6, 11), (12, 14)]
        res = foundSplitRanges(l, n_split)
        self.assertEqual(res, ref)