Ejemplo n.º 1
0
def test_m2m_dicts_blank():
    validator = ModelValidator(Student(name='tim'))

    valid = validator.validate({'courses': [{}, {}]})
    assert valid

    valid = validator.validate({'courses': {}})
    assert valid
Ejemplo n.º 2
0
def test_m2m_empty():
    validator = ModelValidator(Student(name='tim'))

    valid = validator.validate()
    assert valid

    valid = validator.validate({'courses': []})
    assert valid
Ejemplo n.º 3
0
def test_m2m_empty():
    validator = ModelValidator(Student(name='tim'))

    valid = validator.validate()
    assert valid

    valid = validator.validate({'courses': []})
    assert valid
Ejemplo n.º 4
0
def test_m2m_dicts_blank():
    validator = ModelValidator(Student(name='tim'))

    valid = validator.validate({'courses': [{}, {}]})
    assert valid

    valid = validator.validate({'courses': {}})
    assert valid
Ejemplo n.º 5
0
def test_choices():
    validator = ModelValidator(ComplexPerson(name='tim'))

    valid = validator.validate({'organization': 1, 'gender': 'S'})
    assert not valid
    assert validator.errors['gender'] == DEFAULT_MESSAGES['one_of'].format(choices='M, F')
    assert 'name' not in validator.errors

    valid = validator.validate({'organization': 1, 'gender': 'M'})
    assert valid
Ejemplo n.º 6
0
def test_missing_related():
    validator = ModelValidator(ComplexPerson(name='tim', gender='M'))

    valid = validator.validate({'organization': 999})
    assert not valid
    assert validator.errors['organization'] == 'unable to find related object'

    valid = validator.validate()
    assert not valid
    assert validator.errors['organization'] == 'must be provided'
Ejemplo n.º 7
0
def test_m2m_instances():
    validator = ModelValidator(Student(name='tim'))

    c1 = Course.create(name='course1')
    c2 = Course.create(name='course2')

    valid = validator.validate({'courses': [c1, c2]})
    assert valid

    valid = validator.validate({'courses': c1})
    assert valid
Ejemplo n.º 8
0
def test_m2m_dicts():
    validator = ModelValidator(Student(name='tim'))

    c1 = Course.create(name='course1')
    c2 = Course.create(name='course2')

    valid = validator.validate({'courses': [{'id': c1.id}, {'id': c2.id}]})
    assert valid

    valid = validator.validate({'courses': {'id': c1.id}})
    assert valid
Ejemplo n.º 9
0
def test_m2m_dicts():
    validator = ModelValidator(Student(name='tim'))

    c1 = Course.create(name='course1')
    c2 = Course.create(name='course2')

    valid = validator.validate({'courses': [{'id': c1.id}, {'id': c2.id}]})
    assert valid

    valid = validator.validate({'courses': {'id': c1.id}})
    assert valid
Ejemplo n.º 10
0
def test_choices():
    validator = ModelValidator(ComplexPerson(name='tim'))

    valid = validator.validate({'organization': 1, 'gender': 'S'})
    assert not valid
    assert validator.errors['gender'] == DEFAULT_MESSAGES['one_of'].format(
        choices='M, F')
    assert 'name' not in validator.errors

    valid = validator.validate({'organization': 1, 'gender': 'M'})
    assert valid
Ejemplo n.º 11
0
def test_unique():
    person = Person.create(name='tim')

    validator = ModelValidator(Person(name='tim'))
    valid = validator.validate({'gender': 'M'})
    assert not valid
    assert validator.errors['name'] == DEFAULT_MESSAGES['unique']

    validator = ModelValidator(person)
    valid = validator.validate({'gender': 'M'})
    assert valid
Ejemplo n.º 12
0
def test_unique():
    person = Person.create(name='tim')

    validator = ModelValidator(Person(name='tim'))
    valid = validator.validate({'gender': 'M'})
    assert not valid
    assert validator.errors['name'] == DEFAULT_MESSAGES['unique']

    validator = ModelValidator(person)
    valid = validator.validate({'gender': 'M'})
    assert valid
Ejemplo n.º 13
0
def test_m2m_instances():
    validator = ModelValidator(Student(name='tim'))

    c1 = Course.create(name='course1')
    c2 = Course.create(name='course2')

    valid = validator.validate({'courses': [c1, c2]})
    assert valid

    valid = validator.validate({'courses': c1})
    assert valid
Ejemplo n.º 14
0
def test_unique():
    person = Person(name='tim')
    person.save()

    validator = ModelValidator(Person(name='tim'))
    valid = validator.validate({'gender': 'M'})
    assert not valid
    assert validator.errors['name'] == 'must be a unique value'

    validator = ModelValidator(person)
    valid = validator.validate({'gender': 'M'})
    assert valid
