@@ -47,6 +47,10 @@ TTestBoldUndoHandler = class
4747 procedure SetSimpleConfiguration ;
4848 procedure SetTransientConfiguration ;
4949 public
50+ [SetupFixture]
51+ procedure SetUpFixture ;
52+ [TearDownFixture]
53+ procedure TearDownFixture ;
5054 [Setup]
5155 procedure SetUp ;
5256 [TearDown]
@@ -167,18 +171,35 @@ TTestBoldUndoHandler = class
167171implementation
168172
169173uses
170- maan_UndoRedoBase ,
174+ dmBoldTest ,
171175 maan_UndoRedoTestCaseUtils;
172176
173177{ TTestBoldUndoHandler }
174178
179+ procedure TTestBoldUndoHandler.SetUpFixture ;
180+ begin
181+ // Create DataModule and database ONCE for the entire test fixture
182+ EnsureBoldTestDM;
183+ Assert.IsNotNull(dmBoldTest, ' dmBoldTest should be created' );
184+ Assert.IsNotNull(dmBoldTest.BoldSystemHandle1.System, ' System should be active' );
185+ end ;
186+
187+ procedure TTestBoldUndoHandler.TearDownFixture ;
188+ begin
189+ // Clean up DataModule at the end of all tests
190+ CloseBoldTestDM;
191+ end ;
192+
175193procedure TTestBoldUndoHandler.SetUp ;
176194begin
177- EnsureDM;
178- Assert.IsNotNull(dmUndoRedo, ' dmUndoRedo should be created' );
179- Assert.IsNotNull(dmUndoRedo.BoldSystemHandle1.System, ' System should be active' );
195+ // Ensure system is active for this test
196+ if not dmBoldTest.BoldSystemHandle1.Active then
197+ dmBoldTest.BoldSystemHandle1.Active := True;
198+
199+ // Start database transaction for test isolation - will be rolled back in TearDown
200+ dmBoldTest.FDConnection1.StartTransaction;
180201
181- FUndoHandler := dmUndoRedo .BoldSystemHandle1.System.UndoHandler as TBoldUndoHandler;
202+ FUndoHandler := dmBoldTest .BoldSystemHandle1.System.UndoHandler as TBoldUndoHandler;
182203 FUndoHandler.Enabled := True;
183204
184205 FSomeClassList := TSomeClassList.Create;
@@ -189,24 +210,26 @@ procedure TTestBoldUndoHandler.SetUp;
189210
190211procedure TTestBoldUndoHandler.TearDown ;
191212begin
192- if Assigned(dmUndoRedo) then
193- begin
194- if dmUndoRedo.BoldSystemHandle1.Active then
195- begin
196- dmUndoRedo.BoldSystemHandle1.UpdateDatabase;
197- dmUndoRedo.BoldSystemHandle1.Active := False;
198- end ;
199- FreeAndNil(dmUndoRedo);
200- end ;
201213 FreeAndNil(FSomeClassList);
202214 FreeAndNil(FAPersistentClassList);
203215 FreeAndNil(FATransientClassList);
204216 FreeAndNil(FFSValueSpace);
217+
218+ if Assigned(dmBoldTest) and dmBoldTest.BoldSystemHandle1.Active then
219+ begin
220+ // Discard in-memory changes and rollback database transaction for test isolation
221+ dmBoldTest.BoldSystemHandle1.System.Discard;
222+ if dmBoldTest.FDConnection1.InTransaction then
223+ dmBoldTest.FDConnection1.Rollback;
224+ // Deactivate system to get a clean state for next test
225+ dmBoldTest.BoldSystemHandle1.Active := False;
226+ end ;
227+ // Note: dmBoldTest is NOT freed here - it's reused across tests
205228end ;
206229
207230function TTestBoldUndoHandler.GetSystem : TBoldSystem;
208231begin
209- Result := dmUndoRedo .BoldSystemHandle1.System;
232+ Result := dmBoldTest .BoldSystemHandle1.System;
210233end ;
211234
212235function TTestBoldUndoHandler.GetUndoHandler : TBoldUndoHandler;
@@ -238,19 +261,19 @@ procedure TTestBoldUndoHandler.FetchClassSorted(const aSystem: TBoldSystem;
238261procedure TTestBoldUndoHandler.RefreshSystem ;
239262begin
240263 UpdateDatabase;
241- dmUndoRedo .BoldSystemHandle1.Active := False;
264+ dmBoldTest .BoldSystemHandle1.Active := False;
242265 FSomeClassList.Clear;
243266 FAPersistentClassList.Clear;
244267 FATransientClassList.Clear;
245- dmUndoRedo .BoldSystemHandle1.Active := True;
246- FUndoHandler := dmUndoRedo .BoldSystemHandle1.System.UndoHandler as TBoldUndoHandler;
268+ dmBoldTest .BoldSystemHandle1.Active := True;
269+ FUndoHandler := dmBoldTest .BoldSystemHandle1.System.UndoHandler as TBoldUndoHandler;
247270 FUndoHandler.Enabled := True;
248271 FetchClassSorted(System, FSomeClassList, TSomeClass);
249272end ;
250273
251274procedure TTestBoldUndoHandler.UpdateDatabase ;
252275begin
253- dmUndoRedo .BoldSystemHandle1.UpdateDatabase;
276+ dmBoldTest .BoldSystemHandle1.UpdateDatabase;
254277end ;
255278
256279procedure TTestBoldUndoHandler.SetSimpleConfiguration ;
@@ -498,7 +521,7 @@ procedure TTestBoldUndoHandler.TestUndoObjectCreation;
498521
499522 Assert.AreEqual(1 , ParentObj.child.Count, ' Parent should have 1 child' );
500523
501- // Undo the child creation
524+ // Undo the child creation - child was never persisted, so undo should just remove from memory
502525 UndoHandler.UndoBlock(BlockName);
503526
504527 // Re-fetch parent
0 commit comments