2

My shipment has an array of products, but I don't understand how to reference to do that array of product. I am trying to setup the unit test enviroment and need to make test data available.

shipment module

asset Shipment identified by shipmentId{
  o String shipmentId
  --> Product[] allProducts
  --> participant owner
}

and unit-test

const shipment = factory.newResource(namespace, 'Shipment', '001');
shipment.allProducts = factory.newRelationship(namespace, participant, /** what to do here */ )

or can I just pass an array of products like:

shipment.allProducts = products

The reason I am doubting this solution is because I need to use the factory newRelationship function. If you have a suggestion, that would be helpful.

deltu100
  • 581
  • 7
  • 26
  • Yes, you can copy the array with `shipment.allProducts = products.slice();` – Riccardo May 31 '19 at 14:46
  • And then the reference will stay intact? – deltu100 May 31 '19 at 15:06
  • If you use `.slice()` you clone the array in another variable, without it you only copy the reference to the original array (products). This means that if you don't use slice() and you modify `shipment.allProducts` the changes also affects the array `products` – Riccardo May 31 '19 at 15:20
  • But that is what I want, that is why I have a referenced it. So those assets should belong to the owner and he should be able to change it how he likes – deltu100 May 31 '19 at 15:23
  • @RiccardoBonesi the unit test says "model violation in Shipment#001 expected a Relationship" – deltu100 Jun 02 '19 at 13:25

1 Answers1

5

Alright I finally found the solution. My error message was:

Instance org.trader.network.Shipment#001 has property allProducts with type org.trader.network.Product that is not derived from org.trader.network.Product[]

If you want to unit test and add some data to a referenced array type then do this:

    shipment.allProducts= [factory.newRelationship(
        namespace,
        'Product',
        product.$identifier
    )];

Notice the brackets around the factory. I needed to make an array of relationships for product.

I only needed one product for my unit test, but if you want more, just add more relationships of a product in between the brackets.

I found the solution by looking at random github repository (source:here)

deltu100
  • 581
  • 7
  • 26