1. Andreas Stenius
  2. asprovider

Source

asprovider / Test / TestASProvider.pas

  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
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;

  // Test methods for class TASObjectProvider

  TestTASProviders<T: TASObjectProvider, constructor> = class(TTestCase)
  strict private
    FASCustomProvider: T;
  protected
    procedure CheckFieldDef(Expect: TExpectFieldDef; const Msg: string = '');
    procedure CheckMetaData(ExpectFields: array of TExpectFieldDef; const Msg: string = '');
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestProvidedClass;
    procedure TestMetaData;
    procedure TestData;
    procedure TestModifiedFieldDefs;
    procedure TestStreamingDFM;
    procedure TestApply;
  end;

  TestCollectionProvider = class(TTestCase)
  strict private
    FProvider: TASCollectionProvider;
  protected
    procedure CheckItem(Expect: TExpectItem; const Msg: string = '');
    procedure CheckCollection(ExpectItems: array of TExpectItem; const Msg: string = '');
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestProvidedClass;
    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: [])
  );

  TestCollectionItems: array[0..2] of TExpectItem = (
    (Idx: 0; Astring: 'my updated string'; Aint: 123),
    (Idx: 1; Astring: 'my org string'; Aint: 456),
    (Idx: 3; Astring: 'my new string'; Aint: 789)
  );

implementation

uses
  Forms, Rtti, StrUtils;

procedure TestTASProviders<T>.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 FASCustomProvider.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 TestTASProviders<T>.CheckMetaData(ExpectFields: array of TExpectFieldDef; const Msg: string);
var
  I: Integer;
  S: string;
begin
  if Length(Msg) > 0 then
    S := Msg + ', '
  else
    S := '';

  CheckEquals(ExpectFields[High(ExpectFields)].Idx + 1, FASCustomProvider.FieldDefs.Count, S + 'Unexpected number of field defs');

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

procedure TestTASProviders<T>.SetUp;
begin
  FASCustomProvider := T.Create;
  Application.InsertComponent(FASCustomProvider);
end;

procedure TestTASProviders<T>.TearDown;
begin
  FreeAndNil(FASCustomProvider);
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;

    FASCustomProvider.ProvidedObject := Obj;
    FASCustomProvider.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;

    FASCustomProvider.ProvidedObject := Obj;
    FASCustomProvider.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 field');
  finally
    Obj.Free;
    DS.Free;
  end;
end;

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

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

  with FASCustomProvider.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 }
  FASCustomProvider.ProvidedClass := TTestData;
  CheckMetaData(TestModified_fields, 'Reloaded');

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

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

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

procedure TestTASProviders<T>.TestStreamingDFM;
var
  Stream: TMemoryStream;
begin
  FASCustomProvider.ProvidedClass := TTestData;
  with FASCustomProvider.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(FASCustomProvider);
    FreeAndNil(FASCustomProvider);
    Stream.Position := 0;
    FASCustomProvider := T(Stream.ReadComponent(T.Create));
  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 }

procedure TestCollectionProvider.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, FProvider.ProvidedCollection.Count, S + 'Unexpected number of items');

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

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

procedure TestCollectionProvider.SetUp;
begin
  FProvider := TASCollectionProvider.Create(Application);
end;

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

procedure TestCollectionProvider.TestDeleteData;
var
  C: TCollection;
  DS: TClientDataSet;
begin
  C := TCollection.Create(TTestItem);
  DS := TClientDataSet.Create(Application);
  try
    C.Add;
    FProvider.ProvidedCollection := C;
    FProvider.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
    FProvider.ProvidedCollection := C;
    FProvider.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;

procedure TestCollectionProvider.TestInsertUpdateDeleteData;
var
  C: TCollection;
  DS: TClientDataSet;
begin
  C := TCollection.Create(TTestItem);
  DS := TClientDataSet.Create(Application);
  try
    FProvider.ProvidedCollection := C;
    FProvider.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;

procedure TestCollectionProvider.TestProvidedClass;
var
  C: TCollection;
begin
  C := TCollection.Create(TTestItem);
  try
    FProvider.ProvidedCollection := C;
    Check(FProvider.ProvidedClass = TTestItem, 'Provided class');
  finally
    C.Free;
  end;
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.