def test_remove_transform(): jexl = JEXL() jexl.add_transform('foo', lambda x: x) assert jexl.evaluate('4|foo') == 4 jexl.remove_transform('foo') with pytest.raises(MissingTransformError): jexl.evaluate('4|foo')
def test_validate(): jexl = JEXL() jexl.add_transform('foo', lambda x: x + 1) assert list(jexl.validate('5+6|foo')) == [] errors = list(jexl.validate('5+6|bar')) assert len(errors) == 1 assert 'bar' in errors[0] errors = list(jexl.validate('1+')) assert errors == ['Could not parse expression: 1+']
def test_validate(): jexl = JEXL() jexl.add_transform('foo', lambda x: x + 1) assert list(jexl.validate('5+6|foo')) == [] errors = list(jexl.validate('5+6|bar')) assert len(errors) == 1 assert 'bar' in errors[0] errors = list(jexl.validate('1+')) assert errors == ['Could not parse expression: 1+'] errors = list(jexl.validate('"\n"')) assert errors == ['Could not parse expression: "\n"']
#!/usr/bin/env python3 import json import sys import jsonschema from jsonschema.exceptions import best_match, ValidationError from pyjexl.jexl import JEXL # Create a jexl evaluator EVALUATOR = JEXL() EVALUATOR.add_binary_operator('intersect', 40, lambda x, y: set(x).intersection(y)) EVALUATOR.add_transform('date', lambda x: 0) EVALUATOR.add_transform('length', lambda x: 0) EVALUATOR.add_transform('preferenceValue', lambda x: False) EVALUATOR.add_transform('mapToProperty', lambda x, y: []) EVALUATOR.add_transform('keys', lambda x: []) EVALUATOR.add_transform('bucketSample', lambda x, y, z, q: False) EVALUATOR.add_transform('stableSample', lambda x, y: False) # cache all the known schemas to validate experiments ALL_SCHEMAS = dict() SCHEMA_MAP = { "cfr": "schema/cfr.schema.json", "onboarding": "schema/onboarding.schema.json", "onboarding-multistage": "schema/onboarding-multistage.schema.json", "cfr-fxa": "schema/cfr-fxa.schema.json", "cfr-heartbeat": "schema/cfr-heartbeat.schema.json",
def test_add_transform(): jexl = JEXL() jexl.add_transform('foo', lambda x: x + 2) assert jexl.evaluate('4|foo') == 6