Accessing test data within Scripts

Hi

looking at (1) it seems to be common knowledge how to access test data from a Script. But I am failing to figure out how to do that from reading the documentation and this forum.

According to the DAML Script docu (2), the --input-file option serves that purpose. But unless I have been overlooking this repeatedly, it is not clear how the script can access that information. And looking at the docu for the Daml.Script library (3) there seems to be no function that can access that information.

So, is it possible to invoke daml script or daml test and pass in test data in a way so that the scripts can access that test data? If so, how?

Regards,
Alex

(1) Daml Script input data in CSV
(2) Daml Script — Daml SDK 1.12.0 documentation
(3) Module Daml.Script — Daml SDK 1.12.0 documentation

2 Likes

--input-file simply passes the argument as a function argument. Without --input-file, --script-name is expected to point to an identifier of type Script a. With --input-file, we expect something of type a -> Script b where a is the type of the input.

At the moment, there is no way to pass an input file to daml test.

2 Likes

Hi @Alexander_Bernauer,

The answer is contained in the documentation page of Daml Script, but it did take me a few minutes to re-puzzle it out, so I definitely agree it could be more explicit. The mechanism is actually really simple: the input file is used to define the argument to the script function. Here is a complete example. First, Main.daml:

module Main where

import Daml.Script

data InputLine = InputLine with name: Text, age: Int
  deriving (Show, Eq)

setup : [InputLine] -> Script Int
setup input = script do
  return $ sum (map (.age) input)

You can now run (after starting a sandbox with daml sandbox in another terminal):

$ daml build
Compiling t to a DAR.
Created .daml/dist/t-0.0.1.dar
$ RUN="daml script --dar .daml/dist/t-0.0.1.dar --script-name Main:setup --ledger-host localhost --ledger-port 6865 --output-file out.json --input-file"
$ $RUN <(echo '[{"name": "Alice", "age": 23}]')
$ cat out.json 
23
$ $RUN <(echo '[{"name": "Alice", "age": 23}, {"name": "Bob", "age": 25}]')
$ cat out.json 
48
$ 

So the piping is fairly straightforward: --input-file is the argument given to the function named as --script-name, and --output-file contains the return value of said function.

3 Likes

Hi @Alexander_Bernauer,

I have made an attempt at clarifying this section of the documentation. Would you mind taking a look? Would this new wording have helped?

2 Likes

Yes, @Gary_Verhaegen, this is much clearer now, thank you.

1 Like