Esempio n. 1
0
    def test_impossible_type_conversions(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [Param('param', ParamTypes.integer)],
            ['inf'],
            {},
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [Param('param', ParamTypes.boolean)],
            ['1'],
            {},
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [Param('param', ParamTypes.boolean)],
            [3],
            {},
        )
Esempio n. 2
0
    def test_param_type_int_or_inf(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                [Param('param', ParamTypes.intOrInf)],
                [1],
                {},
            ))

        self.assertTrue(
            validateParams(
                'TestParam',
                [Param('param', ParamTypes.intOrInf)],
                [float('inf')],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [Param('param', ParamTypes.intOrInf)],
            [1.2],
            {},
        )
Esempio n. 3
0
 def test_default_value(self):
     # if no value is specified, but there is a default value, we don't
     # want the validator to raise an exception because 'None' is invalid
     self.assertEqual(
         validateParams('TestParam', [
             Param('one', ParamTypes.aggFunc, default='sum'),
         ], [], {}),
         ([], {}),
     )
Esempio n. 4
0
    def test_use_series_function_as_aggregator(self):
        # powSeries is a series function which is marked as a valid aggregator
        self.assertEqual(
            validateParams('TestParam', [
                Param('func', ParamTypes.aggOrSeriesFunc, required=True),
            ], ['powSeries'], {}),
            (['powSeries'], {}),
        )

        # squareRoot is a series function which is not marked as a valid aggregator
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('func', ParamTypes.aggOrSeriesFunc, required=True),
            ],
            ['squareRoot'],
            {},
        )
Esempio n. 5
0
    def test_options_property(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          options=['3', 'three']),
                ],
                ['one', 'two', '3'],
                {},
            ))

        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          options=['3', 'three']),
                ],
                ['one', 'two', 'three'],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three',
                      ParamTypes.string,
                      required=True,
                      options=['3', 'three']),
            ],
            ['one', 'two', 'four'],
            {},
        )
Esempio n. 6
0
 def test_unexpected_kwargs(self):
     self.assertRaises(
         InputParameterError,
         validateParams,
         'TestParam',
         [Param('param', ParamTypes.integer)],
         [],
         {
             'param': 1,
             'param2': 2
         },
     )
Esempio n. 7
0
    def test_multiple_property(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param(
                    'three', ParamTypes.string, required=True, multiple=False),
            ],
            ['one', 'two', 'three', 'four'],
            {},
        )

        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          multiple=True),
                ],
                ['one', 'two', 'three', 'four'],
                {},
            ))
Esempio n. 8
0
    def test_multiple_property(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param(
                    'three', ParamTypes.string, required=True, multiple=False),
            ],
            ['one', 'two', 'three', 'four'],
            {},
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three', ParamTypes.string, required=True,
                      multiple=True),
            ], ['one', 'two', 'three', 'four'], {}),
            (['one', 'two', 'three', 'four'], {}),
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three', ParamTypes.string, required=True,
                      multiple=True),
            ],
            ['one', 'two', 'three'],
            # should fail because parameters which are specified multiple times
            # cannot be in kwargs, only args
            {'three': '3'},
        )
from graphite.functions.params import Param, ParamTypes


def test(seriesList):
    """This is a test function"""
    return seriesList


test.params = [
    Param('seriesList', ParamTypes.seriesList, required=True),
]

