Python sqlalchemy.orm.validates() Examples
The following are 11
code examples of sqlalchemy.orm.validates().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sqlalchemy.orm
, or try the search function
.
Example #1
Source File: gtfs_entities.py From pygtfs with MIT License | 5 votes |
def _validate_date(*field_names): @validates(*field_names) def make_date(self, key, value): return datetime.datetime.strptime(value, '%Y%m%d').date() return make_date
Example #2
Source File: gtfs_entities.py From pygtfs with MIT License | 5 votes |
def _validate_time_delta(*field_names): @validates(*field_names) def time_delta(self, key, value): if value is None or value == "": return None (hours, minutes, seconds) = map(int, value.split(":")) return datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds) return time_delta
Example #3
Source File: gtfs_entities.py From pygtfs with MIT License | 5 votes |
def _validate_int_bool(*field_names): @validates(*field_names) def int_bool(self, key, value): if value not in ("0", "1"): raise PygtfsValidationError("{0} must be 0 or 1, " "was {1}".format(key, value)) return value == "1" return int_bool
Example #4
Source File: gtfs_entities.py From pygtfs with MIT License | 5 votes |
def _validate_int_choice(int_choice, *field_names): @validates(*field_names) def in_range(self, key, value): if value is None or value == "": if (None in int_choice): return None else: raise PygtfsValidationError("Empty value not allowed in {0}".format(key)) else: int_value = int(value) if int_value not in int_choice: raise PygtfsValidationError( "{0} must be in range {1}, was {2}".format(key, int_choice, value)) return int_value return in_range
Example #5
Source File: gtfs_entities.py From pygtfs with MIT License | 5 votes |
def _validate_float_range(float_min, float_max, *field_names): @validates(*field_names) def in_range(self, key, value): float_value = float(value) if not (float_min <= float_value <= float_max): raise PygtfsValidationError( "{0} must be in range [{1}, {2}]," " was {2}".format(key, float_min, float_max, value)) return float_value return in_range
Example #6
Source File: test_validators.py From sqlalchemy with MIT License | 5 votes |
def test_scalar(self): users = self.tables.users canary = Mock() class User(fixtures.ComparableEntity): @validates("name") def validate_name(self, key, name): canary(key, name) ne_(name, "fred") return name + " modified" mapper(User, users) sess = Session() u1 = User(name="ed") eq_(u1.name, "ed modified") assert_raises(AssertionError, setattr, u1, "name", "fred") eq_(u1.name, "ed modified") eq_(canary.mock_calls, [call("name", "ed"), call("name", "fred")]) sess.add(u1) sess.commit() eq_( sess.query(User).filter_by(name="ed modified").one(), User(name="ed"), )
Example #7
Source File: test_validators.py From sqlalchemy with MIT License | 5 votes |
def test_collection(self): users, addresses, Address = ( self.tables.users, self.tables.addresses, self.classes.Address, ) canary = Mock() class User(fixtures.ComparableEntity): @validates("addresses") def validate_address(self, key, ad): canary(key, ad) assert "@" in ad.email_address return ad mapper(User, users, properties={"addresses": relationship(Address)}) mapper(Address, addresses) sess = Session() u1 = User(name="edward") a0 = Address(email_address="noemail") assert_raises(AssertionError, u1.addresses.append, a0) a1 = Address(id=15, email_address="foo@bar.com") u1.addresses.append(a1) eq_(canary.mock_calls, [call("addresses", a0), call("addresses", a1)]) sess.add(u1) sess.commit() eq_( sess.query(User).filter_by(name="edward").one(), User( name="edward", addresses=[Address(email_address="foo@bar.com")] ), )
Example #8
Source File: test_validators.py From sqlalchemy with MIT License | 5 votes |
def test_validators_dict(self): users, addresses, Address = ( self.tables.users, self.tables.addresses, self.classes.Address, ) class User(fixtures.ComparableEntity): @validates("name") def validate_name(self, key, name): ne_(name, "fred") return name + " modified" @validates("addresses") def validate_address(self, key, ad): assert "@" in ad.email_address return ad def simple_function(self, key, value): return key, value u_m = mapper( User, users, properties={"addresses": relationship(Address)} ) mapper(Address, addresses) eq_( dict((k, v[0].__name__) for k, v in list(u_m.validators.items())), {"name": "validate_name", "addresses": "validate_address"}, )
Example #9
Source File: test_validators.py From sqlalchemy with MIT License | 5 votes |
def test_validator_bulk_collection_set(self): users, addresses, Address = ( self.tables.users, self.tables.addresses, self.classes.Address, ) class User(fixtures.ComparableEntity): @validates("addresses", include_removes=True) def validate_address(self, key, item, remove): if not remove: assert isinstance(item, str) else: assert isinstance(item, Address) item = Address(email_address=item) return item mapper(User, users, properties={"addresses": relationship(Address)}) mapper(Address, addresses) u1 = User() u1.addresses.append("e1") u1.addresses.append("e2") eq_( u1.addresses, [Address(email_address="e1"), Address(email_address="e2")], ) u1.addresses = ["e3", "e4"] eq_( u1.addresses, [Address(email_address="e3"), Address(email_address="e4")], )
Example #10
Source File: test_validators.py From sqlalchemy with MIT License | 5 votes |
def test_validator_multi_warning(self): users = self.tables.users class Foo(object): @validates("name") def validate_one(self, key, value): pass @validates("name") def validate_two(self, key, value): pass assert_raises_message( exc.InvalidRequestError, "A validation function for mapped attribute " "'name' on mapper Mapper|Foo|users already exists", mapper, Foo, users, ) class Bar(object): @validates("id") def validate_three(self, key, value): return value + 10 @validates("id", "name") def validate_four(self, key, value): return value + "foo" assert_raises_message( exc.InvalidRequestError, "A validation function for mapped attribute " "'name' on mapper Mapper|Bar|users already exists", mapper, Bar, users, )
Example #11
Source File: test_validators.py From sqlalchemy with MIT License | 4 votes |
def test_validator_w_removes(self): users, addresses, Address = ( self.tables.users, self.tables.addresses, self.classes.Address, ) canary = Mock() class User(fixtures.ComparableEntity): @validates("name", include_removes=True) def validate_name(self, key, item, remove): canary(key, item, remove) return item @validates("addresses", include_removes=True) def validate_address(self, key, item, remove): canary(key, item, remove) return item mapper(User, users, properties={"addresses": relationship(Address)}) mapper(Address, addresses) u1 = User() u1.name = "ed" u1.name = "mary" del u1.name a1, a2, a3 = Address(), Address(), Address() u1.addresses.append(a1) u1.addresses.remove(a1) u1.addresses = [a1, a2] u1.addresses = [a2, a3] eq_( canary.mock_calls, [ call("name", "ed", False), call("name", "mary", False), call("name", "mary", True), # append a1 call("addresses", a1, False), # remove a1 call("addresses", a1, True), # set to [a1, a2] - this is two appends call("addresses", a1, False), call("addresses", a2, False), # set to [a2, a3] - this is a remove of a1, # append of a3. the appends are first. # in 1.2 due to #3896, we also get 'a2' in the # validates as it is part of the set call("addresses", a2, False), call("addresses", a3, False), call("addresses", a1, True), ], )