1 |
unit Test16;
|
2 |
|
3 |
{$mode objfpc}{$H+}
|
4 |
{$codepage UTF8}
|
5 |
|
6 |
{Test 16: Error handling}
|
7 |
|
8 |
{ This test tests for correct responses to various error conditions:
|
9 |
|
10 |
- Malformed database name.
|
11 |
- Invalid User Name
|
12 |
- Invalid password
|
13 |
- Invalid Update SQL Statement
|
14 |
- Invalid Select SQL
|
15 |
- Transaction not started
|
16 |
- Invalid parameter by name when should be positional
|
17 |
- Invalid server name
|
18 |
- invalid user name - logon to server
|
19 |
- invalid password
|
20 |
}
|
21 |
|
22 |
interface
|
23 |
|
24 |
uses
|
25 |
Classes, SysUtils, TestManager, IB;
|
26 |
|
27 |
type
|
28 |
|
29 |
{ TTest16 }
|
30 |
|
31 |
TTest16 = class(TTestBase)
|
32 |
private
|
33 |
procedure DBTests(CharSet: string; SQLDialect: integer);
|
34 |
procedure ServiceTests(CharSet: string; SQLDialect: integer);
|
35 |
public
|
36 |
function TestTitle: string; override;
|
37 |
procedure RunTest(CharSet: string; SQLDialect: integer); override;
|
38 |
end;
|
39 |
|
40 |
|
41 |
implementation
|
42 |
|
43 |
{ TTest16 }
|
44 |
|
45 |
procedure TTest16.DBTests(CharSet: string; SQLDialect: integer);
|
46 |
var DPB: IDPB;
|
47 |
Attachment: IAttachment;
|
48 |
Transaction: ITransaction;
|
49 |
Statement: IStatement;
|
50 |
begin
|
51 |
DPB := FirebirdAPI.AllocateDPB;
|
52 |
DPB.Add(isc_dpb_user_name).setAsString(Owner.GetUserName);
|
53 |
DPB.Add(isc_dpb_password).setAsString(Owner.GetPassword);
|
54 |
DPB.Add(isc_dpb_lc_ctype).setAsString('UTF8');
|
55 |
DPB.Add(isc_dpb_set_db_SQL_dialect).setAsByte(SQLDialect);
|
56 |
try
|
57 |
writeln(OutFile,'Invalid Database Name Test');
|
58 |
Attachment := FirebirdAPI.OpenDatabase('localhost:Malformed Name',DPB);
|
59 |
except on E: Exception do
|
60 |
writeln(OutFile,'Error Handled: ',E.Message);
|
61 |
end;
|
62 |
DPB.Find(isc_dpb_user_name).setAsString('Captain Nemo');
|
63 |
try
|
64 |
writeln(OutFile,'Invalid User Name Test');
|
65 |
Attachment := FirebirdAPI.OpenDatabase(Owner.GetEmployeeDatabaseName,DPB);
|
66 |
except on E: Exception do
|
67 |
writeln(OutFile,'Error Handled: ',E.Message);
|
68 |
end;
|
69 |
DPB.Find(isc_dpb_user_name).setAsString(Owner.GetUserName);
|
70 |
DPB.Find(isc_dpb_password).setAsString('not a pwd');
|
71 |
try
|
72 |
writeln(OutFile,'Invalid password Test');
|
73 |
Attachment := FirebirdAPI.OpenDatabase(Owner.GetEmployeeDatabaseName,DPB);
|
74 |
except on E: Exception do
|
75 |
writeln(OutFile,'Error Handled: ',E.Message);
|
76 |
end;
|
77 |
DPB.Find(isc_dpb_password).setAsString(Owner.GetPassword);
|
78 |
Attachment := FirebirdAPI.OpenDatabase(Owner.GetEmployeeDatabaseName,DPB);
|
79 |
Transaction := Attachment.StartTransaction([isc_tpb_write,isc_tpb_nowait,isc_tpb_concurrency],taRollback);
|
80 |
try
|
81 |
writeln(OutFile,'Invalid Prepare SQL Test');
|
82 |
Statement := Attachment.Prepare(Transaction,'Update Employee Set Unknown_Date = ? Where EMP_NO = ?',3);
|
83 |
except on E: Exception do
|
84 |
writeln(OutFile,'Error Handled: ',E.Message);
|
85 |
end;
|
86 |
try
|
87 |
writeln(OutFile,'Invalid Open Cursor SQL Test');
|
88 |
Attachment.OpenCursorAtStart(Transaction,
|
89 |
'Select X,count(*) As Counter from EMPLOYEE',3);
|
90 |
except on E: Exception do
|
91 |
writeln(OutFile,'Error Handled: ',E.Message);
|
92 |
end;
|
93 |
Transaction.Rollback;
|
94 |
try
|
95 |
writeln(OutFile,'Transaction not started Test');
|
96 |
Attachment.OpenCursorAtStart(Transaction,
|
97 |
'Select count(*) As Counter from EMPLOYEE',3);
|
98 |
except on E: Exception do
|
99 |
writeln(OutFile,'Error Handled: ',E.Message);
|
100 |
end;
|
101 |
Transaction.Start;
|
102 |
try
|
103 |
writeln(OutFile,'Invalid Param SQL Type Test');
|
104 |
Statement := Attachment.Prepare(Transaction,'Update Employee Set Hire_Date = ? Where EMP_NO = ?',3);
|
105 |
Statement.SQLParams.ByName('EMP_NO').AsDate := EncodeDate(2016,11,5);
|
106 |
except on E: Exception do
|
107 |
writeln(OutFile,'Error Handled: ',E.Message);
|
108 |
end;
|
109 |
end;
|
110 |
|
111 |
procedure TTest16.ServiceTests(CharSet: string; SQLDialect: integer);
|
112 |
var SPB: ISPB;
|
113 |
Service: IServiceManager;
|
114 |
I: integer;
|
115 |
ServerName: string;
|
116 |
DBName: string;
|
117 |
begin
|
118 |
if not FirebirdAPI.HasServiceAPI then Exit;
|
119 |
|
120 |
ServerName := Owner.GetEmployeeDatabaseName;
|
121 |
I := Pos(':',ServerName);
|
122 |
if i > 0 then
|
123 |
DBName := system.copy(ServerName,i+1,length(ServerName) - 2);
|
124 |
system.Delete(ServerName,i,Length(ServerName)-i+1);
|
125 |
|
126 |
SPB := FirebirdAPI.AllocateSPB;
|
127 |
SPB.Add(isc_spb_user_name).setAsString(Owner.GetUserName);
|
128 |
SPB.Add(isc_spb_password).setAsString(Owner.GetPassword);
|
129 |
try
|
130 |
writeln(OutFile,'Invalid Server Name Test');
|
131 |
Service := FirebirdAPI.GetServiceManager('unknown',TCP,SPB);
|
132 |
except on E: Exception do
|
133 |
writeln(OutFile,'Error Handled: ',E.Message);
|
134 |
end;
|
135 |
|
136 |
SPB.Find(isc_spb_user_name).setAsString('Captain Nemo');
|
137 |
try
|
138 |
writeln(OutFile,'Invalid User Name Test');
|
139 |
Service := FirebirdAPI.GetServiceManager(ServerName,TCP,SPB);
|
140 |
except on E: Exception do
|
141 |
writeln(OutFile,'Error Handled: ',E.Message);
|
142 |
end;
|
143 |
SPB.Find(isc_spb_user_name).setAsString(Owner.GetUserName);
|
144 |
SPB.Find(isc_spb_password).setAsString('Bad pwd');
|
145 |
try
|
146 |
writeln(OutFile,'Invalid password Test');
|
147 |
Service := FirebirdAPI.GetServiceManager(ServerName,TCP,SPB);
|
148 |
except on E: Exception do
|
149 |
writeln(OutFile,'Error Handled: ',E.Message);
|
150 |
end;
|
151 |
end;
|
152 |
|
153 |
function TTest16.TestTitle: string;
|
154 |
begin
|
155 |
Result := 'Test 16: Error handling';
|
156 |
end;
|
157 |
|
158 |
procedure TTest16.RunTest(CharSet: string; SQLDialect: integer);
|
159 |
begin
|
160 |
DBTests(Charset,SQLDialect);
|
161 |
ServiceTests(Charset,SQLDialect);
|
162 |
end;
|
163 |
|
164 |
initialization
|
165 |
RegisterTest(TTest16);
|
166 |
end.
|
167 |
|