Get property from union typed member in a test

I am trying to create a unit test where one of my templates has a member details that is typed as

template Record
  with
    ...
    details : RecordDetails
    ...

data RecordDetails 
  = SaleRecord SaleRecordDetails
  | OfferRecord OfferRecordDetails
  ...

data SaleRecordDetails = SaleRecordDetails
  with
    ...
    price : Decimal

In my test case, I want to be able to do a check like:

   ...
   pure (product.price == record.details.price)

I get the error:

No instance for (DA.Internal.Record.HasField “price”…

How can I tell the scenario that the record should have details as SaleRecordDetails? I am receiving my Record object from a choice that creates the Record as a SaleRecord.

Thanks again for the help!

2 Likes

When you define a new variant type, out of the box, the only way to distinguish between variants is pattern matching. So in your case, you may want to make up a function along the lines of:

checkPrice: int -> RecordDetails -> Bool
checkPrice n r = case r of
  SaleRecord details -> n == details.price
  _ -> False
3 Likes

Great, thanks!