ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/testsuite/Test17.pas
Revision: 410
Committed: Thu Jun 22 13:52:39 2023 UTC (16 months, 3 weeks ago) by tony
Content type: text/x-pascal
File size: 16424 byte(s)
Log Message:
Release 2.6.0 beta

File Contents

# User Rev Content
1 tony 323 (*
2     * IBX Test suite. This program is used to test the IBX non-visual
3     * components and provides a semi-automated pass/fail check for each test.
4     *
5     * The contents of this file are subject to the Initial Developer's
6     * Public License Version 1.0 (the "License"); you may not use this
7     * file except in compliance with the License. You may obtain a copy
8     * of the License here:
9     *
10     * http://www.firebirdsql.org/index.php?op=doc&id=idpl
11     *
12     * Software distributed under the License is distributed on an "AS
13     * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14     * implied. See the License for the specific language governing rights
15     * and limitations under the License.
16     *
17     * The Initial Developer of the Original Code is Tony Whyman.
18     *
19     * The Original Code is (C) 2021 Tony Whyman, MWA Software
20     * (http://www.mwasoftware.co.uk).
21     *
22     * All Rights Reserved.
23     *
24     * Contributor(s): ______________________________________.
25     *
26     *)
27 tony 315 unit Test17;
28    
29     {$mode objfpc}{$H+}
30    
31     {Test 17: TIBDataSet Tests}
32    
33     { Description
34    
35     TIBDataset Insert/Update/Delete tests.
36     }
37    
38     interface
39    
40     uses
41     Classes, SysUtils, TestApplication, IBXTestBase, DB, IB, IBCustomDataSet;
42    
43     const
44     aTestID = '17';
45     aTestTitle = 'TIBDataset Tests';
46    
47     type
48    
49     { TTest1 }
50    
51     { TTest17 }
52    
53     TTest17 = class(TIBXTestBase)
54     private
55     FIBDataSet1: TIBDataSet;
56     FIBDataSet2: TIBDataSet;
57     procedure HandleDeleteReturning(Sender: TObject; QryResults: IResults);
58     procedure HandleBeforeOpen(DataSet: TDataSet);
59     procedure HandleAfterOpen(DataSet: TDataSet);
60     procedure HandleBeforeClose(DataSet: TDataSet);
61     procedure HandleAfterClose(DataSet: TDataSet);
62     procedure HandleBeforeInsert(DataSet: TDataSet);
63     procedure HandleAfterInsert(DataSet: TDataSet);
64     procedure HandleBeforeEdit(DataSet: TDataSet);
65     procedure HandleAfterEdit(DataSet: TDataSet);
66     procedure HandleBeforePost(DataSet: TDataSet);
67     procedure HandleAfterPost(DataSet: TDataSet);
68     procedure HandleBeforeCancel(DataSet: TDataSet);
69     procedure HandleAfterCancel(DataSet: TDataSet);
70     procedure HandleBeforeDelete(DataSet: TDataSet);
71     procedure HandleAfterDelete(DataSet: TDataSet);
72     procedure HandleBeforeScroll(DataSet: TDataSet);
73     procedure HandleAfterScroll(DataSet: TDataSet);
74     procedure HandleBeforeRefresh(DataSet: TDataSet);
75     procedure HandleAfterRefresh(DataSet: TDataSet);
76     procedure ValidatePostOK(Sender: TObject; var CancelPost: boolean);
77     procedure ValidatePostCancel(Sender: TObject; var CancelPost: boolean);
78     procedure HandlePostError(DataSet: TDataSet; E: EDatabaseError;
79     var DataAction: TDataAction);
80     protected
81     procedure CreateObjects(Application: TTestApplication); override;
82     function GetTestID: AnsiString; override;
83     function GetTestTitle: AnsiString; override;
84     procedure InitTest; override;
85     public
86     procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override;
87     end;
88    
89    
90     implementation
91    
92     { TTest17 }
93    
94     procedure TTest17.HandleDeleteReturning(Sender: TObject; QryResults: IResults);
95     begin
96     writeln(OutFile,'Delete Returning');
97     ReportResult(QryResults);
98     writeln(OutFile);
99     end;
100    
101     procedure TTest17.HandleBeforeOpen(DataSet: TDataSet);
102     Begin
103     writeln(Outfile,'Dataset Event BeforeOpen: State = ',DataSet.State);
104     end;
105     procedure TTest17.HandleAfterOpen(DataSet: TDataSet);
106     Begin
107     writeln(Outfile,'Dataset Event AfterOpen: State = ',DataSet.State);
108     end;
109     procedure TTest17.HandleBeforeClose(DataSet: TDataSet);
110     Begin
111     writeln(Outfile,'Dataset Event BeforeClose: State = ',DataSet.State);
112     end;
113     procedure TTest17.HandleAfterClose(DataSet: TDataSet);
114     Begin
115     writeln(Outfile,'Dataset Event AfterClose: State = ',DataSet.State);
116     end;
117     procedure TTest17.HandleBeforeInsert(DataSet: TDataSet);
118     Begin
119     writeln(Outfile,'Dataset Event BeforeInsert: State = ',DataSet.State);
120     end;
121     procedure TTest17.HandleAfterInsert(DataSet: TDataSet);
122     Begin
123     writeln(Outfile,'Dataset Event AfterInsert: State = ',DataSet.State);
124     end;
125     procedure TTest17.HandleBeforeEdit(DataSet: TDataSet);
126     Begin
127     writeln(Outfile,'Dataset Event BeforeEdit: State = ',DataSet.State);
128     end;
129     procedure TTest17.HandleAfterEdit(DataSet: TDataSet);
130     Begin
131     writeln(Outfile,'Dataset Event AfterEdit: State = ',DataSet.State);
132     end;
133     procedure TTest17.HandleBeforePost(DataSet: TDataSet);
134     Begin
135     writeln(Outfile,'Dataset Event BeforePost: State = ',DataSet.State);
136     end;
137     procedure TTest17.HandleAfterPost(DataSet: TDataSet);
138     Begin
139     writeln(Outfile,'Dataset Event AfterPost: State = ',DataSet.State);
140     end;
141     procedure TTest17.HandleBeforeCancel(DataSet: TDataSet);
142     Begin
143     writeln(Outfile,'Dataset Event BeforeCancel: State = ',DataSet.State);
144     end;
145     procedure TTest17.HandleAfterCancel(DataSet: TDataSet);
146     Begin
147     writeln(Outfile,'Dataset Event AfterCancel: State = ',DataSet.State);
148     end;
149     procedure TTest17.HandleBeforeDelete(DataSet: TDataSet);
150     Begin
151     writeln(Outfile,'Dataset Event BeforeDelete: State = ',DataSet.State);
152     end;
153     procedure TTest17.HandleAfterDelete(DataSet: TDataSet);
154     Begin
155     writeln(Outfile,'Dataset Event AfterDelete: State = ',DataSet.State);
156     end;
157     procedure TTest17.HandleBeforeScroll(DataSet: TDataSet);
158     Begin
159     writeln(Outfile,'Dataset Event BeforeScroll: State = ',DataSet.State);
160     end;
161     procedure TTest17.HandleAfterScroll(DataSet: TDataSet);
162     Begin
163     writeln(Outfile,'Dataset Event AfterScroll: State = ',DataSet.State);
164     end;
165     procedure TTest17.HandleBeforeRefresh(DataSet: TDataSet);
166     Begin
167     writeln(Outfile,'Dataset Event BeforeRefresh: State = ',DataSet.State);
168     end;
169     procedure TTest17.HandleAfterRefresh(DataSet: TDataSet);
170     Begin
171     writeln(Outfile,'Dataset Event AfterRefresh: State = ',DataSet.State);
172     end;
173    
174     procedure TTest17.ValidatePostOK(Sender: TObject; var CancelPost: boolean);
175     begin
176     writeln(Outfile,'Validate Post OK called');
177     CancelPost := false;
178     end;
179    
180     procedure TTest17.ValidatePostCancel(Sender: TObject; var CancelPost: boolean);
181     begin
182     writeln(Outfile,'Validate Post Cancel called');
183     CancelPost := true;
184     end;
185    
186     procedure TTest17.HandlePostError(DataSet: TDataSet; E: EDatabaseError;
187     var DataAction: TDataAction);
188     begin
189     writeln(Outfile,'Post Error Called: ',E.Message);
190     DataAction := daFail;
191     end;
192    
193     procedure TTest17.CreateObjects(Application: TTestApplication);
194     begin
195     inherited CreateObjects(Application);
196     FIBDataSet1 := TIBDataSet.Create(Application);
197     with FIBDataSet1 do
198     begin
199     Database := IBDatabase;
200     Transaction := IBTransaction;
201     Unidirectional := false;
202     SelectSQL.Add('Select * From IBDataSetTest');
203     InsertSQL.Add('Insert into IBDataSetTest(KeyField,PlainText) Values (:KeyField,:PlainText)');
204     ModifySQL.Add('Update IBDataSetTest Set KeyField = :KeyField, PlainText = :PlainText Where KeyField = :Old_KeyField');
205     DeleteSQL.Add('Delete from IBDataSetTest Where KeyField = :old_KeyField');
206     RefreshSQL.Add('Select * from IBDataSetTest Where KeyField = :KeyField');
207     GeneratorField.Field := 'KeyField';
208     GeneratorField.Generator := 'AGENERATOR';
209     GeneratorField.Increment := 1;
210     OnPostError := @HandlePostError;
211     end;
212     FIBDataSet2 := TIBDataSet.Create(Application);
213     with FIBDataSet2 do
214     begin
215     Database := IBDatabase;
216     Transaction := IBTransaction;
217     Unidirectional := false;
218     SelectSQL.Add('Select * From IBDataSetTest');
219     InsertSQL.Add('Insert into IBDataSetTest(KeyField,PlainText) Values (Gen_ID(AGenerator,1),:PlainText) Returning KeyField, TextAndKey');
220     ModifySQL.Add('Update IBDataSetTest Set KeyField = :KeyField, PlainText = :PlainText Where KeyField = :Old_KeyField Returning TextAndKey,ServerSideText');
221     DeleteSQL.Add('Delete from IBDataSetTest Where KeyField = :old_KeyField Returning KeyField');
222     RefreshSQL.Add('Select * from IBDataSetTest Where KeyField = :KeyField');
223     OnDeleteReturning := @HandleDeleteReturning;
224     BeforeOpen := @HandleBeforeOpen;
225     AfterOpen := @HandleAfterOpen;
226     BeforeClose := @HandleBeforeClose;
227     AfterClose := @HandleAfterClose;
228     BeforeInsert := @HandleBeforeInsert;
229     AfterInsert := @HandleAfterInsert;
230     BeforeEdit := @HandleBeforeEdit;
231     AfterEdit := @HandleAfterEdit;
232     BeforePost := @HandleBeforePost;
233     AfterPost := @HandleAfterPost;
234     BeforeCancel := @HandleBeforeCancel;
235     AfterCancel := @HandleAfterCancel;
236     BeforeDelete := @HandleBeforeDelete;
237     AfterDelete := @HandleAfterDelete;
238     BeforeScroll := @HandleBeforeScroll;
239     AfterScroll := @HandleAfterScroll;
240     BeforeRefresh := @HandleBeforeRefresh;
241     AfterRefresh := @HandleAfterRefresh;
242     end;
243     end;
244    
245     function TTest17.GetTestID: AnsiString;
246     begin
247     Result := aTestID;
248     end;
249    
250     function TTest17.GetTestTitle: AnsiString;
251     begin
252     Result := aTestTitle;
253     end;
254    
255     procedure TTest17.InitTest;
256     begin
257     IBDatabase.DatabaseName := Owner.GetNewDatabaseName;
258     IBDatabase.CreateIfNotExists := true;
259     ReadWriteTransaction;
260     end;
261    
262     procedure TTest17.RunTest(CharSet: AnsiString; SQLDialect: integer);
263     var lastKey: integer;
264 tony 410 i: integer;
265     B: TBookmark;
266 tony 315 begin
267     IBDatabase.CreateDatabase;
268     try
269     IBTransaction.Active := true;
270     with FIBDataSet1 do
271     begin
272     Active := true;
273     writeln(OutFile,'FIBDataSet1: Simple Append');
274     Append;
275     // writeln(outfile,'BOF = ',BOF,', EOF = ',EOF);
276     FieldByName('PlainText').AsString := 'This is a test';
277     Post;
278     PrintDataSetRow(FIBDataSet1);
279     Refresh;
280     writeln(OutFile,'After Refresh');
281     PrintDataSetRow(FIBDataSet1);
282     writeln(OutFile,'Append and Update');
283     Append;
284     FieldByName('PlainText').AsString := 'This is another test';
285     Post;
286     PrintDataSetRow(FIBDataSet1);
287     Edit;
288     FieldByName('PlainText').AsString := 'This is the update test';
289     Post;
290     PrintDataSetRow(FIBDataSet1);
291 tony 410 writeln(Outfile,'Show whole Dataset');
292     PrintDataSet(FIBDataSet1);
293 tony 315 writeln(OutFile,'Now delete the first row');
294     First;
295     Delete;
296     PrintDataSet(FIBDataSet1);
297     writeln(Outfile,'On Post KeyField Assignment');
298     FIBDataSet1.GeneratorField.ApplyOnEvent := gaeOnPostRecord;
299     Append;
300     FieldByName('PlainText').AsString := 'On Post KeyField test';
301     PrintDataSetRow(FIBDataSet1);
302     Post;
303     writeln(Outfile,'Row data after post');
304     PrintDataSetRow(FIBDataSet1);
305     FIBDataSet1.GeneratorField.ApplyOnEvent := gaeOnNewRecord; {restore}
306    
307     writeln(Outfile,'Catch a Post Error - duplicate key');
308     lastkey := FieldByName('KeyField').AsInteger;
309     Append;
310     FieldByName('KeyField').AsInteger := lastkey;
311     FieldByName('PlainText').AsString := 'On Post Error test';
312     try
313     Post;
314     except on E: Exception do
315     writeln(Outfile,'Exception handled: ',E.Message);
316     end;
317    
318     IBTransaction.Rollback;
319     IBTransaction.Active := true;
320     DataSetCloseAction := dcSaveChanges;
321     Active := true;
322     writeln(OutFile,'FIBDataSet1: Simple Append with automatic posting on close');
323     Append;
324     FieldByName('PlainText').AsString := 'This is a test';
325     Active := false;
326     Active := true;
327     PrintDataSet(FIBDataSet1);
328    
329     IBTransaction.Rollback;
330     IBTransaction.Active := true;
331     DataSetCloseAction := dcDiscardChanges;
332     Active := true;
333     writeln(OutFile,'FIBDataSet1: Simple Append with discard on close');
334     Append;
335     FieldByName('PlainText').AsString := 'This is a test';
336     Active := false;
337     Active := true;
338     PrintDataSet(FIBDataSet1);
339    
340 tony 410 IBTransaction.Rollback;
341 tony 315 IBTransaction.Active := true;
342     writeln(Outfile);
343     writeln(Outfile,'Unidirectional editing');
344     Unidirectional := true;
345     Active := true;
346     writeln(OutFile,'FIBDataSet1: Simple Append - unidirectional');
347     Insert;
348     writeln(outfile,'BOF = ',BOF,', EOF = ',EOF);
349     FieldByName('PlainText').AsString := 'This is a test - unidirectional';
350     PrintDataSetRow(FIBDataSet1);
351     Post;
352     writeln(outfile,'BOF = ',BOF,', EOF = ',EOF);
353     PrintDataSetRow(FIBDataSet1);
354     Refresh;
355     writeln(OutFile,'After Refresh - unidirectional');
356     PrintDataSetRow(FIBDataSet1);
357 tony 410 writeln(OutFile,' Record Count = ',FIBDataSet1.RecordCount);
358     writeln(OutFile,'Insert and Update');
359 tony 315 Insert;
360     FieldByName('PlainText').AsString := 'This is another test - unidirectional';
361     Post;
362     PrintDataSetRow(FIBDataSet1);
363     Edit;
364     FieldByName('PlainText').AsString := 'This is the update test - unidirectional';
365     Post;
366     PrintDataSetRow(FIBDataSet1);
367 tony 410 writeln(OutFile,'Now delete the first row - unidirectional with Record Count = ',FIBDataSet1.RecordCount);
368     Active := false;
369     Active := true;
370 tony 315 Delete;
371 tony 410 writeln(OutFile,'Show Current Row');
372     PrintDataSetRow(FIBDataSet1);
373     writeln(OutFile,' Record Count = ',FIBDataSet1.RecordCount);
374 tony 315 writeln(Outfile,'Ensure dataset saved to database');
375     Active := false;
376     Active := true;
377 tony 410 PrintDataSet(FIBDataSet1);
378 tony 315
379     end;
380     writeln(Outfile,'==================================');
381     IBTransaction.Rollback;
382     IBTransaction.Active := true;
383     with FIBDataSet2 do
384 tony 410 try
385 tony 315 Active := true;
386     writeln(OutFile,'FIBDataSet2: Simple Append');
387     Append;
388     FieldByName('PlainText').AsString := 'This is a test';
389     Post;
390     PrintDataSetRow(FIBDataSet2);
391     Refresh;
392     writeln(OutFile,'After Refresh');
393     PrintDataSetRow(FIBDataSet2);
394     writeln(OutFile,'Append and Update');
395     Append;
396     FieldByName('PlainText').AsString := 'This is another test';
397     Post;
398     PrintDataSetRow(FIBDataSet2);
399     Edit;
400     FieldByName('PlainText').AsString := 'This is the update test';
401     Post;
402     PrintDataSetRow(FIBDataSet2);
403     writeln(OutFile,'Now delete the first row');
404     PrintDataSet(FIBDataSet2);
405     First;
406     Delete;
407     PrintDataSet(FIBDataSet2);
408     OnValidatePost := @ValidatePostOK;
409     writeln(Outfile,'Validate Post OK');
410     Append;
411     FieldByName('PlainText').AsString := 'This is a validated Post';
412     Post;
413     PrintDataSetRow(FIBDataSet2);
414     OnValidatePost := @ValidatePostCancel;
415     writeln(Outfile,'Validate Post Cancel');
416     Append;
417     FieldByName('PlainText').AsString := 'This is a validated Post which should have been cancelled';
418     Post;
419     PrintDataSetRow(FIBDataSet2);
420     OnValidatePost := nil;
421     writeln(OutFile,'FIBDataSet2: Simple Append with Forced Refresh');
422     ForcedRefresh := true;
423     Append;
424     FieldByName('PlainText').AsString := 'This is a test';
425     Post;
426     PrintDataSetRow(FIBDataSet2);
427 tony 410 except on E: Exception do
428     writeln(Outfile,E.Message);
429     end;
430     IBTransaction.Rollback;
431     IBTransaction.Active := true;
432     with FIBDataSet1 do
433     try
434     Unidirectional := false;
435     Active := true;
436     writeln(outfile,'----------------------------------------------');
437     writeln(OutFile,'FIBDataSet1: Insert at start');
438     for i := 1 to 2 do
439     begin
440     Append;
441     FieldByName('PlainText').AsString := 'Row ' + IntToStr(i);
442     Post;
443     end;
444     First;
445     Insert;
446     FieldByName('PlainText').AsString := 'This is an insert test';
447     Post;
448     B := Bookmark;
449     PrintDataSet(FIBDataSet1);
450     writeln(outfile,'Delete inserted row');
451     Bookmark := B;
452     Delete;
453     PrintDataSet(FIBDataSet1);
454     writeln(outfile,'Repeat');
455     First;
456     Insert;
457     FieldByName('PlainText').AsString := 'This is an insert test #1';
458     Post;
459     B := Bookmark;
460     PrintDataSet(FIBDataSet1);
461     writeln(outfile,'Delete inserted row');
462     Bookmark := B;
463     Delete;
464     PrintDataSet(FIBDataSet1);
465     writeln('Insert/Delete after first row');
466     Next;
467     Insert;
468     FieldByName('PlainText').AsString := 'This is an insert test #2';
469     Post;
470     B := Bookmark;
471     PrintDataSet(FIBDataSet1);
472     writeln(outfile,'Delete inserted row');
473     Bookmark := B;
474     Delete;
475     PrintDataSet(FIBDataSet1);
476     writeln('Insert/Delete at last row');
477     Last;
478     Insert;
479     FieldByName('PlainText').AsString := 'This is an insert test #3';
480     Post;
481     B := Bookmark;
482     PrintDataSet(FIBDataSet1);
483     writeln(outfile,'Delete inserted row');
484     Bookmark := B;
485     Delete;
486     PrintDataSet(FIBDataSet1);
487     except on E: Exception do
488     writeln(Outfile,E.Message);
489     end;
490 tony 315
491     finally
492     IBDatabase.DropDatabase;
493     end;
494     end;
495    
496     initialization
497     RegisterTest(TTest17);
498    
499     end.
500    

Properties

Name Value
svn:eol-style native