def testMementos(self): biz = self.biz cur = biz._CurrentCursor priorVal = biz.Record.cField # Make a change that is the same as the prior value: biz.Record.cField = priorVal self.assertEqual(priorVal, biz.Record.cField) # Make a change that is different: biz.Record.cField = "New test value" self.assertEqual(cur._mementos, {biz.Record.pk: {"cField": priorVal}}) self.assertEqual(biz.isChanged(), True) # Change it back: biz.Record.cField = priorVal self.assertEqual(cur._mementos, {}) self.assertEqual(biz.isChanged(), False) # Make a change that is different and cancel: biz.Record.cField = "New test value" biz.cancel() self.assertEqual(cur._mementos, {}) self.assertEqual(biz.isChanged(), False) # Add a record: biz.new() self.assertEqual(biz.RowCount, 4) self.assertEqual(biz.RowNumber, 3) self.assertEqual(cur._newRecords, {-1: None}) self.assertEqual(biz.isChanged(), False) ## (because no field changed in new record) self.assertEqual(cur.Record.pk, -1) self.assertEqual(biz.Record.cField, "") self.assertEqual(biz.Record.iField, 0) self.assertEqual(biz.Record.nField, 0) biz.save() self.assertEqual( biz.RowCount, 4) ## still have 4 rows, even though the last one wasn't saved biz.requery() # We only have 3 rows, because one of the prior 4 rows was new with no changed fields: self.assertEqual(biz.RowCount, 3) # ...and RowNumber went to 0, because the previous row number (3) doesn't exist anymore: self.assertEqual(biz.RowNumber, 0) self.assertEqual(cur._newRecords, {}) self.assertEqual(biz.isChanged(), False) self.assertEqual(biz.Record.pk, 1)
def testMementoSaveNewPotentialProblem(self): """This attempts to reproduce problems being reported on dabo-users (see thread http://leafe.com/archives/showFullThd/374683) """ biz = self.biz self.assertEqual(biz.isChanged(), False) self.assertEqual(biz.isAnyChanged(), False) biz.new() self.assertEqual(biz.isChanged(), False) self.assertEqual(biz.isAnyChanged(), False) biz.Record.cField = 'ppp' self.assertEqual(biz.isChanged(), True) self.assertEqual(biz.isAnyChanged(), True) biz.save() self.assertEqual(biz.isChanged(), False) self.assertEqual(biz.isAnyChanged(), False)
def testMementos(self): biz = self.biz cur = biz._CurrentCursor priorVal = biz.Record.cField # Make a change that is the same as the prior value: biz.Record.cField = priorVal self.assertEqual(priorVal, biz.Record.cField) # Make a change that is different: biz.Record.cField = "New test value" self.assertEqual(cur._mementos, {biz.Record.pk: {"cField": priorVal}}) self.assertEqual(biz.isChanged(), True) # Change it back: biz.Record.cField = priorVal self.assertEqual(cur._mementos, {}) self.assertEqual(biz.isChanged(), False) # Make a change that is different and cancel: biz.Record.cField = "New test value" biz.cancel() self.assertEqual(cur._mementos, {}) self.assertEqual(biz.isChanged(), False) # Add a record: biz.new() self.assertEqual(biz.RowCount, 4) self.assertEqual(biz.RowNumber, 3) self.assertEqual(cur._newRecords, {-1: None}) self.assertEqual(biz.isChanged(), False) ## (because no field changed in new record) self.assertEqual(cur.Record.pk, -1) self.assertEqual(biz.Record.cField, "") self.assertEqual(biz.Record.iField, 0) self.assertEqual(biz.Record.nField, 0) biz.save() self.assertEqual(biz.RowCount, 4) ## still have 4 rows, even though the last one wasn't saved biz.requery() # We only have 3 rows, because one of the prior 4 rows was new with no changed fields: self.assertEqual(biz.RowCount, 3) # ...and RowNumber went to 0, because the previous row number (3) doesn't exist anymore: self.assertEqual(biz.RowNumber, 0) self.assertEqual(cur._newRecords, {}) self.assertEqual(biz.isChanged(), False) self.assertEqual(biz.Record.pk, 1)
def testDeleteNewSave(self): biz = self.biz biz.delete() biz.new() biz.save() self.assertEqual(biz.RowCount, 3) self.assertEqual(biz.RowNumber, 2) biz.deleteAll() self.assertEqual(biz.RowCount, 0) self.assertRaises(dabo.dException.NoRecordsException, biz.delete) biz.new() self.assertEqual(biz.RowCount, 1) biz.delete() self.assertEqual(biz.RowCount, 0) biz.new() biz.save() self.assertEqual(biz.RowCount, 1) self.assertEqual(biz.RowNumber, 0)
def recordScore(self): """Record the score, if it is possible/allowed. If this game was from a preset, we can optionally record the score to the public database. Present a dialog asking for Name and Comments, and if Ok pressed, record the score. Otherwise, don't even connect to the internet at all. """ if self.preset["Id"] is None or self.preset["Id"] < 1: return class dlgScore(dabo.ui.dOkCancelDialog): def addControls(self): class Label(dabo.ui.dLabel): def initProperties(self): self.Alignment = "Right" self.AutoResize = False self.Width = 100 vs = self.Sizer message = """Since you are playing a game defined in the public presets, you can record your score in the public database. If you'd like to do this, just enter your name and optional comment. You'll need an internet connection for this to work.""" o = self.addObject(dabo.ui.dLabel, Caption=message) vs.append(o, border=10) brdr = 5 hs = dabo.ui.dSizer("horizontal") lbl = Label(self, Name="lblName", Caption="Name:") txt = dabo.ui.dTextBox(self, RegID="txtName") hs.append(lbl, "fixed", alignment="right", border=brdr) hs.append1x(txt, border=brdr) vs.append1x(hs) hs = dabo.ui.dSizer("horizontal") lbl = Label(self, Name="lblComment", Caption="Comment:") txt = dabo.ui.dTextBox(self, RegID="txtComment") hs.append(lbl, "fixed", alignment="right", border=brdr) hs.append1x(txt, border=brdr) vs.append1x(hs) playername = self.Application.PreferenceManager.playername if isinstance(playername, str): self.txtName.Value = playername else: self.txtName.Value = "" self.Application.PreferenceManager.playername = "" dlg = dlgScore(self, Caption="Record Score") dlg.fitToSizer() dlg.show() if dlg.Accepted: # get the bizobj, add the new record, and commit conn = dabo.db.dConnection(MinesweeperCI()) biz = MinesweeperBO_scores(conn) biz.new() while True: try: biz.Record.playername = dlg.txtName.Value biz.Record.playercomments = dlg.txtComment.Value biz.Record.gamedefid = self.preset["Id"] biz.Record.time = self.board.StopWatch.Value biz.save() self.Application.PreferenceManager.playername = biz.Record.playername break except dabo.dException.BusinessRuleViolation as e: dabo.ui.exclaim(ustr(e)) dlg.show() if not dlg.Accepted: break dlg.release()
def recordScore(self): """Record the score, if it is possible/allowed. If this game was from a preset, we can optionally record the score to the public database. Present a dialog asking for Name and Comments, and if Ok pressed, record the score. Otherwise, don't even connect to the internet at all. """ if self.preset["Id"] is None or self.preset["Id"] < 1: return class dlgScore(dabo.ui.dOkCancelDialog): def addControls(self): class Label(dabo.ui.dLabel): def initProperties(self): self.Alignment = "Right" self.AutoResize = False self.Width = 100 vs = self.Sizer message = """Since you are playing a game defined in the public presets, you can record your score in the public database. If you'd like to do this, just enter your name and optional comment. You'll need an internet connection for this to work.""" o = self.addObject(dabo.ui.dLabel, Caption=message) vs.append(o, border=10) brdr = 5 hs = dabo.ui.dSizer("horizontal") lbl = Label(self, Name="lblName", Caption="Name:") txt = dabo.ui.dTextBox(self, RegID="txtName") hs.append(lbl, "fixed", alignment="right", border=brdr) hs.append1x(txt, border=brdr) vs.append1x(hs) hs = dabo.ui.dSizer("horizontal") lbl = Label(self, Name="lblComment", Caption="Comment:") txt = dabo.ui.dTextBox(self, RegID="txtComment") hs.append(lbl, "fixed", alignment="right", border=brdr) hs.append1x(txt, border=brdr) vs.append1x(hs) playername = self.Application.PreferenceManager.playername if isinstance(playername, basestring): self.txtName.Value = playername else: self.txtName.Value = "" self.Application.PreferenceManager.playername = "" dlg = dlgScore(self, Caption="Record Score") dlg.fitToSizer() dlg.show() if dlg.Accepted: # get the bizobj, add the new record, and commit conn = dabo.db.dConnection(MinesweeperCI()) biz = MinesweeperBO_scores(conn) biz.new() while True: try: biz.Record.playername = dlg.txtName.Value biz.Record.playercomments = dlg.txtComment.Value biz.Record.gamedefid = self.preset["Id"] biz.Record.time = self.board.StopWatch.Value biz.save() self.Application.PreferenceManager.playername = biz.Record.playername break except dabo.dException.BusinessRuleViolation, e: dabo.ui.exclaim(ustr(e)) dlg.show() if not dlg.Accepted: break