testdbbasics.pas 58 KB

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