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 testCRUD(self): # create team name = "Houkago Tea Time" team = Team.create(name=name) self.assertEqual(str(team), "<Team '%s'>" % name) # create members for idx, n in enumerate(self.names): member = Member.create(team_id=team.pk, 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 id ritsu = Member.get(1) self.assertEqual(str(ritsu), "<Member 'Ritsu Tainaka : Dr'>") # get team the member Ritsu belongs to is Houkago Tea Time team = member.team 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]) macaron.bake() # Yui changes instrument to castanets yui = Member.get("first_name=? AND last_name=?", ["Yui", "Hirasawa"]) 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) # cancel the changes macaron.rollback() # Add another member 'Sawako' as Gt1 team = Team.get(1) Member.create(team_id=team.pk, first_name="Sawako", last_name="Yamanaka", part="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=? AND last_name=?", ["Azusa", "Nakano"]) self.assertEqual(str(azu), "<Member 'Azusa Nakano : Gt2'>") # Okay, Yui changes part to Gt1 yui = Member.get("first_name=? AND last_name=?", ["Yui", "Hirasawa"]) yui.part = "Gt1" yui.save() # 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])