Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
avoid errors when parsing incomplete batches
Browse files Browse the repository at this point in the history
  • Loading branch information
caiolrm committed Nov 8, 2019
1 parent 8cf164c commit 59170d3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions PanamahSDK.Consts.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
interface

const
SDK_VERSION = '1.0.1';
UTF8_CODEPAGE = 65001;
LATIN_CODEPAGE = 28591;
HTTPS_PROTOCOL = 'https';
Expand Down
6 changes: 4 additions & 2 deletions PanamahSDK.Log.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
interface

uses
SysUtils, Classes, SyncObjs, Windows;
SysUtils, Classes, SyncObjs, PanamahSDK.Consts, Windows;

type

Expand All @@ -20,9 +20,11 @@ implementation
{ TPanamahLog }

class procedure TPanamahLogger.Log(const AMessage: string);
{$IFDEF PANAMAHSDK_LOG}
var
LogFilename: string;
LogFile: TextFile;
{$ENDIF}
begin
{$IFDEF PANAMAHSDK_LOG}
PanamahLogCriticalSection.Acquire;
Expand All @@ -34,7 +36,7 @@ class procedure TPanamahLogger.Log(const AMessage: string);
Append(LogFile)
else
Rewrite(LogFile);
WriteLn(LogFile, Format('[%s thread %d] %s', [FormatDateTime('dd/mm/yyyy hh:nn:ss', Now), GetCurrentThreadId, AMessage]));
WriteLn(LogFile, Format('[%s thread %d Version ' + SDK_VERSION + '] %s', [FormatDateTime('dd/mm/yyyy hh:nn:ss', Now), GetCurrentThreadId, AMessage]));
finally
CloseFile(LogFile);
end;
Expand Down
12 changes: 8 additions & 4 deletions PanamahSDK.Operation.pas
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,15 @@ procedure TPanamahOperationList.SetItem(AIndex: Integer;
end;

procedure TPanamahOperationList.DeserializeFromJSON(const AJSON: string);
var
List: TlkJSONlist;
begin
with TlkJSON.ParseText(AJSON) as TlkJSONlist do
begin
ForEach(AddJSONObjectToList, nil);
Free;
List := TlkJSON.ParseText(AJSON) as TlkJSONlist;
try
if Assigned(List) then
List.ForEach(AddJSONObjectToList, nil);
finally
FreeAndNil(List);
end;
end;

Expand Down
1 change: 1 addition & 0 deletions Tests/JSON/Fixtures/2019_11_08_12_23_17_337.pbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"op":"update","data":{"id":"1","descricao":"DINHEIRO"},"tipo":"FORMA_PAGAMENTO"},{"op":"update","data":{"id":"01234567890123420191101031039784803772470003344","lojaId":"12345678901234","data":"2019-11-01T06:44:00-0300","dataHoraInicio":"2019-11-01T06:44:00-0300","dataHoraFim":"2019-11-01T06:44:00-0300","dataHoraVenda":"2019-11-01T06:44:00-0300","desconto":0,"efetiva":true,"quantidadeItens":1,"quantidadeItensCancelados":0,"sequencial":"397848","servico":0,"tipoPreco":"NORMAL","valor":14.09,"valorItensCancelados":0,"acrescimo":0,"numeroCaixa":"31","itens":[{"acrescimo":0,"desconto":0,"efetivo":true,"funcionarioId":"0003344","preco":
43 changes: 43 additions & 0 deletions Tests/JSON/PanamahSDKTests.JSON.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
unit PanamahSDKTests.JSON;

interface

uses
Math, SysUtils, Classes, TestFramework, IOUtils, PanamahSDK.Batch;

type

TTestJSONTestCase = class(TTestCase)
private
function GetFixturePath(const AFilename: string): string;
published
procedure TestDeserializeCorruptedBatch;
end;

implementation

{ TPanamahNFeDeserializerTestCase }

function TTestJSONTestCase.GetFixturePath(
const AFilename: string): string;
begin
Result := Concat(GetCurrentDir, '\JSON\Fixtures\', AFilename);
end;

procedure TTestJSONTestCase.TestDeserializeCorruptedBatch;
var
Batch: TPanamahBatch;
begin
Batch := TPanamahBatch.Create;
try
Batch.DeserializeFromJSON(TFile.ReadAllText(GetFixturePath('2019_11_08_12_23_17_337.pbt')));
Assert(Batch.GetCount = 0);
finally
Batch.Free;
end;
end;

initialization
RegisterTest(TTestJSONTestCase.Suite);

end.

0 comments on commit 59170d3

Please sign in to comment.