How to print the query result into the console | Robot framework - robotframework

I have the following code:
*** Test Cases ***
Testing Connect to SQL Server
${queryTest} Execute Sql String SELECT * FROM users where Id='1'
#log to console ${queryTest} //This print NONE
#${query_results} SeleniumLibrary.Get Text ${queryTest}
#log to console ${query_results}
#${value}= Set Variable ${queryTest[0][0]}
log to console ${value}
log to console should display result
${rowCount}= Row Count SELECT * FROM users where Id='1'
log to console ${rowCount}
#rowCount print 1
What I'm trying to do is printing the query's result in the console

I think Execute Sql String doesn't return anything. There's no return statement in the method, and documentation doesn't mention such an example either (even though they mention a select statement with this keyword, which might be what leads people to believe they eventually get some rows back).
Try using Query keyword:
#{queryTest} Query SELECT * FROM users where Id='1'

Related

Why is my Airflow MySqlOperator Insert Command Denied?

I'm running a dag with an insert query. Here is some of the code:
QUERY = '''
INSERT INTO bi.target_skus (skus)
SELECT
distinct od.sku,
FROM
bi.orders as od'''
t1 = MySqlOperator(
sql=QUERY,
mysql_conn_id = MYSQL_CONN_ID,
task_id='target_skus',
dag=dag)
It's giving me the following error:
ERROR - (1142, "INSERT command denied to user 'xyz' for table 'target_skus'")
A few notes:
Devops said my user has permission to make inserts into that table
Select commands work fine
The error message does not include the database name (bi) even though my insert query does.
This looks like a standard MySQL "not enough privileges" error.
Are you sure you can perform INSERTs with your user, regardless of what your DBA is saying? You should test the same operation using another tool (like MySQL Workbench) setting up the connection in the same way you set it up in Airflow, i.e. same user, same password, same default schema.
It looks like a privilege error from the user trying to insert but there is a comma in the insert that can cause problems too:
QUERY = '''
INSERT INTO bi.target_skus (skus)
SELECT
distinct od.sku
FROM
bi.orders as od'''

Obtain results from sql query only when apply function to field. Is it a bug of Poco::Data?

I'm using Poco::Data (version 1.8.1) to query a sqlite database (version 3.27) compiling with gcc version 7.3.0 for an onion omega2 (mips) running openwrt 18.06.
I have a strange behaviour.
When I execute a query like
select <real field> from ...
I've got a "NotFound" exception (query from command line returns a result, of course).
If I apply a function to it like "avg", for example, I have the correct result.
It doesn't happen with an integer field.
The field name is "psi": I think it is not a reserved word.
Is it a bug of Poco::Data? Is it something I'm doing wrong?
The code I'm using is:
double result;
std::string query = "select <field> from table where <condition on pk>";
session << query, into(result), now;
this code gives NotFound.
Changing string query to:
std::string query = "select avg(<field>) from table where <condition on pk>";
it works.
Likely your query returns more than one result row. In that case, into(result) will fail if result is a scalar value. Try adding a limit(0, 1), as in:
session << query, into(result), limit(1), now;
to only return a single result row.
This may not be what you want, however, as you'll basically get a random result row that matches your query. If you want all, you can use a vector for receiving the result, as in:
std::vector<double> result;
session << query, into(result), now;
There are also other options for obtaining a multi-row result, e.g. stepping through all result rows by executing the statement multiple times or using a RecordSet. See the Binding and RecordSet samples for the details.

How can I choose to show only failed result and result must be fail not pass

there are 22 records on both tables. at records 1-21 it's was right but on record 22 on both table can't compare cause it's not equal. I would like to show result only failed record that's can't compare and let result fail.
connect to database using custom params cx_Oracle ${DB_CONNECT_STRING}
#{queryResults1}= Query Select * from QA_USER.SealTest_Security_A Order by SECURITY_ID
Log ${queryResults1}
#{queryResults2}= Query Select * from QA_USER.SealTest_Security_B Order by SECURITY_ID
Log ${queryResults2}
should be equal ${queryResults1} ${queryResults2}
Disconnect From Database

Robot Framework Database Library InterfaceError: not a query when combining with Builtin Keyword

