testdbbasics.pas 40 KB

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