コード例 #1
0
ファイル: test_validation.py プロジェクト: nden/stdatamodels
def test_validation_on_write(tmp_path):
    file_path = tmp_path / "test.asdf"
    model = ValidationModel(pass_invalid_values=True)
    with pytest.warns(ValidationWarning):
        model.meta.string_attribute = 42

    with pytest.warns(ValidationWarning):
        model.save(file_path)
コード例 #2
0
ファイル: test_validation.py プロジェクト: nden/stdatamodels
def test_pass_invalid_values_on_write(tmp_path):
    file_path = tmp_path / "test.asdf"
    model = ValidationModel(pass_invalid_values=True)
    with pytest.warns(ValidationWarning):
        model.meta.string_attribute = 42
    with pytest.warns(ValidationWarning):
        model.save(file_path)

    with asdf.open(file_path) as af:
        assert af["meta"]["string_attribute"] == 42
コード例 #3
0
def test_validation_on_init(tmp_path):
    with asdf.AsdfFile() as af:
        af["meta"] = {"string_attribute": "foo"}

        with pytest.warns(None) as warnings:
            ValidationModel(af)
        assert len(warnings) == 0

        af["meta"]["string_attribute"] = 42
        with pytest.warns(ValidationWarning):
            ValidationModel(af)
コード例 #4
0
def test_validate_on_assignment_strict_validation(tmp_path, validate_on_assignment,
                                                  strict_validation,
                                                  expected_context_manager, value):
    model = ValidationModel(validate_on_assignment=validate_on_assignment,
                            strict_validation=strict_validation)

    with expected_context_manager:
        model.meta.string_attribute = 42
    assert model.meta.string_attribute is value
コード例 #5
0
def test_validate_on_assignment(monkeypatch, init_value, env_value,
                                expected_context_manager, string_attribute_value):
    if env_value is not None:
        monkeypatch.setenv("VALIDATE_ON_ASSIGNMENT", env_value)
    model = ValidationModel(validate_on_assignment=init_value)

    with expected_context_manager:
        model.meta.string_attribute = 42  # Bad assignment
    assert model.meta.string_attribute is string_attribute_value
コード例 #6
0
def test_dictionary_like():
    with ValidationModel(strict_validation=True) as x:
        x.meta.string_attribute = 'FOO'
        assert x['meta.string_attribute'] == 'FOO'

        with pytest.raises(jsonschema.ValidationError):
            x['meta.string_attribute'] = 12

        with pytest.raises(KeyError):
            x['meta.FOO.BAR.BAZ']
コード例 #7
0
def test_validate_on_assignment_insert(validate_on_assignment, warning_class,
                                       string_attribute_value):
    model = ValidationModel(validate_on_assignment=validate_on_assignment)

    model.meta.list_attribute.insert(0, {"string_attribute": "bar"})
    assert model.meta.list_attribute[0].string_attribute == "bar"

    with pytest.warns(warning_class):
        model.meta.list_attribute.insert(0, {"string_attribute": 42})
    assert model.meta.list_attribute[0].string_attribute == string_attribute_value
コード例 #8
0
def test_strict_validation_attribute_assignment(monkeypatch, init_value, env_value,
                                                expected_context_manager):
    if env_value is not None:
        monkeypatch.setenv("STRICT_VALIDATION", env_value)

    model = ValidationModel(strict_validation=init_value)

    with expected_context_manager:
        model.meta.string_attribute = 42
    assert model.meta.string_attribute is None
コード例 #9
0
def test_validate_on_assignment_pass_invalid_values(validate_on_assignment,
                                                    pass_invalid_values,
                                                    expected_context_manager,
                                                    value):
    model = ValidationModel(validate_on_assignment=validate_on_assignment,
                            pass_invalid_values=pass_invalid_values)

    # pass_invalid_values=True allows for assignment,
    # even with validate_on_assignment=True
    with expected_context_manager:
        model.meta.string_attribute = 42  # Bad assignment
    assert model.meta.string_attribute == value
コード例 #10
0
ファイル: views.py プロジェクト: piyush0/Warzone-Quiz
def QuestionView(request):
    user = check_validation(request)
    if user:
        if user.count < 20:
            question = QuestionModel.objects.order_by('?')[:1].first()

            print question
            test = ValidationModel.objects.complex_filter(Q(user=user) & Q(question=question))
            while test.exists():
                question = QuestionModel.objects.order_by('?')[:1].first()
                test = ValidationModel.objects.complex_filter(Q(user=user) & Q(question=question))

            new = ValidationModel(user=user, question=question)
            new.save()
            user.count += 1
            user.save()
            return render(request, 'quiz.html', {'question':question})
        else:
            return redirect('/endquiz/')
    else:
        return redirect('/login/')
