Source

asprovider / Test / TestASProvider.pas

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
unit TestASProvider;
{

  Delphi DUnit Test Case
  ----------------------
  This unit contains a skeleton test case class generated by the Test Case Wizard.
  Modify the generated code to correctly setup and call the methods from the unit 
  being tested.

}

interface

uses
  Classes, SysUtils, TestFramework, DB, DBClient, Provider, ASProvider;

type
  TTestData = class
  private
    Fstring: string;
    FroString: string;
    Fint: Integer;
  public
    property Astring: string read Fstring write Fstring;
    property AroString: string read FroString;
    property Aint: Integer read Fint write Fint;
  end;

  TTestGenericProvider = class(TASGenericProvider<TTestData>)
  published
    property Active;
    property FieldDefs;
    property ProvidedTypedObject;
  end;

  TTestItem = class(TCollectionItem)
  private
    Fstring: string;
    Fint: Integer;
  public
    property Astring: string read Fstring write Fstring;
    property Aint: Integer read Fint write Fint;
  end;

  TExpectFieldDef = record
    Idx: Integer;
    Name: string;
    Size: Integer;
    DataType: TFieldType;
    Attr: TFieldAttributes;
  end;

  TExpectItem = record
    Idx: Integer;
    Astring: string;
    Aint: Integer;
  end;

  TASCustomProviderClass = class of TASCustomProvider;

  TTestASProviderTestCase = class(TTestCase)
  strict private
    FProvider: TASCustomProvider;
  protected
    procedure CheckFieldDef(Expect: TExpectFieldDef; const Msg: string = '');
    procedure CheckMetaData(ExpectFields: array of TExpectFieldDef; const Msg: string = '');
    procedure CheckItem(Expect: TExpectItem; const Msg: string = '');
    procedure CheckCollection(ExpectItems: array of TExpectItem; const Msg: string = '');

    function GetProviderClass: TASCustomProviderClass; virtual;
    property CustomProvider: TASCustomProvider read FProvider write FProvider;
  public
    procedure SetUp; override;
    procedure TearDown; override;
  end;

  // Test methods for class TASObjectProvider

  TestTASProviders<T: TASObjectProvider, constructor> = class(TTestASProviderTestCase)
  protected
    function Provider: T;
    function GetProviderClass: TASCustomProviderClass; override;
  published
    procedure TestProvidedClass;
    procedure TestMetaData;
    procedure TestData;
    procedure TestModifiedFieldDefs;
    procedure TestStreamingDFM;
    procedure TestApply;
  end;

  TestCollectionProvider = class(TTestASProviderTestCase)
  protected
    function Provider: TASCollectionProvider;
    function GetProviderClass: TASCustomProviderClass; override;
  published
    procedure TestProvidedClass;
    procedure TestMetaData;
    procedure TestData;
    procedure TestInsertData;
    procedure TestDeleteData;
    procedure TestInsertUpdateDeleteData;
  end;

  // Test methods for class TASResolver

  TestTASResolver = class(TTestCase)
  strict private
    FASResolver: TASResolver;
    FASCustomProvider: TASObjectProvider;
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestInitTreeData;
  end;