Ejemplo n.º 15
0
def test_related_optional_missing():
    validator = ModelValidator(ComplexPerson(name='tim', gender='M', organization=1))

    valid = validator.validate({'pay_grade': 999})
    assert not valid
    assert validator.errors['pay_grade'] == DEFAULT_MESSAGES['related'].format(field='id', values=999)

    valid = validator.validate({'pay_grade': None})
    assert valid

    valid = validator.validate()
    assert valid
Ejemplo n.º 16
0
def test_choices():
    validator = ModelValidator(ComplexPerson(name='tim'))

    valid = validator.validate({'organization': 1, 'gender': 'S'})
    assert not valid
    assert validator.errors['gender'] == 'must be one of the choices: M, F'
    assert 'name' not in validator.errors

    valid = validator.validate({'organization': 1, 'gender': 'M'})
    assert valid

    valid = validator.validate({'gender': 'M'})
    assert valid
Ejemplo n.º 17
0
def test_unique_index():
    obj1 = BasicFields.create(field1='one', field2='two', field3='three')
    obj2 = BasicFields(field1='one', field2='two', field3='three')

    validator = ModelValidator(obj2)
    valid = validator.validate()
    assert not valid
    assert validator.errors['field1'] == DEFAULT_MESSAGES['index']
    assert validator.errors['field2'] == DEFAULT_MESSAGES['index']

    validator = ModelValidator(obj1)
    valid = validator.validate()
    assert valid
Ejemplo n.º 18
0
def test_unique_index():
    obj1 = BasicFields.create(field1='one', field2='two', field3='three')
    obj2 = BasicFields(field1='one', field2='two', field3='three')

    validator = ModelValidator(obj2)
    valid = validator.validate()
    assert not valid
    assert validator.errors['field1'] == DEFAULT_MESSAGES['index']
    assert validator.errors['field2'] == DEFAULT_MESSAGES['index']

    validator = ModelValidator(obj1)
    valid = validator.validate()
    assert valid
Ejemplo n.º 19
0
def test_unique_index():
    obj = BasicFields(field1='one', field2='two', field3='three')
    obj.save()

    validator = ModelValidator(
        BasicFields(field1='one', field2='two', field3='three'))
    valid = validator.validate()
    assert not valid
    assert validator.errors['field1'] == 'fields must be unique together'
    assert validator.errors['field2'] == 'fields must be unique together'

    validator = ModelValidator(obj)
    valid = validator.validate()
    assert valid
Ejemplo n.º 20
0
def test_related_optional_missing():
    validator = ModelValidator(
        ComplexPerson(name='tim', gender='M', organization=1))

    valid = validator.validate({'pay_grade': 999})
    assert not valid
    assert validator.errors['pay_grade'] == DEFAULT_MESSAGES['related'].format(
        field='id', values=999)

    valid = validator.validate({'pay_grade': None})
    assert valid

    valid = validator.validate()
    assert valid
Ejemplo n.º 21
0
def test_related_required_missing():
    validator = ModelValidator(ComplexPerson(name='tim', gender='M'))

    valid = validator.validate({'organization': 999})
    assert not valid
    assert validator.errors['organization'] == DEFAULT_MESSAGES['related'].format(field='id', values=999)

    valid = validator.validate({'organization': None})
    assert not valid
    assert validator.errors['organization'] == DEFAULT_MESSAGES['required']

    valid = validator.validate()
    assert not valid
    assert validator.errors['organization'] == DEFAULT_MESSAGES['required']
Ejemplo n.º 22
0
def test_m2m_ints():
    validator = ModelValidator(Student(name='tim'))

    c1 = Course.create(name='course1')
    c2 = Course.create(name='course2')

    valid = validator.validate({'courses': [c1.id, c2.id]})
    print(validator.errors)
    assert valid

    valid = validator.validate({'courses': c1.id})
    assert valid

    valid = validator.validate({'courses': str(c1.id)})
    assert valid
Ejemplo n.º 23
0
def test_related_required_missing():
    validator = ModelValidator(ComplexPerson(name='tim', gender='M'))

    valid = validator.validate({'organization': 999})
    assert not valid
    assert validator.errors['organization'] == DEFAULT_MESSAGES[
        'related'].format(field='id', values=999)

    valid = validator.validate({'organization': None})
    assert not valid
    assert validator.errors['organization'] == DEFAULT_MESSAGES['required']

    valid = validator.validate()
    assert not valid
    assert validator.errors['organization'] == DEFAULT_MESSAGES['required']
