testdbbasics.pas 51 KB

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