testdbbasics.pas 52 KB

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