testdbbasics.pas 59 KB

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