Error when inserting a UUID into YAML using "!!python/object" - pyyaml

For an automated test script, I would like to generate random UUID values at runtime, so I added some YAML that looks like this:
---
applicant:
idNumbers:
nationalId: !!python/object:uuid.uuid4
However, this generates an error when I try to yaml.load the value:
ConstructorError: expected a mapping node, but found scalar
in "<unicode string>", line 4, column 17:
nationalId: !!python/object:uuid.uuid4
^
How do I inject a UUID value via YAML tags?

I found the error message a bit intimidating at first, but after some thought, I was able to unpack it.
The parser is expecting a "mapping" node, not a scalar. So, what happens if I add a mapping?
>>> yaml.load('''---
... applicant:
... idNumbers:
... nationalId: !!python/object:uuid.uuid4 {}''')
{'applicant': {'idNumbers': {'nationalId': UUID('71e09d1d-e84e-4ea6-855d-be1a2e91b60a')}}}
Additional info: http://yaml.org/type/map.html

Related

Is it possible to use alias for keys in yaml with symfony/yaml?

I've been trying to read the following yaml file using symfony/yaml(v4.4.0) with cakephp3.
But, I get the following error.
Reference "aaa" does not exist in "path to yml" at line xx (near "*aaa:").
Symfony\Component\Yaml\Exception\ParseException
I would like to user the 'aaa' as a key later.
It doesn't work with "*aaa:" and works with "1:".
Basically, is it possible to use alias for keys in yaml file?
Here's the yaml file.
aaa: &aaa 1
bbb: &bbb 2
ccc: &ccc 3
*aaa: # <- this doesn't work and works with '1:'
- *bbb
- *ccc
For general spec-conforming YAML parsers
You need to write it with a space before :.
aaa: &aaa 1
bbb: &bbb 2
ccc: &ccc 3
*aaa :
- *bbb
- *ccc
YAML 1.2 allows : to be part of an anchor and therefore, the line will not be parsed as implicit key if : is written adjacent to the alias (since it becomes part of the alias then).
This has been discussed on the YAML core mailing list.
For Symfony
It seems Symfony parses *aaa: as alias *aaa with : as value indicator. While this is a spec violation, it shouldn't bother us since according to the mailing list, this is more like an oversight in the spec. However, Symfony fails to resolve the alias here, there's nothing much you can do about it but to file an issue for it.

RF 3.0.4 nested dictionary syntax error if first key is number, dot notation doesn't work

I am using rf 3.0.4. I upgraded because of the dot notation upgrade (before I was using rf 2.9).
My problem is when I want to access a nested dictionary item and the first key (it is an id from db) is a number, I got a syntax error.
I have a nested dictionary: &{Attributes}
So what I want to do:
${Attributes.1000.name}
The syntax error I get:
Resolving variable '${Attributes.1000.name}' failed: SyntaxError: invalid syntax (<string>, line 1)
And what is working:
${Attributes["1000"]["name"]}
I'd like to use the dot notation, because it is more readable.
Do any of you know why it doesn't work?
It seems to me to be a limitation of Robot Framework. When a dictionary key item starts with a number (even when a string) then it will fail. In the below two test cases this is shown.
To me this sounds like a defect and you may want to log this as an issue with the project's GitHub issue log.
*** Settings ***
Library Collections
*** Variables ***
${name} MyName
&{person} name=${name}
&{person_valid} A1000=${person} A2000=${person}
&{person_invalid} 1000A=${person} 2000A=${person}
*** Test Cases ***
TC - Valid
${pers} Set Variable ${person_valid.A1000}
Dictionaries Should Be Equal ${pers} ${person}
${pers_name_1} Set Variable ${person_valid["A1000"]["name"]}
Should Be Equal As Strings ${pers_name_1} ${name}
${pers_name_2} Set Variable ${person_valid.A1000.name}
Should Be Equal As Strings ${pers_name_2} ${name}
TC - Fails
Run Keyword And Expect Error
... Resolving variable '\${person_invalid.1000A}' failed: SyntaxError: invalid syntax (<string>, line 1)
... Set Variable ${person_invalid.1000A}
Run Keyword And Expect Error
... Resolving variable '\${person_valid.1000A.name}' failed: SyntaxError: invalid syntax (<string>, line 1)
... Set Variable ${person_valid.1000A.name}

Dictionary as variable in Robot Framework: code runs ok but the IDE yields error

