def test_filter_ags_matching(self): params = { 'name': 12, 'age': 12, 'address': 'street', 'ages': 20, 'age[lt]': 65 } args = Filter.args_matching(params, 'age') self.assertEqual(len(args), 2)
def test_to_json_operator(self): self.assertEqual(Filter('date', operator='lt').to_json()['op'], '<') self.assertEqual(Filter('date', operator='le').to_json()['op'], '<=') self.assertEqual(Filter('date', operator='eq').to_json()['op'], '==') self.assertEqual(Filter('date', operator='ne').to_json()['op'], '!=') self.assertEqual(Filter('date', operator='gt').to_json()['op'], '>') self.assertEqual(Filter('date', operator='ge').to_json()['op'], '>=')
def sensor_history_v2(name): args = request.args date_args = Filter.args_matching(args, 'date') timestamp_args = Filter.args_matching(args, 'timestamp') date_filters = [get_arg(args, d, True) for d in date_args] date_filters += [get_arg(args, d, False) for d in timestamp_args] offset = int(args['page']) if 'page' in args else 0 limit = int(args['page_size']) if 'page_size' in args else 20 sort = request.args['sort'] if 'sort' in args else 'desc' with mydb: date_filters = [a.to_json() for a in date_filters] # range(0, 24, 3) sensors = mydb.sensor(name, filters=date_filters, offset=offset, limit=limit, sort=sort) return jsonify(sensors) return 500
def test_filter_from_gt_operator_arg(self): pair = Filter.from_arg('age[gt]', '18') self.assertEqual('age', pair.name) self.assertEqual(18, pair.value) self.assertEqual(operator.gt, pair.operator)
def test_filter_from_non_operator_arg(self): pair = Filter.from_arg('name', '12') self.assertEqual('name', pair.name) self.assertEqual(12, pair.value) self.assertEqual(operator.eq, pair.operator)
def test_value_parse_int(self): val = Filter.value_parse('12', ignore_type=False) self.assertEqual(val, 12)
def test_split_name_with_operator(self): name, operator = Filter.split_name_operator('name[eq]') self.assertEqual('name', name) self.assertEqual('eq', operator)
def test_split_name_without_operator(self): name, operator = Filter.split_name_operator('name') self.assertEqual('name', name)
def test_to_json_value(self): a_filter = Filter('date', value=12) result = a_filter.to_json() self.assertEqual(result['value'], a_filter.value)
def test_to_json_field(self): a_filter = Filter('date') result = a_filter.to_json() self.assertEqual(result['field'], a_filter.name)
def test_to_json_contains_keys(self): a_filter = Filter('date') result = a_filter.to_json() self.assertTrue('field' in result.keys()) self.assertTrue('op' in result.keys()) self.assertTrue('value' in result.keys())
def test_value_parse_date_ignore_type(self): val = Filter.value_parse('2020-01-23', ignore_type=True) self.assertEqual(val, '2020-01-23')
def test_value_parse_date(self): date = Filter.value_parse('2020-10-10', ignore_type=False) self.assertEqual(date, datetime.strptime('2020-10-10', '%Y-%m-%d'))
def test_value_parse_int_ignore_type(self): val = Filter.value_parse('11', ignore_type=True) self.assertEqual(val, '11')
def get_arg(args, arg, ignore_type): return Filter.from_arg(arg, args[arg], ignore_type)
def test_filter_evaluate(self): f = Filter('age', 'lt', 65) self.assertTrue(f.evaluate(64)) self.assertFalse(f.evaluate(65))
def test_to_json_default_operator(self): a_filter = Filter('date') result = a_filter.to_json() self.assertEqual(result['op'], '==')
def test_calculate_date_range_lower_limit(self): lower = Filter('date', 'gt', '2020-10-01') calculate_date_range([lower])