1. Thomas Rolseth
  2. SnapDevelop
  3. Tuesday, 18 February 2020 17:08 PM UTC

I am creating a unit test (XUnit) for a service in SnapDevelop.  The service method takes a DataUnPacker object as a parameter.  This gets passed to the API from PowerBuilder using the jsonpackage object.  My question is how can I populate this in a C# unit test before calling the service?  I have the following code so far -- see below.  My idea was to insert some rows into a datastore, instantiate a new packer object, add the datastore to it and then pass it to the service (in bold).  It doesn't like the datatype however and I can't compile.  Is there a way to convert the packer object to an unpacker object?  Is there a better way to handle this situation?

Thanks, Tom

 

var ds = new DataStore("d_department", context);
ds.Retrieve();

// Inserts an empty row before the first row of datastore.
ds.InsertRow(0);
ds.SetItem(0, "action", "A");

var packer = new DataPacker();

packer.AddDataStore("Departments", ds);

var result = service.GetDepartments(packer, out errmsg);

Logan Liu @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 19 February 2020 18:21 PM UTC
  2. SnapDevelop
  3. # 1

Hi Tom,

"Is there a way to convert the packer object to an unpacker object?"

1) Call IDataPacker.GetTextString(DataFormat format) to get a JSON string, refer to:

https://docs.appeon.com/appeon_online_help/snapobjects/api_reference/SnapObjects.Data/IDataPacker/Method/GetTextString.html

2) Then call DataUnpacker.DataUnpacker(string raw, DataFormat format) Constructor, refer to:

https://docs.appeon.com/appeon_online_help/snapobjects/api_reference/SnapObjects.Data/DataUnpacker/Constructor/DataUnpacker.html

Regards, Logan

Comment
There are no comments made yet.
Thomas Rolseth Accepted Answer Pending Moderation
  1. Friday, 21 February 2020 15:23 PM UTC
  2. SnapDevelop
  3. # 2

Logan, 

Could provide an actual code example of how to use/call DataUnpacker.DataUnpacker(string raw, DataFormat format)?

I got around the problem for one of my unit tests by doing this:

                    string json = ds.ExportJson(MappingMethod.Key);
                     string new_json = "{\"Values\":" + json + "}";

                    //put the json into an unpacker object
                    var unpacker = new DataUnpacker(new_json, DataFormat.Json);

                    //call the service
                    var result = service.HelloWorld(unpacker, out string errmsg);

I tried the same approach for another unit test and it doesn't work.  When I debug the test it gets to this point in the service method:

            var param1 = unpacker.GetValue<string>("dataobject");

When I hover over the 'unpacker' reference I see the raw json and there is a value for the dataobject.  But when I go past this line in the debugger the value of 'param1' is an empty string.  It's not extracting the value for dataobject like I would expect.

Thanks, Tom

Comment
  1. Logan Liu @Appeon
  2. Saturday, 22 February 2020 17:12 PM UTC
You don't need to combine JSON string by yourself.

I think you could call IDataPacker.AddDataStore to add a DataStore:

https://docs.appeon.com/appeon_online_help/powerbuilder/api_reference/PowerBuilder.Data/DataStoreExtensions/Method/AddDataStore1.html

After converting it to IDataUnpacker, call IDataUnpacker.GetDataStore to get a DataStore:

https://docs.appeon.com/appeon_online_help/powerbuilder/api_reference/PowerBuilder.Data/DataStoreExtensions/Method/GetDataStore1.html
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.