testdbbasics.pas 61 KB

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