const
  TestMetaData_fields: array[0..2] of TExpectFieldDef = (
    (Idx: 0; Name: 'Astring';   Size: 20; DataType: ftWideString; Attr: []),
    (Idx: 1; Name: 'AroString'; Size: 20; DataType: ftWideString; Attr: [faReadonly]),
    (Idx: 2; Name: 'Aint';      Size: 0;  DataType: ftInteger;    Attr: [])
  );

  TestModified_fields: array[0..1] of TExpectFieldDef = (
    (Idx: 0; Name: 'Astring';         Size: 35; DataType: ftString;   Attr: []),
    (Idx: 1; Name: 'MyModifiedInt';   Size: 0;  DataType: ftInteger;  Attr: [])
  );

  TestCollectionMetaData_fields: array[0..4] of TExpectFieldDef = (
    (Idx: 0; Name: 'Astring';     Size: 20; DataType: ftWideString; Attr: []),
    (Idx: 1; Name: 'Aint';        Size: 0;  DataType: ftInteger;    Attr: []),
    (Idx: 2; Name: 'ID';          Size: 0;  DataType: ftInteger;    Attr: [faReadOnly]),
    (Idx: 3; Name: 'Index';       Size: 0;  DataType: ftInteger;    Attr: []),
    (Idx: 4; Name: 'DisplayName'; Size: 20; DataType: ftWideString; Attr: [])
  );

  TestCollectionItems_PreCondition: array[0..2] of TExpectItem = (
    (Idx: 0; Astring: 'My first string'; Aint: 123),
    (Idx: 1; Astring: 'My second string'; Aint: 456),
    (Idx: 2; Astring: 'My third string'; Aint: 789)
  );

  TestCollectionItems_PostCondition: array[0..2] of TExpectItem = (
    (Idx: 0; Astring: 'My modified string'; Aint: 654),
    (Idx: 1; Astring: 'My third string'; Aint: 789),
    (Idx: 2; Astring: 'My new string'; Aint: 321)
  );

implementation

uses
  Forms, Rtti, StrUtils;

function TestTASProviders<T>.GetProviderClass: TASCustomProviderClass;
begin
  Result := T;
end;

function TestTASProviders<T>.Provider: T;
begin
  Result := T(CustomProvider);
end;

procedure TestTASProviders<T>.TestApply;
var
  Obj: TTestData;
  DS: TClientDataSet;
begin
  Obj := TTestData.Create;
  DS := TClientDataSet.Create(Application);
  try
    with Obj do begin
      FroString := 'a ro string';
      Astring := 'a string';
      Aint := 5;
    end;

    Provider.ProvidedObject := Obj;
    Provider.Name := 'TestProvider';

    DS.ProviderName := 'TestProvider';
    DS.Active := True;

    DS.Edit;
    DS.FieldByName('Astring').AsString := 'A modified string';
    DS.FieldByName('Aint').AsInteger := 11;
    DS.Post;
    CheckEquals(0, DS.ApplyUpdates(-1), 'Apply updates');

    CheckEqualsString('a ro string', Obj.AroString);
    CheckEqualsString('A modified string', Obj.Astring);
    CheckEquals(5, 11, Obj.Aint);
  finally
    Obj.Free;
    DS.Free;
  end;
end;

procedure TestTASProviders<T>.TestData;
var
  Obj: TTestData;
  DS: TClientDataSet;
begin
  Obj := TTestData.Create;
  DS := TClientDataSet.Create(Application);
  try
    with Obj do begin
      FroString := 'a ro string';
      Astring := 'a string';
      Aint := 5;
    end;

    Provider.ProvidedObject := Obj;
    Provider.Name := 'TestProvider';

    DS.ProviderName := 'TestProvider';
    DS.Active := True;

    CheckEquals(1, DS.RecordCount, 'Record count');
    CheckEqualsString('a ro string', DS.FieldByName('AroString').AsString);
    CheckEqualsString('a string', DS.FieldByName('Astring').AsString);
    CheckEquals(5, DS.FieldByName('Aint').AsInteger, 'Aint');
  finally
    Obj.Free;
    DS.Free;
  end;
end;

procedure TestTASProviders<T>.TestMetaData;
begin
  Provider.ProvidedClass := TTestData;
  CheckEquals(TTestData, Provider.ProvidedClass, 'Provided class');
  CheckMetaData(TestMetaData_fields);
end;

procedure TestTASProviders<T>.TestModifiedFieldDefs;
begin
  Provider.ProvidedClass := TTestData;
  CheckMetaData(TestMetaData_fields, 'Original');

  with Provider.FieldDefs do begin
    Find('AroString').Free;
    Find('Aint').Name := 'MyModifiedInt';
    with Find('Astring') do begin
      DataType := ftString; {notice: changing datatype also updates the size field: e.g. order is important}
      Size := 35;
    end;
  end;

  CheckMetaData(TestModified_fields, 'Modified');

  { Should not extract meta data from class if there already exists field data }
  Provider.ProvidedClass := TTestData;
  CheckMetaData(TestModified_fields, 'Reloaded');

  Provider.FieldDefs.Clear;
  Provider.ProvidedClass := TTestData;
  CheckMetaData(TestMetaData_fields, 'Cleared and reloaded');