SeriesFunctions = {
    'testFunc': test,
}
Esempio n. 10
0
class TestParam(unittest.TestCase):
    params = [
        Param('one', ParamTypes.string, required=True),
        Param('two', ParamTypes.string, required=True),
        Param('three', ParamTypes.string, required=True),
    ]

    def test_simple_args(self):
        self.assertEqual(
            validateParams('TestParam', self.params, ['arg1', 'arg2', 'arg3'],
                           {}),
            (['arg1', 'arg2', 'arg3'], {}),
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['arg1', 'arg2'],
            {},
        )

    def test_simple_kwargs(self):
        self.assertEqual(
            validateParams('TestParam', self.params, [], {
                'one': '1',
                'two': '2',
                'three': '3'
            }),
            ([], {
                'one': '1',
                'two': '2',
                'three': '3'
            }),
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'one': '1',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'one': '1',
                'two': '2',
                'four': '4'
            },
        )

    def test_mixed_cases(self):
        self.assertEqual(
            validateParams('TestParam', self.params, ['one', 'two'],
                           {'three': '3'}), (['one', 'two'], {
                               'three': '3'
                           }))

        self.assertEqual(
            validateParams('TestParam', self.params, ['one'], {
                'three': '3',
                'two': '2'
            }),
            (['one'], {
                'three': '3',
                'two': '2'
            }),
        )

        # positional args don't check the name
        self.assertEqual(
            validateParams('TestParam', self.params, ['one', 'two', 'four'],
                           {}), (['one', 'two', 'four'], {}))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'three': '3',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'three'],
            {'two': '2'},
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['three'],
            {
                'one': '1',
                'two': '2'
            },
        )

    def test_repeated_args(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one'],
            {
                'three': '3',
                'one': '1'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'two'],
            {
                'three': '3',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'two', 'three'],
            {'one': '1'},
        )

    def test_multiple_property(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param(
                    'three', ParamTypes.string, required=True, multiple=False),
            ],
            ['one', 'two', 'three', 'four'],
            {},
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three', ParamTypes.string, required=True,
                      multiple=True),
            ], ['one', 'two', 'three', 'four'], {}),
            (['one', 'two', 'three', 'four'], {}),
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three', ParamTypes.string, required=True,
                      multiple=True),
            ],
            ['one', 'two', 'three'],
            # should fail because parameters which are specified multiple times
            # cannot be in kwargs, only args
            {'three': '3'},
        )

    def test_options_property(self):
        self.assertEqual(
            validateParams('TestParam', [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three',
                      ParamTypes.string,
                      required=True,
                      options=['3', 'three']),
            ], ['one', 'two', '3'], {}),
            (['one', 'two', '3'], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three',
                      ParamTypes.string,
                      required=True,
                      options=['3', 'three']),
            ], ['one', 'two', 'three'], {}),
            (['one', 'two', 'three'], {}),
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three',
                      ParamTypes.string,
                      required=True,
                      options=['3', 'three']),
            ],
            ['one', 'two', 'four'],
            {},
        )

    def test_use_series_function_as_aggregator(self):
        # powSeries is a series function which is marked as a valid aggregator
        self.assertEqual(
            validateParams('TestParam', [
                Param('func', ParamTypes.aggOrSeriesFunc, required=True),
            ], ['powSeries'], {}),
            (['powSeries'], {}),
        )

        # squareRoot is a series function which is not marked as a valid aggregator
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('func', ParamTypes.aggOrSeriesFunc, required=True),
            ],
            ['squareRoot'],
            {},
        )

    def test_param_type_int_or_inf(self):
        self.assertEqual(
            validateParams('TestParam', [Param('param', ParamTypes.intOrInf)],
                           [1], {}),
            ([1], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [Param('param', ParamTypes.intOrInf)],
                           [float('inf')], {}),
            ([float('inf')], {}),
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [Param('param', ParamTypes.intOrInf)],
            [1.2],
            {},
        )

    def test_unexpected_kwargs(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [Param('param', ParamTypes.integer)],
            [],
            {
                'param': 1,
                'param2': 2
            },
        )

    def test_default_value(self):
        # if no value is specified, but there is a default value, we don't
        # want the validator to raise an exception because 'None' is invalid
        self.assertEqual(
            validateParams('TestParam', [
                Param('one', ParamTypes.aggFunc, default='sum'),
            ], [], {}),
            ([], {}),
        )

    def test_various_type_conversions(self):
        self.assertEqual(
            validateParams('TestParam', [
                Param('bool', ParamTypes.boolean),
            ], ['true'], {}),
            ([True], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('bool', ParamTypes.boolean),
            ], [], {'bool': 'false'}),
            ([], {
                'bool': False
            }),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('bool1', ParamTypes.boolean),
                Param('bool2', ParamTypes.boolean),
            ], [0], {'bool2': 1}),
            ([False], {
                'bool2': True
            }),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('float', ParamTypes.float),
            ], ['1e3'], {}),
            ([float(1000)], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('float', ParamTypes.float),
            ], ['0.123'], {}),
            ([float(0.123)], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('float', ParamTypes.float),
            ], [], {'float': 'inf'}),
            ([], {
                'float': float('inf')
            }),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('int', ParamTypes.integer),
            ], ['123'], {}),
            ([123], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('int', ParamTypes.integer),
            ], [], {'int': '-123'}),
            ([], {
                'int': -123
            }),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('intOrInf', ParamTypes.intOrInf),
            ], ['123'], {}),
            ([123], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('intOrInf', ParamTypes.intOrInf),
            ], [], {'intOrInf': float('inf')}),
            ([], {
                'intOrInf': float('inf')
            }),
        )

    def test_impossible_type_conversions(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [Param('param', ParamTypes.integer)],
            ['inf'],
            {},
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [Param('param', ParamTypes.boolean)],
            ['1'],
            {},
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [Param('param', ParamTypes.boolean)],
            [3],
            {},
        )
