Example #1
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
Example #2
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
Example #3
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])
Example #4
0
def test_m2m_dicts_blank():
    validator = ModelValidator(Student(name='tim'))

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

    valid = validator.validate({'courses': {}})
    assert valid
Example #5
0
def test_m2m_empty():
    validator = ModelValidator(Student(name='tim'))

    valid = validator.validate()
    assert valid

    valid = validator.validate({'courses': []})
    assert valid
Example #6
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
Example #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
Example #8
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'
Example #9
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
Example #10
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
Example #11
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))
Example #12
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)
Example #13
0
def test_required():
    validator = ModelValidator(Person())
    valid = validator.validate()
    assert not valid
    assert validator.errors['name'] == DEFAULT_MESSAGES['required']
Example #14
0
def test_instance():
    instance = Person()
    validator = ModelValidator(instance)
    valid = validator.validate({'name': 'tim'})
    assert valid
    assert validator.data['name'] == 'tim'