def test_add_records_multi_channel(self, test_records1, test_records2):
        """Tests if right records added on calling add_records, add multiple channels."""
        formatted_test_records = {
            test_records1[0][2]: test_records1,
            test_records2[0][2]: test_records2,
        }

        test_slice = LevelSlice('dummy')
        assert test_slice._records == {}

        expected_test_records = formatted_test_records
        test_slice.add_records(formatted_test_records)
        assert test_slice._records == expected_test_records
    def test_save_member(self, test_records1):
        """Tests if object member records saved."""
        tmpfile = NamedTemporaryFile()
        test_save_slice = LevelSlice(tmpfile.name)

        formatted_test_records = {test_records1[0][2]: test_records1}
        test_save_slice.add_records(formatted_test_records)
        test_save_slice.save()

        test_read_slice = LevelSlice(tmpfile.name)
        test_read_slice.read()
        assert test_read_slice._records == formatted_test_records

        tmpfile.close()
    def _single_level_downsample(self, strategy, prev_level, curr_level,
                                 level_metadata):
        """Downsamples for one single level.

        Args:
            strategy: A string representing a downsampling strategy.
            prev_level: A string of the name of the current level.
            curr_level: A string of the name of the previous level.
            level_metadata: A metadata object for this level.

        Returns:
            A dict of metadata for the current level.
        """
        curr_slice_names = self._metadata['levels'][curr_level]['names']
        prev_slice_names = self._metadata['levels'][prev_level]['names']

        slice_index = 0
        curr_slice_path = utils.get_slice_path(
            self._preprocess_dir, curr_level,
            utils.get_slice_name(slice_index), strategy)
        curr_level_slice = LevelSlice(curr_slice_path,
                                      bucket=self._preprocess_bucket)

        for prev_slice_name in prev_slice_names:
            prev_slice_path = utils.get_slice_path(self._preprocess_dir,
                                                   prev_level, prev_slice_name,
                                                   strategy)
            prev_level_slice = LevelSlice(prev_slice_path,
                                          bucket=self._preprocess_bucket)
            prev_level_slice.read()
            prev_level_downsample = prev_level_slice.downsample(
                strategy, self._downsample_level_factor)
            curr_level_slice.add_records(prev_level_downsample)
            if curr_level_slice.get_records_count() >= self._number_per_slice:
                curr_level_slice.save()
                level_metadata[curr_slice_names[
                    slice_index]] = curr_level_slice.get_first_timestamp()
                slice_index += 1
                curr_slice_path = utils.get_slice_path(
                    self._preprocess_dir, curr_level,
                    utils.get_slice_name(slice_index), strategy)
                curr_level_slice = LevelSlice(curr_slice_path,
                                              bucket=self._preprocess_bucket)

        curr_level_slice.save()
        level_metadata[curr_slice_names[
            slice_index]] = curr_level_slice.get_first_timestamp()
        return level_metadata
    def test_add_records_single_channel(self, test_records1):
        """Tests if right records added on calling add_records, add single channel."""
        formatted_test_records = {test_records1[0][2]: test_records1}

        test_slice = LevelSlice('dummy')
        assert test_slice._records == {}

        expected_test_records = formatted_test_records
        test_slice.add_records(formatted_test_records)
        assert test_slice._records == expected_test_records

        expected_test_records = {
            test_records1[0][2]: test_records1 + test_records1
        }
        test_slice.add_records(formatted_test_records)
        assert test_slice._records == expected_test_records