Esempio n. 11
0
    def test_various_type_conversions(self):
        self.assertEqual(
            validateParams('TestParam', [
                Param('bool', ParamTypes.boolean),
            ], ['true'], {}),
            ([True], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('bool', ParamTypes.boolean),
            ], [], {'bool': 'false'}),
            ([], {
                'bool': False
            }),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('bool1', ParamTypes.boolean),
                Param('bool2', ParamTypes.boolean),
            ], [0], {'bool2': 1}),
            ([False], {
                'bool2': True
            }),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('float', ParamTypes.float),
            ], ['1e3'], {}),
            ([float(1000)], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('float', ParamTypes.float),
            ], ['0.123'], {}),
            ([float(0.123)], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('float', ParamTypes.float),
            ], [], {'float': 'inf'}),
            ([], {
                'float': float('inf')
            }),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('int', ParamTypes.integer),
            ], ['123'], {}),
            ([123], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('int', ParamTypes.integer),
            ], [], {'int': '-123'}),
            ([], {
                'int': -123
            }),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('intOrInf', ParamTypes.intOrInf),
            ], ['123'], {}),
            ([123], {}),
        )

        self.assertEqual(
            validateParams('TestParam', [
                Param('intOrInf', ParamTypes.intOrInf),
            ], [], {'intOrInf': float('inf')}),
            ([], {
                'intOrInf': float('inf')
            }),
        )
Esempio n. 12
0
    results = []
    interval = int(to_seconds(parseTimeOffset(intervalString)))

    for series in seriesList:
        if series.step < interval:
            values_per_point = interval // series.step
            series.consolidate(values_per_point)
            series.step = interval
        results.append(series)

    return results


smooth.group = 'Custom'
smooth.params = [
    Param('seriesList', ParamTypes.seriesList, required=True),
    Param('intervalString', ParamTypes.interval, required=True)
]


def points(requestContext, seriesList, maxPoints):
    results = []
    for series in seriesList:
        if not maxPoints or len(series) < maxPoints:
            results.append(series)
            continue
        if len(series) % maxPoints != 0:
            values_per_point = len(series) // maxPoints + 1
        else:
            values_per_point = len(series) // maxPoints
        series.consolidate(values_per_point)
Esempio n. 13
0
class TestParam(unittest.TestCase):
    params = [
        Param('one', ParamTypes.string, required=True),
        Param('two', ParamTypes.string, required=True),
        Param('three', ParamTypes.string, required=True),
    ]

    def test_simple_args(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['arg1', 'arg2', 'arg3'],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['arg1', 'arg2'],
            {},
        )

    def test_simple_kwargs(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                [],
                {
                    'one': '1',
                    'two': '2',
                    'three': '3'
                },
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'one': '1',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'one': '1',
                'two': '2',
                'four': '4'
            },
        )

    def test_mixed_cases(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['one', 'two'],
                {'three': '3'},
            ))

        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['one'],
                {
                    'three': '3',
                    'two': '2'
                },
            ))

        # positional args don't check the name
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['one', 'two', 'four'],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'three': '3',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'three'],
            {'two': '2'},
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['three'],
            {
                'one': '1',
                'two': '2'
            },
        )

    def test_repeated_args(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one'],
            {
                'three': '3',
                'one': '1'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'two'],
            {
                'three': '3',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'two', 'three'],
            {'one': '1'},
        )

    def test_multiple_property(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param(
                    'three', ParamTypes.string, required=True, multiple=False),
            ],
            ['one', 'two', 'three', 'four'],
            {},
        )

        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          multiple=True),
                ],
                ['one', 'two', 'three', 'four'],
                {},
            ))

    def test_options_property(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          options=['3', 'three']),
                ],
                ['one', 'two', '3'],
                {},
            ))

        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          options=['3', 'three']),
                ],
                ['one', 'two', 'three'],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three',
                      ParamTypes.string,
                      required=True,
                      options=['3', 'three']),
            ],
            ['one', 'two', 'four'],
            {},
        )
