testdbbasics.pas 75 KB

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