def testBorderValues(self): azusa = Member.create(first_name="Azusa", last_name="Nakano", part="Gt2", age=16) self.assert_(azusa) def _age_exceeded(): azusa.age = 19 def _age_underrun(): azusa.age = 14 self.assertRaises(macaron.ValidationError, _age_exceeded) self.assertRaises(macaron.ValidationError, _age_underrun) def _too_long_part_name(): azusa.part = "1234567890A" self.assertRaises(macaron.ValidationError, _too_long_part_name) def _name_is_not_set(): Member.create(first_name="Azusa") self.assertRaises(macaron.ValidationError, _name_is_not_set)
def testCRUDObject(self): # Test for creating, reading, updating, deleteing # Create team name = "Houkago Tea Time" team = Team.create(name=name) self.assertEqual(str(team), "<Team '%s'>" % name) self.assertEqual(team.id, 1) # Create members for idx, n in enumerate(self.names): member = Member.create(band=team, first_name=n[0], last_name=n[1], part=n[2]) self.assertEqual(str(member), "<Member '%s'>" % n[3]) self.assertEqual(member.id, idx + 1) # Get member with primary key ritsu = Member.get(1) self.assertEqual(str(ritsu), "<Member 'Ritsu Tainaka : Dr'>") # Get team the member Ritsu belongs to is Houkago Tea Time team = ritsu.band self.assertEqual(str(team), "<Team 'Houkago Tea Time'>") # Get members with iterator for idx, m in enumerate(team.members): self.assertEqual(str(m), "<Member '%s'>" % self.names[idx][3]) # Yui changes instrument to castanets macaron.bake() # Commit before changes yui = Member.get(first_name="Yui", last_name="Hirasawa") self.assert_(yui) yui.part = "Castanets" self.assertEqual(yui.part, "Castanets") yui.save() # Re-fetch Yui member = Member.get(3) self.assertEqual(member.part, "Castanets") # Delete all members self.assertEqual(team.members.count(), 4) team.members.select(first_name="Ritsu").delete() self.assertEqual(team.members.count(), 3) team.members.delete() self.assertEqual(team.members.count(), 0) # Test for rollback # Cancel the changes macaron.rollback() team = Team.get(1) self.assertEqual(team.members.count(), 4) # Add another member 'Sawako' as Gt1 member = Member.create(band=team, first_name="Sawako", last_name="Yamanaka", part="Gt1") self.assertEqual(str(member), "<Member 'Sawako Yamanaka : Gt1'>") # Re-fetch Sawako with index sawako = team.members[4] self.assertEqual(str(sawako), "<Member 'Sawako Yamanaka : Gt1'>") # But, Sawako is not a member of the team sawako.delete() # Add another member Azusa through reverse relation of ManyToOne team.members.append(first_name="Azusa", last_name="Nakano", part="Gt2") azu = Member.get(first_name="Azusa") self.assertEqual(str(azu), "<Member 'Azusa Nakano : Gt2'>") # Okay, Yui changes part to Gt1 yui = Member.get(first_name="Yui") yui.part = "Gt1" yui.save() self.assertEqual(yui.part, "Gt1") # At last, there are five menbers nm = self.names[:] nm[2] = ("Yui", "Hirasawa", "Gt1", "Yui Hirasawa : Gt1") nm.append(("Azusa", "Nakano", "Gt2", "Azusa Nakano : Gt2")) for idx, m in enumerate(team.members): self.assertEqual(str(m), "<Member '%s'>" % nm[idx][3]) # Foreign key constraint works on sqlite3 >= 3.6.19 ver = macaron.sqlite_version_info if ver >= (3, 6, 19): # Test for ON CASCADE team = Team.get(1) team.id = 2 team.save() cnt = 0 for member in Member.all(): cnt += 1 self.assertEqual(member.band_id, 2) self.assertEqual(cnt, Member.all().count()) # Test for ON DELETE team.delete() cnt = 0 for member in Member.all(): self.assertEqual(member.band, None) cnt += 1 self.assertEqual(cnt, Member.all().count()) else: msg = "Foreign key constraint works on SQLite(3.6.19) > Current(%s). Skip." warnings.warn(msg % ".".join([str(x) for x in ver]))
def _name_is_not_set(): Member.create(first_name="Azusa") self.assertRaises(macaron.ValidationError, _name_is_not_set)