Esempio n. 14
0
from graphite.functions.params import Param


def test(seriesList):
    """This is a test function"""
    return seriesList


test.group = 'Test'
test.params = [
    Param('seriesList', 'bad', required=True),
]

SeriesFunctions = {
    'testFunc': test,
}
Esempio n. 15
0
        # Format the data into TimeSeries
        result = []
        timeSeriesCount = matrix.shape[1]
        startTime = seriesLists[0].start
        stepTime = seriesLists[0].step
        endTime = startTime + clusterCount * stepTime
        for j in range(timeSeriesCount):
                result.append( TimeSeries(name = "master.kmeans.dim" + str(j), start = startTime, end = endTime, step = stepTime, values = clusters.T[j].tolist()) )

        return result


applyKmeans.group = 'Custom'
applyKmeans.params = [
        Param('seriesLists', ParamTypes.seriesLists, required=True),
]




def applyCD(requestContext, seriesLists):
        """Custom function that runs cd"""

        # Extract the data into 2D numpy.array
        matrix = []
        for i in range(len(seriesLists)):
                datapoints = seriesLists[i].datapoints()
                current_measurements = []
                for j in range(len(datapoints)):
                        if datapoints[j][0] == None:
Esempio n. 16
0
        )
        newSeries.pathExpression = newName

        # detect "none" lists
        if len([v for v in series if v is not None]) <= 1:
            newSeries.extend(series)
        else:
            # the "resolution" is a suggestion,
            # the algo will alter it some inorder
            # to get the best view for things
            new_s = smooth(series, windowPoints)
            # steps need to be ints, so we must force the issue
            new_step = round((series.end - series.start) / len(new_s))
            newSeries.step = new_step
            newSeries.extend(new_s)
        result.append(newSeries)

    return result


# optionally set the group attribute
ASAP.group = 'Custom'
ASAP.params = [
  Param('seriesList', ParamTypes.seriesList, required=True),
  Param('resolution', ParamTypes.intOrInterval, required=False),
]

SeriesFunctions = {
  'asap': ASAP,
}
Esempio n. 17
0
class TestParam(unittest.TestCase):
    params = [
        Param('one', ParamTypes.string, required=True),
        Param('two', ParamTypes.string, required=True),
        Param('three', ParamTypes.string, required=True),
    ]

    def test_simple_args(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['arg1', 'arg2', 'arg3'],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['arg1', 'arg2'],
            {},
        )

    def test_simple_kwargs(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                [],
                {
                    'one': '1',
                    'two': '2',
                    'three': '3'
                },
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'one': '1',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'one': '1',
                'two': '2',
                'four': '4'
            },
        )

    def test_mixed_cases(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['one', 'two'],
                {'three': '3'},
            ))

        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['one'],
                {
                    'three': '3',
                    'two': '2'
                },
            ))

        # positional args don't check the name
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['one', 'two', 'four'],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'three': '3',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'three'],
            {'two': '2'},
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['three'],
            {
                'one': '1',
                'two': '2'
            },
        )

    def test_repeated_args(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one'],
            {
                'three': '3',
                'one': '1'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'two'],
            {
                'three': '3',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'two', 'three'],
            {'one': '1'},
        )

    def test_multiple_property(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param(
                    'three', ParamTypes.string, required=True, multiple=False),
            ],
            ['one', 'two', 'three', 'four'],
            {},
        )

        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          multiple=True),
                ],
                ['one', 'two', 'three', 'four'],
                {},
            ))

    def test_options_property(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          options=['3', 'three']),
                ],
                ['one', 'two', '3'],
                {},
            ))

        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          options=['3', 'three']),
                ],
                ['one', 'two', 'three'],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three',
                      ParamTypes.string,
                      required=True,
                      options=['3', 'three']),
            ],
            ['one', 'two', 'four'],
            {},
        )

    def test_use_series_function_as_aggregator(self):
        # powSeries is a series function which is marked as a valid aggregator
        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('func', ParamTypes.aggOrSeriesFunc, required=True),
                ],
                ['powSeries'],
                {},
            ))

        # squareRoot is a series function which is not marked as a valid aggregator
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('func', ParamTypes.aggOrSeriesFunc, required=True),
            ],
            ['squareRoot'],
            {},
        )

    def test_param_type_int_or_inf(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                [Param('param', ParamTypes.intOrInf)],
                [1],
                {},
            ))

        self.assertTrue(
            validateParams(
                'TestParam',
                [Param('param', ParamTypes.intOrInf)],
                [float('inf')],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [Param('param', ParamTypes.intOrInf)],
            [1.2],
            {},
        )

    def test_default_value(self):
        # if no value is specified, but there is a default value, we don't
        # want the validator to raise an exception because 'None' is invalid
        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.aggFunc, default='sum'),
                ],
                [],
                {},
            ))