I'm trying to check if an item is in the database before attempting to delete it as part of my test setup.
The issue: The 'Check if Exists in Database' keyword works on its own, but not when combined with the built in keyword 'Run Keyword and Return Status'.
that gets the error: 'InterfaceError: not a query'
Code is below:
***Settings***
Documentation RF DB Test
Library DatabaseLibrary
***Variables***
${token} '<token>'
***Test Cases***
Set Log Level
Set Log Level TRACE
Connect to DB
Connect To Database Using Custom Params cx_Oracle <connection details>
Cleanup DB
${EntryExists}= Run Keyword and Return Status Check if Exists in Database select * from MY_TABLE where token=${token}
Edit
Thanks for your formatting answers and suggestions folks.
I also received the error for this line
Query delete from MY_TABLE where token=${token}
10:14:40.984 FAIL InterfaceError: not a query
10:14:40.984 DEBUG Traceback (most recent call last):
File "...Python\Python35\lib\site-packages\DatabaseLibrary\query.py", line 56, in query
allRows = cur.fetchall()
Basically, I was trying to use the delete command with the Query keyword, but found that the undocumented keyword 'Execute Sql String' worked
Execute Sql String delete from MY_TABLE where token=${token}
Keep your indentation consistent. I would suggest to use 4 spaces (set your IDE and that's it). I can see 4 spaces, 5 spaces, 2 spaces and most importantly: only one space between Check if Exists in Database and select * from MY_TABLE where token=${token}.
Note that RF takes more than two spaces as delimeter (see: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#plain-text-format) but one space is not enough.
Why did you created separate Test Case for every keyword?
I suppose your Test Case could look like:
***Test Cases***
Nice Name Of My Test Case
[Documentation] Let's not forget to describe
[Setup] Set Log Level TRACE
[Teardown] Disconnect From Database
Connect To Database Using Custom Params cx_Oracle <connection_details>
${entry_exists} Run Keyword And Return Status Check If Exists In Database select * from MY_TABLE where token=${token}
Run Keyword If '${entry_exists}' == 'True' DB Cleanup
Obviously all the current steps aren't enough for any meaningful Test Case, they should probably end up as a Test Setup, but that's, I believe, what you mean.
Add 4 spaces or tab after keyword "Check if Exists in Database" mentioned below.
${EntryExists}= Run Keyword and Return Status Check if Exists in Database select * from MY_TABLE where token=${token}
"Check if Exists in Database" keyword will return Boolean value(Pass/ Fail). So you don't required to use "Run Keyword And Return Status"

RowCount,Table must Exist, Delete All Rows from Table keywords from Robotframework

I am new to robotframework and I am trying to get the hang of the keywords of DatabaseLibrary. I am getting error at 3 of such keywords.
1) I am using rowcount keywords as below-
${rowCount} Row Count <sql query>
And I always get ${rowCount}=0 irrespective of the number of rows in my table.
2) I am using Delete All Rows From Table as below-
Delete All Rows From Table <Table_Name>
And I get ORA-00911: invalid character but if use the same table with other keywords like Query ,it works fine.
3) I am using Table Must Exist as below-
Table Must Exist <Table_Name>
And I get ORA-00942: table or view does not exist but this table is very much there.
Please help me find what am I doing wrong.
Thanks in Advance!!!
I could be wrong but I believe a colleague told me there were issues, at the very least with the Row Count keyword.
However, for all three options there are easy solutions, which you've even hinted at in your question by using Query or Execute SQL Script
1)
${result}= Query Select count(id) from table
${rc} = ${result[0][0]} #Play with this as I forget exact syntax
2) Put your delete script in a test scripts folder with your tests and call it using Execute SQL script. You could also use Query to perform a select query before and after to confirm expected states.
3) Again perform a query against the table you're expecting to be there, a simple row count on id would do for this purpose. You could set a variable based on the result and use this again later if required.
I had similar issues.
I use cx_Oracle.
With the Table Must Exist keyword my problem was the same.
I dont really understand why, but first I have to use Encode String to Bytes keyword.
And I need to use a DatabaseLibrary 0.8 at least, because earlier versions didnt have solution for cx_Oracle. These solved this issue for me.
But with Delete all rows from table I still have problems.
Because this keyword puts a ; at the end of the line and it passes on that line to execute query if I understand weel, so it still causes an ORA-00911 error for me.
With Execute Sql String and the command DELETE FROM tablename you can have the same results, but it will work this way.
I hope it helps a little

Resources