testdbbasics.pas 73 KB

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