mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-15 07:45:54 +01:00
Completed ObjectDict tests
This commit is contained in:
parent
c075b1fbca
commit
dd64ab4c49
@ -491,12 +491,13 @@ end;
|
||||
procedure TRenderSampleController.GetCustomersWithObjectDictionary;
|
||||
var
|
||||
lDM: TMyDataModule;
|
||||
lDict: TMVCObjectDictionary;
|
||||
lDict: IMVCObjectDictionary;
|
||||
begin
|
||||
lDM := TMyDataModule.Create(nil);
|
||||
try
|
||||
lDM.qryCustomers.Open;
|
||||
lDict := ObjectDict(False).Add('customers', lDM.qryCustomers,
|
||||
lDict := ObjectDict(False)
|
||||
.Add('customers', lDM.qryCustomers,
|
||||
procedure(const DS: TDataset; const Links: IMVCLinks)
|
||||
begin
|
||||
Links
|
||||
@ -509,12 +510,9 @@ begin
|
||||
.Add(HATEOAS.HREF, '/customers/' + DS.FieldByName('cust_no').AsString + '/orders')
|
||||
.Add(HATEOAS.REL, 'orders')
|
||||
.Add(HATEOAS._TYPE, 'application/json');
|
||||
end);
|
||||
try
|
||||
Render(lDict, False);
|
||||
finally
|
||||
lDict.Free;
|
||||
end;
|
||||
end)
|
||||
.Add('singleCustomer', lDM.qryCustomers, nil, dstSingleRecord, ncPascalCase);
|
||||
Render(lDict);
|
||||
finally
|
||||
lDM.Free;
|
||||
end;
|
||||
@ -549,25 +547,22 @@ end;
|
||||
procedure TRenderSampleController.GetDataSetWithMetadata;
|
||||
var
|
||||
lDM: TMyDataModule;
|
||||
lDict: TMVCObjectDictionary;
|
||||
lDict: IMVCObjectDictionary;
|
||||
begin
|
||||
lDM := TMyDataModule.Create(nil);
|
||||
try
|
||||
lDM.qryCustomers.Open;
|
||||
lDict := ObjectDict(False)
|
||||
.Add('ncUpperCase', lDM.qryCustomers, nil, ncUpperCase)
|
||||
.Add('ncLowerCase', lDM.qryCustomers, nil, ncLowerCase)
|
||||
.Add('ncCamelCase', lDM.qryCustomers, nil, ncCamelCase)
|
||||
.Add('ncPascalCase', lDM.qryCustomers, nil, ncPascalCase)
|
||||
.Add('ncUpperCaseList', lDM.qryCustomers, nil, dstAllRecords, ncUpperCase)
|
||||
.Add('ncLowerCaseList', lDM.qryCustomers, nil, dstAllRecords, ncLowerCase)
|
||||
.Add('ncCamelCaseList', lDM.qryCustomers, nil, dstAllRecords, ncCamelCase)
|
||||
.Add('ncPascalCaseList', lDM.qryCustomers, nil, dstAllRecords, ncPascalCase)
|
||||
.Add('ncUpperCaseSingle', lDM.qryCustomers, nil, dstSingleRecord, ncUpperCase)
|
||||
.Add('ncLowerCaseSingle', lDM.qryCustomers, nil, dstSingleRecord, ncLowerCase)
|
||||
.Add('ncCamelCaseSingle', lDM.qryCustomers, nil, dstSingleRecord, ncCamelCase)
|
||||
.Add('ncPascalCaseSingle', lDM.qryCustomers, nil, dstSingleRecord, ncPascalCase)
|
||||
.Add('meta', StrDict(['page', 'count'], ['1', lDM.qryCustomers.RecordCount.ToString]));
|
||||
//lHolder := TDataSetHolder.Create(lDM.qryCustomers);
|
||||
// lHolder.Metadata.Add('page', '1');
|
||||
// lHolder.Metadata.Add('count', lDM.qryCustomers.RecordCount.ToString);
|
||||
try
|
||||
Render(lDict, False);
|
||||
finally
|
||||
lDict.Free;
|
||||
end;
|
||||
Render(lDict);
|
||||
finally
|
||||
lDM.Free;
|
||||
end;
|
||||
|
@ -320,9 +320,8 @@ type
|
||||
: IMVCObjectDictionary; overload;
|
||||
function Add(const Name: string; const Value: TDataset;
|
||||
const SerializationAction: TMVCDataSetSerializationAction = nil;
|
||||
const DataSetSerializationType: TMVCDatasetSerializationType = dstAllRecords;
|
||||
const NameCase: TMVCNameCase = TMVCNameCase.ncLowerCase): IMVCObjectDictionary; overload;
|
||||
function Add(const Name: string; const Value: IInterface;
|
||||
const SerializationAction: TMVCSerializationAction = nil): IMVCObjectDictionary; overload;
|
||||
function TryGetValue(const Name: string; out Value: TObject): Boolean; overload;
|
||||
function Count: Integer;
|
||||
function ContainsKey(const Key: string): Boolean;
|
||||
@ -344,6 +343,7 @@ type
|
||||
fSerializationAction: TMVCSerializationAction;
|
||||
fDataSetSerializationAction: TMVCDataSetSerializationAction;
|
||||
fDataSetFieldNameCase: TMVCNameCase;
|
||||
fDataSetSerializationType: TMVCDatasetSerializationType;
|
||||
public
|
||||
constructor Create(
|
||||
const Owns: Boolean;
|
||||
@ -353,12 +353,14 @@ type
|
||||
const Owns: Boolean;
|
||||
const Data: TDataset;
|
||||
const SerializationAction: TMVCDataSetSerializationAction;
|
||||
const DataSetSerializationType: TMVCDatasetSerializationType;
|
||||
const NameCase: TMVCNameCase); overload;
|
||||
destructor Destroy; override;
|
||||
property Data: TObject read fData;
|
||||
property SerializationAction: TMVCSerializationAction read fSerializationAction;
|
||||
property DataSetSerializationAction: TMVCDataSetSerializationAction read fDataSetSerializationAction;
|
||||
property DataSetFieldNameCase: TMVCNameCase read fDataSetFieldNameCase;
|
||||
property DataSetSerializationType: TMVCDatasetSerializationType read fDataSetSerializationType;
|
||||
end;
|
||||
strict private
|
||||
function GetItem(const Key: string): TMVCObjectDictionaryValueItem;
|
||||
@ -375,9 +377,8 @@ type
|
||||
: IMVCObjectDictionary; overload;
|
||||
function Add(const Name: string; const Value: TDataset;
|
||||
const SerializationAction: TMVCDataSetSerializationAction = nil;
|
||||
const DataSetSerializationType: TMVCDatasetSerializationType = dstAllRecords;
|
||||
const NameCase: TMVCNameCase = TMVCNameCase.ncLowerCase): IMVCObjectDictionary; overload;
|
||||
function Add(const Name: string; const Value: IInterface;
|
||||
const SerializationAction: TMVCSerializationAction = nil): IMVCObjectDictionary; overload;
|
||||
function TryGetValue(const Name: string; out Value: TObject): Boolean; overload;
|
||||
function Count: Integer;
|
||||
function ContainsKey(const Key: string): Boolean;
|
||||
@ -1551,16 +1552,12 @@ begin
|
||||
end;
|
||||
|
||||
function TMVCObjectDictionary.Add(const Name: string; const Value: TDataset;
|
||||
const SerializationAction: TMVCDataSetSerializationAction; const NameCase: TMVCNameCase): IMVCObjectDictionary;
|
||||
const SerializationAction: TMVCDataSetSerializationAction;
|
||||
const DataSetSerializationType: TMVCDatasetSerializationType;
|
||||
const NameCase: TMVCNameCase): IMVCObjectDictionary;
|
||||
begin
|
||||
fDict.Add(name, TMVCObjectDictionaryValueItem.Create(fOwnsValueItemData, Value, SerializationAction, NameCase));
|
||||
Result := Self;
|
||||
end;
|
||||
|
||||
function TMVCObjectDictionary.Add(const Name: string; const Value: IInterface;
|
||||
const SerializationAction: TMVCSerializationAction): IMVCObjectDictionary;
|
||||
begin
|
||||
fDict.Add(name, TMVCObjectDictionaryValueItem.Create(false, TObject(Value), SerializationAction));
|
||||
fDict.Add(name, TMVCObjectDictionaryValueItem.Create(fOwnsValueItemData, Value, SerializationAction,
|
||||
DataSetSerializationType, NameCase));
|
||||
Result := Self;
|
||||
end;
|
||||
|
||||
@ -1632,10 +1629,13 @@ end;
|
||||
|
||||
constructor TMVCObjectDictionary.TMVCObjectDictionaryValueItem.Create(
|
||||
const Owns: Boolean; const Data: TDataset;
|
||||
const SerializationAction: TMVCDataSetSerializationAction; const NameCase: TMVCNameCase);
|
||||
const SerializationAction: TMVCDataSetSerializationAction;
|
||||
const DataSetSerializationType: TMVCDatasetSerializationType;
|
||||
const NameCase: TMVCNameCase);
|
||||
begin
|
||||
Create(Owns, Data, nil);
|
||||
fDataSetFieldNameCase := NameCase;
|
||||
fDataSetSerializationType := DataSetSerializationType;
|
||||
fDataSetSerializationAction := SerializationAction;
|
||||
end;
|
||||
|
||||
|
@ -453,9 +453,26 @@ begin
|
||||
end
|
||||
else if lObj.Data is TDataSet then
|
||||
begin
|
||||
fCurrentSerializer.InternalSerializeDataSet(TDataSet(lObj.Data), lOutObject.A[lName], [],
|
||||
lObj.DataSetFieldNameCase,
|
||||
lObj.DataSetSerializationAction)
|
||||
case lObj.DataSetSerializationType of
|
||||
dstSingleRecord:
|
||||
begin
|
||||
fCurrentSerializer.InternalSerializeDataSetRecord(
|
||||
TDataSet(lObj.Data),
|
||||
lOutObject.O[lName],
|
||||
[],
|
||||
lObj.DataSetFieldNameCase,
|
||||
lObj.DataSetSerializationAction)
|
||||
end;
|
||||
dstAllRecords:
|
||||
begin
|
||||
fCurrentSerializer.InternalSerializeDataSet(
|
||||
TDataSet(lObj.Data),
|
||||
lOutObject.A[lName],
|
||||
[],
|
||||
lObj.DataSetFieldNameCase,
|
||||
lObj.DataSetSerializationAction)
|
||||
end;
|
||||
end;
|
||||
end
|
||||
else
|
||||
begin
|
||||
|
@ -28,6 +28,7 @@ unit MVCFramework.Serializer.JsonDataObjects;
|
||||
|
||||
{$I dmvcframework.inc}
|
||||
{$WARN SYMBOL_DEPRECATED OFF}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
@ -129,8 +130,9 @@ type
|
||||
function SerializeDataSet(const ADataSet: TDataSet; const AIgnoredFields: TMVCIgnoredList = [];
|
||||
const ANameCase: TMVCNameCase = ncAsIs; const ASerializationAction: TMVCDatasetSerializationAction = nil): string;
|
||||
|
||||
function SerializeDataSetRecord(const ADataSet: TDataSet; const AIgnoredFields: TMVCIgnoredList = [];
|
||||
const ANameCase: TMVCNameCase = ncAsIs; const ASerializationAction: TMVCDatasetSerializationAction = nil): string;
|
||||
function SerializeDataSetRecord(const DataSet: TDataSet;
|
||||
const IgnoredFields: TMVCIgnoredList = []; const NameCase: TMVCNameCase = ncAsIs;
|
||||
const SerializationAction: TMVCDatasetSerializationAction = nil): string;
|
||||
|
||||
procedure DeserializeObject(const ASerializedObject: string; const AObject: TObject;
|
||||
const AType: TMVCSerializationType = stDefault; const AIgnoredAttributes: TMVCIgnoredList = []); overload;
|
||||
@ -152,6 +154,11 @@ type
|
||||
const ANameCase: TMVCNameCase;
|
||||
const ASerializationAction: TMVCDatasetSerializationAction);
|
||||
|
||||
procedure InternalSerializeDataSetRecord(
|
||||
const DataSet: TDataSet; const JSONObject: TJsonObject;
|
||||
const IgnoredFields: TMVCIgnoredList; const NameCase: TMVCNameCase;
|
||||
const SerializationAction: TMVCDatasetSerializationAction);
|
||||
|
||||
procedure DeserializeDataSetRecord(const ASerializedDataSetRecord: string; const ADataSet: TDataSet;
|
||||
const AIgnoredFields: TMVCIgnoredList = []; const ANameCase: TMVCNameCase = ncAsIs);
|
||||
class function ParseObject(const AString: string): TJDOJsonObject;
|
||||
@ -1467,6 +1474,32 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TMVCJsonDataObjectsSerializer.InternalSerializeDataSetRecord(
|
||||
const DataSet: TDataSet; const JSONObject: TJsonObject;
|
||||
const IgnoredFields: TMVCIgnoredList; const NameCase: TMVCNameCase;
|
||||
const SerializationAction: TMVCDatasetSerializationAction);
|
||||
var
|
||||
lNameCase: TMVCNameCase;
|
||||
lDataSetFields: TList<TMVCDataSetField>;
|
||||
lLinks: IMVCLinks;
|
||||
begin
|
||||
lNameCase := GetNameCase(DataSet, NameCase);
|
||||
lDataSetFields := GetDataSetFields(DataSet, IgnoredFields, lNameCase);
|
||||
try
|
||||
DataSetToJsonObject(DataSet, JSONObject, ncAsIs { lNameCase } , IgnoredFields, lDataSetFields);
|
||||
lLinks := TJDOLinks.Create;
|
||||
if Assigned(SerializationAction) then
|
||||
begin
|
||||
SerializationAction(DataSet, lLinks);
|
||||
TJDOLinks(lLinks).FillJSONArray(JSONObject.A[
|
||||
TMVCSerializerHelper.ApplyNameCase(lNameCase, TMVCConstants.HATEOAS_PROP_NAME)
|
||||
]);
|
||||
end;
|
||||
finally
|
||||
lDataSetFields.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
class
|
||||
function TMVCJsonDataObjectsSerializer.Parse<T>(const AString: string): T;
|
||||
begin
|
||||
@ -1567,76 +1600,24 @@ begin
|
||||
finally
|
||||
JsonArray.Free;
|
||||
end;
|
||||
|
||||
// lLinks := nil;
|
||||
// if Assigned(ASerializationAction) then
|
||||
// begin
|
||||
// lLinks := TJDOLinks.Create;
|
||||
// end;
|
||||
// lDataSetFields := GetDataSetFields(ADataSet, AIgnoredFields, ANameCase);
|
||||
// try
|
||||
// JsonArray := TJDOJsonArray.Create;
|
||||
// try
|
||||
// BookMark := ADataSet.BookMark;
|
||||
// lNameCase := GetNameCase(ADataSet, ANameCase);
|
||||
// ADataSet.First;
|
||||
// while not ADataSet.Eof do
|
||||
// begin
|
||||
// LJObj := JsonArray.AddObject;
|
||||
// DataSetToJsonObject(ADataSet, LJObj, lNameCase, AIgnoredFields, lDataSetFields);
|
||||
// if Assigned(ASerializationAction) then
|
||||
// begin
|
||||
// lLinks.Clear;
|
||||
// ASerializationAction(ADataSet, lLinks);
|
||||
// TJDOLinks(lLinks).FillJSONArray(LJObj.A[TMVCConstants.HATEOAS_PROP_NAME]);
|
||||
// end;
|
||||
// ADataSet.Next;
|
||||
// end;
|
||||
// Result := JsonArray.ToJSON(True);
|
||||
// finally
|
||||
// JsonArray.Free;
|
||||
// if ADataSet.BookmarkValid(BookMark) then
|
||||
// ADataSet.GotoBookmark(BookMark);
|
||||
// ADataSet.FreeBookmark(BookMark);
|
||||
// end;
|
||||
// finally
|
||||
// lDataSetFields.Free;
|
||||
// end;
|
||||
end;
|
||||
|
||||
function TMVCJsonDataObjectsSerializer.SerializeDataSetRecord(const ADataSet: TDataSet;
|
||||
const AIgnoredFields: TMVCIgnoredList = []; const ANameCase: TMVCNameCase = ncAsIs;
|
||||
const ASerializationAction: TMVCDatasetSerializationAction = nil): string;
|
||||
function TMVCJsonDataObjectsSerializer.SerializeDataSetRecord(const DataSet: TDataSet;
|
||||
const IgnoredFields: TMVCIgnoredList = []; const NameCase: TMVCNameCase = ncAsIs;
|
||||
const SerializationAction: TMVCDatasetSerializationAction = nil): string;
|
||||
var
|
||||
JsonObject: TJDOJsonObject;
|
||||
lDataSetFields: TMVCDataSetFields;
|
||||
lLinks: IMVCLinks;
|
||||
lNameCase: TMVCNameCase;
|
||||
lJSONObject: TJDOJsonObject;
|
||||
begin
|
||||
Result := EmptyStr;
|
||||
if (not Assigned(ADataSet)) or ADataSet.IsEmpty then
|
||||
if (not Assigned(DataSet)) or DataSet.IsEmpty then
|
||||
Exit('null');
|
||||
|
||||
lNameCase := GetNameCase(ADataSet, ANameCase);
|
||||
lDataSetFields := GetDataSetFields(ADataSet, AIgnoredFields, lNameCase);
|
||||
lJSONObject := TJDOJsonObject.Create;
|
||||
try
|
||||
JsonObject := TJDOJsonObject.Create;
|
||||
try
|
||||
DataSetToJsonObject(ADataSet, JsonObject, lNameCase, AIgnoredFields, lDataSetFields);
|
||||
lLinks := TJDOLinks.Create;
|
||||
if Assigned(ASerializationAction) then
|
||||
begin
|
||||
ASerializationAction(ADataSet, lLinks);
|
||||
TJDOLinks(lLinks).FillJSONArray(JsonObject.A[
|
||||
TMVCSerializerHelper.ApplyNameCase(lNameCase, TMVCConstants.HATEOAS_PROP_NAME)
|
||||
]);
|
||||
end;
|
||||
Result := JsonObject.ToJSON(True);
|
||||
finally
|
||||
JsonObject.Free;
|
||||
end;
|
||||
InternalSerializeDataSetRecord(DataSet, lJSONObject, IgnoredFields, NameCase, SerializationAction);
|
||||
Result := lJSONObject.ToJSON(True);
|
||||
finally
|
||||
lDataSetFields.Free;
|
||||
lJSONObject.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
@ -1720,7 +1701,7 @@ function TMVCJsonDataObjectsSerializer.SerializeObjectToJSON(const AObject: TObj
|
||||
const AIgnoredAttributes: TMVCIgnoredList; const ASerializationAction: TMVCSerializationAction)
|
||||
: TJDOJsonObject;
|
||||
var
|
||||
JsonObject: TJDOJsonObject;
|
||||
JSONObject: TJDOJsonObject;
|
||||
ObjType: TRttiType;
|
||||
begin
|
||||
if not Assigned(AObject) then
|
||||
@ -1743,11 +1724,11 @@ begin
|
||||
|
||||
if GetTypeSerializers.ContainsKey(ObjType.Handle) then
|
||||
begin
|
||||
GetTypeSerializers.Items[ObjType.Handle].SerializeRoot(AObject, TObject(JsonObject), []);
|
||||
GetTypeSerializers.Items[ObjType.Handle].SerializeRoot(AObject, TObject(JSONObject), []);
|
||||
try
|
||||
Result := JsonObject;
|
||||
Result := JSONObject;
|
||||
except
|
||||
JsonObject.Free;
|
||||
JSONObject.Free;
|
||||
raise;
|
||||
end;
|
||||
Exit;
|
||||
@ -1985,7 +1966,7 @@ procedure TMVCJsonDataObjectsSerializer.DeserializeObject(const ASerializedObjec
|
||||
const AObject: TObject;
|
||||
const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList);
|
||||
var
|
||||
JsonObject: TJDOJsonObject;
|
||||
JSONObject: TJDOJsonObject;
|
||||
JsonBase: TJsonBaseObject;
|
||||
begin
|
||||
if (ASerializedObject = EmptyStr) then
|
||||
@ -2001,7 +1982,7 @@ begin
|
||||
raise EMVCSerializationException.CreateFmt('Invalid JSON. Expected %s got %s',
|
||||
[TJDOJsonObject.ClassName, JsonBase.ClassName]);
|
||||
end;
|
||||
JsonObject := TJDOJsonObject(JsonBase);
|
||||
JSONObject := TJDOJsonObject(JsonBase);
|
||||
except
|
||||
on E: EJsonParserException do
|
||||
begin
|
||||
@ -2011,14 +1992,14 @@ begin
|
||||
try
|
||||
if GetTypeSerializers.ContainsKey(AObject.ClassInfo) then
|
||||
begin
|
||||
GetTypeSerializers.Items[AObject.ClassInfo].DeserializeRoot(JsonObject, AObject, []);
|
||||
GetTypeSerializers.Items[AObject.ClassInfo].DeserializeRoot(JSONObject, AObject, []);
|
||||
end
|
||||
else
|
||||
begin
|
||||
JsonObjectToObject(JsonObject, AObject, GetSerializationType(AObject, AType), AIgnoredAttributes);
|
||||
JsonObjectToObject(JSONObject, AObject, GetSerializationType(AObject, AType), AIgnoredAttributes);
|
||||
end;
|
||||
finally
|
||||
JsonObject.Free;
|
||||
JSONObject.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
2
tasks.py
2
tasks.py
@ -272,7 +272,7 @@ def tests(ctx, delphi_version=DEFAULT_DELPHI_VERSION):
|
||||
# os.spawnl(os.P_NOWAIT, r"unittests\general\TestServer\Win32\Debug\TestServer.exe")
|
||||
import subprocess
|
||||
print("\nExecuting tests...")
|
||||
subprocess.Popen([r"unittests\general\TestServer\Win32\Debug\TestServer.exe"])
|
||||
subprocess.Popen([r"unittests\general\TestServer\bin\TestServer.exe"])
|
||||
r = subprocess.run([r"unittests\general\Several\bin\DMVCFrameworkTests.exe"])
|
||||
subprocess.run(["taskkill", "/f", "/im", "TestServer.exe"])
|
||||
if r.returncode > 0:
|
||||
|
@ -123,6 +123,8 @@ type
|
||||
[Test]
|
||||
procedure TestRenderStreamAndFreeWithOwnerTrue;
|
||||
[Test]
|
||||
procedure TestObjectDict;
|
||||
[Test]
|
||||
procedure TestGetImagePng;
|
||||
[Test]
|
||||
procedure TestProducesConsumes01;
|
||||
@ -953,6 +955,54 @@ begin
|
||||
Assert.AreNotEqual('', r.HeaderValue('request_gen_time'));
|
||||
end;
|
||||
|
||||
procedure TServerTest.TestObjectDict;
|
||||
var
|
||||
lRes: IRESTResponse;
|
||||
lJSON: TJSONObject;
|
||||
begin
|
||||
lRes := RESTClient.doGET('/objectdict', []);
|
||||
Assert.areEqual<Integer>(HTTP_STATUS.OK, lRes.ResponseCode, lRes.BodyAsString);
|
||||
lJSON := StrToJSONObject(lRes.BodyAsString);
|
||||
try
|
||||
Assert.isTrue(lJSON.Contains('ncCamelCase_Single'), lJSON.ToJSON());
|
||||
Assert.isTrue(lJSON.Contains('ncLowerCase_Single'), lJSON.ToJSON());
|
||||
Assert.isTrue(lJSON.Contains('ncPascalCase_Single'), lJSON.ToJSON());
|
||||
Assert.isTrue(lJSON.Contains('ncUpperCase_Single'), lJSON.ToJSON());
|
||||
|
||||
Assert.isTrue(lJSON.Contains('ncCamelCase_List'), lJSON.ToJSON());
|
||||
Assert.isTrue(lJSON.Contains('ncLowerCase_List'), lJSON.ToJSON());
|
||||
Assert.isTrue(lJSON.Contains('ncPascalCase_List'), lJSON.ToJSON());
|
||||
Assert.isTrue(lJSON.Contains('ncUpperCase_List'), lJSON.ToJSON());
|
||||
|
||||
Assert.areEqual(jdtObject, lJSON.Types['ncCamelCase_Single']);
|
||||
Assert.areEqual(jdtObject, lJSON.Types['ncLowerCase_Single']);
|
||||
Assert.areEqual(jdtObject, lJSON.Types['ncPascalCase_Single']);
|
||||
Assert.areEqual(jdtObject, lJSON.Types['ncUpperCase_Single']);
|
||||
|
||||
Assert.isTrue(lJSON.O['ncCamelCase_Single'].Contains('custNo'), lJSON.O['ncCamelCase_Single'].ToJSON());
|
||||
Assert.isTrue(lJSON.O['ncLowerCase_Single'].Contains('cust_no'), lJSON.O['ncLowerCase_Single'].ToJSON());
|
||||
Assert.isTrue(lJSON.O['ncPascalCase_Single'].Contains('CustNo'), lJSON.O['ncPascalCase_Single'].ToJSON());
|
||||
Assert.isTrue(lJSON.O['ncUpperCase_Single'].Contains('CUST_NO'), lJSON.O['ncUpperCase_Single'].ToJSON());
|
||||
|
||||
Assert.areEqual(jdtArray, lJSON.Types['ncCamelCase_List']);
|
||||
Assert.areEqual(jdtArray, lJSON.Types['ncLowerCase_List']);
|
||||
Assert.areEqual(jdtArray, lJSON.Types['ncPascalCase_List']);
|
||||
Assert.areEqual(jdtArray, lJSON.Types['ncUpperCase_List']);
|
||||
|
||||
Assert.isTrue(lJSON.A['ncCamelCase_List'][0].ObjectValue.Contains('custNo'),
|
||||
lJSON.A['ncCamelCase_List'][0].ObjectValue.ToJSON());
|
||||
Assert.isTrue(lJSON.A['ncLowerCase_List'][0].ObjectValue.Contains('cust_no'),
|
||||
lJSON.A['ncLowerCase_List'][0].ObjectValue.ToJSON());
|
||||
Assert.isTrue(lJSON.A['ncPascalCase_List'][0].ObjectValue.Contains('CustNo'),
|
||||
lJSON.A['ncPascalCase_List'][0].ObjectValue.ToJSON());
|
||||
Assert.isTrue(lJSON.A['ncUpperCase_List'][0].ObjectValue.Contains('CUST_NO'),
|
||||
lJSON.A['ncUpperCase_List'][0].ObjectValue.ToJSON());
|
||||
|
||||
finally
|
||||
lJSON.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
// procedure TServerTest.TestPATCHWithParamsAndJSONBody;
|
||||
// var
|
||||
// r: IRESTResponse;
|
||||
@ -1676,6 +1726,7 @@ var
|
||||
lRes: IRESTResponse;
|
||||
begin
|
||||
lRes := RESTClient.Accept(TMVCMediaType.TEXT_PLAIN).doGET('/website/list', []);
|
||||
Assert.areEqual(HTTP_STATUS.OK, lRes.ResponseCode, lRes.BodyAsString);
|
||||
var
|
||||
lLines := lRes.BodyAsString.Split([sLineBreak]);
|
||||
var lCount: Integer := 1001;
|
||||
@ -1692,7 +1743,6 @@ begin
|
||||
Assert.areEqual(lCount, lLinePieces[0].ToInteger);
|
||||
Inc(lCount);
|
||||
end;
|
||||
Assert.areEqual(HTTP_STATUS.OK, lRes.ResponseCode);
|
||||
end;
|
||||
|
||||
procedure TServerTest.TestWrongJSONBody;
|
||||
|
@ -3,7 +3,7 @@
|
||||
<ProjectGuid>{5AE76D67-6F30-42AF-8BDE-E11D109DC496}</ProjectGuid>
|
||||
<MainSource>TestServer.dpr</MainSource>
|
||||
<Base>True</Base>
|
||||
<Config Condition="'$(Config)'==''">Debug</Config>
|
||||
<Config Condition="'$(Config)'==''">CI</Config>
|
||||
<TargetedPlatforms>36993</TargetedPlatforms>
|
||||
<AppType>Console</AppType>
|
||||
<FrameworkType>None</FrameworkType>
|
||||
@ -39,7 +39,7 @@
|
||||
<Cfg_1>true</Cfg_1>
|
||||
<Base>true</Base>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Config)'=='Debug' or '$(Cfg_2)'!=''">
|
||||
<PropertyGroup Condition="'$(Config)'=='CI' or '$(Cfg_2)'!=''">
|
||||
<Cfg_2>true</Cfg_2>
|
||||
<CfgParent>Base</CfgParent>
|
||||
<Base>true</Base>
|
||||
@ -111,6 +111,8 @@
|
||||
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=;ProgramID=com.embarcadero.$(MSBuildProjectName)</VerInfo_Keys>
|
||||
<VerInfo_Locale>1033</VerInfo_Locale>
|
||||
<Icon_MainIcon>TestServer_Icon.ico</Icon_MainIcon>
|
||||
<Manifest_File>(None)</Manifest_File>
|
||||
<DCC_ExeOutput>.\bin</DCC_ExeOutput>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Base_Win64)'!=''">
|
||||
<Icon_MainIcon>TestServer_Icon.ico</Icon_MainIcon>
|
||||
@ -122,11 +124,9 @@
|
||||
<DCC_SymbolReferenceInfo>0</DCC_SymbolReferenceInfo>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Cfg_1_Win32)'!=''">
|
||||
<DCC_ExeOutput>.\$(Platform)\$(Config)</DCC_ExeOutput>
|
||||
<VerInfo_Locale>1033</VerInfo_Locale>
|
||||
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=;ProgramID=com.embarcadero.$(MSBuildProjectName)</VerInfo_Keys>
|
||||
<Icon_MainIcon>TestServer_Icon.ico</Icon_MainIcon>
|
||||
<Manifest_File>(None)</Manifest_File>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Cfg_2)'!=''">
|
||||
<DCC_Define>DEBUG;$(DCC_Define)</DCC_Define>
|
||||
@ -140,11 +140,9 @@
|
||||
<BT_BuildType>Debug</BT_BuildType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Cfg_2_Win32)'!=''">
|
||||
<DCC_ExeOutput>.\$(Platform)\$(Config)</DCC_ExeOutput>
|
||||
<VerInfo_Locale>1033</VerInfo_Locale>
|
||||
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=;ProgramID=com.embarcadero.$(MSBuildProjectName)</VerInfo_Keys>
|
||||
<Icon_MainIcon>TestServer_Icon.ico</Icon_MainIcon>
|
||||
<Manifest_File>(None)</Manifest_File>
|
||||
<DCC_DcuOutput>.\dcus\$(Platform)\$(Config)</DCC_DcuOutput>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
@ -166,7 +164,7 @@
|
||||
<DCCReference Include="..\..\..\sources\MVCFramework.JSONRPC.pas"/>
|
||||
<DCCReference Include="..\..\..\samples\commons\RandomUtilsU.pas"/>
|
||||
<DCCReference Include="..\..\..\sources\MVCFramework.Serializer.JsonDataObjects.OptionalCustomTypes.pas"/>
|
||||
<BuildConfiguration Include="Debug">
|
||||
<BuildConfiguration Include="CI">
|
||||
<Key>Cfg_2</Key>
|
||||
<CfgParent>Base</CfgParent>
|
||||
</BuildConfiguration>
|
||||
|
@ -30,7 +30,7 @@ interface
|
||||
uses
|
||||
MVCFramework,
|
||||
System.SysUtils,
|
||||
MVCFramework.Commons;
|
||||
MVCFramework.Commons, FireDAC.Comp.Client, Data.DB;
|
||||
|
||||
type
|
||||
|
||||
@ -38,8 +38,11 @@ type
|
||||
TTestServerController = class(TMVCController)
|
||||
private
|
||||
FFormatSettings: TFormatSettings;
|
||||
fDataSet: TFDMemTable;
|
||||
protected
|
||||
procedure MVCControllerAfterCreate; override;
|
||||
function GetDataSet: TDataSet;
|
||||
procedure MVCControllerBeforeDestroy; override;
|
||||
public
|
||||
[MVCPath('/req/with/params/($par1)/($par2)/($par3)')]
|
||||
[MVCHTTPMethod([httpGET, httpDELETE])]
|
||||
@ -199,6 +202,9 @@ type
|
||||
[MVCHTTPMethod([httpGET])]
|
||||
procedure TestGetImagePng;
|
||||
|
||||
[MVCPath('/objectdict')]
|
||||
procedure TestObjectDict;
|
||||
|
||||
// Nullables Tests
|
||||
[MVCHTTPMethod([httpPOST])]
|
||||
[MVCPath('/nullables/pingpong')]
|
||||
@ -280,7 +286,7 @@ uses
|
||||
MVCFramework.Serializer.Defaults,
|
||||
MVCFramework.DuckTyping,
|
||||
System.IOUtils,
|
||||
System.Classes, FireDAC.Comp.Client;
|
||||
System.Classes;
|
||||
|
||||
{ TTestServerController }
|
||||
|
||||
@ -357,6 +363,12 @@ begin
|
||||
|
||||
end;
|
||||
|
||||
function TTestServerController.GetDataSet: TDataSet;
|
||||
begin
|
||||
Result := TFDMemTable.Create(nil);
|
||||
TFDMemTable(Result).LoadFromFile(TPath.Combine(AppPath, 'customers.json'));
|
||||
end;
|
||||
|
||||
procedure TTestServerController.GetImage;
|
||||
begin
|
||||
// do nothing
|
||||
@ -390,6 +402,12 @@ begin
|
||||
FFormatSettings.DecimalSeparator := '.';
|
||||
end;
|
||||
|
||||
procedure TTestServerController.MVCControllerBeforeDestroy;
|
||||
begin
|
||||
inherited;
|
||||
|
||||
end;
|
||||
|
||||
procedure TTestServerController.ReqWithParams;
|
||||
begin
|
||||
Render(TJSONObject.Create.AddPair('par1', Context.Request.Params['par1']).AddPair('par2',
|
||||
@ -474,8 +492,7 @@ var
|
||||
lFName: string;
|
||||
begin
|
||||
ContentType := TMVCMediaType.IMAGE_PNG;
|
||||
lFName := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '..\..') + '\sample.png';
|
||||
// Render(TFile.OpenRead('..\..\sample.png'));
|
||||
lFName := TPath.Combine(AppPath, 'sample.png');
|
||||
Render(TFile.OpenRead(lFName));
|
||||
end;
|
||||
|
||||
@ -591,6 +608,23 @@ begin
|
||||
Render(Context.Request.Params['id']);
|
||||
end;
|
||||
|
||||
procedure TTestServerController.TestObjectDict;
|
||||
var
|
||||
lDict: IMVCObjectDictionary;
|
||||
begin
|
||||
lDict := ObjectDict(false)
|
||||
.Add('ncUpperCase_List', GetDataSet, nil, dstAllRecords, ncUpperCase)
|
||||
.Add('ncLowerCase_List', GetDataSet, nil, dstAllRecords, ncLowerCase)
|
||||
.Add('ncCamelCase_List', GetDataSet, nil, dstAllRecords, ncCamelCase)
|
||||
.Add('ncPascalCase_List', GetDataSet, nil, dstAllRecords, ncPascalCase)
|
||||
.Add('ncUpperCase_Single', GetDataSet, nil, dstSingleRecord, ncUpperCase)
|
||||
.Add('ncLowerCase_Single', GetDataSet, nil, dstSingleRecord, ncLowerCase)
|
||||
.Add('ncCamelCase_Single', GetDataSet, nil, dstSingleRecord, ncCamelCase)
|
||||
.Add('ncPascalCase_Single', GetDataSet, nil, dstSingleRecord, ncPascalCase)
|
||||
.Add('meta', StrDict(['page'], ['1']));
|
||||
Render(lDict);
|
||||
end;
|
||||
|
||||
procedure TTestServerController.TestPOSTObject;
|
||||
var
|
||||
Person: TPerson;
|
||||
@ -752,7 +786,7 @@ var
|
||||
begin
|
||||
lDS := TFDMemTable.Create(nil);
|
||||
try
|
||||
var lFName: string := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '..\..') + '\customers.json';
|
||||
var lFName: string := TPath.Combine(AppPath, 'customers.json');
|
||||
lDS.LoadFromFile(lFName);
|
||||
ViewDataset['customers'] := lDS;
|
||||
ViewData['customers2'] := lDS;
|
||||
|
@ -75,8 +75,8 @@ begin
|
||||
// no config here
|
||||
Config[TMVCConfigKey.SessionTimeout] := '0'; // setting cookie
|
||||
Config[TMVCConfigKey.PathPrefix] := '';
|
||||
Config[TMVCConfigKey.DocumentRoot] := '..\..\www';
|
||||
Config[TMVCConfigKey.ViewPath] := '..\..\templates';
|
||||
Config[TMVCConfigKey.DocumentRoot] := '..\www';
|
||||
Config[TMVCConfigKey.ViewPath] := '..\templates';
|
||||
Config[TMVCConfigKey.DefaultViewFileExtension] := 'html';
|
||||
end, nil);
|
||||
MVCEngine.AddController(TTestServerController)
|
||||
|
Before Width: | Height: | Size: 249 B After Width: | Height: | Size: 249 B |
Loading…
Reference in New Issue
Block a user