Skip to content

Commit f7bb17b

Browse files
committed
Add generic dmBoldTest DataModule with optimized test setup
- Create dmBoldTest as reusable DataModule for Bold unit tests - Use fixture-level setup/teardown to create DB once per test fixture - Skip schema creation if tables already exist (faster subsequent runs) - Clear tables instead of recreating database between test runs - Use transaction rollback for test isolation within fixture - Update Test.BoldUndoHandler to use new DataModule
1 parent 54e56a0 commit f7bb17b

4 files changed

Lines changed: 899 additions & 20 deletions

File tree

UnitTest/Code/ObjectSpace/Test.BoldUndoHandler.pas

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
167171
implementation
168172

169173
uses
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+
175193
procedure TTestBoldUndoHandler.SetUp;
176194
begin
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

190211
procedure TTestBoldUndoHandler.TearDown;
191212
begin
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
205228
end;
206229

207230
function TTestBoldUndoHandler.GetSystem: TBoldSystem;
208231
begin
209-
Result := dmUndoRedo.BoldSystemHandle1.System;
232+
Result := dmBoldTest.BoldSystemHandle1.System;
210233
end;
211234

212235
function TTestBoldUndoHandler.GetUndoHandler: TBoldUndoHandler;
@@ -238,19 +261,19 @@ procedure TTestBoldUndoHandler.FetchClassSorted(const aSystem: TBoldSystem;
238261
procedure TTestBoldUndoHandler.RefreshSystem;
239262
begin
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);
249272
end;
250273

251274
procedure TTestBoldUndoHandler.UpdateDatabase;
252275
begin
253-
dmUndoRedo.BoldSystemHandle1.UpdateDatabase;
276+
dmBoldTest.BoldSystemHandle1.UpdateDatabase;
254277
end;
255278

256279
procedure 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

Comments
 (0)