tsqlt testing an output parameter from a stored procedure - tsqlt

I have a stored procedure that I am trying test for the proper generation of an output parameter. I experimented with tsqlt.ExpectException, but that did not work. I instead am trying tsqlt.AssertEqualsTable.
CREATE TABLE #actual (msg NVARCHAR(MAX));
CREATE TABLE #expected (msg NVARCHAR(MAX));
INSERT #expected (msg) VALUES (N'Location w1005 has LPNs that were produced against a different production order than 1')
--EXEC tSQLt.ExpectException #ExpectedMessage = N'Location 1 has LPNs that were produced agains a different production order than orderNumber';
EXEC dbo.wms_whse_check_location
#command = #Command, #operLocationHasOtherLPN=#operLocationHasOtherLPN OUTPUT;
INSERT #actual (msg) VALUES (#operLocationHasOtherLPN)
EXEC tsqlt.AssertEqualsTable #Expected = '#expected', #actual = '#actual'
The test fails, and the output from tsqlt is:
Unexpected/missing resultset rows!
|m|msg |
+---+--------------------------------------------------------------------------------------+
|< |Location w1005 has LPNs that were produced against a different production order than 1|
|> |Location w1005 has LPNs that were produced against a different production order than l|
It may be hard to see in the above snip, but the < (expected) row is identical to the > (actual) row -- tsqlt finds a difference that in fact doesn't exist. I'm not choosing the correct method it seems.
Has anyone written tests to check ouput parameters? What is the appropriate method? Thanks
p.s. Apologies for the messy formatting. I'm not a regular poster.

tSQLt.AssertEqualsString is in fact the appropriate test. I don't know where I went wrong, but when I concatenated the appropriate expected message in code (as opposed to typing it out), then ran the test, it succeeded.

Use tSQLt.AssertEqualsString, as you found out already.
Also, your two strings are not identical. The one ends in “1”, the other one doesn’t.

Related

Send multiple parameters to SQL Server using dbBind in R