Ejemplo n.º 24
0
def test_m2m_ints():
    validator = ModelValidator(Student(name='tim'))

    c1 = Course.create(name='course1')
    c2 = Course.create(name='course2')

    valid = validator.validate({'courses': [c1.id, c2.id]})
    print(validator.errors)
    assert valid

    valid = validator.validate({'courses': c1.id})
    assert valid

    valid = validator.validate({'courses': str(c1.id)})
    assert valid
Ejemplo n.º 25
0
def test_default():
    validator = ModelValidator(BasicFields())
    valid = validator.validate()
    assert not valid
    assert validator.data['field1'] == 'Tim'
    assert validator.errors['field2'] == DEFAULT_MESSAGES['required']
    assert validator.errors['field3'] == DEFAULT_MESSAGES['required']
Ejemplo n.º 26
0
def test_default():
    validator = ModelValidator(BasicFields())
    valid = validator.validate()
    assert not valid
    assert validator.data['field1'] == 'Tim'
    assert validator.errors['field2'] == 'must be provided'
    assert validator.errors['field3'] == 'must be provided'
Ejemplo n.º 27
0
def test_default():
    validator = ModelValidator(BasicFields())
    valid = validator.validate()
    assert not valid
    assert validator.data['field1'] == 'Tim'
    assert validator.errors['field2'] == DEFAULT_MESSAGES['required']
    assert validator.errors['field3'] == DEFAULT_MESSAGES['required']
Ejemplo n.º 28
0
def test_m2m_missing():
    validator = ModelValidator(Student(name='tim'))

    valid = validator.validate({'courses': [1, 33]})
    assert not valid
    assert validator.errors['courses'] == DEFAULT_MESSAGES['related'].format(
        field='id', values=[1, 33])
Ejemplo n.º 29
0
    def save(self, *args, **kwargs):
        # Ensure all fields are entered and email is valid
        validator = type(self).CustomValidator(self)
        validator.validate()
        self.errors = validator.errors

        # Ensure unique email and username
        validator = ModelValidator(self)
        validator.validate()
        self.errors.update(validator.errors)

        if self.errors:
            return 0
        else:
            self.updated_at = datetime.datetime.now()
            return super(BaseModel, self).save(*args, **kwargs)
Ejemplo n.º 30
0
def test_m2m_save_blank():
    obj = Student(name='tim')
    validator = ModelValidator(obj)

    valid = validator.validate({'courses': [{}, {}]})
    assert valid

    validator.save()

    assert obj.id
Ejemplo n.º 31
0
def test_m2m_save_blank():
    obj = Student(name='tim')
    validator = ModelValidator(obj)

    valid = validator.validate({'courses': [{}, {}]})
    assert valid

    validator.save()

    assert obj.id
Ejemplo n.º 32
0
def test_save():
    obj = BasicFields(field1='one', field2='124124', field3='1232314')

    validator = ModelValidator(obj)
    valid = validator.validate({'field1': 'updated'})
    assert valid

    validator.save()

    assert obj.id
    assert obj.field1 == 'updated'
Ejemplo n.º 33
0
class BaseModel(Model):
    id = AutoField()
    created_at = BigIntegerField()
    updated_at = BigIntegerField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        t = time.time()
        if not self.created_at:
            self.created_at = t
            self.updated_at = t
            # if kwargs.pop('__no_default__', None):
            #     self.__data__ = {}
            # else:
            #     self.__data__ = self._meta.get_default_dict()
            # self._dirty = set(self.__data__)
            # self.__rel__ = {}

            # for k in kwargs:
            #     setattr(self, k, kwargs[k])

    @property
    def json(self):
        return model_to_dict(self)

    def to_json(self, **kwargs):
        default = {"recurse": False}
        default.update(kwargs)
        return model_to_dict(self, **default)

    # model_to_dict(model, recurse=True, backrefs=False, only=None,
    #                 exclude=None, seen=None, extra_attrs=None,
    #                 fields_from_query=None, max_depth=None, manytomany=False):

    @property
    def is_valid(self):
        self.validator = ModelValidator(self)

        return self.validator.validate()

    @property
    def errors(self):
        return self.validator.errors

    def save(self, *args, **kwargs):
        t = time.time()
        if not self.created_at:
            self.created_at = t
        self.updated_at = t

        super().save(*args, **kwargs)

    class Meta:
        database = Database.conn
Ejemplo n.º 34
0
def test_save():
    obj = BasicFields(field1='one', field2='124124', field3='1232314')

    validator = ModelValidator(obj)
    valid = validator.validate({'field1': 'updated'})
    assert valid

    validator.save()

    assert obj.id
    assert obj.field1 == 'updated'
