testdbbasics.pas 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  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, ToolsUnit;
  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. protected
  19. procedure SetUp; override;
  20. procedure TearDown; override;
  21. published
  22. procedure TestCancelUpdDelete1;
  23. procedure TestCancelUpdDelete2;
  24. procedure TestAppendInsertRecord;
  25. procedure TestBookmarks;
  26. procedure TestBookmarkValid;
  27. procedure TestLocate;
  28. procedure TestLocateCaseIns;
  29. procedure TestFirst;
  30. procedure TestDelete1;
  31. procedure TestDelete2;
  32. procedure TestIntFilter;
  33. procedure TestOnFilter;
  34. procedure TestStringFilter;
  35. procedure TestSetFieldValues;
  36. procedure TestGetFieldValues;
  37. procedure TestNullAtOpen;
  38. procedure TestSupportIntegerFields;
  39. procedure TestSupportSmallIntFields;
  40. procedure TestSupportStringFields;
  41. procedure TestSupportBooleanFields;
  42. procedure TestSupportFloatFields;
  43. procedure TestSupportLargeIntFields;
  44. procedure TestSupportDateFields;
  45. procedure TestSupportCurrencyFields;
  46. procedure TestSupportBCDFields;
  47. procedure TestSupportFixedStringFields;
  48. procedure TestAppendOnEmptyDataset;
  49. procedure TestInsertOnEmptyDataset;
  50. procedure TestEofAfterFirst; //bug 7211
  51. procedure TestDoubleClose;
  52. procedure TestCalculatedField;
  53. procedure TestAssignFieldftString;
  54. procedure TestAssignFieldftFixedChar;
  55. procedure TestSelectQueryBasics;
  56. procedure TestPostOnlyInEditState;
  57. procedure TestMove; // bug 5048
  58. procedure TestActiveBufferWhenClosed;
  59. procedure TestEOFBOFClosedDataset;
  60. procedure TestDataEventsResync;
  61. procedure TestBug7007;
  62. procedure TestBug6893;
  63. procedure TestRecordcountAfterReopen; // partly bug 8228
  64. procedure TestdeFieldListChange;
  65. procedure TestLastAppendCancel; // bug 5058
  66. procedure TestRecNo; // bug 5061
  67. procedure TestSetRecNo; // bug 6919
  68. procedure TestRequired;
  69. procedure TestExceptionLocateClosed; // bug 13938
  70. procedure TestCanModifySpecialFields;
  71. end;
  72. { TTestBufDatasetDBBasics }
  73. TTestBufDatasetDBBasics = class(TTestCase)
  74. private
  75. procedure FTestXMLDatasetDefinition(ADataset : TDataset);
  76. procedure TestAddIndexFieldType(AFieldType : TFieldType; ActiveDS : boolean);
  77. protected
  78. procedure SetUp; override;
  79. procedure TearDown; override;
  80. published
  81. procedure TestClosedIndexFieldNames; // bug 16695
  82. procedure TestFileNameProperty;
  83. procedure TestClientDatasetAsMemDataset;
  84. procedure TestSafeAsXML;
  85. procedure TestIsEmpty;
  86. procedure TestBufDatasetCancelUpd; //bug 6938
  87. procedure TestBufDatasetCancelUpd1;
  88. procedure TestMultipleDeleteUpdateBuffer;
  89. procedure TestDoubleDelete;
  90. // index tests
  91. procedure TestAddIndexInteger;
  92. procedure TestAddIndexSmallInt;
  93. procedure TestAddIndexBoolean;
  94. procedure TestAddIndexFloat;
  95. procedure TestAddIndexLargeInt;
  96. procedure TestAddIndexDateTime;
  97. procedure TestAddIndexCurrency;
  98. procedure TestAddIndexBCD;
  99. procedure TestAddIndex;
  100. procedure TestAddDescIndex;
  101. procedure TestAddCaseInsIndex;
  102. procedure TestInactSwitchIndex;
  103. procedure TestAddIndexActiveDS;
  104. procedure TestAddIndexEditDS;
  105. procedure TestIndexFieldNames;
  106. procedure TestIndexFieldNamesAct;
  107. procedure TestIndexCurRecord;
  108. procedure TestAddDblIndex;
  109. procedure TestIndexEditRecord;
  110. end;
  111. TTestUniDirectionalDBBasics = class(TTestDBBasics)
  112. end;
  113. { TDBBasicsUniDirectionalTestSetup }
  114. TDBBasicsUniDirectionalTestSetup = class(TDBBasicsTestSetup)
  115. protected
  116. procedure OneTimeSetup; override;
  117. procedure OneTimeTearDown; override;
  118. end;
  119. implementation
  120. uses bufdataset, variants, strutils, sqldb;
  121. type THackDataLink=class(TdataLink);
  122. procedure TTestBufDatasetDBBasics.TestIsEmpty;
  123. begin
  124. with tCustombufdataset(DBConnector.GetNDataset(True,1)) do
  125. begin
  126. open;
  127. delete;
  128. Resync([]);
  129. applyupdates;
  130. AssertTrue(IsEmpty);
  131. end;
  132. end;
  133. procedure TTestDBBasics.TestAppendOnEmptyDataset;
  134. begin
  135. with DBConnector.GetNDataset(0) do
  136. begin
  137. open;
  138. AssertTrue(CanModify);
  139. AssertTrue(eof);
  140. AssertTrue(bof);
  141. append;
  142. FieldByName('id').AsInteger:=0;
  143. AssertFalse(Bof);
  144. AssertTrue(Eof);
  145. post;
  146. AssertFalse(eof);
  147. AssertFalse(bof);
  148. end;
  149. end;
  150. procedure TTestDBBasics.TestInsertOnEmptyDataset;
  151. begin
  152. with DBConnector.GetNDataset(0) do
  153. begin
  154. open;
  155. AssertTrue(CanModify);
  156. AssertTrue(eof);
  157. AssertTrue(bof);
  158. AssertTrue(IsEmpty);
  159. insert;
  160. FieldByName('id').AsInteger:=0;
  161. AssertTrue(Bof);
  162. AssertTrue(Eof);
  163. AssertFalse(IsEmpty);
  164. post;
  165. AssertFalse(IsEmpty);
  166. AssertFalse(eof);
  167. AssertFalse(bof);
  168. end;
  169. end;
  170. procedure TTestDBBasics.TestSelectQueryBasics;
  171. var b : TFieldType;
  172. begin
  173. with DBConnector.GetNDataset(1) do
  174. begin
  175. Open;
  176. AssertEquals(1,RecNo);
  177. AssertEquals(1,RecordCount);
  178. AssertEquals(2,FieldCount);
  179. AssertTrue(CompareText('ID',fields[0].FieldName)=0);
  180. AssertTrue(CompareText('ID',fields[0].DisplayName)=0);
  181. AssertTrue('The datatype of the field ''ID'' is incorrect, it should be ftInteger',ftInteger=fields[0].DataType);
  182. AssertTrue(CompareText('NAME',fields[1].FieldName)=0);
  183. AssertTrue(CompareText('NAME',fields[1].DisplayName)=0);
  184. AssertTrue(ftString=fields[1].DataType);
  185. AssertEquals(1,fields[0].Value);
  186. AssertEquals('TestName1',fields[1].Value);
  187. Close;
  188. end;
  189. end;
  190. procedure TTestDBBasics.TestPostOnlyInEditState;
  191. begin
  192. with DBConnector.GetNDataset(1) do
  193. begin
  194. open;
  195. AssertException('Post was called in a non-edit state',EDatabaseError,Post);
  196. end;
  197. end;
  198. procedure TTestDBBasics.TestMove;
  199. var i,count : integer;
  200. aDatasource : TDataSource;
  201. aDatalink : TDataLink;
  202. ABufferCount : Integer;
  203. begin
  204. aDatasource := TDataSource.Create(nil);
  205. aDatalink := TTestDataLink.Create;
  206. try
  207. aDatalink.DataSource := aDatasource;
  208. ABufferCount := 11;
  209. aDatalink.BufferCount := ABufferCount;
  210. DataEvents := '';
  211. for count := 0 to 32 do
  212. begin
  213. aDatasource.DataSet := DBConnector.GetNDataset(count);
  214. with aDatasource.Dataset do
  215. begin
  216. i := 1;
  217. Open;
  218. AssertEquals('deUpdateState:0;',DataEvents);
  219. DataEvents := '';
  220. while not EOF do
  221. begin
  222. AssertEquals(i,fields[0].AsInteger);
  223. AssertEquals('TestName'+inttostr(i),fields[1].AsString);
  224. inc(i);
  225. Next;
  226. if (i > ABufferCount) and not EOF then
  227. AssertEquals('deCheckBrowseMode:0;deDataSetScroll:-1;DataSetScrolled:1;',DataEvents)
  228. else
  229. AssertEquals('deCheckBrowseMode:0;deDataSetScroll:0;DataSetScrolled:0;',DataEvents);
  230. DataEvents := '';
  231. end;
  232. AssertEquals(count,i-1);
  233. close;
  234. AssertEquals('deUpdateState:0;',DataEvents);
  235. DataEvents := '';
  236. end;
  237. end;
  238. finally
  239. aDatalink.Free;
  240. aDatasource.Free;
  241. end;
  242. end;
  243. procedure TTestDBBasics.TestdeFieldListChange;
  244. var i,count : integer;
  245. aDatasource : TDataSource;
  246. aDatalink : TDataLink;
  247. ds : TDataset;
  248. begin
  249. aDatasource := TDataSource.Create(nil);
  250. aDatalink := TTestDataLink.Create;
  251. aDatalink.DataSource := aDatasource;
  252. ds := DBConnector.GetNDataset(1);
  253. with ds do
  254. begin
  255. aDatasource.DataSet := ds;
  256. DataEvents := '';
  257. open;
  258. Fields.add(tfield.Create(DBConnector.GetNDataset(1)));
  259. AssertEquals('deUpdateState:0;deFieldListChange:0;',DataEvents);
  260. DataEvents := '';
  261. fields.Clear;
  262. AssertEquals('deFieldListChange:0;',DataEvents)
  263. end;
  264. aDatasource.Free;
  265. aDatalink.Free;
  266. end;
  267. procedure TTestDBBasics.TestActiveBufferWhenClosed;
  268. begin
  269. with DBConnector.GetNDataset(0) do
  270. begin
  271. AssertNull(ActiveBuffer);
  272. open;
  273. AssertFalse('Activebuffer of an empty dataset shouldn''t be nil',ActiveBuffer = nil);
  274. end;
  275. end;
  276. procedure TTestDBBasics.TestEOFBOFClosedDataset;
  277. begin
  278. with DBConnector.GetNDataset(1) do
  279. begin
  280. AssertTrue(EOF);
  281. AssertTrue(BOF);
  282. open;
  283. close;
  284. AssertTrue(EOF);
  285. AssertTrue(BOF);
  286. end;
  287. end;
  288. procedure TTestDBBasics.TestDataEventsResync;
  289. var i,count : integer;
  290. aDatasource : TDataSource;
  291. aDatalink : TDataLink;
  292. ds : tdataset;
  293. begin
  294. aDatasource := TDataSource.Create(nil);
  295. aDatalink := TTestDataLink.Create;
  296. try
  297. aDatalink.DataSource := aDatasource;
  298. ds := DBConnector.GetNDataset(6);
  299. ds.BeforeScroll := DBConnector.DataEvent;
  300. with ds do
  301. begin
  302. aDatasource.DataSet := ds;
  303. open;
  304. DataEvents := '';
  305. Resync([rmExact]);
  306. AssertEquals('deDataSetChange:0;',DataEvents);
  307. DataEvents := '';
  308. next;
  309. AssertEquals('deCheckBrowseMode:0;DataEvent;deDataSetScroll:0;DataSetScrolled:1;',DataEvents);
  310. close;
  311. end;
  312. finally
  313. aDatasource.Free;
  314. aDatalink.Free;
  315. end;
  316. end;
  317. procedure TTestDBBasics.TestLastAppendCancel;
  318. var count : integer;
  319. begin
  320. for count := 0 to 32 do with DBConnector.GetNDataset(count) do
  321. begin
  322. open;
  323. Last;
  324. Append;
  325. Cancel;
  326. AssertEquals(count,fields[0].asinteger);
  327. AssertEquals(count,RecordCount);
  328. Close;
  329. end;
  330. end;
  331. procedure TTestDBBasics.TestRecNo;
  332. var i : longint;
  333. passed : boolean;
  334. begin
  335. with DBConnector.GetNDataset(0) do
  336. begin
  337. // Accessing RecNo on a closed dataset should raise an EDatabaseError or should
  338. // return 0
  339. passed := false;
  340. try
  341. i := recno;
  342. except on E: Exception do
  343. begin
  344. passed := E.classname = EDatabaseError.className
  345. end;
  346. end;
  347. if not passed then
  348. AssertEquals('Failed to get the RecNo from a closed dataset',0,RecNo);
  349. // Accessing Recordcount on a closed dataset should raise an EDatabaseError or should
  350. // return 0
  351. passed := false;
  352. try
  353. i := recordcount;
  354. except on E: Exception do
  355. begin
  356. passed := E.classname = EDatabaseError.className
  357. end;
  358. end;
  359. if not passed then
  360. AssertEquals('Failed to get the Recordcount from a closed dataset',0,RecNo);
  361. Open;
  362. AssertEquals(0,RecordCount);
  363. AssertEquals(0,RecNo);
  364. first;
  365. AssertEquals(0,RecordCount);
  366. AssertEquals(0,RecNo);
  367. last;
  368. AssertEquals(0,RecordCount);
  369. AssertEquals(0,RecNo);
  370. append;
  371. AssertEquals(0,RecNo);
  372. AssertEquals(0,RecordCount);
  373. first;
  374. AssertEquals(0,RecNo);
  375. AssertEquals(0,RecordCount);
  376. append;
  377. FieldByName('id').AsInteger := 1;
  378. AssertEquals(0,RecNo);
  379. AssertEquals(0,RecordCount);
  380. first;
  381. AssertEquals(1,RecNo);
  382. AssertEquals(1,RecordCount);
  383. last;
  384. AssertEquals(1,RecNo);
  385. AssertEquals(1,RecordCount);
  386. append;
  387. FieldByName('id').AsInteger := 1;
  388. AssertEquals(0,RecNo);
  389. AssertEquals(1,RecordCount);
  390. Close;
  391. end;
  392. end;
  393. procedure TTestDBBasics.TestSetRecNo;
  394. begin
  395. with DBConnector.GetNDataset(15) do
  396. begin
  397. Open;
  398. RecNo := 1;
  399. AssertEquals(1,fields[0].AsInteger);
  400. AssertEquals(1,RecNo);
  401. RecNo := 2;
  402. AssertEquals(2,fields[0].AsInteger);
  403. AssertEquals(2,RecNo);
  404. RecNo := 8;
  405. AssertEquals(8,fields[0].AsInteger);
  406. AssertEquals(8,RecNo);
  407. RecNo := 15;
  408. AssertEquals(15,fields[0].AsInteger);
  409. AssertEquals(15,RecNo);
  410. RecNo := 3;
  411. AssertEquals(3,fields[0].AsInteger);
  412. AssertEquals(3,RecNo);
  413. RecNo := 14;
  414. AssertEquals(14,fields[0].AsInteger);
  415. AssertEquals(14,RecNo);
  416. RecNo := 15;
  417. AssertEquals(15,fields[0].AsInteger);
  418. AssertEquals(15,RecNo);
  419. // test for exceptions...
  420. { RecNo := 16;
  421. AssertEquals(15,fields[0].AsInteger);
  422. AssertEquals(15,RecNo);}
  423. Close;
  424. end;
  425. end;
  426. procedure TTestDBBasics.TestRequired;
  427. begin
  428. with DBConnector.GetNDataset(2) do
  429. begin
  430. Open;
  431. FieldByName('ID').Required := True;
  432. Append;
  433. AssertException(EDatabaseError,Post);
  434. FieldByName('ID').AsInteger := 1000;
  435. Post;
  436. Close;
  437. end;
  438. end;
  439. procedure TTestDBBasics.TestExceptionLocateClosed;
  440. var passed: boolean;
  441. begin
  442. with DBConnector.GetNDataset(15) do
  443. begin
  444. passed := false;
  445. try
  446. locate('name','TestName1',[]);
  447. except on E: Exception do
  448. begin
  449. passed := E.classname = EDatabaseError.className
  450. end;
  451. end;
  452. AssertTrue(passed);
  453. end;
  454. end;
  455. procedure TTestDBBasics.SetUp;
  456. begin
  457. DBConnector.StartTest;
  458. end;
  459. procedure TTestDBBasics.TearDown;
  460. begin
  461. DBConnector.StopTest;
  462. end;
  463. procedure TTestBufDatasetDBBasics.TestClosedIndexFieldNames;
  464. var s : string;
  465. bufds: TCustomBufDataset;
  466. begin
  467. bufds := DBConnector.GetNDataset(5) as TCustomBufDataset;
  468. s := bufds.IndexFieldNames;
  469. s := bufds.IndexName;
  470. bufds.CompareBookmarks(nil,nil);
  471. end;
  472. procedure TTestDBBasics.TestCanModifySpecialFields;
  473. var ds : TDataset;
  474. lds : TDataset;
  475. fld : TField;
  476. begin
  477. lds := DBConnector.GetNDataset(10);
  478. ds := DBConnector.GetNDataset(5);
  479. with ds do
  480. begin
  481. Fld := TIntegerField.Create(ds);
  482. Fld.FieldName:='ID';
  483. Fld.DataSet:=ds;
  484. Fld := TStringField.Create(ds);
  485. Fld.FieldName:='LookupFld';
  486. Fld.FieldKind:=fkLookup;
  487. Fld.DataSet:=ds;
  488. Fld.LookupDataSet:=lds;
  489. Fld.LookupResultField:='NAME';
  490. Fld.LookupKeyFields:='ID';
  491. Fld.KeyFields:='ID';
  492. lds.Open;
  493. Open;
  494. AssertTrue(FieldByName('ID').CanModify);
  495. AssertFalse(FieldByName('LookupFld').CanModify);
  496. AssertFalse(FieldByName('ID').ReadOnly);
  497. AssertFalse(FieldByName('LookupFld').ReadOnly);
  498. AssertEquals(1,FieldByName('ID').AsInteger);
  499. AssertEquals('name1',FieldByName('LookupFld').AsString);
  500. close;
  501. lds.Close;
  502. end;
  503. end;
  504. procedure TTestBufDatasetDBBasics.TestSafeAsXML;
  505. var ds : TDataset;
  506. LoadDs: TCustomBufDataset;
  507. begin
  508. ds := DBConnector.GetNDataset(true,5);
  509. ds.open;
  510. TCustomBufDataset(ds).SaveToFile('test.xml');
  511. ds.close;
  512. LoadDs := TCustomBufDataset.Create(nil);
  513. LoadDs.LoadFromFile('test.xml');
  514. FTestXMLDatasetDefinition(LoadDS);
  515. end;
  516. procedure TTestBufDatasetDBBasics.TestFileNameProperty;
  517. var ds1,ds2: TDataset;
  518. LoadDs: TCustomBufDataset;
  519. begin
  520. ds2 := nil;
  521. ds1 := DBConnector.GetNDataset(true,5);
  522. try
  523. ds1.open;
  524. TCustomBufDataset(ds1).FileName:='test.xml';
  525. ds1.close;
  526. ds2 := DBConnector.GetNDataset(True,7);
  527. TCustomBufDataset(ds2).FileName:='test.xml';
  528. ds2.Open;
  529. FTestXMLDatasetDefinition(Ds2);
  530. finally
  531. TCustomBufDataset(ds1).FileName:='';
  532. if assigned(ds2) then
  533. TCustomBufDataset(ds2).FileName:='';
  534. end;
  535. end;
  536. procedure TTestBufDatasetDBBasics.TestClientDatasetAsMemDataset;
  537. var ds : TCustomBufDataset;
  538. i : integer;
  539. begin
  540. ds := TCustomBufDataset.Create(nil);
  541. DS.FieldDefs.Add('ID',ftInteger);
  542. DS.FieldDefs.Add('NAME',ftString,50);
  543. DS.CreateDataset;
  544. DS.Open;
  545. for i := 1 to 10 do
  546. begin
  547. ds.Append;
  548. ds.FieldByName('ID').AsInteger := i;
  549. ds.FieldByName('NAME').AsString := 'TestName' + inttostr(i);
  550. DS.Post;
  551. end;
  552. ds.first;
  553. for i := 1 to 10 do
  554. begin
  555. AssertEquals(i,ds.fieldbyname('ID').asinteger);
  556. AssertEquals('TestName' + inttostr(i),ds.fieldbyname('NAME').AsString);
  557. ds.next;
  558. end;
  559. AssertTrue(ds.EOF);
  560. DS.Close;
  561. end;
  562. procedure TTestDBBasics.TestAppendInsertRecord;
  563. begin
  564. with DBConnector.GetNDataset(true,6) do
  565. begin
  566. open;
  567. // InsertRecord should insert a record, set the values, post the record and
  568. // make the new record active.
  569. InsertRecord([152,'TestInsRec']);
  570. AssertEquals(152,fields[0].AsInteger);
  571. AssertEquals('TestInsRec',fields[1].AsString);
  572. AssertTrue(state=dsBrowse);
  573. // AppendRecord should append a record, further the same as InsertRecord
  574. AppendRecord([151,'TestInsRec']);
  575. AssertEquals(151,fields[0].AsInteger);
  576. AssertEquals('TestInsRec',fields[1].AsString);
  577. AssertTrue(state=dsBrowse);
  578. next;
  579. AssertTrue(EOF);
  580. end;
  581. end;
  582. procedure TTestDBBasics.TestBookmarks;
  583. var BM1,BM2,BM3,BM4,BM5 : TBookmark;
  584. begin
  585. with DBConnector.GetNDataset(true,14) do
  586. begin
  587. AssertNull(GetBookmark);
  588. open;
  589. BM1:=GetBookmark; // id=1, BOF
  590. next;next;
  591. BM2:=GetBookmark; // id=3
  592. next;next;next;
  593. BM3:=GetBookmark; // id=6
  594. next;next;next;next;next;next;next;next;
  595. BM4:=GetBookmark; // id=14
  596. next;
  597. BM5:=GetBookmark; // id=14, EOF
  598. GotoBookmark(BM2);
  599. AssertEquals(3,FieldByName('id').AsInteger);
  600. GotoBookmark(BM1);
  601. AssertEquals(1,FieldByName('id').AsInteger);
  602. GotoBookmark(BM3);
  603. AssertEquals(6,FieldByName('id').AsInteger);
  604. GotoBookmark(BM4);
  605. AssertEquals(14,FieldByName('id').AsInteger);
  606. GotoBookmark(BM3);
  607. AssertEquals(6,FieldByName('id').AsInteger);
  608. GotoBookmark(BM5);
  609. AssertEquals(14,FieldByName('id').AsInteger);
  610. GotoBookmark(BM1);
  611. AssertEquals(1,FieldByName('id').AsInteger);
  612. next;
  613. delete;
  614. GotoBookmark(BM2);
  615. AssertEquals(3,FieldByName('id').AsInteger);
  616. delete;delete;
  617. GotoBookmark(BM3);
  618. AssertEquals(6,FieldByName('id').AsInteger);
  619. GotoBookmark(BM1);
  620. AssertEquals(1,FieldByName('id').AsInteger);
  621. insert;
  622. fieldbyname('id').AsInteger:=20;
  623. insert;
  624. fieldbyname('id').AsInteger:=21;
  625. insert;
  626. fieldbyname('id').AsInteger:=22;
  627. insert;
  628. fieldbyname('id').AsInteger:=23;
  629. post;
  630. GotoBookmark(BM3);
  631. AssertEquals(6,FieldByName('id').AsInteger);
  632. GotoBookmark(BM1);
  633. AssertEquals(1,FieldByName('id').AsInteger);
  634. GotoBookmark(BM5);
  635. AssertEquals(14,FieldByName('id').AsInteger);
  636. end;
  637. end;
  638. procedure TTestDBBasics.TestBookmarkValid;
  639. var BM1,BM2,BM3,BM4,BM5 : TBookmark;
  640. begin
  641. with DBConnector.GetNDataset(true,14) do
  642. begin
  643. BM1 := Nil;
  644. AssertFalse(BookmarkValid(BM1));
  645. open;
  646. BM1:=GetBookmark; // id=1, BOF
  647. AssertTrue(BookmarkValid(BM1));
  648. next;next;
  649. BM2:=GetBookmark; // id=3
  650. AssertTrue(BookmarkValid(BM2));
  651. next;next;next;
  652. BM3:=GetBookmark; // id=6
  653. AssertTrue(BookmarkValid(BM3));
  654. next;next;next;next;next;next;next;next;
  655. BM4:=GetBookmark; // id=14
  656. AssertTrue(BookmarkValid(BM4));
  657. next;
  658. BM5:=GetBookmark; // id=14, EOF
  659. AssertTrue(BookmarkValid(BM5));
  660. AssertTrue(BookmarkValid(BM4));
  661. AssertTrue(BookmarkValid(BM3));
  662. AssertTrue(BookmarkValid(BM2));
  663. AssertTrue(BookmarkValid(BM1));
  664. GotoBookmark(BM2);
  665. AssertTrue(BookmarkValid(BM5));
  666. AssertTrue(BookmarkValid(BM4));
  667. AssertTrue(BookmarkValid(BM3));
  668. AssertTrue(BookmarkValid(BM2));
  669. AssertTrue(BookmarkValid(BM1));
  670. end;
  671. end;
  672. procedure TTestDBBasics.TestLocate;
  673. begin
  674. with DBConnector.GetNDataset(true,13) do
  675. begin
  676. open;
  677. asserttrue(Locate('id',vararrayof([5]),[]));
  678. AssertEquals(5,FieldByName('id').AsInteger);
  679. AssertFalse(Locate('id',vararrayof([15]),[]));
  680. asserttrue(Locate('id',vararrayof([12]),[]));
  681. AssertEquals(12,FieldByName('id').AsInteger);
  682. close;
  683. open;
  684. asserttrue(Locate('id',vararrayof([12]),[]));
  685. AssertEquals(12,FieldByName('id').AsInteger);
  686. asserttrue(Locate('id;name',vararrayof([4,'TestName4']),[]));
  687. AssertEquals(4,FieldByName('id').AsInteger);
  688. assertFalse(Locate('id;name',vararrayof([4,'TestName5']),[]));
  689. end;
  690. end;
  691. procedure TTestDBBasics.TestLocateCaseIns;
  692. begin
  693. with DBConnector.GetNDataset(true,13) do
  694. begin
  695. open;
  696. assertfalse(Locate('name',vararrayof(['TEstName5']),[]));
  697. asserttrue(Locate('name',vararrayof(['TEstName5']),[loCaseInsensitive]));
  698. AssertEquals(5,FieldByName('id').AsInteger);
  699. assertfalse(Locate('name',vararrayof(['TestN']),[]));
  700. asserttrue(Locate('name',vararrayof(['TestN']),[loPartialKey]));
  701. assertfalse(Locate('name',vararrayof(['TestNA']),[loPartialKey]));
  702. asserttrue(Locate('name',vararrayof(['TestNA']),[loPartialKey, loCaseInsensitive]));
  703. close;
  704. end;
  705. end;
  706. procedure TTestDBBasics.TestSetFieldValues;
  707. var PassException : boolean;
  708. begin
  709. with DBConnector.GetNDataset(true,11) do
  710. begin
  711. open;
  712. first;
  713. edit;
  714. FieldValues['id']:=5;
  715. post;
  716. AssertEquals('TestName1',FieldByName('name').AsString);
  717. AssertEquals(5,FieldByName('id').AsInteger);
  718. edit;
  719. FieldValues['name']:='FieldValuesTestName';
  720. post;
  721. AssertEquals('FieldValuesTestName',FieldByName('name').AsString);
  722. AssertEquals(5,FieldByName('id').AsInteger);
  723. edit;
  724. FieldValues['id;name']:= VarArrayOf([243,'ValuesTestName']);
  725. post;
  726. AssertEquals('ValuesTestName',FieldByName('name').AsString);
  727. AssertEquals(243,FieldByName('id').AsInteger);
  728. PassException:=false;
  729. try
  730. edit;
  731. FieldValues['id;name;fake']:= VarArrayOf([243,'ValuesTestName',4]);
  732. except
  733. on E: EDatabaseError do PassException := True;
  734. end;
  735. post;
  736. AssertTrue(PassException);
  737. end;
  738. end;
  739. procedure TTestDBBasics.TestGetFieldValues;
  740. var AVar : Variant;
  741. PassException : boolean;
  742. begin
  743. with DBConnector.GetNDataset(true,14) do
  744. begin
  745. open;
  746. AVar:=FieldValues['id'];
  747. AssertEquals(AVar,1);
  748. AVar:=FieldValues['name'];
  749. AssertEquals(AVar,'TestName1');
  750. AVar:=FieldValues['id;name'];
  751. AssertEquals(AVar[0],1);
  752. AssertEquals(AVar[1],'TestName1');
  753. AVar:=FieldValues['name;id;'];
  754. AssertEquals(AVar[1],1);
  755. AssertEquals(AVar[0],'TestName1');
  756. PassException:=false;
  757. try
  758. AVar:=FieldValues['name;id;fake'];
  759. except
  760. on E: EDatabaseError do PassException := True;
  761. end;
  762. AssertTrue(PassException);
  763. end;
  764. end;
  765. procedure TTestDBBasics.TestFirst;
  766. var i : integer;
  767. begin
  768. with DBConnector.GetNDataset(true,14) do
  769. begin
  770. open;
  771. AssertEquals(1,FieldByName('ID').AsInteger);
  772. First;
  773. AssertEquals(1,FieldByName('ID').AsInteger);
  774. next;
  775. AssertEquals(2,FieldByName('ID').AsInteger);
  776. First;
  777. AssertEquals(1,FieldByName('ID').AsInteger);
  778. for i := 0 to 12 do
  779. next;
  780. AssertEquals(14,FieldByName('ID').AsInteger);
  781. First;
  782. AssertEquals(1,FieldByName('ID').AsInteger);
  783. close;
  784. end;
  785. end;
  786. procedure TTestDBBasics.TestDelete1;
  787. begin
  788. FTestDelete1(false);
  789. end;
  790. procedure TTestDBBasics.TestDelete2;
  791. begin
  792. FTestDelete2(false);
  793. end;
  794. procedure TTestDBBasics.TestCancelUpdDelete1;
  795. begin
  796. FTestDelete1(true);
  797. end;
  798. procedure TTestDBBasics.TestCancelUpdDelete2;
  799. begin
  800. FTestDelete2(true);
  801. end;
  802. procedure TTestDBBasics.FTestDelete1(TestCancelUpdate : boolean);
  803. // Test the deletion of records, including the first and the last one
  804. var i : integer;
  805. ds : TDataset;
  806. begin
  807. ds := DBConnector.GetNDataset(true,17);
  808. with ds do
  809. begin
  810. Open;
  811. for i := 0 to 16 do if i mod 4=0 then
  812. delete
  813. else
  814. next;
  815. First;
  816. for i := 0 to 16 do
  817. begin
  818. if i mod 4<>0 then
  819. begin
  820. AssertEquals(i+1,FieldByName('ID').AsInteger);
  821. AssertEquals('TestName'+inttostr(i+1),FieldByName('NAME').AsString);
  822. next;
  823. end;
  824. end;
  825. end;
  826. if TestCancelUpdate then
  827. begin
  828. if not (ds is TCustomBufDataset) then
  829. Ignore('This test only applies to TCustomBufDataset and descendents.');
  830. with TCustomBufDataset(ds) do
  831. begin
  832. CancelUpdates;
  833. First;
  834. for i := 0 to 16 do
  835. begin
  836. AssertEquals(i+1,FieldByName('ID').AsInteger);
  837. AssertEquals('TestName'+inttostr(i+1),FieldByName('NAME').AsString);
  838. next;
  839. end;
  840. close;
  841. end;
  842. end;
  843. end;
  844. procedure TTestDBBasics.FTestDelete2(TestCancelUpdate : boolean);
  845. // Test the deletion of edited and appended records
  846. var i : integer;
  847. ds : TDataset;
  848. begin
  849. ds := DBConnector.GetNDataset(true,17);
  850. with ds do
  851. begin
  852. Open;
  853. for i := 0 to 16 do
  854. begin
  855. if i mod 4=0 then
  856. begin
  857. edit;
  858. fieldbyname('name').AsString:='this record will be gone soon';
  859. post;
  860. end;
  861. next;
  862. end;
  863. for i := 17 to 20 do
  864. begin
  865. append;
  866. fieldbyname('id').AsInteger:=i+1;
  867. fieldbyname('name').AsString:='TestName'+inttostr(i+1);
  868. post;
  869. end;
  870. first;
  871. for i := 0 to 20 do if i mod 4=0 then
  872. delete
  873. else
  874. next;
  875. First;
  876. i := 0;
  877. for i := 0 to 20 do
  878. begin
  879. if i mod 4<>0 then
  880. begin
  881. AssertEquals(i+1,FieldByName('ID').AsInteger);
  882. AssertEquals('TestName'+inttostr(i+1),FieldByName('NAME').AsString);
  883. next;
  884. end;
  885. end;
  886. end;
  887. if TestCancelUpdate then
  888. begin
  889. if not (ds is TCustomBufDataset) then
  890. Ignore('This test only applies to TCustomBufDataset and descendents.');
  891. with TCustomBufDataset(ds) do
  892. begin
  893. CancelUpdates;
  894. First;
  895. for i := 0 to 16 do
  896. begin
  897. AssertEquals(i+1,FieldByName('ID').AsInteger);
  898. AssertEquals('TestName'+inttostr(i+1),FieldByName('NAME').AsString);
  899. next;
  900. end;
  901. close;
  902. end;
  903. end;
  904. end;
  905. procedure TTestBufDatasetDBBasics.FTestXMLDatasetDefinition(ADataset: TDataset);
  906. var i : integer;
  907. begin
  908. AssertEquals(2,ADataset.FieldDefs.Count);
  909. AssertEquals(2,ADataset.Fields.Count);
  910. AssertTrue(SameText('ID',ADataset.Fields[0].FieldName));
  911. AssertTrue(SameText('NAME',ADataset.Fields[1].FieldName));
  912. AssertTrue('Incorrect fieldtype',ADataset.fields[1].DataType=ftString);
  913. i := 1;
  914. while not ADataset.EOF do
  915. begin
  916. AssertEquals('TestName'+inttostr(i),ADataset.FieldByName('name').AsString);
  917. ADataset.Next;
  918. inc(i);
  919. end;
  920. end;
  921. procedure TTestDBBasics.TestOnFilterProc(DataSet: TDataSet; var Accept: Boolean);
  922. var a : TDataSetState;
  923. begin
  924. Accept := odd(Dataset.FieldByName('ID').AsInteger);
  925. end;
  926. procedure TTestDBBasics.TestOnFilter;
  927. var tel : byte;
  928. begin
  929. with DBConnector.GetNDataset(15) do
  930. begin
  931. OnFilterRecord := TestOnFilterProc;
  932. Filtered := True;
  933. Open;
  934. for tel := 1 to 8 do
  935. begin
  936. AssertTrue(odd(FieldByName('ID').asinteger));
  937. next;
  938. end;
  939. AssertTrue(EOF);
  940. end;
  941. end;
  942. procedure TTestDBBasics.TestIntFilter;
  943. var tel : byte;
  944. begin
  945. with DBConnector.GetNDataset(15) do
  946. begin
  947. Filtered := True;
  948. Filter := '(id>4) and (id<9)';
  949. Open;
  950. for tel := 5 to 8 do
  951. begin
  952. AssertEquals(tel,FieldByName('ID').asinteger);
  953. next;
  954. end;
  955. AssertTrue(EOF);
  956. Close;
  957. end;
  958. end;
  959. procedure TTestDBBasics.TestRecordcountAfterReopen;
  960. var
  961. datalink1: tdatalink;
  962. datasource1: tdatasource;
  963. query1: TDataSet;
  964. begin
  965. query1:= DBConnector.GetNDataset(11);
  966. datalink1:= TDataLink.create;
  967. datasource1:= tdatasource.create(nil);
  968. try
  969. datalink1.datasource:= datasource1;
  970. datasource1.dataset:= query1;
  971. query1.active := true;
  972. query1.active := False;
  973. AssertEquals(0, THackDataLink(datalink1).RecordCount);
  974. query1.active := true;
  975. AssertTrue(THackDataLink(datalink1).RecordCount>0);
  976. query1.active := false;
  977. finally
  978. datalink1.free;
  979. datasource1.free;
  980. end;
  981. end;
  982. procedure TTestDBBasics.TestStringFilter;
  983. var tel : byte;
  984. begin
  985. with DBConnector.GetNDataset(15) do
  986. begin
  987. Open;
  988. //FilterOptions := [foNoPartialCompare];
  989. //FilterOptions := [];
  990. Filter := '(name=''*Name3'')';
  991. Filtered := True;
  992. AssertFalse(EOF);
  993. AssertEquals(3,FieldByName('ID').asinteger);
  994. AssertEquals('TestName3',FieldByName('NAME').asstring);
  995. next;
  996. AssertTrue(EOF);
  997. Close;
  998. end;
  999. end;
  1000. procedure TTestBufDatasetDBBasics.TestAddIndexFieldType(AFieldType: TFieldType; ActiveDS : boolean);
  1001. var ds : TCustomBufDataset;
  1002. FList : TStringList;
  1003. LastValue : Variant;
  1004. StrValue : String;
  1005. begin
  1006. ds := DBConnector.GetFieldDataset as TCustomBufDataset;
  1007. with ds do
  1008. begin
  1009. if not ActiveDS then
  1010. begin
  1011. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  1012. IndexName:='testindex';
  1013. end
  1014. else
  1015. MaxIndexesCount := 3;
  1016. try
  1017. open;
  1018. except
  1019. if not assigned(ds.FindField('F'+FieldTypeNames[AfieldType])) then
  1020. Ignore('Fields of the type ' + FieldTypeNames[AfieldType] + ' are not supported by this type of dataset')
  1021. else
  1022. raise;
  1023. end;
  1024. if ActiveDS then
  1025. begin
  1026. if not assigned(ds.FindField('F'+FieldTypeNames[AfieldType])) then
  1027. Ignore('Fields of the type ' + FieldTypeNames[AfieldType] + ' are not supported by this type of dataset');
  1028. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  1029. IndexName:='testindex';
  1030. First;
  1031. end;
  1032. LastValue:=null;
  1033. while not eof do
  1034. begin
  1035. if AFieldType=ftString then
  1036. AssertTrue(AnsiCompareStr(VarToStr(LastValue),VarToStr(FieldByName('F'+FieldTypeNames[AfieldType]).AsString))<=0)
  1037. else
  1038. AssertTrue(LastValue<=FieldByName('F'+FieldTypeNames[AfieldType]).AsVariant);
  1039. LastValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsVariant;
  1040. Next;
  1041. end;
  1042. while not bof do
  1043. begin
  1044. if AFieldType=ftString then
  1045. AssertTrue(AnsiCompareStr(VarToStr(LastValue),VarToStr(FieldByName('F'+FieldTypeNames[AfieldType]).AsString))>=0)
  1046. else
  1047. AssertTrue(LastValue>=FieldByName('F'+FieldTypeNames[AfieldType]).AsVariant);
  1048. LastValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsVariant;
  1049. Prior;
  1050. end;
  1051. end;
  1052. end;
  1053. procedure TTestBufDatasetDBBasics.TestAddIndexSmallInt;
  1054. begin
  1055. TestAddIndexFieldType(ftSmallint,False);
  1056. end;
  1057. procedure TTestBufDatasetDBBasics.TestAddIndexBoolean;
  1058. begin
  1059. TestAddIndexFieldType(ftBoolean,False);
  1060. end;
  1061. procedure TTestBufDatasetDBBasics.TestAddIndexFloat;
  1062. begin
  1063. TestAddIndexFieldType(ftFloat,False);
  1064. end;
  1065. procedure TTestBufDatasetDBBasics.TestAddIndexInteger;
  1066. begin
  1067. TestAddIndexFieldType(ftInteger,False);
  1068. end;
  1069. procedure TTestBufDatasetDBBasics.TestAddIndexLargeInt;
  1070. begin
  1071. TestAddIndexFieldType(ftLargeint,False);
  1072. end;
  1073. procedure TTestBufDatasetDBBasics.TestAddIndexDateTime;
  1074. begin
  1075. TestAddIndexFieldType(ftDateTime,False);
  1076. end;
  1077. procedure TTestBufDatasetDBBasics.TestAddIndexCurrency;
  1078. begin
  1079. TestAddIndexFieldType(ftCurrency,False);
  1080. end;
  1081. procedure TTestBufDatasetDBBasics.TestAddIndexBCD;
  1082. begin
  1083. TestAddIndexFieldType(ftBCD,False);
  1084. end;
  1085. procedure TTestBufDatasetDBBasics.TestAddIndex;
  1086. var ds : TCustomBufDataset;
  1087. AFieldType : TFieldType;
  1088. FList : TStringList;
  1089. i : integer;
  1090. begin
  1091. ds := DBConnector.GetFieldDataset as TCustomBufDataset;
  1092. with ds do
  1093. begin
  1094. AFieldType:=ftString;
  1095. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  1096. FList := TStringList.Create;
  1097. FList.Sorted:=true;
  1098. FList.CaseSensitive:=True;
  1099. FList.Duplicates:=dupAccept;
  1100. open;
  1101. while not eof do
  1102. begin
  1103. flist.Add(FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1104. Next;
  1105. end;
  1106. IndexName:='testindex';
  1107. first;
  1108. i:=0;
  1109. while not eof do
  1110. begin
  1111. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1112. inc(i);
  1113. Next;
  1114. end;
  1115. while not bof do
  1116. begin
  1117. dec(i);
  1118. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1119. Prior;
  1120. end;
  1121. end;
  1122. end;
  1123. procedure TTestBufDatasetDBBasics.TestAddDescIndex;
  1124. var ds : TCustomBufDataset;
  1125. AFieldType : TFieldType;
  1126. FList : TStringList;
  1127. i : integer;
  1128. begin
  1129. ds := DBConnector.GetFieldDataset as TCustomBufDataset;
  1130. with ds do
  1131. begin
  1132. AFieldType:=ftString;
  1133. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[],'F'+FieldTypeNames[AfieldType]);
  1134. FList := TStringList.Create;
  1135. FList.Sorted:=true;
  1136. FList.CaseSensitive:=True;
  1137. FList.Duplicates:=dupAccept;
  1138. open;
  1139. while not eof do
  1140. begin
  1141. flist.Add(FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1142. Next;
  1143. end;
  1144. IndexName:='testindex';
  1145. first;
  1146. i:=FList.Count-1;
  1147. while not eof do
  1148. begin
  1149. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1150. dec(i);
  1151. Next;
  1152. end;
  1153. while not bof do
  1154. begin
  1155. inc(i);
  1156. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1157. Prior;
  1158. end;
  1159. end;
  1160. end;
  1161. procedure TTestBufDatasetDBBasics.TestAddCaseInsIndex;
  1162. var ds : TCustomBufDataset;
  1163. AFieldType : TFieldType;
  1164. FList : TStringList;
  1165. i : integer;
  1166. begin
  1167. ds := DBConnector.GetFieldDataset as TCustomBufDataset;
  1168. with ds do
  1169. begin
  1170. AFieldType:=ftString;
  1171. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[],'','F'+FieldTypeNames[AfieldType]);
  1172. FList := TStringList.Create;
  1173. FList.Sorted:=true;
  1174. FList.Duplicates:=dupAccept;
  1175. open;
  1176. while not eof do
  1177. begin
  1178. flist.Add(FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1179. Next;
  1180. end;
  1181. IndexName:='testindex';
  1182. first;
  1183. i:=0;
  1184. while not eof do
  1185. begin
  1186. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1187. inc(i);
  1188. Next;
  1189. end;
  1190. while not bof do
  1191. begin
  1192. dec(i);
  1193. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1194. Prior;
  1195. end;
  1196. end;
  1197. end;
  1198. procedure TTestBufDatasetDBBasics.TestInactSwitchIndex;
  1199. // Test if the default-index is properly build when the active index is not
  1200. // the default-index while opening then dataset
  1201. var ds : TCustomBufDataset;
  1202. AFieldType : TFieldType;
  1203. i : integer;
  1204. begin
  1205. ds := DBConnector.GetFieldDataset as TCustomBufDataset;
  1206. with ds do
  1207. begin
  1208. AFieldType:=ftString;
  1209. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  1210. IndexName:='testindex';
  1211. open;
  1212. IndexName:=''; // This should set the default index (default_order)
  1213. first;
  1214. i := 0;
  1215. while not eof do
  1216. begin
  1217. AssertEquals(testStringValues[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1218. inc(i);
  1219. Next;
  1220. end;
  1221. end;
  1222. end;
  1223. procedure TTestBufDatasetDBBasics.TestAddIndexActiveDS;
  1224. var ds : TCustomBufDataset;
  1225. I : integer;
  1226. begin
  1227. TestAddIndexFieldType(ftString,true);
  1228. end;
  1229. procedure TTestBufDatasetDBBasics.TestAddIndexEditDS;
  1230. var ds : TCustomBufDataset;
  1231. I : integer;
  1232. LastValue : String;
  1233. begin
  1234. ds := DBConnector.GetNDataset(True,5) as TCustomBufDataset;
  1235. with ds do
  1236. begin
  1237. MaxIndexesCount:=3;
  1238. open;
  1239. edit;
  1240. FieldByName('name').asstring := 'Zz';
  1241. post;
  1242. next;
  1243. next;
  1244. edit;
  1245. FieldByName('name').asstring := 'aA';
  1246. post;
  1247. AddIndex('test','name',[]);
  1248. first;
  1249. ds.IndexName:='test';
  1250. first;
  1251. LastValue:=FieldByName('name').AsString;
  1252. while not eof do
  1253. begin
  1254. AssertTrue(AnsiCompareStr(LastValue,FieldByName('name').AsString)<=0);
  1255. Next;
  1256. end;
  1257. end;
  1258. end;
  1259. procedure TTestBufDatasetDBBasics.TestIndexFieldNamesAct;
  1260. var ds : TCustomBufDataset;
  1261. AFieldType : TFieldType;
  1262. FList : TStringList;
  1263. i : integer;
  1264. begin
  1265. ds := DBConnector.GetFieldDataset as TCustomBufDataset;
  1266. with ds do
  1267. begin
  1268. AFieldType:=ftString;
  1269. FList := TStringList.Create;
  1270. FList.Sorted:=true;
  1271. FList.CaseSensitive:=True;
  1272. FList.Duplicates:=dupAccept;
  1273. open;
  1274. while not eof do
  1275. begin
  1276. flist.Add(FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1277. Next;
  1278. end;
  1279. IndexFieldNames:='F'+FieldTypeNames[AfieldType];
  1280. first;
  1281. i:=0;
  1282. while not eof do
  1283. begin
  1284. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1285. inc(i);
  1286. Next;
  1287. end;
  1288. while not bof do
  1289. begin
  1290. dec(i);
  1291. AssertEquals(flist[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1292. Prior;
  1293. end;
  1294. AssertEquals('F'+FieldTypeNames[AfieldType],IndexFieldNames);
  1295. IndexFieldNames:='ID';
  1296. first;
  1297. i:=0;
  1298. while not eof do
  1299. begin
  1300. AssertEquals(testStringValues[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1301. inc(i);
  1302. Next;
  1303. end;
  1304. AssertEquals('ID',IndexFieldNames);
  1305. IndexFieldNames:='';
  1306. first;
  1307. i:=0;
  1308. while not eof do
  1309. begin
  1310. AssertEquals(testStringValues[i],FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1311. inc(i);
  1312. Next;
  1313. end;
  1314. AssertEquals('',IndexFieldNames);
  1315. end;
  1316. end;
  1317. procedure TTestBufDatasetDBBasics.TestIndexCurRecord;
  1318. // Test if the currentrecord stays the same after an index change
  1319. var ds : TCustomBufDataset;
  1320. AFieldType : TFieldType;
  1321. i : integer;
  1322. OldID : Integer;
  1323. OldStringValue : string;
  1324. begin
  1325. ds := DBConnector.GetFieldDataset as TCustomBufDataset;
  1326. with ds do
  1327. begin
  1328. AFieldType:=ftString;
  1329. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  1330. open;
  1331. for i := 0 to (testValuesCount div 3) do
  1332. Next;
  1333. OldID:=FieldByName('id').AsInteger;
  1334. OldStringValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsString;
  1335. IndexName:='testindex';
  1336. AssertEquals(OldID,FieldByName('id').AsInteger);
  1337. AssertEquals(OldStringValue,FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1338. next;
  1339. AssertTrue(OldStringValue<=FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1340. prior;
  1341. prior;
  1342. AssertTrue(OldStringValue>=FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1343. OldID:=FieldByName('id').AsInteger;
  1344. OldStringValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsString;
  1345. IndexName:='';
  1346. AssertEquals(OldID,FieldByName('id').AsInteger);
  1347. AssertEquals(OldStringValue,FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1348. next;
  1349. AssertEquals(OldID+1,FieldByName('ID').AsInteger);
  1350. prior;
  1351. prior;
  1352. AssertEquals(OldID-1,FieldByName('ID').AsInteger);
  1353. end;
  1354. end;
  1355. procedure TTestBufDatasetDBBasics.TestAddDblIndex;
  1356. var ds : TCustomBufDataset;
  1357. LastInteger : Integer;
  1358. LastString : string;
  1359. begin
  1360. ds := DBConnector.GetFieldDataset as TCustomBufDataset;
  1361. with ds do
  1362. begin
  1363. AddIndex('testindex','F'+FieldTypeNames[ftString]+';F'+FieldTypeNames[ftInteger],[]);
  1364. open;
  1365. IndexName:='testindex';
  1366. first;
  1367. LastString:='';
  1368. while not eof do
  1369. begin
  1370. AssertTrue(AnsiCompareStr(FieldByName('F'+FieldTypeNames[ftString]).AsString,LastString)>=0);
  1371. LastString:= FieldByName('F'+FieldTypeNames[ftString]).AsString;
  1372. LastInteger:=-MaxInt;
  1373. while (FieldByName('F'+FieldTypeNames[ftString]).AsString=LastString) and not eof do
  1374. begin
  1375. AssertTrue(FieldByName('F'+FieldTypeNames[ftInteger]).AsInteger>=LastInteger);
  1376. LastInteger:=FieldByName('F'+FieldTypeNames[ftInteger]).AsInteger;
  1377. next;
  1378. end;
  1379. end;
  1380. while not bof do
  1381. begin
  1382. AssertTrue(AnsiCompareStr(FieldByName('F'+FieldTypeNames[ftString]).AsString,LastString)<=0);
  1383. LastString:= FieldByName('F'+FieldTypeNames[ftString]).AsString;
  1384. LastInteger:=+MaxInt;
  1385. while (FieldByName('F'+FieldTypeNames[ftString]).AsString=LastString) and not bof do
  1386. begin
  1387. AssertTrue(FieldByName('F'+FieldTypeNames[ftInteger]).AsInteger<=LastInteger);
  1388. LastInteger:=FieldByName('F'+FieldTypeNames[ftInteger]).AsInteger;
  1389. prior;
  1390. end;
  1391. end;
  1392. end;
  1393. end;
  1394. procedure TTestBufDatasetDBBasics.TestIndexEditRecord;
  1395. var ds : TCustomBufDataset;
  1396. AFieldType : TFieldType;
  1397. i : integer;
  1398. OldID : Integer;
  1399. OldStringValue : string;
  1400. begin
  1401. ds := DBConnector.GetFieldDataset as TCustomBufDataset;
  1402. with ds do
  1403. begin
  1404. AFieldType:=ftString;
  1405. AddIndex('testindex','F'+FieldTypeNames[AfieldType],[]);
  1406. IndexName:='testindex';
  1407. open;
  1408. OldStringValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsString;
  1409. next;
  1410. AssertTrue(OldStringValue<=FieldByName('F'+FieldTypeNames[AfieldType]).AsString);
  1411. OldStringValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsString;
  1412. next;
  1413. AssertTrue(AnsiCompareStr(OldStringValue,FieldByName('F'+FieldTypeNames[AfieldType]).AsString)<=0);
  1414. prior;
  1415. edit;
  1416. FieldByName('F'+FieldTypeNames[AfieldType]).AsString := 'ZZZ';
  1417. post;
  1418. prior;
  1419. AssertTrue(AnsiCompareStr('ZZZ',FieldByName('F'+FieldTypeNames[AfieldType]).AsString)>=0);
  1420. next;
  1421. next;
  1422. AssertTrue(AnsiCompareStr('ZZZ',FieldByName('F'+FieldTypeNames[AfieldType]).AsString)<=0);
  1423. close;
  1424. end;
  1425. end;
  1426. procedure TTestBufDatasetDBBasics.TestIndexFieldNames;
  1427. var ds : TCustomBufDataset;
  1428. AFieldType : TFieldType;
  1429. PrevValue : String;
  1430. begin
  1431. ds := DBConnector.GetFieldDataset as TCustomBufDataset;
  1432. with ds do
  1433. begin
  1434. AFieldType:=ftString;
  1435. IndexFieldNames:='F'+FieldTypeNames[AfieldType];
  1436. open;
  1437. PrevValue:='';
  1438. while not eof do
  1439. begin
  1440. AssertTrue(AnsiCompareStr(FieldByName('F'+FieldTypeNames[AfieldType]).AsString,PrevValue)>=0);
  1441. PrevValue:=FieldByName('F'+FieldTypeNames[AfieldType]).AsString;
  1442. Next;
  1443. end;
  1444. AssertEquals('F'+FieldTypeNames[AfieldType],IndexFieldNames);
  1445. end;
  1446. end;
  1447. procedure TTestDBBasics.TestcalculatedField_OnCalcfields(DataSet: TDataSet);
  1448. begin
  1449. case dataset.fieldbyname('ID').asinteger of
  1450. 1 : dataset.fieldbyname('CALCFLD').AsInteger := 5;
  1451. 2 : dataset.fieldbyname('CALCFLD').AsInteger := 70000;
  1452. 3 : dataset.fieldbyname('CALCFLD').Clear;
  1453. 4 : dataset.fieldbyname('CALCFLD').AsInteger := 1234;
  1454. 10 : dataset.fieldbyname('CALCFLD').Clear;
  1455. else
  1456. dataset.fieldbyname('CALCFLD').AsInteger := 1;
  1457. end;
  1458. end;
  1459. procedure TTestDBBasics.TestCalculatedField;
  1460. var ds : TDataset;
  1461. AFld1, AFld2, AFld3 : Tfield;
  1462. begin
  1463. ds := DBConnector.GetNDataset(5);
  1464. with ds do
  1465. begin
  1466. AFld1 := TIntegerField.Create(ds);
  1467. AFld1.FieldName := 'ID';
  1468. AFld1.DataSet := ds;
  1469. AFld2 := TStringField.Create(ds);
  1470. AFld2.FieldName := 'NAME';
  1471. AFld2.DataSet := ds;
  1472. AFld3 := TIntegerField.Create(ds);
  1473. AFld3.FieldName := 'CALCFLD';
  1474. AFld3.DataSet := ds;
  1475. Afld3.FieldKind := fkCalculated;
  1476. AssertEquals(3,FieldCount);
  1477. ds.OnCalcFields := TestcalculatedField_OnCalcfields;
  1478. open;
  1479. AssertEquals(1,FieldByName('ID').asinteger);
  1480. AssertEquals(5,FieldByName('CALCFLD').asinteger);
  1481. next;
  1482. AssertEquals(70000,FieldByName('CALCFLD').asinteger);
  1483. next;
  1484. AssertEquals(true,FieldByName('CALCFLD').isnull);
  1485. next;
  1486. AssertEquals(1234,FieldByName('CALCFLD').AsInteger);
  1487. edit;
  1488. FieldByName('ID').AsInteger := 10;
  1489. post;
  1490. AssertEquals(true,FieldByName('CALCFLD').isnull);
  1491. close;
  1492. AFld1.Free;
  1493. AFld2.Free;
  1494. AFld3.Free;
  1495. end;
  1496. end;
  1497. procedure TTestDBBasics.TestEofAfterFirst;
  1498. begin
  1499. with DBConnector.GetNDataset(0) do
  1500. begin
  1501. open;
  1502. AssertTrue(eof);
  1503. AssertTrue(BOF);
  1504. first;
  1505. AssertTrue(eof);
  1506. AssertTrue(BOF);
  1507. end;
  1508. end;
  1509. procedure TTestDBBasics.TestfieldDefinition(AFieldType : TFieldType;ADatasize : integer;var ADS : TDataset; var AFld: TField);
  1510. var i : byte;
  1511. begin
  1512. ADS := DBConnector.GetFieldDataset;
  1513. ADS.Open;
  1514. AFld := ADS.FindField('F'+FieldTypeNames[AfieldType]);
  1515. if not assigned (AFld) then
  1516. Ignore('Fields of the type ' + FieldTypeNames[AfieldType] + ' are not supported by this type of dataset');
  1517. AssertTrue(Afld.DataType = AFieldType);
  1518. AssertEquals(ADatasize,Afld.DataSize );
  1519. end;
  1520. procedure TTestDBBasics.TestSupportIntegerFields;
  1521. var i : byte;
  1522. ds : TDataset;
  1523. Fld : TField;
  1524. begin
  1525. TestfieldDefinition(ftInteger,4,ds,Fld);
  1526. for i := 0 to testValuesCount-1 do
  1527. begin
  1528. AssertEquals(testIntValues[i],Fld.AsInteger);
  1529. ds.Next;
  1530. end;
  1531. ds.close;
  1532. end;
  1533. procedure TTestDBBasics.TestSupportSmallIntFields;
  1534. var i : byte;
  1535. ds : TDataset;
  1536. Fld : TField;
  1537. begin
  1538. TestfieldDefinition(ftSmallint,2,ds,Fld);
  1539. for i := 0 to testValuesCount-1 do
  1540. begin
  1541. AssertEquals(testSmallIntValues[i],Fld.AsInteger);
  1542. ds.Next;
  1543. end;
  1544. ds.close;
  1545. end;
  1546. procedure TTestDBBasics.TestSupportStringFields;
  1547. var i : byte;
  1548. ds : TDataset;
  1549. Fld : TField;
  1550. begin
  1551. TestfieldDefinition(ftString,11,ds,Fld);
  1552. for i := 0 to testValuesCount-1 do
  1553. begin
  1554. AssertEquals(testStringValues[i],Fld.AsString);
  1555. ds.Next;
  1556. end;
  1557. ds.close;
  1558. end;
  1559. procedure TTestDBBasics.TestSupportBooleanFields;
  1560. var i : byte;
  1561. ds : TDataset;
  1562. Fld : TField;
  1563. begin
  1564. TestfieldDefinition(ftBoolean,2,ds,Fld);
  1565. for i := 0 to testValuesCount-1 do
  1566. begin
  1567. AssertEquals(testBooleanValues[i],Fld.AsBoolean);
  1568. ds.Next;
  1569. end;
  1570. ds.close;
  1571. end;
  1572. procedure TTestDBBasics.TestSupportFloatFields;
  1573. var i : byte;
  1574. ds : TDataset;
  1575. Fld : TField;
  1576. begin
  1577. TestfieldDefinition(ftFloat,8,ds,Fld);
  1578. for i := 0 to testValuesCount-1 do
  1579. begin
  1580. AssertEquals(testFloatValues[i],Fld.AsFloat);
  1581. ds.Next;
  1582. end;
  1583. ds.close;
  1584. end;
  1585. procedure TTestDBBasics.TestSupportLargeIntFields;
  1586. var i : byte;
  1587. ds : TDataset;
  1588. Fld : TField;
  1589. begin
  1590. TestfieldDefinition(ftLargeint,8,ds,Fld);
  1591. for i := 0 to testValuesCount-1 do
  1592. begin
  1593. AssertEquals(testLargeIntValues[i],Fld.AsLargeInt);
  1594. ds.Next;
  1595. end;
  1596. ds.close;
  1597. end;
  1598. procedure TTestDBBasics.TestSupportDateFields;
  1599. var i : byte;
  1600. ds : TDataset;
  1601. Fld : TField;
  1602. begin
  1603. TestfieldDefinition(ftDate,8,ds,Fld);
  1604. for i := 0 to testValuesCount-1 do
  1605. begin
  1606. AssertEquals(testDateValues[i],FormatDateTime('yyyy/mm/dd',Fld.AsDateTime));
  1607. ds.Next;
  1608. end;
  1609. ds.close;
  1610. end;
  1611. procedure TTestDBBasics.TestSupportCurrencyFields;
  1612. var i : byte;
  1613. ds : TDataset;
  1614. Fld : TField;
  1615. begin
  1616. TestfieldDefinition(ftCurrency,8,ds,Fld);
  1617. for i := 0 to testValuesCount-1 do
  1618. begin
  1619. AssertEquals(testCurrencyValues[i],Fld.AsCurrency);
  1620. AssertEquals(testCurrencyValues[i],Fld.AsFloat);
  1621. ds.Next;
  1622. end;
  1623. ds.close;
  1624. end;
  1625. procedure TTestDBBasics.TestSupportBCDFields;
  1626. var i : byte;
  1627. ds : TDataset;
  1628. Fld : TField;
  1629. begin
  1630. TestfieldDefinition(ftBCD,8,ds,Fld);
  1631. for i := 0 to testValuesCount-1 do
  1632. begin
  1633. AssertEquals(CurrToStr(testCurrencyValues[i]),Fld.AsString);
  1634. AssertEquals(testCurrencyValues[i],Fld.AsCurrency);
  1635. AssertEquals(testCurrencyValues[i],Fld.AsFloat);
  1636. ds.Next;
  1637. end;
  1638. ds.close;
  1639. end;
  1640. procedure TTestDBBasics.TestSupportFixedStringFields;
  1641. var i : byte;
  1642. ds : TDataset;
  1643. Fld : TField;
  1644. begin
  1645. TestfieldDefinition(ftFixedChar,10,ds,Fld);
  1646. for i := 0 to testValuesCount-1 do
  1647. begin
  1648. if Fld.IsNull then // If the field is null, .AsString always returns an empty, non-padded string
  1649. AssertEquals(testStringValues[i],Fld.AsString)
  1650. else
  1651. AssertEquals(PadRight(testStringValues[i],10),Fld.AsString);
  1652. ds.Next;
  1653. end;
  1654. ds.close;
  1655. end;
  1656. procedure TTestDBBasics.TestDoubleClose;
  1657. begin
  1658. with DBConnector.GetNDataset(1) do
  1659. begin
  1660. close;
  1661. close;
  1662. open;
  1663. close;
  1664. close;
  1665. end;
  1666. end;
  1667. procedure TTestDBBasics.TestAssignFieldftString;
  1668. var AParam : TParam;
  1669. AField : TField;
  1670. begin
  1671. AParam := TParam.Create(nil);
  1672. with DBConnector.GetNDataset(1) do
  1673. begin
  1674. open;
  1675. AField := fieldbyname('name');
  1676. AParam.AssignField(AField);
  1677. AssertTrue(ftString=AParam.DataType);
  1678. close;
  1679. end;
  1680. AParam.Free;
  1681. end;
  1682. procedure TTestDBBasics.TestAssignFieldftFixedChar;
  1683. var AParam : TParam;
  1684. AField : TField;
  1685. begin
  1686. AParam := TParam.Create(nil);
  1687. with DBConnector.GetNDataset(1) do
  1688. begin
  1689. open;
  1690. AField := fieldbyname('name');
  1691. (AField as tstringfield).FixedChar := true;
  1692. AParam.AssignField(AField);
  1693. AssertTrue(ftFixedChar=AParam.DataType);
  1694. close;
  1695. end;
  1696. AParam.Free;
  1697. end;
  1698. procedure TTestBufDatasetDBBasics.TestBufDatasetCancelUpd;
  1699. var i : byte;
  1700. begin
  1701. with DBConnector.GetNDataset(5) as TCustomBufDataset do
  1702. begin
  1703. open;
  1704. next;
  1705. next;
  1706. edit;
  1707. FieldByName('name').AsString := 'changed';
  1708. post;
  1709. next;
  1710. delete;
  1711. CancelUpdates;
  1712. First;
  1713. for i := 1 to 5 do
  1714. begin
  1715. AssertEquals(i,fields[0].AsInteger);
  1716. AssertEquals('TestName'+inttostr(i),fields[1].AsString);
  1717. Next;
  1718. end;
  1719. end;
  1720. end;
  1721. procedure TTestDBBasics.Testbug7007;
  1722. var
  1723. datalink1: tdatalink;
  1724. datasource1: tdatasource;
  1725. query1: TDataSet;
  1726. begin
  1727. query1:= DBConnector.GetNDataset(6);
  1728. datalink1:= TTestDataLink.create;
  1729. datasource1:= tdatasource.create(nil);
  1730. try
  1731. datalink1.datasource:= datasource1;
  1732. datasource1.dataset:= query1;
  1733. datalink1.datasource:= datasource1;
  1734. DataEvents := '';
  1735. query1.open;
  1736. datalink1.buffercount:= query1.recordcount;
  1737. AssertEquals('deUpdateState:0;',DataEvents);
  1738. AssertEquals(0, datalink1.ActiveRecord);
  1739. AssertEquals(6, datalink1.RecordCount);
  1740. AssertEquals(6, query1.RecordCount);
  1741. AssertEquals(1, query1.RecNo);
  1742. DataEvents := '';
  1743. query1.append;
  1744. AssertEquals('deCheckBrowseMode:0;deUpdateState:0;deDataSetChange:0;',DataEvents);
  1745. AssertEquals(5, datalink1.ActiveRecord);
  1746. AssertEquals(6, datalink1.RecordCount);
  1747. AssertEquals(6, query1.RecordCount);
  1748. AssertTrue(query1.RecNo in [0,7]);
  1749. DataEvents := '';
  1750. query1.cancel;
  1751. AssertEquals('deCheckBrowseMode:0;deUpdateState:0;deDataSetChange:0;',DataEvents);
  1752. AssertEquals(5, datalink1.ActiveRecord);
  1753. AssertEquals(6, datalink1.RecordCount);
  1754. AssertEquals(6, query1.RecordCount);
  1755. AssertEquals(6, query1.RecNo);
  1756. finally
  1757. datalink1.free;
  1758. datasource1.free;
  1759. end;
  1760. end;
  1761. procedure TTestDBBasics.TestBug6893;
  1762. var
  1763. datalink1: tdatalink;
  1764. datasource1: tdatasource;
  1765. query1: TDataSet;
  1766. begin
  1767. query1:= DBConnector.GetNDataset(25);
  1768. datalink1:= TDataLink.create;
  1769. datasource1:= tdatasource.create(nil);
  1770. try
  1771. datalink1.datasource:= datasource1;
  1772. datasource1.dataset:= query1;
  1773. datalink1.buffercount:= 5;
  1774. query1.active := true;
  1775. query1.MoveBy(20);
  1776. AssertEquals(5, THackDataLink(datalink1).Firstrecord);
  1777. AssertEquals(4, datalink1.ActiveRecord);
  1778. AssertEquals(21, query1.RecNo);
  1779. query1.active := False;
  1780. AssertEquals(0, THackDataLink(datalink1).Firstrecord);
  1781. AssertEquals(0, datalink1.ActiveRecord);
  1782. query1.active := true;
  1783. AssertEquals(0, THackDataLink(datalink1).Firstrecord);
  1784. AssertEquals(0, datalink1.ActiveRecord);
  1785. AssertEquals(1, query1.RecNo);
  1786. finally
  1787. datalink1.free;
  1788. datasource1.free;
  1789. end;
  1790. end;
  1791. procedure TTestBufDatasetDBBasics.TestBufDatasetCancelUpd1;
  1792. var i : byte;
  1793. begin
  1794. with DBConnector.GetNDataset(5) as TCustomBufDataset do
  1795. begin
  1796. open;
  1797. next;
  1798. next;
  1799. delete;
  1800. insert;
  1801. FieldByName('id').AsInteger := 100;
  1802. post;
  1803. CancelUpdates;
  1804. last;
  1805. for i := 5 downto 1 do
  1806. begin
  1807. AssertEquals(i,fields[0].AsInteger);
  1808. AssertEquals('TestName'+inttostr(i),fields[1].AsString);
  1809. Prior;
  1810. end;
  1811. end;
  1812. end;
  1813. procedure TTestBufDatasetDBBasics.TestMultipleDeleteUpdateBuffer;
  1814. var ds : TDataset;
  1815. begin
  1816. ds := DBConnector.GetNDataset(true,5);
  1817. ds.open;
  1818. with TCustomBufDataset(ds) do
  1819. begin
  1820. AssertEquals(0,ChangeCount);
  1821. edit;
  1822. fieldbyname('id').asinteger := 500;
  1823. fieldbyname('name').AsString := 'JoJo';
  1824. post;
  1825. AssertEquals(1,ChangeCount);
  1826. next; next;
  1827. Delete;
  1828. AssertEquals(2,ChangeCount);
  1829. Delete;
  1830. AssertEquals(3,ChangeCount);
  1831. CancelUpdates;
  1832. end;
  1833. ds.close;
  1834. end;
  1835. procedure TTestBufDatasetDBBasics.TestDoubleDelete;
  1836. var ds : TCustomBufDataset;
  1837. begin
  1838. ds := TCustomBufDataset(DBConnector.GetNDataset(true,5));
  1839. with ds do
  1840. begin
  1841. open;
  1842. next; next;
  1843. Delete;
  1844. Delete;
  1845. first;
  1846. AssertEquals(1,fieldbyname('id').AsInteger);
  1847. next;
  1848. AssertEquals(2,fieldbyname('id').AsInteger);
  1849. next;
  1850. AssertEquals(5,fieldbyname('id').AsInteger);
  1851. CancelUpdates;
  1852. first;
  1853. AssertEquals(1,fieldbyname('id').AsInteger);
  1854. next;
  1855. AssertEquals(2,fieldbyname('id').AsInteger);
  1856. next;
  1857. AssertEquals(3,fieldbyname('id').AsInteger);
  1858. next;
  1859. AssertEquals(4,fieldbyname('id').AsInteger);
  1860. next;
  1861. AssertEquals(5,fieldbyname('id').AsInteger);
  1862. end;
  1863. end;
  1864. procedure TTestDBBasics.TestNullAtOpen;
  1865. begin
  1866. with dbconnector.getndataset(0) do
  1867. begin
  1868. active:= true;
  1869. AssertTrue('Field isn''t NULL on a just-opened empty dataset',fieldbyname('id').IsNull);
  1870. append;
  1871. AssertTrue('Field isn''t NULL after append on an empty dataset',fieldbyname('id').IsNull);
  1872. fieldbyname('id').asinteger:= 123;
  1873. cancel;
  1874. AssertTrue('Field isn''t NULL after cancel',fieldbyname('id').IsNull);
  1875. end;
  1876. end;
  1877. { TDBBasicsUniDirectionalTestSetup }
  1878. procedure TDBBasicsUniDirectionalTestSetup.OneTimeSetup;
  1879. begin
  1880. inherited OneTimeSetup;
  1881. DBConnector.TestUniDirectional:=true;
  1882. end;
  1883. procedure TDBBasicsUniDirectionalTestSetup.OneTimeTearDown;
  1884. begin
  1885. DBConnector.TestUniDirectional:=false;
  1886. inherited OneTimeTearDown;
  1887. end;
  1888. { TTestBufDatasetDBBasics }
  1889. procedure TTestBufDatasetDBBasics.SetUp;
  1890. begin
  1891. DBConnector.StartTest;
  1892. end;
  1893. procedure TTestBufDatasetDBBasics.TearDown;
  1894. begin
  1895. DBConnector.StopTest;
  1896. end;
  1897. initialization
  1898. RegisterTestDecorator(TDBBasicsTestSetup, TTestDBBasics);
  1899. if uppercase(dbconnectorname)='SQL' then
  1900. begin
  1901. RegisterTestDecorator(TDBBasicsTestSetup, TTestBufDatasetDBBasics);
  1902. RegisterTestDecorator(TDBBasicsUniDirectionalTestSetup, TTestUniDirectionalDBBasics);
  1903. end;
  1904. end.