testdbbasics.pas 54 KB

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