def test_unassigned_name(self): """ Names must be assigned to a user or organization if not reserved """ unassigned_name = models.Profile(name='unassigned') db.session.add(unassigned_name) with self.assertRaises(IntegrityError): db.session.commit()
def test_user_two_names(self): """ A user cannot have two names """ piglet = self.fixtures.piglet db.session.add(piglet) assert piglet.profile.name == 'piglet' peppa = models.Profile(name='peppa', user=piglet) db.session.add(peppa) with self.assertRaises(IntegrityError): db.session.commit()
def test_org_two_names(self): """ An organization cannot have two names """ batdog = self.fixtures.batdog db.session.add(batdog) assert batdog.profile.name == 'batdog' bathound = models.Profile(name='bathound', organization=batdog) db.session.add(bathound) with self.assertRaises(IntegrityError): db.session.commit()
def test_validate_name_candidate(self): """ The name validator returns error codes as expected """ assert models.Profile.validate_name_candidate(None) == 'blank' assert models.Profile.validate_name_candidate('') == 'blank' assert models.Profile.validate_name_candidate( 'invalid_name') == 'invalid' assert models.Profile.validate_name_candidate('0123456789' * 7) == 'long' assert models.Profile.validate_name_candidate('0123456789' * 6) is None assert models.Profile.validate_name_candidate('ValidName') is None assert models.Profile.validate_name_candidate('test-reserved') is None db.session.add(models.Profile(name='test-reserved', reserved=True)) db.session.commit() assert models.Profile.validate_name_candidate( 'test-reserved') == 'reserved' assert models.Profile.validate_name_candidate( 'Test-Reserved') == 'reserved' assert models.Profile.validate_name_candidate('TestReserved') is None assert models.Profile.validate_name_candidate('piglet') == 'user' assert models.Profile.validate_name_candidate('batdog') == 'org'
def test_reserved_name(self): """ Names can be reserved, with no user or organization """ reserved_name = models.Profile(name='reserved-name', reserved=True) db.session.add(reserved_name) db.session.commit() # Profile.get() no longer works for non-public profiles retrieved_name = models.Profile.query.filter( db.func.lower(models.Profile.name) == db.func.lower( 'reserved-name')).first() assert retrieved_name is reserved_name assert reserved_name.user is None assert reserved_name.user_id is None assert reserved_name.organization is None assert reserved_name.organization_id is None reserved_name.name = 'Reserved-Name' db.session.commit() retrieved_name = models.Profile.query.filter( db.func.lower(models.Profile.name) == db.func.lower( 'Reserved-Name')).first() assert retrieved_name is reserved_name