コード例 #11
0
ファイル: test_validation.py プロジェクト: nden/stdatamodels
def test_pass_invalid_values_attribute_assignment(monkeypatch, init_value,
                                                  env_value, passed):
    if env_value is not None:
        monkeypatch.setenv("PASS_INVALID_VALUES", env_value)

    model = ValidationModel(pass_invalid_values=init_value)

    with pytest.warns(ValidationWarning):
        model.meta.string_attribute = 42

    if passed:
        assert model.meta.string_attribute == 42
    else:
        assert model.meta.string_attribute is None
コード例 #12
0
ファイル: test_validation.py プロジェクト: nden/stdatamodels
def test_strict_validation_attribute_assignment(monkeypatch, init_value,
                                                env_value, exception_class):
    if env_value is not None:
        monkeypatch.setenv("STRICT_VALIDATION", env_value)

    model = ValidationModel(strict_validation=init_value)

    if issubclass(exception_class, Warning):
        with pytest.warns(exception_class):
            model.meta.string_attribute = 42
        assert model.meta.string_attribute is None
    else:
        with pytest.raises(exception_class):
            model.meta.string_attribute = 42
        assert model.meta.string_attribute is None
コード例 #13
0
ファイル: test_validation.py プロジェクト: nden/stdatamodels
def test_scalar_attribute_assignment():
    model = ValidationModel()

    assert model.meta.string_attribute is None
    with pytest.warns(None) as warnings:
        model.meta.string_attribute = "foo"
    assert len(warnings) == 0
    assert model.meta.string_attribute == "foo"

    with pytest.warns(ValidationWarning):
        model.meta.string_attribute = 42
    assert model.meta.string_attribute == "foo"

    with pytest.warns(None) as warnings:
        model.meta.string_attribute = None
    assert len(warnings) == 0
    assert model.meta.string_attribute is None
コード例 #14
0
ファイル: test_validation.py プロジェクト: nden/stdatamodels
def test_list_attribute_ssignment():
    model = ValidationModel()

    assert len(model.meta.list_attribute) == 0
    with pytest.warns(None) as warnings:
        model.meta.list_attribute.append({"string_attribute": "foo"})
    assert len(warnings) == 0
    assert model.meta.list_attribute[0].string_attribute == "foo"

    with pytest.warns(ValidationWarning):
        model.meta.list_attribute.append({"string_attribute": 42})
    assert len(model.meta.list_attribute) == 1
    assert model.meta.list_attribute[0].string_attribute == "foo"

    with pytest.warns(None) as warnings:
        model.meta.list_attribute = None
    assert len(warnings) == 0
    assert len(model.meta.list_attribute) == 0
コード例 #15
0
ファイル: test_validation.py プロジェクト: nden/stdatamodels
def test_validate():
    model = ValidationModel(pass_invalid_values=True)

    with pytest.warns(None) as warnings:
        model.meta.string_attribute = "foo"
        model.validate()

    with pytest.warns(ValidationWarning):
        model.meta.string_attribute = 42
    assert model.meta.string_attribute == 42

    with pytest.warns(ValidationWarning):
        model.validate()
コード例 #16
0
def test_validate_on_assignment_setitem(init_value, warning_class,
                                        string_attribute_value):
    model = ValidationModel(validate_on_assignment=init_value)

    # Check values assigned that are valid
    value = "foo"
    model.meta.list_attribute.append({"string_attribute": value})
    assert model.meta.list_attribute[0].string_attribute == value

    value2 = "bar"
    model.meta.list_attribute[0] = {"string_attribute": value2}
    assert model.meta.list_attribute[0].string_attribute == value2

    # Now check invalid assignments.  Currently string_attribute="bar".  Try
    # assigning an invalid type
    value3 = 42
    with pytest.warns(warning_class):
        model.meta.list_attribute[0] = {"string_attribute": value3}
    assert model.meta.list_attribute[0].string_attribute == string_attribute_value
コード例 #17
0
ファイル: test_validation.py プロジェクト: nden/stdatamodels
def test_object_attribute_assignment():
    model = ValidationModel()

    assert model.meta.object_attribute.string_attribute is None
    with pytest.warns(None) as warnings:
        model.meta.object_attribute = {"string_attribute": "foo"}
    assert len(warnings) == 0
    assert model.meta.object_attribute.string_attribute == "foo"

    with pytest.warns(ValidationWarning):
        model.meta.object_attribute = "bar"
    assert model.meta.object_attribute.string_attribute == "foo"

    with pytest.warns(ValidationWarning):
        model.meta.object_attribute = {"string_attribute": 42}
    assert model.meta.object_attribute.string_attribute == "foo"

    with pytest.warns(None) as warnings:
        model.meta.object_attribute = None
    assert len(warnings) == 0
    assert model.meta.object_attribute.string_attribute is None
コード例 #18
0
def test_object_assignment_with_nested_null():
    model = ValidationModel()

    with pytest.warns(None) as warnings:
        model.meta.object_attribute = {"string_attribute": None}
    assert len(warnings) == 0