end;

procedure TestTASProviders<T>.TestProvidedClass;
begin
  if not T.ClassNameIs('TASProvider') then
    ExpectedException := EArgumentException;

  Provider.ProvidedClass := Self.ClassType;
  Check(Provider.ProvidedClass = Self.ClassType);
end;

procedure TestTASProviders<T>.TestStreamingDFM;
var
  Stream: TMemoryStream;
begin
  Provider.ProvidedClass := TTestData;
  with Provider.FieldDefs do begin
    Find('AroString').Free;
    Find('Aint').Name := 'MyModifiedInt';
    with Find('Astring') do begin
      DataType := ftString; {notice: changing datatype also updates the size field: e.g. order is important}
      Size := 35;
    end;
  end;

  CheckMetaData(TestModified_fields, 'Write');

  Stream := TMemoryStream.Create;
  try
    Stream.WriteComponent(Provider);
    CustomProvider.Free;
    CustomProvider := nil;

    Stream.Position := 0;
    CustomProvider := T(Stream.ReadComponent(GetProviderClass.Create(Application)));
  finally
    Stream.Free;
  end;

  CheckMetaData(TestModified_fields, 'Read');
end;

procedure TestTASResolver.SetUp;
begin
  FASCustomProvider := TASObjectProvider.Create(nil);
  FASResolver := TASResolver.Create(FASCustomProvider);
end;

procedure TestTASResolver.TearDown;
begin
  FASResolver.Free;
  FASResolver := nil;
  FASCustomProvider.Free;
  FASCustomProvider := nil;
end;

procedure TestTASResolver.TestInitTreeData;
var
  Tree: TUpdateTree;
begin
  Tree := TUpdateTree.Create(nil, FASResolver);
  try
    CheckFalse(Assigned(Tree.Source), 'Didn''t expect Tree.Source to be assigned already');
    FASResolver.InitTreeData(Tree);
    CheckTrue(Assigned(Tree.Source), 'Resolver did not assign a data set to Tree.Source');
  finally
    Tree.Free;
  end;
end;

{ TestCollectionProvider }

function TestCollectionProvider.GetProviderClass: TASCustomProviderClass;
begin
  Result := TASCollectionProvider;
end;

function TestCollectionProvider.Provider: TASCollectionProvider;
begin
  Result := CustomProvider as TASCollectionProvider;
end;

procedure TestCollectionProvider.TestData;
var
  C: TCollection;
  DS: TClientDataSet;
begin
  C := TCollection.Create(TTestItem);
  DS := TClientDataSet.Create(Application);
  try
    with C.Add as TTestItem do begin
      Astring := 'a string';
      Aint := 69;
    end;

    with C.Add as TTestItem do begin
      Astring := 'a second string';
      Aint := 96;
    end;

    Provider.ProvidedCollection := C;
    Provider.Name := 'TestProvider';
    DS.ProviderName := 'TestProvider';
    DS.Open;

    CheckEquals(2, DS.RecordCount, 'Record count');
    CheckEqualsString('a string', DS.FieldByName('Astring').AsString, 'First record');
    CheckEquals(69, DS.FieldByName('Aint').AsInteger, 'First record');

    DS.Next;
    CheckEqualsString('a second string', DS.FieldByName('Astring').AsString, 'Second record');
    CheckEquals(96, DS.FieldByName('Aint').AsInteger, 'Second record');
  finally
    DS.Free;
    C.Free;
  end;
end;

procedure TestCollectionProvider.TestDeleteData;
var
  C: TCollection;
  DS: TClientDataSet;
