Пример #1
0
    def test_aggregate(self):
        """
        aggregate functions
        """
        # min of 1-4
        self.assertEqual(whisper.aggregate('min', [1, 2, 3, 4]), 1)
        # max of 1-4
        self.assertEqual(whisper.aggregate('max', [1, 2, 3, 4]), 4)
        # last element in the known values
        self.assertEqual(whisper.aggregate('last', [3, 2, 5, 4]), 4)
        # sum ALL THE VALUES!
        self.assertEqual(whisper.aggregate('sum', [10, 2, 3, 4]), 19)
        # average of the list elements
        self.assertEqual(whisper.aggregate('average', [1, 2, 3, 4]), 2.5)
        avg_zero = [1, 2, 3, 4, None, None, None, None]
        non_null = [i for i in avg_zero if i is not None]
        self.assertEqual(whisper.aggregate('avg_zero', non_null, avg_zero), 1.25)
        # avg_zero without neighborValues
        with self.assertRaises(whisper.InvalidAggregationMethod):
            whisper.aggregate('avg_zero', non_null)
        # absmax with negative max
        self.assertEqual(whisper.aggregate('absmax', [-3, -2, 1, 2]), -3)
        # absmax with positive max
        self.assertEqual(whisper.aggregate('absmax', [-2, -1, 2, 3]), 3)
        # absmin with positive min
        self.assertEqual(whisper.aggregate('absmin', [-3, -2, 1, 2]), 1)
        # absmin with negative min
        self.assertEqual(whisper.aggregate('absmin', [-2, -1, 2, 3]), -1)

        with AssertRaisesException(
                whisper.InvalidAggregationMethod(
                    'Unrecognized aggregation method derp')):
            whisper.aggregate('derp', [12, 2, 3123, 1])
Пример #2
0
    def test_setAggregation(self):
        """
        Create a db, change aggregation, xFilesFactor, then use info() to validate
        """
        original_lock = whisper.LOCK
        original_caching = whisper.CACHE_HEADERS
        original_autoflush = whisper.AUTOFLUSH

        whisper.LOCK = True
        whisper.AUTOFLUSH = True
        whisper.CACHE_HEADERS = True
        # create a new db with a valid configuration
        whisper.create(self.filename, self.retention)

        with AssertRaisesException(
                whisper.InvalidAggregationMethod(
                    'Unrecognized aggregation method: yummy beer')):
            whisper.setAggregationMethod(self.filename, 'yummy beer')

        #set setting every AggregationMethod available
        for ag in whisper.aggregationMethods:
            for xff in 0.0, 0.2, 0.4, 0.7, 0.75, 1.0:
                # original xFilesFactor
                info0 = whisper.info(self.filename)
                # optional xFilesFactor not passed
                whisper.setAggregationMethod(self.filename, ag)

                # original value should not change
                info1 = whisper.info(self.filename)
                self.assertEqual(info0['xFilesFactor'], info1['xFilesFactor'])

                # the selected aggregation method should have applied
                self.assertEqual(ag, info1['aggregationMethod'])

                # optional xFilesFactor used
                whisper.setAggregationMethod(self.filename, ag, xff)
                # new info should match what we just set it to
                info2 = whisper.info(self.filename)
                # packing and unpacking because
                # AssertionError: 0.20000000298023224 != 0.2
                target_xff = struct.unpack("!f", struct.pack("!f", xff))[0]
                self.assertEqual(info2['xFilesFactor'], target_xff)

                # same aggregationMethod asssertion again, but double-checking since
                # we are playing with packed values and seek()
                self.assertEqual(ag, info2['aggregationMethod'])

                with SimulatedCorruptWhisperFile():
                    with AssertRaisesException(
                            whisper.CorruptWhisperFile('Unable to read header',
                                                       self.filename)):
                        whisper.setAggregationMethod(self.filename, ag)

        whisper.LOCK = original_lock
        whisper.AUTOFLUSH = original_autoflush
        whisper.CACHE_HEADERS = original_caching
Пример #3
0
    def test_aggregate(self):
        """
        aggregate functions
        """
        # min of 1-4
        self.assertEqual(whisper.aggregate('min', [1, 2, 3, 4]), 1)
        # max of 1-4
        self.assertEqual(whisper.aggregate('max', [1, 2, 3, 4]), 4)
        # last element in the known values
        self.assertEqual(whisper.aggregate('last', [3, 2, 5, 4]), 4)
        # sum ALL THE VALUES!
        self.assertEqual(whisper.aggregate('sum', [10, 2, 3, 4]), 19)
        # average of the list elements
        self.assertEqual(whisper.aggregate('average', [1, 2, 3, 4]), 2.5)

        with AssertRaisesException(
                whisper.InvalidAggregationMethod(
                    'Unrecognized aggregation method derp')):
            whisper.aggregate('derp', [12, 2, 3123, 1])