Accessing test data within Scripts
- Alexander_BernauerOP 2
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-fileoption 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 scriptordaml testand 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 - cocreature 2
--input-filesimply passes the argument as a function argument. Without--input-file,--script-nameis expected to point to an identifier of typeScript a. With--input-file, we expect something of typea -> Script bwhereais the type of the input.At the moment, there is no way to pass an input file to
daml test. - Gary_Verhaegen 3
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 sandboxin 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-fileis the argument given to the function named as--script-name, and--output-filecontains the return value of said function. - Gary_Verhaegen 2
I have made an attempt at clarifying this section of the documentation. Would you mind taking a look? Would this new wording have helped?
- Alexander_Bernauer 1
Yes, @Gary_Verhaegen, this is much clearer now, thank you.