I am working on a program that has to send data to Microsoft SQL Server through R. Since SQL Server doesn't have any INSERT OR IGNORE INTO like PostgreSQL, I have to check if the value is already in the table. However, I don't know if I'm doing it right. How do I send multiple parameters (or the same parameter twice) into SQL Server using dbBind?
Here is my code:
statement <- "IF NOT EXISTS (SELECT * FROM dbo.nodes WHERE node_id=?) INSERT INTO dbo.nodes (node_id) VALUES (?)"
insertnew <- dbSendQuery(conn, statement)
test_p1 <- list(5, 6)
test_p2 <- list(test_p1, test_p1)
dbBind(insertnew, params=test_p2)
When I run it it gives me the following error:
Error in result_bind(res#ptr, params, batch_rows) :
RAW() can only be applied to a 'raw', not a 'double'
Using RStudio's Show Traceback feature, it shows this:
result_bind(res#ptr, params, batch_rows)
.local(res, params, ...)
dbBind(insertnew, params = test_p1)
Does anybody know what I'm doing wrong here? If the parameters should be something other than a list of lists, what should they be? Is there a way to name parameters to use twice in the same statement? What is RAW() and what is it trying to apply it to?
dbo.nodes is a table with one column, node_id NVARCHAR(1000) NOT NULL PRIMARY KEY.
conn is a DBIConnection object created earlier using dbConnect(odbc::odbc(), ...). It works for executing other statements so there shouldn't be a problem there.
Adding the argument batch_rows=1 to dbBind doesn't change anything (not that I understand what that is supposed to do).
Thanks in advance for the help!
test_p1 = c(5, 6)
test_p2 = unname(data.frame(I(test_p1), I(test_p1)))
In case this helps someone looking this up later, changing it from a list of lists to a data frame made from vectors worked.

Julia print statement not working in certain cases

I've written a prime-generating function generatePrimes (full code here) that takes input bound::Int64 and returns a Vector{Int64} of all primes up to bound. After the function definition, I have the following code:
println("Generating primes...")
println("Last prime: ", generatePrimes(10^7)[end])
println("Primes generated.")
which prints, unexpectedly,
Generating primes...
9999991
Primes generated.
This output misses the "Last prime: " segment of the second print statement. The output does work as expected for smaller inputs; any input at least up to 10^6, but somehow fails for 10^7. I've tried several workarounds for this (e.g. assigning the returned value or converting it to a string before calling it in a print statement, combining the print statements, et cetera) and discovered some other weird behaviour: if the "Last prime", is removed from the second print statement, for input 10^7, the last prime doesn't print at all and all I get is a blank line between the first and third print statements. These issues are probably related, and I can't seem to find anything online about why some print statements wouldn't work in Julia.
Thanks so much for any clarification!
Edit: Per DNF's suggestion, following are some reductions to this issue:
Removing the first and last print statements doesn't change anything -- a blank line is always printed in the case I outlined and each of the cases below.
println(generatePrimes(10^7)[end]) # output: empty line
Calling the function and storing the last index in a variable before calling println doesn't change anything either; the cases below work exactly the same either way.
lastPrime::Int = generatePrimes(10^7)[end]
println(lastPrime) # output: empty line
If I call the function in whatever form immediately before a println, an empty line is printed regardless of what's inside the println.
lastPrime::Int = generatePrimes(10^7)[end]
println("This doesn't print") # output: empty line
println("This does print") # output: This does print
If I call the function (or print the pre-generated-and-stored function result) inside a println, anything before the function call (that's also inside the println) isn't printed. The 9999991 and anything else there may be after the function call is printed only if there is something else inside the println before the function call.
# Example 1
println(generatePrimes(10^7)[end]) # output: empty line
# Example 2
println("This first part doesn't print", generatePrimes(10^7)[end]) # output: 9999991
# Example 3
println("This first part doesn't print", generatePrimes(10^7)[end], " prints") # output: 9999991 prints
# Example 4
println(generatePrimes(10^7)[end], "prime doesn't print") # output: prime doesn't print
I could probably list twenty different variations of this same thing, but that probably wouldn't make things any clearer. In every single case version of this issue I've seen so far, the issue only manifests if there's that function call somewhere; println prints large integers just fine. That said, please let me know if anyone feels like they need more info. Thanks so much!
Most likely you are running this code from Atom Juno which recently has some issues with buffering standard output (already reported by others and I also sometimes have this problem).
One thing you can try to do is to flush your standard output
flush(stdout)
Like with any unstable bug restarting Atom Juno also seems to help.
I had the same issue. For me, changing the terminal renderer (File -> Settings -> Packages -> julia-client -> Terminal Options) from webgl to canvas (see pic below) seems to solve the issue.
change terminal renderer
I've also encountered this problem many times. (First time, it was triggered after using the debugger. It is probably unrelated but I have been using Julia+Juno for 2 weeks prior to this issue.)
In my case, the code before the println statement needed to have multiple dictionary assignation (with new keys) in order to trigger the behavior.
I also confirmed that the same code ran in Command Prompt (with same Julia interpreter) prints fine. Any hints about how to further investigate this will be appreciated.
I temporarily solve this issue by printing to stderr, thinking that this stream has more stringent flush mechanism: println(stderr, "hello!")

BatchJobs results gives the function result * -1 + job#?

I am running a minimal example using BatchJobs, and the results are not as expected. I'm using the global_config settings, with debug=TRUE. I am running the following code in R on my HPC server:
library(BatchJobs)
reg <- makeRegistry(id = "batchtest")
batchMap(reg, identity, 1)
submitJobs(reg)
showStatus(reg)
load("batchtest-files/jobs/01/1-result.RData")
1-result
[1] 0
If I run batchMap(reg, identity, 2) the result is -1, and with batchMap(reg, identity, 3) the result is -2.
Any ideas why this might be happening? The identity function should just return the argument (so it should be 1 for the code above). I find the same issue with other functions. For example, if I use mean(rnorm(100, mean=100)) for the function I send to batchMap, I end up with results around -99. If I run this on multiple nodes, the results from each node are around -100 + node number (so the results from the 5th node are around -95).
Try an ls(). Probably the correct result from the load command is stored in the variable result. When you calculate 1-result you will get exactly the results you described.

R f\Functions/Macros and Generating Calls From Data

So I'm a proficient SAS user but very new to R. I am finding myself frustrated because I cannot figure out how to do something in R that's pretty simple in SAS, and so I assume simple in R. I think there's something that I'm missing at a very fundamental level about the way R works.
In R, I am using the airquality dataset and trying to do a scatter plot of every variable by every variable.
In SAS I would do something like the following:
proc contents data=airquality noprint out=contents;
run;
proc sql noprint;
create table all_combs as select A.name,B.name as name2 from contents A, contents B where a.name ne B.name;
select cats('%scatter(',name,',',name2,')') into :scatter separated by ' ' from all_combs;
quit;
%macro scatter(x,y);
proc sqplot data=airquality;
scatter x=&x. y=&y.;
run;
%mend;
&scatter.;
The basic process is to generate a list of variables from the data and generate the cartesian product. The result is thrown into a series of macro calls that are stored in a macro variable which is called after you define the macro.
I assume the way to do this in R is to generate a function to do this, but I failed there. I expected the below to work and it didn't and I don't understand why.
plotfun=function(v1,v2){plotfun=plot(airquality$v1,airquality$v2)}
plotfun(Wind,Temp)
Even after I do that part I don't know how to automatically generate the calls of the function.
Any suggestions?
This might do what you were hoping:
plotfun=function(df, v1,v2){ plot(df[[v1]],df[[v2]]) }
plotfun(airquality, 'Wind','Temp')
Note that your code would have caused the R interpreter to go looking for a variable named 'Wind', then passed it to v1 but it would have failed even if it had found such a variable, since $ does not evaluate its second argument, v1 in your case.

How do I properly perform modulus operations in Batch?

I'm trying to write a batch file that performs operations depending on the result of a modulus operation performed on a set variable. However, I can't seem to get it quite right.
To first of all test my syntax for the mathematical operation, I've been trying to get a simpler script to produce desired results.
:START
SETLOCAL
SET /P Input-Num="Input Number: "
SET /A Input-Num=%Input-Num% %% 2
ECHO %Input-Num%
ENDLOCAL
PAUSE
:END
If I input 5, the expected output is 1. However, instead I get a message saying Missing operator. and then it outputs 5.
What am I doing wrong here?
Using SET /P is your problem, as 5 is no longer treated as a numerical value. Your example as above works as expected

Resources