testdbbasics.pas 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. unit TestDBBasics;
  2. {$IFDEF FPC}
  3. {$mode Delphi}{$H+}
  4. {$ENDIF}
  5. interface
  6. uses
  7. fpcunit, testutils, testregistry, testdecorator,
  8. Classes, SysUtils, db;
  9. type
  10. { TTestDBBasics }
  11. TTestDBBasics = class(TTestCase)
  12. private
  13. procedure TestOnFilterProc(DataSet: TDataSet; var Accept: Boolean);
  14. procedure TestfieldDefinition(AFieldType : TFieldType;ADatasize : integer;var ADS : TDataset; var AFld: TField);
  15. procedure TestcalculatedField_OnCalcfields(DataSet: TDataSet);
  16. protected
  17. procedure SetUp; override;
  18. procedure TearDown; override;
  19. published
  20. procedure TestIntFilter;
  21. procedure TestOnFilter;
  22. procedure TestStringFilter;
  23. procedure TestNullAtOpen;
  24. procedure TestSupportIntegerFields;
  25. procedure TestSupportSmallIntFields;
  26. procedure TestSupportStringFields;
  27. procedure TestSupportBooleanFields;
  28. procedure TestSupportFloatFields;
  29. procedure TestSupportLargeIntFields;
  30. procedure TestSupportDateFields;
  31. procedure TestSupportCurrencyFields;
  32. procedure TestSupportBCDFields;
  33. procedure TestIsEmpty;
  34. procedure TestAppendOnEmptyDataset;
  35. procedure TestInsertOnEmptyDataset;
  36. procedure TestBufDatasetCancelUpd; //bug 6938
  37. procedure TestEofAfterFirst; //bug 7211
  38. procedure TestBufDatasetCancelUpd1;
  39. procedure TestDoubleClose;
  40. procedure TestCalculatedField;
  41. procedure TestAssignFieldftString;
  42. procedure TestAssignFieldftFixedChar;
  43. procedure TestSelectQueryBasics;
  44. procedure TestPostOnlyInEditState;
  45. procedure TestMove; // bug 5048
  46. procedure TestActiveBufferWhenClosed;
  47. procedure TestEOFBOFClosedDataset;
  48. procedure TestDataEventsResync;
  49. procedure TestBug7007;
  50. procedure TestBug6893;
  51. procedure TestRecordcountAfterReopen; // partly bug 8228
  52. procedure TestdeFieldListChange;
  53. procedure TestLastAppendCancel; // bug 5058
  54. procedure TestRecNo; // bug 5061
  55. procedure TestSetRecNo; // bug 6919
  56. procedure TestRequired;
  57. end;
  58. { TSQLTestSetup }
  59. TDBBasicsTestSetup = class(TTestSetup)
  60. protected
  61. procedure OneTimeSetup; override;
  62. procedure OneTimeTearDown; override;
  63. end;
  64. implementation
  65. uses toolsunit, bufdataset;
  66. type THackDataLink=class(TdataLink);
  67. procedure TTestDBBasics.TestIsEmpty;
  68. begin
  69. if not (DBConnector.GetNDataset(5) is TBufDataset) then
  70. Ignore('This test only applies to TBufDataset and descendents.');
  71. with tbufdataset(DBConnector.GetNDataset(True,1)) do
  72. begin
  73. open;
  74. delete;
  75. refresh;
  76. applyupdates;
  77. AssertTrue(IsEmpty);
  78. end;
  79. end;
  80. procedure TTestDBBasics.TestAppendOnEmptyDataset;
  81. begin
  82. with DBConnector.GetNDataset(0) do
  83. begin
  84. open;
  85. AssertTrue(CanModify);
  86. AssertTrue(eof);
  87. AssertTrue(bof);
  88. append;
  89. AssertFalse(Bof);
  90. AssertTrue(Eof);
  91. post;
  92. AssertFalse(eof);
  93. AssertFalse(bof);
  94. end;
  95. end;
  96. procedure TTestDBBasics.TestInsertOnEmptyDataset;
  97. begin
  98. with DBConnector.GetNDataset(0) do
  99. begin
  100. open;
  101. AssertTrue(CanModify);
  102. AssertTrue(eof);
  103. AssertTrue(bof);
  104. AssertTrue(IsEmpty);
  105. insert;
  106. AssertTrue(Bof);
  107. AssertTrue(Eof);
  108. AssertFalse(IsEmpty);
  109. post;
  110. AssertFalse(IsEmpty);
  111. AssertFalse(eof);
  112. AssertFalse(bof);
  113. end;
  114. end;
  115. procedure TTestDBBasics.TestSelectQueryBasics;
  116. var b : TFieldType;
  117. begin
  118. with DBConnector.GetNDataset(1) do
  119. begin
  120. Open;
  121. AssertEquals(1,RecNo);
  122. AssertEquals(1,RecordCount);
  123. AssertEquals(2,FieldCount);
  124. AssertTrue(CompareText('ID',fields[0].FieldName)=0);
  125. AssertTrue(CompareText('ID',fields[0].DisplayName)=0);
  126. AssertTrue('The datatype of the field ''ID'' is incorrect, it should be ftInteger',ftInteger=fields[0].DataType);
  127. AssertTrue(CompareText('NAME',fields[1].FieldName)=0);
  128. AssertTrue(CompareText('NAME',fields[1].DisplayName)=0);
  129. AssertTrue(ftString=fields[1].DataType);
  130. AssertEquals(1,fields[0].Value);
  131. AssertEquals('TestName1',fields[1].Value);
  132. Close;
  133. end;
  134. end;
  135. procedure TTestDBBasics.TestPostOnlyInEditState;
  136. begin
  137. with DBConnector.GetNDataset(1) do
  138. begin
  139. open;
  140. AssertException('Post was called in a non-edit state',EDatabaseError,Post);
  141. end;
  142. end;
  143. procedure TTestDBBasics.TestMove;
  144. var i,count : integer;
  145. aDatasource : TDataSource;
  146. aDatalink : TDataLink;
  147. ABufferCount : Integer;
  148. begin
  149. aDatasource := TDataSource.Create(nil);
  150. aDatalink := TTestDataLink.Create;
  151. aDatalink.DataSource := aDatasource;
  152. ABufferCount := 11;
  153. aDatalink.BufferCount := ABufferCount;
  154. DataEvents := '';
  155. for count := 0 to 32 do
  156. begin
  157. aDatasource.DataSet := DBConnector.GetNDataset(count);
  158. with aDatasource.Dataset do
  159. begin
  160. i := 1;
  161. Open;
  162. AssertEquals('deUpdateState:0;',DataEvents);
  163. DataEvents := '';
  164. while not EOF do
  165. begin
  166. AssertEquals(i,fields[0].AsInteger);
  167. AssertEquals('TestName'+inttostr(i),fields[1].AsString);
  168. inc(i);
  169. Next;
  170. if (i > ABufferCount) and not EOF then
  171. AssertEquals('deCheckBrowseMode:0;deDataSetScroll:-1;',DataEvents)
  172. else
  173. AssertEquals('deCheckBrowseMode:0;deDataSetScroll:0;',DataEvents);
  174. DataEvents := '';
  175. end;
  176. AssertEquals(count,i-1);
  177. close;
  178. AssertEquals('deUpdateState:0;',DataEvents);
  179. DataEvents := '';
  180. end;
  181. end;
  182. end;
  183. procedure TTestDBBasics.TestdeFieldListChange;
  184. var i,count : integer;
  185. aDatasource : TDataSource;
  186. aDatalink : TDataLink;
  187. ds : TDataset;
  188. begin
  189. aDatasource := TDataSource.Create(nil);
  190. aDatalink := TTestDataLink.Create;
  191. aDatalink.DataSource := aDatasource;
  192. ds := DBConnector.GetNDataset(1);
  193. with ds do
  194. begin
  195. aDatasource.DataSet := ds;
  196. DataEvents := '';
  197. open;
  198. Fields.add(tfield.Create(DBConnector.GetNDataset(1)));
  199. AssertEquals('deUpdateState:0;deFieldListChange:0;',DataEvents);
  200. DataEvents := '';
  201. fields.Clear;
  202. AssertEquals('deFieldListChange:0;',DataEvents)
  203. end;
  204. aDatasource.Free;
  205. aDatalink.Free;
  206. end;
  207. procedure TTestDBBasics.TestActiveBufferWhenClosed;
  208. begin
  209. with DBConnector.GetNDataset(0) do
  210. begin
  211. AssertNull(ActiveBuffer);
  212. open;
  213. AssertFalse('Activebuffer of an empty dataset shouldn''t be nil',ActiveBuffer = nil);
  214. end;
  215. end;
  216. procedure TTestDBBasics.TestEOFBOFClosedDataset;
  217. begin
  218. with DBConnector.GetNDataset(1) do
  219. begin
  220. AssertTrue(EOF);
  221. AssertTrue(BOF);
  222. open;
  223. close;
  224. AssertTrue(EOF);
  225. AssertTrue(BOF);
  226. end;
  227. end;
  228. procedure TTestDBBasics.TestDataEventsResync;
  229. var i,count : integer;
  230. aDatasource : TDataSource;
  231. aDatalink : TDataLink;
  232. ds : tdataset;
  233. begin
  234. aDatasource := TDataSource.Create(nil);
  235. aDatalink := TTestDataLink.Create;
  236. aDatalink.DataSource := aDatasource;
  237. ds := DBConnector.GetNDataset(6);
  238. ds.BeforeScroll := DBConnector.DataEvent;
  239. with ds do
  240. begin
  241. aDatasource.DataSet := ds;
  242. open;
  243. DataEvents := '';
  244. Resync([rmExact]);
  245. AssertEquals('deDataSetChange:0;',DataEvents);
  246. DataEvents := '';
  247. next;
  248. AssertEquals('deCheckBrowseMode:0;DataEvent;deDataSetScroll:0;',DataEvents);
  249. close;
  250. end;
  251. aDatasource.Free;
  252. aDatalink.Free;
  253. end;
  254. procedure TTestDBBasics.TestLastAppendCancel;
  255. var count : integer;
  256. begin
  257. for count := 0 to 32 do with DBConnector.GetNDataset(count) do
  258. begin
  259. open;
  260. Last;
  261. Append;
  262. Cancel;
  263. AssertEquals(count,fields[0].asinteger);
  264. AssertEquals(count,RecordCount);
  265. Close;
  266. end;
  267. end;
  268. procedure TTestDBBasics.TestRecNo;
  269. var i : longint;
  270. passed : boolean;
  271. begin
  272. with DBConnector.GetNDataset(0) do
  273. begin
  274. // Accessing RecNo on a closed dataset should raise an EDatabaseError or should
  275. // return 0
  276. passed := false;
  277. try
  278. i := recno;
  279. except on E: Exception do
  280. begin
  281. passed := E.classname = EDatabaseError.className
  282. end;
  283. end;
  284. if not passed then
  285. AssertEquals('Failed to get the RecNo from a closed dataset',0,RecNo);
  286. // Accessing Recordcount on a closed dataset should raise an EDatabaseError or should
  287. // return 0
  288. passed := false;
  289. try
  290. i := recordcount;
  291. except on E: Exception do
  292. begin
  293. passed := E.classname = EDatabaseError.className
  294. end;
  295. end;
  296. if not passed then
  297. AssertEquals('Failed to get the Recordcount from a closed dataset',0,RecNo);
  298. Open;
  299. AssertEquals(0,RecordCount);
  300. AssertEquals(0,RecNo);
  301. first;
  302. AssertEquals(0,RecordCount);
  303. AssertEquals(0,RecNo);
  304. last;
  305. AssertEquals(0,RecordCount);
  306. AssertEquals(0,RecNo);
  307. append;
  308. AssertEquals(0,RecNo);
  309. AssertEquals(0,RecordCount);
  310. first;
  311. AssertEquals(0,RecNo);
  312. AssertEquals(0,RecordCount);
  313. append;
  314. FieldByName('id').AsInteger := 1;
  315. AssertEquals(0,RecNo);
  316. AssertEquals(0,RecordCount);
  317. first;
  318. AssertEquals(1,RecNo);
  319. AssertEquals(1,RecordCount);
  320. last;
  321. AssertEquals(1,RecNo);
  322. AssertEquals(1,RecordCount);
  323. append;
  324. FieldByName('id').AsInteger := 1;
  325. AssertEquals(0,RecNo);
  326. AssertEquals(1,RecordCount);
  327. Close;
  328. end;
  329. end;
  330. procedure TTestDBBasics.TestSetRecNo;
  331. begin
  332. with DBConnector.GetNDataset(15) do
  333. begin
  334. Open;
  335. RecNo := 1;
  336. AssertEquals(1,fields[0].AsInteger);
  337. AssertEquals(1,RecNo);
  338. RecNo := 2;
  339. AssertEquals(2,fields[0].AsInteger);
  340. AssertEquals(2,RecNo);
  341. RecNo := 8;
  342. AssertEquals(8,fields[0].AsInteger);
  343. AssertEquals(8,RecNo);
  344. RecNo := 15;
  345. AssertEquals(15,fields[0].AsInteger);
  346. AssertEquals(15,RecNo);
  347. RecNo := 3;
  348. AssertEquals(3,fields[0].AsInteger);
  349. AssertEquals(3,RecNo);
  350. RecNo := 14;
  351. AssertEquals(14,fields[0].AsInteger);
  352. AssertEquals(14,RecNo);
  353. RecNo := 15;
  354. AssertEquals(15,fields[0].AsInteger);
  355. AssertEquals(15,RecNo);
  356. // test for exceptions...
  357. { RecNo := 16;
  358. AssertEquals(15,fields[0].AsInteger);
  359. AssertEquals(15,RecNo);}
  360. Close;
  361. end;
  362. end;
  363. procedure TTestDBBasics.TestRequired;
  364. begin
  365. with DBConnector.GetNDataset(2) do
  366. begin
  367. Open;
  368. FieldByName('ID').Required := True;
  369. Append;
  370. AssertException(EDatabaseError,Post);
  371. FieldByName('ID').AsInteger := 1000;
  372. Post;
  373. Close;
  374. end;
  375. end;
  376. procedure TTestDBBasics.SetUp;
  377. begin
  378. DBConnector.StartTest;
  379. end;
  380. procedure TTestDBBasics.TearDown;
  381. begin
  382. DBConnector.StopTest;
  383. end;
  384. procedure TTestDBBasics.TestOnFilterProc(DataSet: TDataSet; var Accept: Boolean);
  385. var a : TDataSetState;
  386. begin
  387. Accept := odd(Dataset.FieldByName('ID').AsInteger);
  388. end;
  389. procedure TTestDBBasics.TestOnFilter;
  390. var tel : byte;
  391. begin
  392. with DBConnector.GetNDataset(15) do
  393. begin
  394. OnFilterRecord := TestOnFilterProc;
  395. Filtered := True;
  396. Open;
  397. for tel := 1 to 8 do
  398. begin
  399. AssertTrue(odd(FieldByName('ID').asinteger));
  400. next;
  401. end;
  402. AssertTrue(EOF);
  403. end;
  404. end;
  405. procedure TTestDBBasics.TestIntFilter;
  406. var tel : byte;
  407. begin
  408. with DBConnector.GetNDataset(15) do
  409. begin
  410. Filtered := True;
  411. Filter := '(id>4) and (id<9)';
  412. Open;
  413. for tel := 5 to 8 do
  414. begin
  415. AssertEquals(tel,FieldByName('ID').asinteger);
  416. next;
  417. end;
  418. AssertTrue(EOF);
  419. Close;
  420. end;
  421. end;
  422. procedure TTestDBBasics.TestRecordcountAfterReopen;
  423. var
  424. datalink1: tdatalink;
  425. datasource1: tdatasource;
  426. query1: TDataSet;
  427. begin
  428. query1:= DBConnector.GetNDataset(11);
  429. datalink1:= TDataLink.create;
  430. datasource1:= tdatasource.create(nil);
  431. try
  432. datalink1.datasource:= datasource1;
  433. datasource1.dataset:= query1;
  434. query1.active := true;
  435. query1.active := False;
  436. AssertEquals(0, THackDataLink(datalink1).RecordCount);
  437. query1.active := true;
  438. AssertTrue(THackDataLink(datalink1).RecordCount>0);
  439. query1.active := false;
  440. finally
  441. datalink1.free;
  442. datasource1.free;
  443. end;
  444. end;
  445. procedure TTestDBBasics.TestStringFilter;
  446. var tel : byte;
  447. begin
  448. with DBConnector.GetNDataset(15) do
  449. begin
  450. Open;
  451. //FilterOptions := [foNoPartialCompare];
  452. //FilterOptions := [];
  453. Filter := '(name=''*Name3'')';
  454. Filtered := True;
  455. AssertFalse(EOF);
  456. AssertEquals(3,FieldByName('ID').asinteger);
  457. AssertEquals('TestName3',FieldByName('NAME').asstring);
  458. next;
  459. AssertTrue(EOF);
  460. Close;
  461. end;
  462. end;
  463. procedure TTestDBBasics.TestcalculatedField_OnCalcfields(DataSet: TDataSet);
  464. begin
  465. case dataset.fieldbyname('ID').asinteger of
  466. 1 : dataset.fieldbyname('CALCFLD').AsInteger := 5;
  467. 2 : dataset.fieldbyname('CALCFLD').AsInteger := 70000;
  468. 3 : dataset.fieldbyname('CALCFLD').Clear;
  469. 4 : dataset.fieldbyname('CALCFLD').AsInteger := 1234;
  470. 10 : dataset.fieldbyname('CALCFLD').Clear;
  471. else
  472. dataset.fieldbyname('CALCFLD').AsInteger := 1;
  473. end;
  474. end;
  475. procedure TTestDBBasics.TestCalculatedField;
  476. var ds : TDataset;
  477. AFld1, AFld2, AFld3 : Tfield;
  478. begin
  479. ds := DBConnector.GetNDataset(5);
  480. with ds do
  481. begin
  482. AFld1 := TIntegerField.Create(ds);
  483. AFld1.FieldName := 'ID';
  484. AFld1.DataSet := ds;
  485. AFld2 := TStringField.Create(ds);
  486. AFld2.FieldName := 'NAME';
  487. AFld2.DataSet := ds;
  488. AFld3 := TIntegerField.Create(ds);
  489. AFld3.FieldName := 'CALCFLD';
  490. AFld3.DataSet := ds;
  491. Afld3.FieldKind := fkCalculated;
  492. AssertEquals(3,FieldCount);
  493. ds.OnCalcFields := TestcalculatedField_OnCalcfields;
  494. open;
  495. AssertEquals(1,FieldByName('ID').asinteger);
  496. AssertEquals(5,FieldByName('CALCFLD').asinteger);
  497. next;
  498. AssertEquals(70000,FieldByName('CALCFLD').asinteger);
  499. next;
  500. AssertEquals(true,FieldByName('CALCFLD').isnull);
  501. next;
  502. AssertEquals(1234,FieldByName('CALCFLD').AsInteger);
  503. edit;
  504. FieldByName('ID').AsInteger := 10;
  505. post;
  506. AssertEquals(true,FieldByName('CALCFLD').isnull);
  507. close;
  508. AFld1.Free;
  509. AFld2.Free;
  510. AFld3.Free;
  511. end;
  512. end;
  513. procedure TTestDBBasics.TestEofAfterFirst;
  514. begin
  515. with DBConnector.GetNDataset(0) do
  516. begin
  517. open;
  518. AssertTrue(eof);
  519. AssertTrue(BOF);
  520. first;
  521. AssertTrue(eof);
  522. AssertTrue(BOF);
  523. end;
  524. end;
  525. procedure TTestDBBasics.TestfieldDefinition(AFieldType : TFieldType;ADatasize : integer;var ADS : TDataset; var AFld: TField);
  526. var i : byte;
  527. begin
  528. ADS := DBConnector.GetFieldDataset;
  529. ADS.Open;
  530. AFld := ADS.FindField('F'+FieldTypeNames[AfieldType]);
  531. AssertNotNull('Fields of the type ' + FieldTypeNames[AfieldType] + ' are not supported by this type of dataset',AFld);
  532. AssertTrue(Afld.DataType = AFieldType);
  533. AssertEquals(ADatasize,Afld.DataSize );
  534. end;
  535. procedure TTestDBBasics.TestSupportIntegerFields;
  536. var i : byte;
  537. ds : TDataset;
  538. Fld : TField;
  539. begin
  540. TestfieldDefinition(ftInteger,4,ds,Fld);
  541. for i := 0 to testValuesCount-1 do
  542. begin
  543. AssertEquals(testIntValues[i],Fld.AsInteger);
  544. ds.Next;
  545. end;
  546. ds.close;
  547. end;
  548. procedure TTestDBBasics.TestSupportSmallIntFields;
  549. var i : byte;
  550. ds : TDataset;
  551. Fld : TField;
  552. begin
  553. TestfieldDefinition(ftSmallint,2,ds,Fld);
  554. for i := 0 to testValuesCount-1 do
  555. begin
  556. AssertEquals(testSmallIntValues[i],Fld.AsInteger);
  557. ds.Next;
  558. end;
  559. ds.close;
  560. end;
  561. procedure TTestDBBasics.TestSupportStringFields;
  562. var i : byte;
  563. ds : TDataset;
  564. Fld : TField;
  565. begin
  566. TestfieldDefinition(ftString,11,ds,Fld);
  567. for i := 0 to testValuesCount-1 do
  568. begin
  569. AssertEquals(testStringValues[i],Fld.AsString);
  570. ds.Next;
  571. end;
  572. ds.close;
  573. end;
  574. procedure TTestDBBasics.TestSupportBooleanFields;
  575. var i : byte;
  576. ds : TDataset;
  577. Fld : TField;
  578. begin
  579. TestfieldDefinition(ftBoolean,2,ds,Fld);
  580. for i := 0 to testValuesCount-1 do
  581. begin
  582. AssertEquals(testBooleanValues[i],Fld.AsBoolean);
  583. ds.Next;
  584. end;
  585. ds.close;
  586. end;
  587. procedure TTestDBBasics.TestSupportFloatFields;
  588. var i : byte;
  589. ds : TDataset;
  590. Fld : TField;
  591. begin
  592. TestfieldDefinition(ftFloat,8,ds,Fld);
  593. for i := 0 to testValuesCount-1 do
  594. begin
  595. AssertEquals(testFloatValues[i],Fld.AsFloat);
  596. ds.Next;
  597. end;
  598. ds.close;
  599. end;
  600. procedure TTestDBBasics.TestSupportLargeIntFields;
  601. var i : byte;
  602. ds : TDataset;
  603. Fld : TField;
  604. begin
  605. TestfieldDefinition(ftLargeint,8,ds,Fld);
  606. for i := 0 to testValuesCount-1 do
  607. begin
  608. AssertEquals(testLargeIntValues[i],Fld.AsLargeInt);
  609. ds.Next;
  610. end;
  611. ds.close;
  612. end;
  613. procedure TTestDBBasics.TestSupportDateFields;
  614. var i : byte;
  615. ds : TDataset;
  616. Fld : TField;
  617. begin
  618. TestfieldDefinition(ftDate,8,ds,Fld);
  619. for i := 0 to testValuesCount-1 do
  620. begin
  621. AssertEquals(testDateValues[i],FormatDateTime('yyyy/mm/dd',Fld.AsDateTime));
  622. ds.Next;
  623. end;
  624. ds.close;
  625. end;
  626. procedure TTestDBBasics.TestSupportCurrencyFields;
  627. var i : byte;
  628. ds : TDataset;
  629. Fld : TField;
  630. begin
  631. TestfieldDefinition(ftCurrency,8,ds,Fld);
  632. for i := 0 to testValuesCount-1 do
  633. begin
  634. AssertEquals(testCurrencyValues[i],Fld.AsCurrency);
  635. AssertEquals(testCurrencyValues[i],Fld.AsFloat);
  636. ds.Next;
  637. end;
  638. ds.close;
  639. end;
  640. procedure TTestDBBasics.TestSupportBCDFields;
  641. var i : byte;
  642. ds : TDataset;
  643. Fld : TField;
  644. begin
  645. TestfieldDefinition(ftBCD,8,ds,Fld);
  646. for i := 0 to testValuesCount-1 do
  647. begin
  648. AssertEquals(testCurrencyValues[i],Fld.AsCurrency);
  649. AssertEquals(testCurrencyValues[i],Fld.AsFloat);
  650. ds.Next;
  651. end;
  652. ds.close;
  653. end;
  654. procedure TTestDBBasics.TestDoubleClose;
  655. begin
  656. with DBConnector.GetNDataset(1) do
  657. begin
  658. close;
  659. close;
  660. open;
  661. close;
  662. close;
  663. end;
  664. end;
  665. procedure TTestDBBasics.TestAssignFieldftString;
  666. var AParam : TParam;
  667. AField : TField;
  668. begin
  669. AParam := TParam.Create(nil);
  670. with DBConnector.GetNDataset(1) do
  671. begin
  672. open;
  673. AField := fieldbyname('name');
  674. AParam.AssignField(AField);
  675. AssertTrue(ftString=AParam.DataType);
  676. close;
  677. end;
  678. AParam.Free;
  679. end;
  680. procedure TTestDBBasics.TestAssignFieldftFixedChar;
  681. var AParam : TParam;
  682. AField : TField;
  683. begin
  684. AParam := TParam.Create(nil);
  685. with DBConnector.GetNDataset(1) do
  686. begin
  687. open;
  688. AField := fieldbyname('name');
  689. (AField as tstringfield).FixedChar := true;
  690. AParam.AssignField(AField);
  691. AssertTrue(ftFixedChar=AParam.DataType);
  692. close;
  693. end;
  694. AParam.Free;
  695. end;
  696. procedure TTestDBBasics.TestBufDatasetCancelUpd;
  697. var i : byte;
  698. begin
  699. if not (DBConnector.GetNDataset(5) is TBufDataset) then
  700. Ignore('This test only applies to TBufDataset and descendents.');
  701. with DBConnector.GetNDataset(5) as TBufDataset do
  702. begin
  703. open;
  704. next;
  705. next;
  706. edit;
  707. FieldByName('name').AsString := 'changed';
  708. post;
  709. next;
  710. delete;
  711. CancelUpdates;
  712. First;
  713. for i := 1 to 5 do
  714. begin
  715. AssertEquals(i,fields[0].AsInteger);
  716. AssertEquals('TestName'+inttostr(i),fields[1].AsString);
  717. Next;
  718. end;
  719. end;
  720. end;
  721. procedure TTestDBBasics.Testbug7007;
  722. var
  723. datalink1: tdatalink;
  724. datasource1: tdatasource;
  725. query1: TDataSet;
  726. begin
  727. query1:= DBConnector.GetNDataset(6);
  728. datalink1:= TTestDataLink.create;
  729. datasource1:= tdatasource.create(nil);
  730. try
  731. datalink1.datasource:= datasource1;
  732. datasource1.dataset:= query1;
  733. datalink1.datasource:= datasource1;
  734. DataEvents := '';
  735. query1.open;
  736. datalink1.buffercount:= query1.recordcount;
  737. AssertEquals('deUpdateState:0;',DataEvents);
  738. AssertEquals(0, datalink1.ActiveRecord);
  739. AssertEquals(6, datalink1.RecordCount);
  740. AssertEquals(6, query1.RecordCount);
  741. AssertEquals(1, query1.RecNo);
  742. DataEvents := '';
  743. query1.append;
  744. AssertEquals('deCheckBrowseMode:0;deUpdateState:0;deDataSetChange:0;',DataEvents);
  745. AssertEquals(5, datalink1.ActiveRecord);
  746. AssertEquals(6, datalink1.RecordCount);
  747. AssertEquals(6, query1.RecordCount);
  748. AssertTrue(query1.RecNo in [0,7]);
  749. DataEvents := '';
  750. query1.cancel;
  751. AssertEquals('deCheckBrowseMode:0;deUpdateState:0;deDataSetChange:0;',DataEvents);
  752. AssertEquals(5, datalink1.ActiveRecord);
  753. AssertEquals(6, datalink1.RecordCount);
  754. AssertEquals(6, query1.RecordCount);
  755. AssertEquals(6, query1.RecNo);
  756. finally
  757. datalink1.free;
  758. datasource1.free;
  759. end;
  760. end;
  761. procedure TTestDBBasics.TestBug6893;
  762. var
  763. datalink1: tdatalink;
  764. datasource1: tdatasource;
  765. query1: TDataSet;
  766. begin
  767. query1:= DBConnector.GetNDataset(25);
  768. datalink1:= TDataLink.create;
  769. datasource1:= tdatasource.create(nil);
  770. try
  771. datalink1.datasource:= datasource1;
  772. datasource1.dataset:= query1;
  773. datalink1.buffercount:= 5;
  774. query1.active := true;
  775. query1.MoveBy(20);
  776. AssertEquals(5, THackDataLink(datalink1).Firstrecord);
  777. AssertEquals(4, datalink1.ActiveRecord);
  778. AssertEquals(21, query1.RecNo);
  779. query1.active := False;
  780. AssertEquals(0, THackDataLink(datalink1).Firstrecord);
  781. AssertEquals(0, datalink1.ActiveRecord);
  782. query1.active := true;
  783. AssertEquals(0, THackDataLink(datalink1).Firstrecord);
  784. AssertEquals(0, datalink1.ActiveRecord);
  785. AssertEquals(1, query1.RecNo);
  786. finally
  787. datalink1.free;
  788. datasource1.free;
  789. end;
  790. end;
  791. procedure TTestDBBasics.TestBufDatasetCancelUpd1;
  792. var i : byte;
  793. begin
  794. if not (DBConnector.GetNDataset(5) is TBufDataset) then
  795. Ignore('This test only applies to TBufDataset and descendents.');
  796. with DBConnector.GetNDataset(5) as TBufDataset do
  797. begin
  798. open;
  799. next;
  800. next;
  801. delete;
  802. insert;
  803. FieldByName('id').AsInteger := 100;
  804. post;
  805. CancelUpdates;
  806. last;
  807. for i := 5 downto 1 do
  808. begin
  809. AssertEquals(i,fields[0].AsInteger);
  810. AssertEquals('TestName'+inttostr(i),fields[1].AsString);
  811. Prior;
  812. end;
  813. end;
  814. end;
  815. procedure TTestDBBasics.TestNullAtOpen;
  816. begin
  817. with dbconnector.getndataset(0) do
  818. begin
  819. active:= true;
  820. AssertTrue('Field isn''t NULL on a just-opened empty dataset',fieldbyname('id').IsNull);
  821. append;
  822. AssertTrue('Field isn''t NULL after append on an empty dataset',fieldbyname('id').IsNull);
  823. fieldbyname('id').asinteger:= 123;
  824. cancel;
  825. AssertTrue('Field isn''t NULL after cancel',fieldbyname('id').IsNull);
  826. end;
  827. end;
  828. { TSQLTestSetup }
  829. procedure TDBBasicsTestSetup.OneTimeSetup;
  830. begin
  831. InitialiseDBConnector;
  832. end;
  833. procedure TDBBasicsTestSetup.OneTimeTearDown;
  834. begin
  835. FreeAndNil(DBConnector);
  836. end;
  837. initialization
  838. RegisterTestDecorator(TDBBasicsTestSetup, TTestDBBasics);
  839. end.