begin
  C := TCollection.Create(TTestItem);
  DS := TClientDataSet.Create(Application);
  try
    C.Add;
    Provider.ProvidedCollection := C;
    Provider.Name := 'TestProvider';
    DS.ProviderName := 'TestProvider';
    DS.Active := True;
    DS.Delete;
    CheckEquals(0, DS.ApplyUpdates(-1), 'Apply updates');

    CheckEquals(0, C.Count, 'Collection count');
  finally
    DS.Free;
    C.Free;
  end;
end;

procedure TestCollectionProvider.TestInsertData;
var
  C: TCollection;
  DS: TClientDataSet;
begin
  C := TCollection.Create(TTestItem);
  DS := TClientDataSet.Create(Application);
  try
    Provider.ProvidedCollection := C;
    Provider.Name := 'TestProvider';
    DS.ProviderName := 'TestProvider';
    DS.Active := True;
    DS.Insert;
    DS.FindField('Astring').AsString := 'my string';
    DS.FindField('Aint').AsInteger := 55;
    CheckEquals(0, DS.ApplyUpdates(-1), 'Apply updates');
    CheckEquals(1, C.Count, 'Collection count');
    CheckEqualsString('my string', TTestItem(C.Items[0]).Astring, 'Astring');
    CheckEquals(55, TTestItem(C.Items[0]).Aint, 'Aint');
  finally
    DS.Free;
    C.Free;
  end;
end;

{
TEST insert update delete

  Pre condition:
    collection with 3 items:
      0: "My first string" #123
      1: "My second string" #456
      2: "My third string" #789

  Test:
    modify data:
      a) Delete item 0
      b) Modify item 1: "My modified string" #654
      c) Add item: "My new string" #321

  Post condition:
    collection with 3 items:
      0: "My modified string" #654
      1: "My third string" #789
      2: "My new string" #321
}
procedure TestCollectionProvider.TestInsertUpdateDeleteData;
var
  C: TCollection;
  DS: TClientDataSet;
begin
  C := TCollection.Create(TTestItem);
  DS := TClientDataSet.Create(Application);
  try
    with C.Add as TTestItem do begin
      Astring := 'My first string';
      Aint := 123;
    end;
    with C.Add as TTestItem do begin
      Astring := 'My second string';
      Aint := 456;
    end;
    with C.Add as TTestItem do begin
      Astring := 'My third string';
      Aint := 789;
    end;

    Provider.ProvidedCollection := C;
    Provider.Name := 'TestProvider';
    DS.ProviderName := 'TestProvider';
    DS.Active := True;

    { Pre condition }
    CheckCollection(TestCollectionItems_PreCondition, 'Pre condition');

    { Test A }
    CheckTrue(DS.FindFirst, 'Find first');
    CheckEqualsString('My first string', DS.FindField('Astring').AsString, 'Current Astring');
    DS.Delete;

    { Test B }
    DS.Edit;
    CheckEqualsString('My second string', DS.FindField('Astring').AsString, 'Current Astring');
    DS.FindField('Astring').AsString := 'My modified string';
    DS.FindField('Aint').AsInteger := 654;
    DS.Post;

    { Test C }
    DS.AppendRecord(['My new string', 321]);

    { Post condition }
    CheckEquals(3, DS.ChangeCount, 'Change count');
    CheckEquals(0, DS.ApplyUpdates(-1), 'Apply updates');
    CheckCollection(TestCollectionItems_PostCondition, 'Post condition');
  finally
    DS.Free;
    C.Free;
  end;
end;

procedure TestCollectionProvider.TestMetaData;
var
  C: TCollection;
begin
  C := TCollection.Create(TTestItem);
  try
    Provider.ProvidedCollection := C;
    CheckEquals(TTestItem, Provider.ProvidedClass, 'Provided class');
    CheckMetaData(TestCollectionMetaData_fields);
  finally
    C.Free;
  end;
end;

procedure TestCollectionProvider.TestProvidedClass;
var
  C: TCollection;
begin
  C := TCollection.Create(TTestItem);
  try
    Provider.ProvidedCollection := C;
    Check(Provider.ProvidedClass = TTestItem, 'Provided class');
  finally
    C.Free;
  end;