I'm trying to set up a dictionary as a variable (so I can use it as a Resource and access its values from another file) and there is something that is driving me crazy.
Here is the code I have (just for testing purposes):
*** Settings ***
Documentation Suite description
Library Collections
*** Variables ***
&{SOME DICT} key1=value1 key2=value2
*** Test Cases ***
Dict Test # why $ instead of &?
${RANDOM VAR}= Get From Dictionary ${SOME DICT} key1
Log ${RANDOM VAR} WARN
If I run that, I got the expected result ([ WARN ] value1) BUT the IDE (PyCharm) is complaining about that ${SOME DICT} variable is not defined, and the dictionary declaration is not highlighted the same as variable or a list.
If I change that to &{SOME DICT} the IDE won't complain anymore, but the test fails with the following output:
Dict Test | FAIL |
Keyword 'Collections.Get From Dictionary' got positional argument after named arguments.
That is puzzling me to no end: why I have to use a $ instead of a & if it's a dictionary to make it work? Is there something I am doing wrong and it is just running by luck?
Thanks for any advice or guidance you may have!
Have a look into "Get from Dictionary" libdoc,looks like example is showing the same as your working snippet:
Name: Get From Dictionary
Source: Library (Collections)
Arguments: [dictionary, key]
Returns a value from the given ``dictionary`` based on the given ``key``.
If the given ``key`` cannot be found from the ``dictionary``, this
keyword fails.
The given dictionary is never altered by this keyword.
Example:
| ${value} = | Get From Dictionary | ${D3} | b |
=>
| ${value} = 2
Keyword implementation details are as follows:
try:
return dictionary[key]
except KeyError:
raise RuntimeError("Dictionary does not contain key '%s'." % key)
So indeed, Robot sends representation of dict content and not dict name thus value for key can be returned.
This is the same as direct call in python:
a = {u'key1': u'value1', u'key2': u'value2'}
print(a['key1'])
In the end, libdoc for that KW is not straightforward but your PyCharm plugin for Robot does not work properly in this case.
In RED Robot Editor (Eclipse based), proper case does not rise any warnings in editor, wrong-case provides error marker about arguments (better but still not clear what is exactly wrong. Blame minimalistic libdoc info).
ps. I am lead of RED project to be clear.
Simple Example to Use Key Value Variable in robot framework
Set value to dictionary
Get value from dictionary
&{initValues} Create Dictionary key1=value1 key2=value2
Set To Dictionary ${initValues} key1=newvalue1
Set To Dictionary ${initValues} key2=newvalue2
Set To Dictionary ${initValues} key3=newvalue3
${value} Get From Dictionary ${intialValues} key1

How to check if a substring exists inside a 'Result' object with Robot Framework?

I am running the following test inside Robot Framework:
*** Settings ***
Documentation This initializes testrun.robot
Library Process
Library OperatingSystem
Library XML
Library Collections
Output Is A Valid XML File Against JATS format
Start Process xmllint --dtdvalid http://jats.nlm.nih.gov/archiving/1.1d3/JATS-archivearticle1.dtd ./output/nlm/out.xml shell=True
${result}= Wait For Process timeout=45 secs
Log ${result.stdout}
Log ${result.stderr}
Run Keyword Unless '${result.stderr} == ${EMPTY}' Should Contain ${result.stderr} element xref: validity error : IDREFS attribute rid references an unknown
The variable ${result.stderr} is a string that contains the substring 'element xref: validity error : IDREFS attribute rid references an unknown'. As far as I know, I'm not dealing with any whitespace errors or quotation problems, although I could be wrong. (I've been fiddling around with that for a while now.)
Thanks for the help!
Edit: The log that Robot Framework generates tells me that the process has finished (that's how I know what result.stderr contains.)
Consider this statement:
Run Keyword Unless '${result.stderr} == ${EMPTY}' ...
The keyword will never run because you aren't comparing two variables, you are simply checking whether the string literal string '${result.stderr} == ${EMPTY}' is not empty (and it's not, because it has 28 characters).
It is the same as if you did this in python:
if len('${result.stderr} == ${EMPTY}') > 0:
...
You need to put the single quotes around each variable separately so that you are passing a valid expression to the if statement, rather than a string that looks like a valid expression:
Run Keyword Unless '${result.stderr}' == '${EMPTY}' ...

Errors when loading ci-merchant library

I'm trying to use http://ci-merchant.org/ for CodeIgniter. But when I load the merchant library, I get these errors:
A PHP Error was encountered
Severity: Warning
Message: stripos() expects parameter 1 to be string, array given
Filename: libraries/merchant.php
Line Number: 97
A PHP Error was encountered
Severity: Warning
Message: strtolower() expects parameter 1 to be string, array given
Filename: libraries/merchant.php
Line Number: 103
Here is my code:
$this->load->library('merchant');
$this->merchant->load('paypal_express');
Looking at the source of that file, the driver name needs to be passed as a string. So I highly doubt the two lines of code you put above is actually what's being called, it looks like you are passing an array as the driver name.
If you aren't sure where it's coming from, try adding some debug_print_backtrace() lines to the merchant.php file to figure out where the array is getting passed from.

Resources