testdbbasics.pas 68 KB

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