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']
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'
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']
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])
def test_m2m_dicts_blank(): validator = ModelValidator(Student(name='tim')) valid = validator.validate({'courses': [{}, {}]}) assert valid valid = validator.validate({'courses': {}}) assert valid
def test_m2m_empty(): validator = ModelValidator(Student(name='tim')) valid = validator.validate() assert valid valid = validator.validate({'courses': []}) assert valid
def test_m2m_dicts_blank(): validator = ModelValidator(Student(name='tim')) valid = validator.validate({'courses': [{}, {}]}) assert valid valid = validator.validate({'courses': {}}) assert valid
def test_m2m_empty(): validator = ModelValidator(Student(name='tim')) valid = validator.validate() assert valid valid = validator.validate({'courses': []}) assert valid
def test_m2m_save_blank(): obj = Student(name='tim') validator = ModelValidator(obj) valid = validator.validate({'courses': [{}, {}]}) assert valid validator.save() assert obj.id
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
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'
def test_m2m_save_blank(): obj = Student(name='tim') validator = ModelValidator(obj) valid = validator.validate({'courses': [{}, {}]}) assert valid validator.save() assert obj.id
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
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
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'
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
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
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
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'
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
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
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
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
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']
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
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
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
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']
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
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
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)
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))
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
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)
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
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
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
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
def test_related_required_dict_missing(): validator = ModelValidator(ComplexPerson(name='tim', gender='M')) validator.validate({'organization': {}}) assert validator.errors['organization'] == DEFAULT_MESSAGES['required']
def test_related_optional_dict_missing(): validator = ModelValidator(ComplexPerson(name='tim', gender='M', organization=1)) valid = validator.validate({'pay_grade': {}}) assert valid
def test_required(): validator = ModelValidator(Person()) valid = validator.validate() assert not valid assert validator.errors['name'] == 'must be provided'
def assert_instance_works(): validator = ModelValidator(Person()) valid = validator.validate({'name': 'timster'}) assert valid assert isinstance(validator.data, Person) assert validator.data.name == 'timster'
def test_not_instance(): with pytest.raises(AttributeError): ModelValidator(Person)
def test_validate_only(): obj = BasicFields(field1='one') validator = ModelValidator(obj) valid = validator.validate(only=('field1', )) assert valid
def is_valid(self): self.validator = ModelValidator(self) return self.validator.validate()
def test_instance(): instance = Person() validator = ModelValidator(instance) valid = validator.validate({'name': 'tim'}) assert valid assert validator.data['name'] == 'tim'
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])
def test_required(): validator = ModelValidator(Person()) valid = validator.validate() assert not valid assert validator.errors['name'] == DEFAULT_MESSAGES['required']
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
def validate(cls, **data) -> dict: validator = ModelValidator(cls(**data)) validator.validate() return validator.errors
def test_required(): validator = ModelValidator(Person()) valid = validator.validate() assert not valid assert validator.errors['name'] == DEFAULT_MESSAGES['required']