Ejemplo n.º 35
0
    async def store(request: Request):
        with connection.atomic() as transaction:
            data = request.json
            print(data, request.json)

            validator = ModelValidator(User(**data))
            validator.validate()

            if bool(validator.errors):
                return response.json(validator.errors, status=400)

            user: User = User.create(**data)
            _user = model_to_dict(user, recurse=False, backrefs=True)

        # _user = json.dumps(_user, cls=Serialize)
        #              dumps(body, **kwargs)

        return response.json(_user,
                             status=201,
                             dumps=json.dumps,
                             cls=Serialize)
Ejemplo n.º 36
0
def test_m2m_save():
    obj = Student(name='tim')
    validator = ModelValidator(obj)

    c1 = Course.create(name='course1')
    c2 = Course.create(name='course2')

    valid = validator.validate({'courses': [c1, c2]})
    assert valid

    validator.save()

    assert obj.id
    assert c1 in obj.courses
    assert c2 in obj.courses
Ejemplo n.º 37
0
def test_m2m_save():
    obj = Student(name='tim')
    validator = ModelValidator(obj)

    c1 = Course.create(name='course1')
    c2 = Course.create(name='course2')

    valid = validator.validate({'courses': [c1, c2]})
    assert valid

    validator.save()

    assert obj.id
    assert c1 in obj.courses
    assert c2 in obj.courses
Ejemplo n.º 38
0
    def add_record(self):

        new_record = Record()
        for field in self.fieldnames:
            if self.entry_form_values[field].get():
                setattr(new_record, field, self.entry_form_values[field].get())
        validator = ModelValidator(new_record)
        if validator.validate():
            new_record.save()
            self.populate_list_items()
            self.draw_footer()
        else:
            errorlist = []
            for key, value in validator.errors.items():
                errorlist.append("Error in field '%s': %s" % (key, value))
            messagebox.showerror("Invalid Data", "\n".join(errorlist))
Ejemplo n.º 39
0
def test_instance():
    instance = Person()
    validator = ModelValidator(instance)
    valid = validator.validate({'name': 'tim'})
    assert valid
    assert validator.data['name'] == 'tim'
Ejemplo n.º 40
0
def test_related_required_dict():
    org = Organization.create(name='new1')
    validator = ModelValidator(ComplexPerson(name='tim', gender='M'))
    valid = validator.validate({'organization': {'id': org.id}})
    assert valid
Ejemplo n.º 41
0
def test_required():
    validator = ModelValidator(Person())
    valid = validator.validate()
    assert not valid
    assert validator.errors['name'] == 'must be provided'
Ejemplo n.º 42
0
def assert_instance_works():
    validator = ModelValidator(Person())
    valid = validator.validate({'name': 'timster'})
    assert valid
    assert isinstance(validator.data, Person)
    assert validator.data.name == 'timster'
Ejemplo n.º 43
0
def test_related_required_dict_missing():
    validator = ModelValidator(ComplexPerson(name='tim', gender='M'))
    validator.validate({'organization': {}})
    assert validator.errors['organization'] == DEFAULT_MESSAGES['required']
Ejemplo n.º 44
0
def test_related_optional_dict_missing():
    validator = ModelValidator(ComplexPerson(name='tim', gender='M', organization=1))
    valid = validator.validate({'pay_grade': {}})
    assert valid
Ejemplo n.º 45
0
def test_instance():
    instance = Person()
    validator = ModelValidator(instance)
    valid = validator.validate({'name': 'tim'})
    assert valid
    assert validator.data['name'] == 'tim'
Ejemplo n.º 46
0
def test_required():
    validator = ModelValidator(Person())
    valid = validator.validate()
    assert not valid
    assert validator.errors['name'] == DEFAULT_MESSAGES['required']
Ejemplo n.º 47
0
def test_validate_only():
    obj = BasicFields(field1='one')

    validator = ModelValidator(obj)
    valid = validator.validate(only=('field1', ))
    assert valid
Ejemplo n.º 48
0
def test_m2m_missing():
    validator = ModelValidator(Student(name='tim'))

    valid = validator.validate({'courses': [1, 33]})
    assert not valid
    assert validator.errors['courses'] == DEFAULT_MESSAGES['related'].format(field='id', values=[1, 33])
Ejemplo n.º 49
0
def test_required():
    validator = ModelValidator(Person())
    valid = validator.validate()
    assert not valid
    assert validator.errors['name'] == DEFAULT_MESSAGES['required']
Ejemplo n.º 50
0
    def validate(cls, **data) -> dict:
        validator = ModelValidator(cls(**data))
        validator.validate()

        return validator.errors
Ejemplo n.º 51
0
def test_validate_only():
    obj = BasicFields(field1='one')

    validator = ModelValidator(obj)
    valid = validator.validate(only=('field1', ))
    assert valid