# Copyright (c) 2017 Luiz Menezes
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

import pytest

from simple_model import Model
from simple_model.exceptions import EmptyField, ValidationError


class MyModel(Model):
    fields = ('foo', 'bar', 'baz', 'qux')
    allow_empty = ('baz', 'qux')

    def validate_foo(self, value):
        if len(value) != 3:
            raise ValidationError()


@pytest.fixture
def model():
    return MyModel(foo='foo', bar='bar', baz='', qux='')


def test_model_fields(model):
    assert model.foo == 'foo'
    assert model.bar == 'bar'
    assert model.baz == ''
    assert model.qux == ''


def test_model_fields_allow_empty():
    model = MyModel(foo='foo', bar='bar')
    assert model.foo == 'foo'
    assert model.bar == 'bar'
    assert model.baz is None
    assert model.qux is None


@pytest.mark.parametrize('empty_value', (None, '', 0))
def test_model_fields_allow_empty_error(empty_value):
    with pytest.raises(EmptyField):
        MyModel()

    with pytest.raises(EmptyField):
        MyModel(foo=empty_value)

    with pytest.raises(EmptyField):
        MyModel(bar=empty_value)

    with pytest.raises(EmptyField):
        MyModel(foo=empty_value, bar=empty_value)


def test_model_fields_field_validation(model):
    assert model.validate() is True


def test_model_fields_field_validation_without_raise(model):
    model.foo = ''
    assert model.validate(raise_exception=False) is False


def test_model_fields_field_validation_error(model):
    model.foo = 'fo'
    with pytest.raises(ValidationError):
        model.validate()


def test_model_fields_field_validation_error_without_raise(model):
    model.foo = 'fo'
    assert model.validate(raise_exception=False) is False


def test_model_serialzie(model):
    serialized_model = {
        'foo': 'foo',
        'bar': 'bar',
        'baz': '',
        'qux': '',
    }
    assert model.serialize() == serialized_model

@@ -1288,32 +1288,41 @@
 st_model_fields_
+validate_
 allow_empty_erro
@@ -1390,16 +1390,27 @@
 yModel()
+.validate()
 %0A%0A    wi
@@ -1463,32 +1463,43 @@
 foo=empty_value)
+.validate()
 %0A%0A    with pytes
@@ -1552,16 +1552,27 @@
 y_value)
+.validate()
 %0A%0A    wi
@@ -1650,16 +1650,27 @@
 y_value)
+.validate()
 %0A%0A%0Adef t

Fix tests to validate fields only on creation