1 |
(*
|
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 |
unit Test29;
|
28 |
|
29 |
{$mode objfpc}{$H+}
|
30 |
|
31 |
{Test 29: Journalling}
|
32 |
|
33 |
{ Description
|
34 |
}
|
35 |
|
36 |
interface
|
37 |
|
38 |
uses
|
39 |
Classes, SysUtils, TestApplication, IBXTestBase, DB, IB, IBJournal,
|
40 |
IBCustomDataSet;
|
41 |
|
42 |
const
|
43 |
aTestID = '29';
|
44 |
aTestTitle = 'IBJournal Tests';
|
45 |
|
46 |
type
|
47 |
|
48 |
{ TTest29 }
|
49 |
|
50 |
TTest29 = class(TIBXTestBase)
|
51 |
private
|
52 |
FDataSet: TIBDataSet;
|
53 |
FJournal: TIBJournal;
|
54 |
FCreateArrayOnInsert: boolean;
|
55 |
procedure HandleAfterInsert(DataSet: TDataSet);
|
56 |
protected
|
57 |
procedure CreateObjects(Application: TTestApplication); override;
|
58 |
function GetTestID: AnsiString; override;
|
59 |
function GetTestTitle: AnsiString; override;
|
60 |
procedure InitTest; override;
|
61 |
public
|
62 |
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override;
|
63 |
end;
|
64 |
|
65 |
|
66 |
implementation
|
67 |
|
68 |
uses DateUtils;
|
69 |
|
70 |
{ TTest29 }
|
71 |
|
72 |
procedure TTest29.HandleAfterInsert(DataSet: TDataSet);
|
73 |
var S, F: TStream;
|
74 |
Str: TStringList;
|
75 |
i,j: integer;
|
76 |
ar: IArray;
|
77 |
begin
|
78 |
with DataSet do
|
79 |
begin
|
80 |
FieldByName('F1').AsInteger := 2;
|
81 |
FieldByName('f2').AsFloat := 0.314;
|
82 |
FieldByName('f3').AsFloat := 0.31412345678;
|
83 |
FieldByName('F4').AsFloat := 101.314;
|
84 |
FieldByName('F5').AsCurrency := 101.99;
|
85 |
FieldByName('F6').AsDateTime := EncodeDateTime(2007,12,25,12,30,15,0);
|
86 |
FieldByName('F7').AsDateTime := EncodeDateTime(2007,12,25,12,30,29,130);
|
87 |
FieldByName('F8').AsString := 'XX';
|
88 |
FieldByName('F9').AsString := 'The Quick Brown Fox jumps over the lazy dog';
|
89 |
S := CreateBlobStream(FieldByName('F10'),bmWrite);
|
90 |
F := TFileStream.Create('resources/Test29.dat',fmOpenRead);
|
91 |
try
|
92 |
S.CopyFrom(F,0);
|
93 |
finally
|
94 |
S.Free;
|
95 |
F.Free;
|
96 |
end;
|
97 |
FieldByName('F11').AsLargeInt := 9223372036854775807;
|
98 |
FieldByName('F12').AsInteger := 65566;
|
99 |
FieldByName('F13').AsDateTime := EncodeDateTime(2007,12,26,12,30,45,0);
|
100 |
Str := TStringList.Create;
|
101 |
try
|
102 |
Str.LoadFromFile('resources/Test04.txt');
|
103 |
FieldByName('F14').AsString := Str.Text;
|
104 |
finally
|
105 |
Str.Free;
|
106 |
end;
|
107 |
if FCreateArrayOnInsert then
|
108 |
ar := TIBArrayField(FieldByName('MyArray')).CreateArray
|
109 |
else
|
110 |
ar := (DataSet as TIBCustomDataset).GetArray(TIBArrayField(FieldByName('MyArray')));
|
111 |
j := 100;
|
112 |
for i := 0 to 16 do
|
113 |
begin
|
114 |
ar.SetAsInteger([i],j);
|
115 |
dec(j);
|
116 |
end;
|
117 |
TIBArrayField(FieldByName('MyArray')).ArrayIntf := ar;
|
118 |
// WriteArray(TIBArrayField(FieldByName('MyArray')).ArrayIntf);
|
119 |
end;
|
120 |
end;
|
121 |
|
122 |
procedure TTest29.CreateObjects(Application: TTestApplication);
|
123 |
begin
|
124 |
inherited CreateObjects(Application);
|
125 |
FDataSet := TIBDataSet.Create(Application);
|
126 |
with FDataSet do
|
127 |
begin
|
128 |
Database := IBDatabase;
|
129 |
Transaction := IBTransaction;
|
130 |
SelectSQL.Text := 'Select A.TABLEKEY, A.F1, A.F2, A.F3, A.F4, A.F5, A.F6,'+
|
131 |
' A.F7, A.F8, A.F9, A.F10, A.F11, A."f12", A.F13, A.F14, A.MyArray, A.'+
|
132 |
'GRANTS, A."My Field" as MYFIELD1, A."MY Field" as MYFIELD2 From IBXTEST A';
|
133 |
InsertSQL.Text :=
|
134 |
'Insert Into IBXTEST(TABLEKEY, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, "f12", F13, F14, MyArray, '+
|
135 |
' GRANTS) Values(:TABLEKEY, :F1, :F2, :F3, :F4, :F5, :F6, :F7,'+
|
136 |
':F8, :F9, :F10, :F11, :F12, :F13, :F14, :MyArray, :GRANTS) Returning MyArray';
|
137 |
RefreshSQL.Text :=
|
138 |
'Select A.TABLEKEY, A.F1, A.F2, A.F3, A.F4, A.F5, A.F6,' +
|
139 |
' A.F7, A.F8, A.F9, A.F10, A.F11, A."f12", A.F13, A.F14, A.MyArray, A.'+
|
140 |
'GRANTS, A."My Field" as MYFIELD1, A."MY Field" as MYFIELD2 From IBXTEST A '+
|
141 |
'Where A.TABLEKEY = :TABLEKEY';
|
142 |
ModifySQL.Text :=
|
143 |
'Update IBXTEST A Set ' +
|
144 |
' A.F1 = :F1,' +
|
145 |
' A.F2 = :F2,' +
|
146 |
' A.F3 = :F3,' +
|
147 |
' A.F4 = :F4,' +
|
148 |
' A.F5 = :F5,' +
|
149 |
' A.F6 = :F6,' +
|
150 |
' A.F7 = :F7,' +
|
151 |
' A.F8 = :F8,' +
|
152 |
' A.F9 = :F9,' +
|
153 |
' A.F10 = :F10,' +
|
154 |
' A.F11 = :F11,' +
|
155 |
' A."f12" = :F12,' +
|
156 |
' A.F13 = :F13,' +
|
157 |
' A.F14 = :F14,' +
|
158 |
' A.MyArray = :MyArray, ' +
|
159 |
' A."My Field" = :MYFIELD1,'+
|
160 |
' A."MY Field" = :MYFIELD2,'+
|
161 |
' A.GRANTS = :GRANTS '+
|
162 |
'Where A.TABLEKEY = :OLD_TABLEKEY RETURNING A.MyArray';
|
163 |
DeleteSQL.Text :=
|
164 |
'Delete From IBXTEST A '+
|
165 |
'Where A.TABLEKEY = :OLD_TABLEKEY';
|
166 |
DataSetCloseAction := dcSaveChanges;
|
167 |
AutoCommit := acDisabled;
|
168 |
GeneratorField.Generator := 'IBXGEN';
|
169 |
GeneratorField.Field := 'TABLEKEY';
|
170 |
GeneratorField.ApplyOnEvent := gaeOnNewRecord;
|
171 |
AfterInsert := @HandleAfterInsert;
|
172 |
end;
|
173 |
FJournal := TIBJournal.Create(Application);
|
174 |
with FJournal do
|
175 |
begin
|
176 |
Database := IBDatabase;
|
177 |
ApplicationName := 'Test29';
|
178 |
RetainJournal := true;
|
179 |
end;
|
180 |
end;
|
181 |
|
182 |
function TTest29.GetTestID: AnsiString;
|
183 |
begin
|
184 |
Result := aTestID;
|
185 |
end;
|
186 |
|
187 |
function TTest29.GetTestTitle: AnsiString;
|
188 |
begin
|
189 |
Result := aTestTitle;
|
190 |
end;
|
191 |
|
192 |
procedure TTest29.InitTest;
|
193 |
begin
|
194 |
IBDatabase.DatabaseName := Owner.GetNewDatabaseName;
|
195 |
IBDatabase.CreateIfNotExists := true;
|
196 |
ReadWriteTransaction;
|
197 |
end;
|
198 |
|
199 |
procedure TTest29.RunTest(CharSet: AnsiString; SQLDialect: integer);
|
200 |
var OldDefaultFormatSettings: TFormatSettings;
|
201 |
begin
|
202 |
OldDefaultFormatSettings := DefaultFormatSettings;
|
203 |
IBDatabase.CreateDatabase;
|
204 |
try
|
205 |
try
|
206 |
DefaultFormatSettings.LongTimeFormat := 'HH:MM:SS.zzzz';
|
207 |
FJournal.Enabled := true;
|
208 |
IBTransaction.Active := true;
|
209 |
FDataSet.Active := true;
|
210 |
writeln(OutFile,'Add a record');
|
211 |
FDataSet.Append;
|
212 |
FDataSet.Post;
|
213 |
writeln(OutFile,'Add and edit a record');
|
214 |
FCreateArrayOnInsert := true;
|
215 |
FDataSet.Append;
|
216 |
FDataSet.Post;
|
217 |
FDataSet.Edit;
|
218 |
FDataSet.FieldByName('MYField1').AsString := 'My Field';
|
219 |
FDataSet.FieldByName('MYFIELD2').AsString := 'MY Field';
|
220 |
FDataSet.Post;
|
221 |
IBTransaction.Commit;
|
222 |
|
223 |
IBTransaction.Active := true;
|
224 |
FDataSet.Active := true;
|
225 |
PrintDataSet(FDataSet);
|
226 |
writeln(OutFile,'Delete a record');
|
227 |
FDataSet.First;
|
228 |
FDataSet.Delete;
|
229 |
PrintDataSet(FDataSet);
|
230 |
writeln(OutFile,'Rollback Retaining');
|
231 |
IBTransaction.RollbackRetaining;
|
232 |
FDataSet.Active := false;
|
233 |
FDataSet.Active := true;
|
234 |
PrintDataSet(FDataSet);
|
235 |
writeln(OutFile,'Delete a record');
|
236 |
FDataSet.First;
|
237 |
FDataSet.Delete;
|
238 |
PrintDataSet(FDataSet);
|
239 |
writeln(OutFile,'Rollback');
|
240 |
IBTransaction.Rollback;
|
241 |
IBTransaction.Active := true;
|
242 |
FDataSet.Active := true;
|
243 |
PrintDataSet(FDataSet);
|
244 |
writeln(OutFile,'Commit Retaining');
|
245 |
FDataSet.Append;
|
246 |
FDataSet.Post;
|
247 |
IBTransaction.CommitRetaining;
|
248 |
FDataSet.Active := false;
|
249 |
FDataSet.Active := true;
|
250 |
PrintDataSet(FDataSet);
|
251 |
writeln(OutFile,'Commit');
|
252 |
IBTransaction.Commit;
|
253 |
IBTransaction.Active := true;
|
254 |
FDataSet.Active := true;
|
255 |
PrintDataSet(FDataSet);
|
256 |
IBTransaction.Active := false;
|
257 |
PrintJournalTable(IBDatabase.Attachment);
|
258 |
|
259 |
except on E: Exception do
|
260 |
begin
|
261 |
writeln('Terminated with Exception: ' + E.Message);
|
262 |
IBDatabase.ForceClose;
|
263 |
end;
|
264 |
end;
|
265 |
FJournal.Enabled := false;
|
266 |
PrintJournalFile(FJournal.JournalFilePath);
|
267 |
finally
|
268 |
DefaultFormatSettings := OldDefaultFormatSettings;
|
269 |
IBDatabase.DropDatabase;
|
270 |
end;
|
271 |
end;
|
272 |
|
273 |
initialization
|
274 |
RegisterTest(TTest29);
|
275 |
|
276 |
end.
|
277 |
|