testdbbasics.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. unit TestDBBasics;
  2. {$IFDEF FPC}
  3. {$mode Delphi}{$H+}
  4. {$ENDIF}
  5. interface
  6. uses
  7. fpcunit, testutils, testregistry, testdecorator,
  8. Classes, SysUtils, db;
  9. type
  10. { TTestDBBasics }
  11. TTestDBBasics = class(TTestCase)
  12. private
  13. procedure TestOnFilterProc(DataSet: TDataSet; var Accept: Boolean);
  14. procedure TestfieldDefinition(AFieldType : TFieldType;ADatasize : integer;var ADS : TDataset; var AFld: TField);
  15. procedure TestcalculatedField_OnCalcfields(DataSet: TDataSet);
  16. procedure FTestDelete1(TestCancelUpdate : boolean);
  17. procedure FTestDelete2(TestCancelUpdate : boolean);
  18. procedure TestAddIndexFieldType(AFieldType : TFieldType; ActiveDS : boolean);
  19. protected
  20. procedure SetUp; override;
  21. procedure TearDown; override;
  22. published
  23. procedure TestCancelUpdDelete1;
  24. procedure TestCancelUpdDelete2;
  25. procedure TestBookmarks;
  26. procedure TestFirst;
  27. procedure TestDelete1;
  28. procedure TestDelete2;
  29. procedure TestIntFilter;
  30. procedure TestOnFilter;
  31. procedure TestStringFilter;
  32. procedure TestSetFieldValues;
  33. procedure TestGetFieldValues;
  34. procedure TestAddIndex;
  35. procedure TestAddDescIndex;
  36. procedure TestInactSwitchIndex;
  37. procedure TestAddIndexInteger;
  38. procedure TestAddIndexSmallInt;
  39. procedure TestAddIndexBoolean;
  40. procedure TestAddIndexFloat;
  41. procedure TestAddIndexLargeInt;
  42. procedure TestAddIndexDateTime;
  43. procedure TestAddIndexCurrency;
  44. procedure TestAddIndexBCD;
  45. procedure TestAddIndexActiveDS;
  46. procedure TestAddIndexEditDS;
  47. procedure TestIndexFieldNames;
  48. procedure TestIndexFieldNamesAct;
  49. procedure TestIndexCurRecord;
  50. procedure TestAddDblIndex;
  51. procedure TestIndexEditRecord;
  52. procedure TestNullAtOpen;
  53. procedure TestSupportIntegerFields;
  54. procedure TestSupportSmallIntFields;
  55. procedure TestSupportStringFields;
  56. procedure TestSupportBooleanFields;
  57. procedure TestSupportFloatFields;
  58. procedure TestSupportLargeIntFields;
  59. procedure TestSupportDateFields;
  60. procedure TestSupportCurrencyFields;
  61. procedure TestSupportBCDFields;
  62. procedure TestIsEmpty;
  63. procedure TestAppendOnEmptyDataset;
  64. procedure TestInsertOnEmptyDataset;
  65. procedure TestBufDatasetCancelUpd; //bug 6938
  66. procedure TestEofAfterFirst; //bug 7211
  67. procedure TestBufDatasetCancelUpd1;
  68. procedure TestDoubleClose;
  69. procedure TestCalculatedField;
  70. procedure TestAssignFieldftString;
  71. procedure TestAssignFieldftFixedChar;
  72. procedure TestSelectQueryBasics;
  73. procedure TestPostOnlyInEditState;
  74. procedure TestMove; // bug 5048
  75. procedure TestActiveBufferWhenClosed;
  76. procedure TestEOFBOFClosedDataset;
  77. procedure TestDataEventsResync;
  78. procedure TestBug7007;
  79. procedure TestBug6893;
  80. procedure TestRecordcountAfterReopen; // partly bug 8228
  81. procedure TestdeFieldListChange;
  82. procedure TestLastAppendCancel; // bug 5058
  83. procedure TestRecNo; // bug 5061
  84. procedure TestSetRecNo; // bug 6919
  85. procedure TestRequired;
  86. end;
  87. { TSQLTestSetup }
  88. TDBBasicsTestSetup = class(TTestSetup)
  89. protected
  90. procedure OneTimeSetup; override;
  91. procedure OneTimeTearDown; override;
  92. end;
  93. implementation
  94. uses toolsunit, bufdataset, variants;
  95. type THackDataLink=class(TdataLink);
  96. procedure TTestDBBasics.TestIsEmpty;
  97. begin
  98. if not (DBConnector.GetNDataset(5) is TBufDataset) then
  99. Ignore('This test only applies to TBufDataset and descendents.');
  100. with tbufdataset(DBConnector.GetNDataset(True,1)) do
  101. begin
  102. open;
  103. delete;
  104. refresh;
  105. applyupdates;
  106. AssertTrue(IsEmpty);
  107. end;
  108. end;
  109. procedure TTestDBBasics.TestAppendOnEmptyDataset;
  110. begin
  111. with DBConnector.GetNDataset(0) do
  112. begin
  113. open;
  114. AssertTrue(CanModify);
  115. AssertTrue(eof);
  116. AssertTrue(bof);
  117. append;
  118. AssertFalse(Bof);
  119. AssertTrue(Eof);
  120. post;
  121. AssertFalse(eof);
  122. AssertFalse(bof);
  123. end;
  124. end;
  125. procedure TTestDBBasics.TestInsertOnEmptyDataset;
  126. begin
  127. with DBConnector.GetNDataset(0) do
  128. begin
  129. open;
  130. AssertTrue(CanModify);
  131. AssertTrue(eof);
  132. AssertTrue(bof);
  133. AssertTrue(IsEmpty);
  134. insert;
  135. AssertTrue(Bof);
  136. AssertTrue(Eof);
  137. AssertFalse(IsEmpty);
  138. post;
  139. AssertFalse(IsEmpty);
  140. AssertFalse(eof);
  141. AssertFalse(bof);
  142. end;
  143. end;
  144. procedure TTestDBBasics.TestSelectQueryBasics;
  145. var b : TFieldType;
  146. begin
  147. with DBConnector.GetNDataset(1) do
  148. begin
  149. Open;
  150. AssertEquals(1,RecNo);
  151. AssertEquals(1,RecordCount);
  152. AssertEquals(2,FieldCount);
  153. AssertTrue(CompareText('ID',fields[0].FieldName)=0);
  154. AssertTrue(CompareText('ID',fields[0].DisplayName)=0);
  155. AssertTrue('The datatype of the field ''ID'' is incorrect, it should be ftInteger',ftInteger=fields[0].DataType);
  156. AssertTrue(CompareText('NAME',fields[1].FieldName)=0);
  157. AssertTrue(CompareText('NAME',fields[1].DisplayName)=0);
  158. AssertTrue(ftString=fields[1].DataType);
  159. AssertEquals(1,fields[0].Value);
  160. AssertEquals('TestName1',fields[1].Value);
  161. Close;
  162. end;
  163. end;
  164. procedure TTestDBBasics.TestPostOnlyInEditState;
  165. begin
  166. with DBConnector.GetNDataset(1) do
  167. begin
  168. open;
  169. AssertException('Post was called in a non-edit state',EDatabaseError,Post);
  170. end;
  171. end;
  172. procedure TTestDBBasics.TestMove;
  173. var i,count : integer;
  174. aDatasource : TDataSource;
  175. aDatalink : TDataLink;
  176. ABufferCount : Integer;
  177. begin
  178. aDatasource := TDataSource.Create(nil);
  179. aDatalink := TTestDataLink.Create;
  180. aDatalink.DataSource := aDatasource;
  181. ABufferCount := 11;
  182. aDatalink.BufferCount := ABufferCount;
  183. DataEvents := '';
  184. for count := 0 to 32 do
  185. begin
  186. aDatasource.DataSet := DBConnector.GetNDataset(count);
  187. with aDatasource.Dataset do
  188. begin
  189. i := 1;
  190. Open;
  191. AssertEquals('deUpdateState:0;',DataEvents);
  192. DataEvents := '';
  193. while not EOF do
  194. begin
  195. AssertEquals(i,fields[0].AsInteger);
  196. AssertEquals('TestName'+inttostr(i),fields[1].AsString);
  197. inc(i);
  198. Next;
  199. if (i > ABufferCount) and not EOF then
  200. AssertEquals('deCheckBrowseMode:0;deDataSetScroll:-1;',DataEvents)
  201. else
  202. AssertEquals('deCheckBrowseMode:0;deDataSetScroll:0;',DataEvents);
  203. DataEvents := '';
  204. end;
  205. AssertEquals(count,i-1);
  206. close;
  207. AssertEquals('deUpdateState:0;',DataEvents);
  208. DataEvents := '';
  209. end;
  210. end;
  211. end;
  212. procedure TTestDBBasics.TestdeFieldListChange;
  213. var i,count : integer;
  214. aDatasource : TDataSource;
  215. aDatalink : TDataLink;
  216. ds : TDataset;
  217. begin
  218. aDatasource := TDataSource.Create(nil);
  219. aDatalink := TTestDataLink.Create;
  220. aDatalink.DataSource := aDatasource;
  221. ds := DBConnector.GetNDataset(1);
  222. with ds do
  223. begin
  224. aDatasource.DataSet := ds;
  225. DataEvents := '';
  226. open;
  227. Fields.add(tfield.Create(DBConnector.GetNDataset(1)));
  228. AssertEquals('deUpdateState:0;deFieldListChange:0;',DataEvents);
  229. DataEvents := '';
  230. fields.Clear;
  231. AssertEquals('deFieldListChange:0;',DataEvents)
  232. end;
  233. aDatasource.Free;
  234. aDatalink.Free;
  235. end;
  236. procedure TTestDBBasics.TestActiveBufferWhenClosed;
  237. begin
  238. with DBConnector.GetNDataset(0) do
  239. begin
  240. AssertNull(ActiveBuffer);
  241. open;
  242. AssertFalse('Activebuffer of an empty dataset shouldn''t be nil',ActiveBuffer = nil);
  243. end;
  244. end;
  245. procedure TTestDBBasics.TestEOFBOFClosedDataset;
  246. begin
  247. with DBConnector.GetNDataset(1) do
  248. begin
  249. AssertTrue(EOF);
  250. AssertTrue(BOF);
  251. open;
  252. close;
  253. AssertTrue(EOF);
  254. AssertTrue(BOF);
  255. end;
  256. end;
  257. procedure TTestDBBasics.TestDataEventsResync;
  258. var i,count : integer;
  259. aDatasource : TDataSource;
  260. aDatalink : TDataLink;
  261. ds : tdataset;
  262. begin
  263. aDatasource := TDataSource.Create(nil);
  264. aDatalink := TTestDataLink.Create;
  265. aDatalink.DataSource := aDatasource;
  266. ds := DBConnector.GetNDataset(6);
  267. ds.BeforeScroll := DBConnector.DataEvent;
  268. with ds do
  269. begin
  270. aDatasource.DataSet := ds;
  271. open;
  272. DataEvents := '';
  273. Resync([rmExact]);
  274. AssertEquals('deDataSetChange:0;',DataEvents);
  275. DataEvents := '';
  276. next;
  277. AssertEquals('deCheckBrowseMode:0;DataEvent;deDataSetScroll:0;',DataEvents);
  278. close;
  279. end;
  280. aDatasource.Free;
  281. aDatalink.Free;
  282. end;
  283. procedure TTestDBBasics.TestLastAppendCancel;
  284. var count : integer;
  285. begin
  286. for count := 0 to 32 do with DBConnector.GetNDataset(count) do
  287. begin
  288. open;
  289. Last;
  290. Append;
  291. Cancel;
  292. AssertEquals(count,fields[0].asinteger);
  293. AssertEquals(count,RecordCount);
  294. Close;
  295. end;
  296. end;
  297. procedure TTestDBBasics.TestRecNo;
  298. var i : longint;
  299. passed : boolean;
  300. begin
  301. with DBConnector.GetNDataset(0) do
  302. begin
  303. // Accessing RecNo on a closed dataset should raise an EDatabaseError or should
  304. // return 0
  305. passed := false;
  306. try
  307. i := recno;
  308. except on E: Exception do
  309. begin
  310. passed := E.classname = EDatabaseError.className
  311. end;
  312. end;
  313. if not passed then
  314. AssertEquals('Failed to get the RecNo from a closed dataset',0,RecNo);
  315. // Accessing Recordcount on a closed dataset should raise an EDatabaseError or should
  316. // return 0
  317. passed := false;
  318. try
  319. i := recordcount;
  320. except on E: Exception do
  321. begin
  322. passed := E.classname = EDatabaseError.className
  323. end;
  324. end;
  325. if not passed then
  326. AssertEquals('Failed to get the Recordcount from a closed dataset',0,RecNo);
  327. Open;
  328. AssertEquals(0,RecordCount);
  329. AssertEquals(0,RecNo);
  330. first;
  331. AssertEquals(0,RecordCount);
  332. AssertEquals(0,RecNo);
  333. last;
  334. AssertEquals(0,RecordCount);
  335. AssertEquals(0,RecNo);
  336. append;
  337. AssertEquals(0,RecNo);
  338. AssertEquals(0,RecordCount);
  339. first;
  340. AssertEquals(0,RecNo);
  341. AssertEquals(0,RecordCount);
  342. append;
  343. FieldByName('id').AsInteger := 1;
  344. AssertEquals(0,RecNo);
  345. AssertEquals(0,RecordCount);
  346. first;
  347. AssertEquals(1,RecNo);
  348. AssertEquals(1,RecordCount);
  349. last;
  350. AssertEquals(1,RecNo);
  351. AssertEquals(1,RecordCount);
  352. append;
  353. FieldByName('id').AsInteger := 1;
  354. AssertEquals(0,RecNo);
  355. AssertEquals(1,RecordCount);
  356. Close;
  357. end;
  358. end;
  359. procedure TTestDBBasics.TestSetRecNo;
  360. begin
  361. with DBConnector.GetNDataset(15) do
  362. begin
  363. Open;
  364. RecNo := 1;
  365. AssertEquals(1,fields[0].AsInteger);
  366. AssertEquals(1,RecNo);
  367. RecNo := 2;
  368. AssertEquals(2,fields[0].AsInteger);
  369. AssertEquals(2,RecNo);
  370. RecNo := 8;
  371. AssertEquals(8,fields[0].AsInteger);
  372. AssertEquals(8,RecNo);
  373. RecNo := 15;
  374. AssertEquals(15,fields[0].AsInteger);
  375. AssertEquals(15,RecNo);
  376. RecNo := 3;
  377. AssertEquals(3,fields[0].AsInteger);
  378. AssertEquals(3,RecNo);
  379. RecNo := 14;
  380. AssertEquals(14,fields[0].AsInteger);
  381. AssertEquals(14,RecNo);
  382. RecNo := 15;
  383. AssertEquals(15,fields[0].AsInteger);
  384. AssertEquals(15,RecNo);
  385. // test for exceptions...
  386. { RecNo := 16;
  387. AssertEquals(15,fields[0].AsInteger);
  388. AssertEquals(15,RecNo);}
  389. Close;
  390. end;
  391. end;
  392. procedure TTestDBBasics.TestRequired;
  393. begin
  394. with DBConnector.GetNDataset(2) do
  395. begin
  396. Open;
  397. FieldByName('ID').Required := True;
  398. Append;
  399. AssertException(EDatabaseError,Post);
  400. FieldByName('ID').AsInteger := 1000;
  401. Post;
  402. Close;
  403. end;
  404. end;
  405. procedure TTestDBBasics.SetUp;
  406. begin
  407. DBConnector.StartTest;
  408. end;
  409. procedure TTestDBBasics.TearDown;
  410. begin
  411. DBConnector.StopTest;
  412. end;
  413. procedure TTestDBBasics.TestBookmarks;
  414. var BM1,BM2,BM3,BM4,BM5 : TBookmark;
  415. begin
  416. with DBConnector.GetNDataset(true,14) do
  417. begin
  418. AssertNull(GetBookmark);
  419. open;
  420. BM1:=GetBookmark; // id=1, BOF
  421. next;next;
  422. BM2:=GetBookmark; // id=3
  423. next;next;next;
  424. BM3:=GetBookmark; // id=6
  425. next;next;next;next;next;next;next;next;
  426. BM4:=GetBookmark; // id=14
  427. next;
  428. BM5:=GetBookmark; // id=14, EOF
  429. GotoBookmark(BM2);
  430. AssertEquals(3,FieldByName('id').AsInteger);
  431. GotoBookmark(BM1);
  432. AssertEquals(1,FieldByName('id').AsInteger);
  433. GotoBookmark(BM3);
  434. AssertEquals(6,FieldByName('id').AsInteger);
  435. GotoBookmark(BM4);
  436. AssertEquals(14,FieldByName('id').AsInteger);
  437. GotoBookmark(BM3);
  438. AssertEquals(6,FieldByName('id').AsInteger);
  439. GotoBookmark(BM5);
  440. AssertEquals(14,FieldByName('id').AsInteger);
  441. GotoBookmark(BM1);
  442. AssertEquals(1,FieldByName('id').AsInteger);
  443. next;
  444. delete;
  445. GotoBookmark(BM2);
  446. AssertEquals(3,FieldByName('id').AsInteger);
  447. delete;delete;
  448. GotoBookmark(BM3);
  449. AssertEquals(6,FieldByName('id').AsInteger);
  450. GotoBookmark(BM1);
  451. AssertEquals(1,FieldByName('id').AsInteger);
  452. insert;
  453. fieldbyname('id').AsInteger:=20;
  454. insert;
  455. fieldbyname('id').AsInteger:=21;
  456. insert;
  457. fieldbyname('id').AsInteger:=22;
  458. insert;
  459. fieldbyname('id').AsInteger:=23;
  460. post;
  461. GotoBookmark(BM3);
  462. AssertEquals(6,FieldByName('id').AsInteger);
  463. GotoBookmark(BM1);
  464. AssertEquals(1,FieldByName('id').AsInteger);
  465. GotoBookmark(BM5);
  466. AssertEquals(14,FieldByName('id').AsInteger);
  467. end;
  468. end;
  469. procedure TTestDBBasics.TestSetFieldValues;
  470. var PassException : boolean;
  471. begin
  472. with DBConnector.GetNDataset(true,11) do
  473. begin
  474. open;
  475. first;
  476. edit;
  477. FieldValues['id']:=5;
  478. post;
  479. AssertEquals('TestName1',FieldByName('name').AsString);
  480. AssertEquals(5,FieldByName('id').AsInteger);
  481. edit;
  482. FieldValues['name']:='FieldValuesTestName';
  483. post;
  484. AssertEquals('FieldValuesTestName',FieldByName('name').AsString);
  485. AssertEquals(5,FieldByName('id').AsInteger);
  486. edit;
  487. FieldValues['id;name']:= VarArrayOf([243,'ValuesTestName']);
  488. post;
  489. AssertEquals('ValuesTestName',FieldByName('name').AsString);
  490. AssertEquals(243,FieldByName('id').AsInteger);
  491. PassException:=false;
  492. try
  493. edit;
  494. FieldValues['id;name;fake']:= VarArrayOf([243,'ValuesTestName',4]);
  495. except
  496. on E: EDatabaseError do PassException := True;
  497. end;
  498. post;
  499. AssertTrue(PassException);
  500. end;
  501. end;
  502. procedure TTestDBBasics.TestGetFieldValues;
  503. var AVar : Variant;
  504. PassException : boolean;
  505. begin
  506. with DBConnector.GetNDataset(true,14) do
  507. begin
  508. open;
  509. AVar:=FieldValues['id'];
  510. AssertEquals(AVar,1);
  511. AVar:=FieldValues['name'];
  512. AssertEquals(AVar,'TestName1');
  513. AVar:=FieldValues['id;name'];
  514. AssertEquals(AVar[0],1);
  515. AssertEquals(AVar[1],'TestName1');
  516. AVar:=FieldValues['name;id;'];
  517. AssertEquals(AVar[1],1);
  518. AssertEquals(AVar[0],'TestName1');
  519. PassException:=false;
  520. try
  521. AVar:=FieldValues['name;id;fake'];
  522. except
  523. on E: EDatabaseError do PassException := True;
  524. end;
  525. AssertTrue(PassException);
  526. end;
  527. end;
  528. procedure TTestDBBasics.TestFirst;
  529. var i : integer;
  530. begin
  531. with DBConnector.GetNDataset(true,14) do
  532. begin
  533. open;
  534. AssertEquals(1,FieldByName('ID').AsInteger);
  535. First;
  536. AssertEquals(1,FieldByName('ID').AsInteger);
  537. next;
  538. AssertEquals(2,FieldByName('ID').AsInteger);
  539. First;
  540. AssertEquals(1,FieldByName('ID').AsInteger);
  541. for i := 0 to 12 do
  542. next;
  543. AssertEquals(14,FieldByName('ID').AsInteger);
  544. First;
  545. AssertEquals(1,FieldByName('ID').AsInteger);
  546. close;
  547. end;
  548. end;
  549. procedure TTestDBBasics.TestDelete1;
  550. begin
  551. FTestDelete1(false);
  552. end;
  553. procedure TTestDBBasics.TestDelete2;
  554. begin
  555. FTestDelete2(false);
  556. end;
  557. procedure TTestDBBasics.TestCancelUpdDelete1;
  558. begin
  559. FTestDelete1(true);
  560. end;
  561. procedure TTestDBBasics.TestCancelUpdDelete2;
  562. begin
  563. FTestDelete2(true);
  564. end;
  565. procedure TTestDBBasics.FTestDelete1(TestCancelUpdate : boolean);
  566. // Test the deletion of records, including the first and the last one
  567. var i : integer;
  568. ds : TDataset;
  569. begin
  570. ds := DBConnector.GetNDataset(true,17);
  571. with ds do
  572. begin
  573. Open;
  574. for i := 0 to 16 do if i mod 4=0 then
  575. delete
  576. else
  577. next;
  578. First;
  579. for i := 0 to 16 do
  580. begin
  581. if i mod 4<>0 then
  582. begin
  583. AssertEquals(i+1,FieldByName('ID').AsInteger);
  584. AssertEquals('TestName'+inttostr(i+1),FieldByName('NAME').AsString);
  585. next;
  586. end;
  587. end;
  588. end;
  589. if TestCancelUpdate then
  590. begin
  591. if not (ds is TBufDataset) then
  592. Ignore('This test only applies to TBufDataset and descendents.');
  593. with TBufDataset(ds) do
  594. begin
  595. CancelUpdates;
  596. First;
  597. for i := 0 to 16 do
  598. begin
  599. AssertEquals(i+1,FieldByName('ID').AsInteger);
  600. AssertEquals('TestName'+inttostr(i+1),FieldByName('NAME').AsString);
  601. next;
  602. end;
  603. close;
  604. end;
  605. end;
  606. end;
  607. procedure TTestDBBasics.FTestDelete2(TestCancelUpdate : boolean);
  608. // Test the deletion of edited and appended records
  609. var i : integer;
  610. ds : TDataset;
  611. begin
  612. ds := DBConnector.GetNDataset(true,17);
  613. with ds do
  614. begin
  615. Open;
  616. for i := 0 to 16 do
  617. begin
  618. if i mod 4=0 then
  619. begin
  620. edit;
  621. fieldbyname('name').AsString:='this record will be gone soon';
  622. post;
  623. end;
  624. next;
  625. end;
  626. for i := 17 to 20 do
  627. begin
  628. append;
  629. fieldbyname('id').AsInteger:=i+1;
  630. fieldbyname('name').AsString:='TestName'+inttostr(i+1);
  631. post;
  632. end;
  633. first;
  634. for i := 0 to 20 do if i mod 4=0 then
  635. delete
  636. else
  637. next;
  638. First;
  639. i := 0;
  640. for i := 0 to 20 do
  641. begin
  642. if i mod 4<>0 then
  643. begin
  644. AssertEquals(i+1,FieldByName('ID').AsInteger);
  645. AssertEquals('TestName'+inttostr(i+1),FieldByName('NAME').AsString);
  646. next;
  647. end;
  648. end;
  649. end;
  650. if TestCancelUpdate then
  651. begin
  652. if not (ds is TBufDataset) then
  653. Ignore('This test only applies to TBufDataset and descendents.');
  654. with TBufDataset(ds) do
  655. begin
  656. CancelUpdates;
  657. First;
  658. for i := 0 to 16 do
  659. begin
  660. AssertEquals(i+1,FieldByName('ID').AsInteger);
  661. AssertEquals('TestName'+inttostr(i+1),FieldByName('NAME').AsString);
  662. next;
  663. end;
  664. close;
  665. end;
  666. end;
  667. end;
  668. procedure TTestDBBasics.TestOnFilterProc(DataSet: TDataSet; var Accept: Boolean);
  669. var a : TDataSetState;
  670. begin
  671. Accept := odd(Dataset.FieldByName('ID').AsInteger);
  672. end;
  673. procedure TTestDBBasics.TestOnFilter;
  674. var tel : byte;
  675. begin
  676. with DBConnector.GetNDataset(15) do
  677. begin
  678. OnFilterRecord := TestOnFilterProc;
  679. Filtered := True;
  680. Open;
  681. for tel := 1 to 8 do
  682. begin
  683. AssertTrue(odd(FieldByName('ID').asinteger));
  684. next;
  685. end;
  686. AssertTrue(EOF);
  687. end;
  688. end;
  689. procedure TTestDBBasics.TestIntFilter;
  690. var tel : byte;
  691. begin
  692. with DBConnector.GetNDataset(15) do
  693. begin
  694. Filtered := True;
  695. Filter := '(id>4) and (id<9)';
  696. Open;
  697. for tel := 5 to 8 do
  698. begin
  699. AssertEquals(tel,FieldByName('ID').asinteger);
  700. next;
  701. end;
  702. AssertTrue(EOF);
  703. Close;
  704. end;
  705. end;
  706. procedure TTestDBBasics.TestRecordcountAfterReopen;
  707. var
  708. datalink1: tdatalink;
  709. datasource1: tdatasource;
  710. query1: TDataSet;
  711. begin
  712. query1:= DBConnector.GetNDataset(11);
  713. datalink1:= TDataLink.create;
  714. datasource1:= tdatasource.create(nil);
  715. try
  716. datalink1.datasource:= datasource1;
  717. datasource1.dataset:= query1;
  718. query1.active := true;
  719. query1.active := False;
  720. AssertEquals(0, THackDataLink(datalink1).RecordCount);
  721. query1.active := true;
  722. AssertTrue(THackDataLink(datalink1).RecordCount>0);
  723. query1.active := false;
  724. finally
  725. datalink1.free;
  726. datasource1.free;
  727. end;
  728. end;
  729. procedure TTestDBBasics.TestStringFilter;
  730. var tel : byte;
  731. begin
  732. with DBConnector.GetNDataset(15) do
  733. begin
  734. Open;
  735. //FilterOptions := [foNoPartialCompare];
  736. //FilterOptions := [];
  737. Filter := '(name=''*Name3'')';
  738. Filtered := True;
  739. AssertFalse(EOF);
  740. AssertEquals(3,FieldByName('ID').asinteger);
  741. AssertEquals('TestName3',FieldByName('NAME').asstring);
  742. next;
  743. AssertTrue(EOF);
  744. Close;
  745. end;
  746. end;
  747. procedure TTestDBBasics.TestAddIndexFieldType(AFieldType: TFieldType; ActiveDS : boolean);
  748. var ds : TBufDataset;
  749. FList : TStringList;
  750. LastValue : Variant;
  751. begin
  752. ds := DBConnector.GetFieldDataset as TBufDataset;
  753. with ds do
  754. begin
  755. if not ActiveDS then
  756. begin
  757. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  758. IndexName:='testindex';
  759. end
  760. else
  761. MaxIndexesCount := 3;
  762. try
  763. open;
  764. except
  765. if not assigned(ds.FindField('F'+FieldTypeNames[AfieldType])) then
  766. Ignore('Fields of the type ' + FieldTypeNames[AfieldType] + ' are not supported by this type of dataset')
  767. else
  768. raise;
  769. end;
  770. if ActiveDS then
  771. begin
  772. if not assigned(ds.FindField('F'+FieldTypeNames[AfieldType])) then
  773. Ignore('Fields of the type ' + FieldTypeNames[AfieldType] + ' are not supported by this type of dataset');
  774. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  775. IndexName:='testindex';
  776. First;
  777. end;
  778. LastValue:=null;
  779. while not eof do
  780. begin
  781. AssertTrue(LastValue<=FieldByName('F'+FieldTypeNames[AfieldType]).AsVariant);
  782. LastValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsVariant;
  783. Next;
  784. end;
  785. while not bof do
  786. begin
  787. AssertTrue(LastValue>=FieldByName('F'+FieldTypeNames[AfieldType]).AsVariant);
  788. LastValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsVariant;
  789. Prior;
  790. end;
  791. end;
  792. end;
  793. procedure TTestDBBasics.TestAddIndexSmallInt;
  794. begin
  795. TestAddIndexFieldType(ftSmallint,False);
  796. end;
  797. procedure TTestDBBasics.TestAddIndexBoolean;
  798. begin
  799. TestAddIndexFieldType(ftBoolean,False);
  800. end;
  801. procedure TTestDBBasics.TestAddIndexFloat;
  802. begin
  803. TestAddIndexFieldType(ftFloat,False);
  804. end;
  805. procedure TTestDBBasics.TestAddIndexInteger;
  806. begin
  807. TestAddIndexFieldType(ftInteger,False);
  808. end;
  809. procedure TTestDBBasics.TestAddIndexLargeInt;
  810. begin
  811. TestAddIndexFieldType(ftLargeint,False);
  812. end;
  813. procedure TTestDBBasics.TestAddIndexDateTime;
  814. begin
  815. TestAddIndexFieldType(ftDateTime,False);
  816. end;
  817. procedure TTestDBBasics.TestAddIndexCurrency;
  818. begin
  819. TestAddIndexFieldType(ftCurrency,False);
  820. end;
  821. procedure TTestDBBasics.TestAddIndexBCD;
  822. begin
  823. TestAddIndexFieldType(ftBCD,False);
  824. end;
  825. procedure TTestDBBasics.TestAddIndex;
  826. var ds : TBufDataset;
  827. AFieldType : TFieldType;
  828. FList : TStringList;
  829. i : integer;
  830. begin
  831. ds := DBConnector.GetFieldDataset as TBufDataset;
  832. with ds do
  833. begin
  834. AFieldType:=ftString;
  835. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  836. FList := TStringList.Create;
  837. FList.Sorted:=true;
  838. FList.CaseSensitive:=True;
  839. FList.Duplicates:=dupAccept;
  840. open;
  841. while not eof do
  842. begin
  843. flist.Add(FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  844. Next;
  845. end;
  846. IndexName:='testindex';
  847. first;
  848. i:=0;
  849. while not eof do
  850. begin
  851. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  852. inc(i);
  853. Next;
  854. end;
  855. while not bof do
  856. begin
  857. dec(i);
  858. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  859. Prior;
  860. end;
  861. end;
  862. end;
  863. procedure TTestDBBasics.TestAddDescIndex;
  864. var ds : TBufDataset;
  865. AFieldType : TFieldType;
  866. FList : TStringList;
  867. i : integer;
  868. begin
  869. ds := DBConnector.GetFieldDataset as TBufDataset;
  870. with ds do
  871. begin
  872. AFieldType:=ftString;
  873. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[],'F'+FieldTypeNames[AfieldType]);
  874. FList := TStringList.Create;
  875. FList.Sorted:=true;
  876. FList.CaseSensitive:=True;
  877. FList.Duplicates:=dupAccept;
  878. open;
  879. while not eof do
  880. begin
  881. flist.Add(FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  882. Next;
  883. end;
  884. IndexName:='testindex';
  885. first;
  886. i:=FList.Count-1;
  887. while not eof do
  888. begin
  889. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  890. dec(i);
  891. Next;
  892. end;
  893. while not bof do
  894. begin
  895. inc(i);
  896. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  897. Prior;
  898. end;
  899. end;
  900. end;
  901. procedure TTestDBBasics.TestInactSwitchIndex;
  902. // Test if the default-index is properly build when the active index is not
  903. // the default-index while opening then dataset
  904. var ds : TBufDataset;
  905. AFieldType : TFieldType;
  906. i : integer;
  907. begin
  908. ds := DBConnector.GetFieldDataset as TBufDataset;
  909. with ds do
  910. begin
  911. AFieldType:=ftString;
  912. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  913. IndexName:='testindex';
  914. open;
  915. IndexName:=''; // This should set the default index (default_order)
  916. first;
  917. i := 0;
  918. while not eof do
  919. begin
  920. AssertEquals(testStringValues[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  921. inc(i);
  922. Next;
  923. end;
  924. end;
  925. end;
  926. procedure TTestDBBasics.TestAddIndexActiveDS;
  927. var ds : TBufDataset;
  928. I : integer;
  929. begin
  930. TestAddIndexFieldType(ftString,true);
  931. end;
  932. procedure TTestDBBasics.TestAddIndexEditDS;
  933. var ds : TBufDataset;
  934. I : integer;
  935. LastValue : String;
  936. begin
  937. ds := DBConnector.GetNDataset(True,5) as TBufDataset;
  938. with ds do
  939. begin
  940. MaxIndexesCount:=3;
  941. open;
  942. edit;
  943. FieldByName('name').asstring := 'Zz';
  944. post;
  945. next;
  946. next;
  947. edit;
  948. FieldByName('name').asstring := 'aA';
  949. post;
  950. AddIndex('test','name',[]);
  951. first;
  952. ds.IndexName:='test';
  953. first;
  954. LastValue:=FieldByName('name').AsString;
  955. while not eof do
  956. begin
  957. AssertTrue(LastValue<=FieldByName('name').AsString);
  958. Next;
  959. end;
  960. end;
  961. end;
  962. procedure TTestDBBasics.TestIndexFieldNamesAct;
  963. var ds : TBufDataset;
  964. AFieldType : TFieldType;
  965. FList : TStringList;
  966. i : integer;
  967. begin
  968. ds := DBConnector.GetFieldDataset as TBufDataset;
  969. with ds do
  970. begin
  971. AFieldType:=ftString;
  972. FList := TStringList.Create;
  973. FList.Sorted:=true;
  974. FList.CaseSensitive:=True;
  975. FList.Duplicates:=dupAccept;
  976. open;
  977. while not eof do
  978. begin
  979. flist.Add(FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  980. Next;
  981. end;
  982. IndexFieldNames:='F'+FieldTypeNames[AfieldType];
  983. first;
  984. i:=0;
  985. while not eof do
  986. begin
  987. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  988. inc(i);
  989. Next;
  990. end;
  991. while not bof do
  992. begin
  993. dec(i);
  994. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  995. Prior;
  996. end;
  997. AssertEquals('F'+FieldTypeNames[AfieldType],IndexFieldNames);
  998. IndexFieldNames:='ID';
  999. first;
  1000. i:=0;
  1001. while not eof do
  1002. begin
  1003. AssertEquals(testStringValues[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1004. inc(i);
  1005. Next;
  1006. end;
  1007. AssertEquals('ID',IndexFieldNames);
  1008. IndexFieldNames:='';
  1009. first;
  1010. i:=0;
  1011. while not eof do
  1012. begin
  1013. AssertEquals(testStringValues[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1014. inc(i);
  1015. Next;
  1016. end;
  1017. AssertEquals('',IndexFieldNames);
  1018. end;
  1019. end;
  1020. procedure TTestDBBasics.TestIndexCurRecord;
  1021. // Test if the currentrecord stays the same after an index change
  1022. var ds : TBufDataset;
  1023. AFieldType : TFieldType;
  1024. i : integer;
  1025. OldID : Integer;
  1026. OldStringValue : string;
  1027. begin
  1028. ds := DBConnector.GetFieldDataset as TBufDataset;
  1029. with ds do
  1030. begin
  1031. AFieldType:=ftString;
  1032. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  1033. open;
  1034. for i := 0 to (testValuesCount div 3) do
  1035. Next;
  1036. OldID:=FieldByName('id').AsInteger;
  1037. OldStringValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsString;
  1038. IndexName:='testindex';
  1039. AssertEquals(OldID,FieldByName('id').AsInteger);
  1040. AssertEquals(OldStringValue,FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1041. next;
  1042. AssertTrue(OldStringValue<=FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1043. prior;
  1044. prior;
  1045. AssertTrue(OldStringValue>=FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1046. OldID:=FieldByName('id').AsInteger;
  1047. OldStringValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsString;
  1048. IndexName:='';
  1049. AssertEquals(OldID,FieldByName('id').AsInteger);
  1050. AssertEquals(OldStringValue,FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1051. next;
  1052. AssertEquals(OldID+1,FieldByName('ID').AsInteger);
  1053. prior;
  1054. prior;
  1055. AssertEquals(OldID-1,FieldByName('ID').AsInteger);
  1056. end;
  1057. end;
  1058. procedure TTestDBBasics.TestAddDblIndex;
  1059. var ds : TBufDataset;
  1060. FList : TStringList;
  1061. i : integer;
  1062. begin
  1063. ds := DBConnector.GetFieldDataset as TBufDataset;
  1064. with ds do
  1065. begin
  1066. AddIndex('testindex','F'+FieldTypeNames[ftString]+', F'+FieldTypeNames[ftInteger],[]);
  1067. FList := TStringList.Create;
  1068. FList.Sorted:=true;
  1069. FList.CaseSensitive:=True;
  1070. FList.Duplicates:=dupAccept;
  1071. open;
  1072. while not eof do
  1073. begin
  1074. // If the first field of the index is null then the compound string in
  1075. // FList isn't sorted right...
  1076. if FieldByName('F'+FieldTypeNames[ftString]).IsNull then
  1077. flist.Add(' -'+ Format('%.12d',[FieldByName('F'+FieldTypeNames[ftInteger]).AsInteger]))
  1078. else
  1079. flist.Add(FieldByName('F'+FieldTypeNames[ftString]).AsString+'-'+ Format('%.12d',[FieldByName('F'+FieldTypeNames[ftInteger]).AsInteger]));
  1080. Next;
  1081. end;
  1082. IndexName:='testindex';
  1083. first;
  1084. i:=0;
  1085. while not eof do
  1086. begin
  1087. if (not FieldByName('F'+FieldTypeNames[ftString]).IsNull) then
  1088. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[ftString]).AsString+'-'+ Format('%.12d',[FieldByName('F'+FieldTypeNames[ftInteger]).AsInteger]));
  1089. inc(i);
  1090. Next;
  1091. end;
  1092. while not bof do
  1093. begin
  1094. dec(i);
  1095. if not FieldByName('F'+FieldTypeNames[ftString]).IsNull then
  1096. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[ftString]).AsString+'-'+ Format('%.12d',[FieldByName('F'+FieldTypeNames[ftInteger]).AsInteger]));
  1097. Prior;
  1098. end;
  1099. end;
  1100. end;
  1101. procedure TTestDBBasics.TestIndexEditRecord;
  1102. var ds : TBufDataset;
  1103. AFieldType : TFieldType;
  1104. i : integer;
  1105. OldID : Integer;
  1106. OldStringValue : string;
  1107. begin
  1108. ds := DBConnector.GetFieldDataset as TBufDataset;
  1109. with ds do
  1110. begin
  1111. AFieldType:=ftString;
  1112. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  1113. IndexName:='testindex';
  1114. open;
  1115. OldStringValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsString;
  1116. next;
  1117. AssertTrue(OldStringValue<=FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1118. OldStringValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsString;
  1119. next;
  1120. AssertTrue(OldStringValue<=FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1121. prior;
  1122. edit;
  1123. FieldByName('F'+FieldTypeNames[AfieldType]).AsString := 'ZZZ';
  1124. post;
  1125. prior;
  1126. AssertTrue('ZZZ'>=FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1127. next;
  1128. next;
  1129. AssertTrue('ZZZ'<=FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1130. close;
  1131. end;
  1132. end;
  1133. procedure TTestDBBasics.TestIndexFieldNames;
  1134. var ds : TBufDataset;
  1135. AFieldType : TFieldType;
  1136. PrevValue : String;
  1137. begin
  1138. ds := DBConnector.GetFieldDataset as TBufDataset;
  1139. with ds do
  1140. begin
  1141. AFieldType:=ftString;
  1142. IndexFieldNames:='F'+FieldTypeNames[AfieldType];
  1143. open;
  1144. PrevValue:='';
  1145. while not eof do
  1146. begin
  1147. AssertTrue(FieldByName('F'+FieldTypeNames[AfieldType]).AsString>=PrevValue);
  1148. PrevValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsString;
  1149. Next;
  1150. end;
  1151. AssertEquals('F'+FieldTypeNames[AfieldType],IndexFieldNames);
  1152. end;
  1153. end;
  1154. procedure TTestDBBasics.TestcalculatedField_OnCalcfields(DataSet: TDataSet);
  1155. begin
  1156. case dataset.fieldbyname('ID').asinteger of
  1157. 1 : dataset.fieldbyname('CALCFLD').AsInteger := 5;
  1158. 2 : dataset.fieldbyname('CALCFLD').AsInteger := 70000;
  1159. 3 : dataset.fieldbyname('CALCFLD').Clear;
  1160. 4 : dataset.fieldbyname('CALCFLD').AsInteger := 1234;
  1161. 10 : dataset.fieldbyname('CALCFLD').Clear;
  1162. else
  1163. dataset.fieldbyname('CALCFLD').AsInteger := 1;
  1164. end;
  1165. end;
  1166. procedure TTestDBBasics.TestCalculatedField;
  1167. var ds : TDataset;
  1168. AFld1, AFld2, AFld3 : Tfield;
  1169. begin
  1170. ds := DBConnector.GetNDataset(5);
  1171. with ds do
  1172. begin
  1173. AFld1 := TIntegerField.Create(ds);
  1174. AFld1.FieldName := 'ID';
  1175. AFld1.DataSet := ds;
  1176. AFld2 := TStringField.Create(ds);
  1177. AFld2.FieldName := 'NAME';
  1178. AFld2.DataSet := ds;
  1179. AFld3 := TIntegerField.Create(ds);
  1180. AFld3.FieldName := 'CALCFLD';
  1181. AFld3.DataSet := ds;
  1182. Afld3.FieldKind := fkCalculated;
  1183. AssertEquals(3,FieldCount);
  1184. ds.OnCalcFields := TestcalculatedField_OnCalcfields;
  1185. open;
  1186. AssertEquals(1,FieldByName('ID').asinteger);
  1187. AssertEquals(5,FieldByName('CALCFLD').asinteger);
  1188. next;
  1189. AssertEquals(70000,FieldByName('CALCFLD').asinteger);
  1190. next;
  1191. AssertEquals(true,FieldByName('CALCFLD').isnull);
  1192. next;
  1193. AssertEquals(1234,FieldByName('CALCFLD').AsInteger);
  1194. edit;
  1195. FieldByName('ID').AsInteger := 10;
  1196. post;
  1197. AssertEquals(true,FieldByName('CALCFLD').isnull);
  1198. close;
  1199. AFld1.Free;
  1200. AFld2.Free;
  1201. AFld3.Free;
  1202. end;
  1203. end;
  1204. procedure TTestDBBasics.TestEofAfterFirst;
  1205. begin
  1206. with DBConnector.GetNDataset(0) do
  1207. begin
  1208. open;
  1209. AssertTrue(eof);
  1210. AssertTrue(BOF);
  1211. first;
  1212. AssertTrue(eof);
  1213. AssertTrue(BOF);
  1214. end;
  1215. end;
  1216. procedure TTestDBBasics.TestfieldDefinition(AFieldType : TFieldType;ADatasize : integer;var ADS : TDataset; var AFld: TField);
  1217. var i : byte;
  1218. begin
  1219. ADS := DBConnector.GetFieldDataset;
  1220. ADS.Open;
  1221. AFld := ADS.FindField('F'+FieldTypeNames[AfieldType]);
  1222. if not assigned (AFld) then
  1223. Ignore('Fields of the type ' + FieldTypeNames[AfieldType] + ' are not supported by this type of dataset');
  1224. AssertTrue(Afld.DataType = AFieldType);
  1225. AssertEquals(ADatasize,Afld.DataSize );
  1226. end;
  1227. procedure TTestDBBasics.TestSupportIntegerFields;
  1228. var i : byte;
  1229. ds : TDataset;
  1230. Fld : TField;
  1231. begin
  1232. TestfieldDefinition(ftInteger,4,ds,Fld);
  1233. for i := 0 to testValuesCount-1 do
  1234. begin
  1235. AssertEquals(testIntValues[i],Fld.AsInteger);
  1236. ds.Next;
  1237. end;
  1238. ds.close;
  1239. end;
  1240. procedure TTestDBBasics.TestSupportSmallIntFields;
  1241. var i : byte;
  1242. ds : TDataset;
  1243. Fld : TField;
  1244. begin
  1245. TestfieldDefinition(ftSmallint,2,ds,Fld);
  1246. for i := 0 to testValuesCount-1 do
  1247. begin
  1248. AssertEquals(testSmallIntValues[i],Fld.AsInteger);
  1249. ds.Next;
  1250. end;
  1251. ds.close;
  1252. end;
  1253. procedure TTestDBBasics.TestSupportStringFields;
  1254. var i : byte;
  1255. ds : TDataset;
  1256. Fld : TField;
  1257. begin
  1258. TestfieldDefinition(ftString,11,ds,Fld);
  1259. for i := 0 to testValuesCount-1 do
  1260. begin
  1261. AssertEquals(testStringValues[i],Fld.AsString);
  1262. ds.Next;
  1263. end;
  1264. ds.close;
  1265. end;
  1266. procedure TTestDBBasics.TestSupportBooleanFields;
  1267. var i : byte;
  1268. ds : TDataset;
  1269. Fld : TField;
  1270. begin
  1271. TestfieldDefinition(ftBoolean,2,ds,Fld);
  1272. for i := 0 to testValuesCount-1 do
  1273. begin
  1274. AssertEquals(testBooleanValues[i],Fld.AsBoolean);
  1275. ds.Next;
  1276. end;
  1277. ds.close;
  1278. end;
  1279. procedure TTestDBBasics.TestSupportFloatFields;
  1280. var i : byte;
  1281. ds : TDataset;
  1282. Fld : TField;
  1283. begin
  1284. TestfieldDefinition(ftFloat,8,ds,Fld);
  1285. for i := 0 to testValuesCount-1 do
  1286. begin
  1287. AssertEquals(testFloatValues[i],Fld.AsFloat);
  1288. ds.Next;
  1289. end;
  1290. ds.close;
  1291. end;
  1292. procedure TTestDBBasics.TestSupportLargeIntFields;
  1293. var i : byte;
  1294. ds : TDataset;
  1295. Fld : TField;
  1296. begin
  1297. TestfieldDefinition(ftLargeint,8,ds,Fld);
  1298. for i := 0 to testValuesCount-1 do
  1299. begin
  1300. AssertEquals(testLargeIntValues[i],Fld.AsLargeInt);
  1301. ds.Next;
  1302. end;
  1303. ds.close;
  1304. end;
  1305. procedure TTestDBBasics.TestSupportDateFields;
  1306. var i : byte;
  1307. ds : TDataset;
  1308. Fld : TField;
  1309. begin
  1310. TestfieldDefinition(ftDate,8,ds,Fld);
  1311. for i := 0 to testValuesCount-1 do
  1312. begin
  1313. AssertEquals(testDateValues[i],FormatDateTime('yyyy/mm/dd',Fld.AsDateTime));
  1314. ds.Next;
  1315. end;
  1316. ds.close;
  1317. end;
  1318. procedure TTestDBBasics.TestSupportCurrencyFields;
  1319. var i : byte;
  1320. ds : TDataset;
  1321. Fld : TField;
  1322. begin
  1323. TestfieldDefinition(ftCurrency,8,ds,Fld);
  1324. for i := 0 to testValuesCount-1 do
  1325. begin
  1326. AssertEquals(testCurrencyValues[i],Fld.AsCurrency);
  1327. AssertEquals(testCurrencyValues[i],Fld.AsFloat);
  1328. ds.Next;
  1329. end;
  1330. ds.close;
  1331. end;
  1332. procedure TTestDBBasics.TestSupportBCDFields;
  1333. var i : byte;
  1334. ds : TDataset;
  1335. Fld : TField;
  1336. begin
  1337. TestfieldDefinition(ftBCD,8,ds,Fld);
  1338. for i := 0 to testValuesCount-1 do
  1339. begin
  1340. AssertEquals(testCurrencyValues[i],Fld.AsCurrency);
  1341. AssertEquals(testCurrencyValues[i],Fld.AsFloat);
  1342. ds.Next;
  1343. end;
  1344. ds.close;
  1345. end;
  1346. procedure TTestDBBasics.TestDoubleClose;
  1347. begin
  1348. with DBConnector.GetNDataset(1) do
  1349. begin
  1350. close;
  1351. close;
  1352. open;
  1353. close;
  1354. close;
  1355. end;
  1356. end;
  1357. procedure TTestDBBasics.TestAssignFieldftString;
  1358. var AParam : TParam;
  1359. AField : TField;
  1360. begin
  1361. AParam := TParam.Create(nil);
  1362. with DBConnector.GetNDataset(1) do
  1363. begin
  1364. open;
  1365. AField := fieldbyname('name');
  1366. AParam.AssignField(AField);
  1367. AssertTrue(ftString=AParam.DataType);
  1368. close;
  1369. end;
  1370. AParam.Free;
  1371. end;
  1372. procedure TTestDBBasics.TestAssignFieldftFixedChar;
  1373. var AParam : TParam;
  1374. AField : TField;
  1375. begin
  1376. AParam := TParam.Create(nil);
  1377. with DBConnector.GetNDataset(1) do
  1378. begin
  1379. open;
  1380. AField := fieldbyname('name');
  1381. (AField as tstringfield).FixedChar := true;
  1382. AParam.AssignField(AField);
  1383. AssertTrue(ftFixedChar=AParam.DataType);
  1384. close;
  1385. end;
  1386. AParam.Free;
  1387. end;
  1388. procedure TTestDBBasics.TestBufDatasetCancelUpd;
  1389. var i : byte;
  1390. begin
  1391. if not (DBConnector.GetNDataset(5) is TBufDataset) then
  1392. Ignore('This test only applies to TBufDataset and descendents.');
  1393. with DBConnector.GetNDataset(5) as TBufDataset do
  1394. begin
  1395. open;
  1396. next;
  1397. next;
  1398. edit;
  1399. FieldByName('name').AsString := 'changed';
  1400. post;
  1401. next;
  1402. delete;
  1403. CancelUpdates;
  1404. First;
  1405. for i := 1 to 5 do
  1406. begin
  1407. AssertEquals(i,fields[0].AsInteger);
  1408. AssertEquals('TestName'+inttostr(i),fields[1].AsString);
  1409. Next;
  1410. end;
  1411. end;
  1412. end;
  1413. procedure TTestDBBasics.Testbug7007;
  1414. var
  1415. datalink1: tdatalink;
  1416. datasource1: tdatasource;
  1417. query1: TDataSet;
  1418. begin
  1419. query1:= DBConnector.GetNDataset(6);
  1420. datalink1:= TTestDataLink.create;
  1421. datasource1:= tdatasource.create(nil);
  1422. try
  1423. datalink1.datasource:= datasource1;
  1424. datasource1.dataset:= query1;
  1425. datalink1.datasource:= datasource1;
  1426. DataEvents := '';
  1427. query1.open;
  1428. datalink1.buffercount:= query1.recordcount;
  1429. AssertEquals('deUpdateState:0;',DataEvents);
  1430. AssertEquals(0, datalink1.ActiveRecord);
  1431. AssertEquals(6, datalink1.RecordCount);
  1432. AssertEquals(6, query1.RecordCount);
  1433. AssertEquals(1, query1.RecNo);
  1434. DataEvents := '';
  1435. query1.append;
  1436. AssertEquals('deCheckBrowseMode:0;deUpdateState:0;deDataSetChange:0;',DataEvents);
  1437. AssertEquals(5, datalink1.ActiveRecord);
  1438. AssertEquals(6, datalink1.RecordCount);
  1439. AssertEquals(6, query1.RecordCount);
  1440. AssertTrue(query1.RecNo in [0,7]);
  1441. DataEvents := '';
  1442. query1.cancel;
  1443. AssertEquals('deCheckBrowseMode:0;deUpdateState:0;deDataSetChange:0;',DataEvents);
  1444. AssertEquals(5, datalink1.ActiveRecord);
  1445. AssertEquals(6, datalink1.RecordCount);
  1446. AssertEquals(6, query1.RecordCount);
  1447. AssertEquals(6, query1.RecNo);
  1448. finally
  1449. datalink1.free;
  1450. datasource1.free;
  1451. end;
  1452. end;
  1453. procedure TTestDBBasics.TestBug6893;
  1454. var
  1455. datalink1: tdatalink;
  1456. datasource1: tdatasource;
  1457. query1: TDataSet;
  1458. begin
  1459. query1:= DBConnector.GetNDataset(25);
  1460. datalink1:= TDataLink.create;
  1461. datasource1:= tdatasource.create(nil);
  1462. try
  1463. datalink1.datasource:= datasource1;
  1464. datasource1.dataset:= query1;
  1465. datalink1.buffercount:= 5;
  1466. query1.active := true;
  1467. query1.MoveBy(20);
  1468. AssertEquals(5, THackDataLink(datalink1).Firstrecord);
  1469. AssertEquals(4, datalink1.ActiveRecord);
  1470. AssertEquals(21, query1.RecNo);
  1471. query1.active := False;
  1472. AssertEquals(0, THackDataLink(datalink1).Firstrecord);
  1473. AssertEquals(0, datalink1.ActiveRecord);
  1474. query1.active := true;
  1475. AssertEquals(0, THackDataLink(datalink1).Firstrecord);
  1476. AssertEquals(0, datalink1.ActiveRecord);
  1477. AssertEquals(1, query1.RecNo);
  1478. finally
  1479. datalink1.free;
  1480. datasource1.free;
  1481. end;
  1482. end;
  1483. procedure TTestDBBasics.TestBufDatasetCancelUpd1;
  1484. var i : byte;
  1485. begin
  1486. if not (DBConnector.GetNDataset(5) is TBufDataset) then
  1487. Ignore('This test only applies to TBufDataset and descendents.');
  1488. with DBConnector.GetNDataset(5) as TBufDataset do
  1489. begin
  1490. open;
  1491. next;
  1492. next;
  1493. delete;
  1494. insert;
  1495. FieldByName('id').AsInteger := 100;
  1496. post;
  1497. CancelUpdates;
  1498. last;
  1499. for i := 5 downto 1 do
  1500. begin
  1501. AssertEquals(i,fields[0].AsInteger);
  1502. AssertEquals('TestName'+inttostr(i),fields[1].AsString);
  1503. Prior;
  1504. end;
  1505. end;
  1506. end;
  1507. procedure TTestDBBasics.TestNullAtOpen;
  1508. begin
  1509. with dbconnector.getndataset(0) do
  1510. begin
  1511. active:= true;
  1512. AssertTrue('Field isn''t NULL on a just-opened empty dataset',fieldbyname('id').IsNull);
  1513. append;
  1514. AssertTrue('Field isn''t NULL after append on an empty dataset',fieldbyname('id').IsNull);
  1515. fieldbyname('id').asinteger:= 123;
  1516. cancel;
  1517. AssertTrue('Field isn''t NULL after cancel',fieldbyname('id').IsNull);
  1518. end;
  1519. end;
  1520. { TSQLTestSetup }
  1521. procedure TDBBasicsTestSetup.OneTimeSetup;
  1522. begin
  1523. InitialiseDBConnector;
  1524. end;
  1525. procedure TDBBasicsTestSetup.OneTimeTearDown;
  1526. begin
  1527. FreeAndNil(DBConnector);
  1528. end;
  1529. initialization
  1530. RegisterTestDecorator(TDBBasicsTestSetup, TTestDBBasics);
  1531. end.