Retrieve an entity with a stream

Issue #197 closed
Henrique Gouveia created an issue

I tried working with Lazy <Stream> to save the pdf file, but I got an error. To simulate ...

TestEntities

...

var
  PictureFilename, ScannerFileName, OutputDir: string;

ORMTests

...
begin
  OutputDir := IncludeTrailingPathDelimiter(ExtractFileDir(ParamStr(0)));
  PictureFilename := IncludeTrailingPathDelimiter(ExpandFileName(OutputDir + '..\..')) + 'DelphiOOP.png';
  ScannerFileName := IncludeTrailingPathDelimiter(ExpandFileName(OutputDir + '..\..')) + 'DelphiOOP.pdf';
...

TestSession

I wrote the test...

procedure TSessionTest.GetLazyStream;
var
  LCustomer: TCustomer;
  stream: TMemoryStream;
begin
  stream := TMemoryStream.Create;
  try
    stream.LoadFromFile(ScannerFileName);

    LCustomer := FSession.SingleOrDefault<TCustomer>(SQL_GET_ALL_CUSTOMERS, []);
    CheckFalse(Assigned(LCustomer));
    InsertCustomerStream(25, 'Stream Lazy', 2.36, 'Middle', stream);

    LCustomer := FSession.SingleOrDefault<TCustomer>(SQL_GET_ALL_CUSTOMERS, []);
    try
      CheckNotNull(LCustomer.CustStream, 'Lazy should have value');
      CheckTrue(LCustomer.CustStream.Size > 0, 'Size should be more than 0');
      CheckEquals(stream.Size, LCustomer.CustStream.Size, 'file"s size not equals.');
    finally
      LCustomer.Free;
    end;
  finally
    stream.Free;
  end;
end;

The test fails, but if comment value.free at TAbstractSession.MapAggregatedObject ...

function TAbstractSession.MapAggregatedObject(const resultSet: IDBResultSet;
  const baseEntity: TObject; entityClass: TClass): TObject;
    ...
    value := Convert(fieldValue);
    try
      if value.TryConvert(entityClass.ClassInfo, convertedValue) then
        Result := convertedValue.AsObject;
    finally
      // value.Free;
    end;
    ...

The test pass.

I'm using D10Seattle, sorry if it's a mistake.

Comments (8)

  1. Log in to comment