Esempio n. 18
0
class TestParam(unittest.TestCase):
    params = [
        Param('one', ParamTypes.string, required=True),
        Param('two', ParamTypes.string, required=True),
        Param('three', ParamTypes.string, required=True),
    ]

    def test_simple_args(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['arg1', 'arg2', 'arg3'],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['arg1', 'arg2'],
            {},
        )

    def test_simple_kwargs(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                [],
                {
                    'one': '1',
                    'two': '2',
                    'three': '3'
                },
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'one': '1',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'one': '1',
                'two': '2',
                'four': '4'
            },
        )

    def test_mixed_cases(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['one', 'two'],
                {'three': '3'},
            ))

        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['one'],
                {
                    'three': '3',
                    'two': '2'
                },
            ))

        # positional args don't check the name
        self.assertTrue(
            validateParams(
                'TestParam',
                self.params,
                ['one', 'two', 'four'],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            [],
            {
                'three': '3',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'three'],
            {'two': '2'},
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['three'],
            {
                'one': '1',
                'two': '2'
            },
        )

    def test_repeated_args(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one'],
            {
                'three': '3',
                'one': '1'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'two'],
            {
                'three': '3',
                'two': '2'
            },
        )

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            self.params,
            ['one', 'two', 'three'],
            {'one': '1'},
        )

    def test_multiple_property(self):
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param(
                    'three', ParamTypes.string, required=True, multiple=False),
            ],
            ['one', 'two', 'three', 'four'],
            {},
        )

        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          multiple=True),
                ],
                ['one', 'two', 'three', 'four'],
                {},
            ))

    def test_options_property(self):
        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          options=['3', 'three']),
                ],
                ['one', 'two', '3'],
                {},
            ))

        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('one', ParamTypes.string, required=True),
                    Param('two', ParamTypes.string, required=True),
                    Param('three',
                          ParamTypes.string,
                          required=True,
                          options=['3', 'three']),
                ],
                ['one', 'two', 'three'],
                {},
            ))

        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('one', ParamTypes.string, required=True),
                Param('two', ParamTypes.string, required=True),
                Param('three',
                      ParamTypes.string,
                      required=True,
                      options=['3', 'three']),
            ],
            ['one', 'two', 'four'],
            {},
        )

    def test_use_series_function_as_aggregator(self):
        # powSeries is a series function which is marked as a valid aggregator
        self.assertTrue(
            validateParams(
                'TestParam',
                [
                    Param('func', ParamTypes.aggOrSeriesFunc, required=True),
                ],
                ['powSeries'],
                {},
            ))

        # squareRoot is a series function which is not marked as a valid aggregator
        self.assertRaises(
            InputParameterError,
            validateParams,
            'TestParam',
            [
                Param('func', ParamTypes.aggOrSeriesFunc, required=True),
            ],
            ['squareRoot'],
            {},
        )