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