def test_validate_old_allowed_values_list(): # Test that list parameter with only "allowedValues" validates based on that variable = { "minElements": 0, "allowedValues": ["v1"], "type": "string", "value": ["v1"], } sample_question = {"instance": {"variables": {"v": variable}}} assert _validate(sample_question) expected_message = ( "\n Value: 'bad value' is not among allowed values " + "['v1'] of parameter: 'v'\n") with pytest.raises(QuestionValidationException) as err: variable["value"][0] = "bad value" _validate(sample_question) assert expected_message == str(err.value)
def test_min_length(): numbers = { 'minElements': 0, 'minLength': 4, 'type': 'string', 'value': ['one', 'three', 'four', 'five', 'seven'] } sample_question = { 'instance': { 'variables': { 'numbers': numbers } } } expected_message = "\n Length of value: 'one' for element : 0 of parameter: 'numbers' below minimum length: 4\n" with pytest.raises(QuestionValidationException) as err: _validate(sample_question) assert expected_message == str(err.value)
def test_validate_allowed_values(): # Test that parameter validates based on "values", not "allowedValues" variable = { 'allowedValues': ['obsolete value'], 'type': 'string', 'value': 'v1', 'values': [{ 'name': 'v1' }] } sample_question = {'instance': {'variables': {'v': variable}}} assert _validate(sample_question) expected_message = "\n Value: 'obsolete value' is not among allowed" \ + " values ['v1'] of parameter: 'v'\n" with pytest.raises(QuestionValidationException) as err: variable['value'] = 'obsolete value' _validate(sample_question) assert expected_message == str(err.value)
def test_validate_allowed_values(): # Test that parameter validates based on "values", not "allowedValues" variable = { "allowedValues": ["obsolete value"], "type": "string", "value": "v1", "values": [{ "name": "v1" }], } sample_question = {"instance": {"variables": {"v": variable}}} assert _validate(sample_question) expected_message = ("\n Value: 'obsolete value' is not among allowed" + " values ['v1'] of parameter: 'v'\n") with pytest.raises(QuestionValidationException) as err: variable["value"] = "obsolete value" _validate(sample_question) assert expected_message == str(err.value)
def test_valid_comparator(): comparators = { 'type': 'comparator', 'value': '>' } sample_question = { 'instance': { 'comparators': comparators } } assert _validate(sample_question)
def test_validate_old_allowed_values(): # Test that parameter with only "allowedValues" validates based on that variable = { 'allowedValues': ['v1'], 'type': 'string', 'value': 'v1' } sample_question = { 'instance': { 'variables': { 'v': variable } } } assert _validate(sample_question) expected_message = "\n Value: 'bad value' is not among allowed values " \ + "['v1'] of parameter: 'v'\n" with pytest.raises(QuestionValidationException) as err: variable['value'] = 'bad value' _validate(sample_question) assert expected_message == str(err.value)
def test_valid_comparator(): comparators = {"type": "comparator", "value": ">"} sample_question = {"instance": {"comparators": comparators}} assert _validate(sample_question)