end;

{ TTestASProviderTestCase }

procedure TTestASProviderTestCase.CheckCollection(ExpectItems: array of TExpectItem; const Msg: string);
var
  I: Integer;
  S: string;
begin
  if Length(Msg) > 0 then
    S := Msg + ', '
  else
    S := '';

  CheckEquals(ExpectItems[High(ExpectItems)].Idx + 1, (CustomProvider as TASCollectionProvider).ProvidedCollection.Count, S + 'Unexpected number of items');

  for I := Low(ExpectItems) to High(ExpectItems) do
    CheckItem(ExpectItems[I], Msg);
end;

procedure TTestASProviderTestCase.CheckFieldDef(Expect: TExpectFieldDef; const Msg: string);
var
  S: string;
begin
  S := Format('%sField #%d ''%s'' ', [IfThen(Length(Msg) > 0, Msg + ', ', ''), Expect.Idx, Expect.Name]);
  with CustomProvider.FieldDefs[Expect.Idx] do begin
    CheckEqualsString(Expect.Name, Name, S + 'name');
    CheckEquals(Expect.Size, Size, S + 'size');
    Check(Expect.DataType = DataType,
      Format(
        S + 'expected field type ''%s'' <> actual field type ''%s''',
        [
          FieldTypeNames[Expect.DataType],
          FieldTypeNames[DataType]
        ]
      )
    );
    Check(Expect.Attr = Attributes,
      Format(
        S + 'attributes missmatch; missing attributes %s; unexpected attributes %s',
        [
          TValue.From<TFieldAttributes>(Expect.Attr - Attributes).ToString,
          TValue.From<TFieldAttributes>(Attributes - Expect.Attr).ToString
        ]
      )
    );
  end;
end;

procedure TTestASProviderTestCase.CheckItem(Expect: TExpectItem; const Msg: string);
var
  S: string;
begin
  S := Format('%sItem #%d ', [IfThen(Length(Msg) > 0, Msg + ', ', ''), Expect.Idx]);
  with (CustomProvider as TASCollectionProvider).ProvidedCollection.Items[Expect.Idx] as TTestItem do begin
    CheckEqualsString(Expect.Astring, Astring, S + 'Astring');
    CheckEquals(Expect.Aint, Aint, S + 'Aint');
  end;
end;

procedure TTestASProviderTestCase.CheckMetaData(ExpectFields: array of TExpectFieldDef; const Msg: string);
var
  I: Integer;
  S: string;
begin
  if Length(Msg) > 0 then
    S := Msg + ', '
  else
    S := '';

  try
    CheckEquals(ExpectFields[High(ExpectFields)].Idx + 1, CustomProvider.FieldDefs.Count, S + 'Unexpected number of field defs');
  except
    with TStringBuilder.Create do try
      Append('Actual fields:').AppendLine;
      for I := 0 to CustomProvider.FieldDefs.Count - 1 do
        AppendFormat('%d: %s', [I, CustomProvider.FieldDefs[I].Name]).AppendLine;

      Status(ToString);
    finally
      Free;
    end;

    raise;
  end;

  for I := Low(ExpectFields) to High(ExpectFields) do
    CheckFieldDef(ExpectFields[I], Msg);
end;

function TTestASProviderTestCase.GetProviderClass: TASCustomProviderClass;
begin
  Result := TASCustomProvider;
end;

procedure TTestASProviderTestCase.SetUp;
begin
  FProvider := GetProviderClass.Create(Application);
end;

procedure TTestASProviderTestCase.TearDown;
begin
  FreeAndNil(FProvider);
end;

initialization
  // Register any test cases with the test runner
  RegisterTest('Provider', TestTASProviders<TASProvider>.Suite);
  RegisterTest('Provider', TestTASProviders<TTestGenericProvider>.Suite);
  RegisterTest('Provider', TestCollectionProvider.Suite);
  RegisterTest('Resolver', TestTASResolver.Suite);
end.