Skip to content

Commit 054c248

Browse files
seantleonardSean Leonard
andauthored
Fix R Unit Tests and Invalid Cursor State Occurrences (#100)
* Fix code causing 'length(x) = 5 > 1' in coercion to 'logical(1)' warning. * fix failing func execute in sql test due to differing environments * add unittest for getServerVersion to detect bad cursor error on odbc * add new R 4.2 to test workflow. * adjust comment text and variable name * Fix for spees generation codepath triggering cursor state invalid error * Fix warning longer object length is not a multiple of shorter object length * support for special characters in connection string password * updated version number of R package to 1.2.1 in DESCRIPTION * update CRAN snapshot reference Co-authored-by: Sean Leonard <[email protected]>
1 parent 27612c6 commit 054c248

File tree

7 files changed

+25
-11
lines changed

7 files changed

+25
-11
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
strategy:
3030
fail-fast: false
3131
matrix:
32-
r-version: ["3.5.2"]
32+
r-version: ["3.5.2", "4.2"]
3333

3434
env:
3535
# Define CI to skip some test case.
@@ -58,7 +58,7 @@ jobs:
5858
#Updated odbc pkg is still compatible with R >= 3.2.0
5959
cran::odbc
6060
rcmdcheck
61-
61+
6262
- uses: r-lib/actions/check-r-package@v2
6363
with:
6464
working-directory: ./R
@@ -91,4 +91,4 @@ jobs:
9191
- name: Test with pytest
9292
working-directory: ./Python/tests
9393
run: |
94-
pytest
94+
pytest

R/DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: sqlmlutils
22
Type: Package
33
Title: Wraps R code into executable SQL Server stored procedures
4-
Version: 1.0.0
4+
Version: 1.2.1
55
Author: Microsoft Corporation
66
Maintainer: Microsoft Corporation <[email protected]>
77
Depends:
@@ -13,7 +13,7 @@ Description: sqlmlutils is a package designed to help users interact with SQL Se
1313
creating and running stored procedures, and managing packages on the database.
1414
License: MIT + file LICENSE
1515
Copyright: Copyright 2016 Microsoft Corporation
16-
RoxygenNote: 7.1.0
16+
RoxygenNote: 7.1.2
1717
Encoding: UTF-8
1818
Suggests: testthat (>= 2.0.0),
1919
roxygen2

R/R/executeInSQL.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ connectionInfo <- function(driver = "SQL Server", server = "localhost", database
3939
}
4040
else
4141
{
42-
authorization = sprintf("uid=%s;pwd=%s",uid,pwd)
42+
authorization = sprintf("uid=%s;pwd={%s}",uid,pwd)
4343
}
4444
}
4545

R/R/sqlPackage.R

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,14 @@ sqlRemoteExecuteFun <- function(connection, FUN, ..., useRemoteFun = FALSE, asus
474474
query <- paste0("EXECUTE AS USER = '", asuser, "';")
475475
}
476476

477+
# Addresses Invalid Cursor State issue triggered by PRINT() statements
478+
# and DIAG messages from SQL Server.
479+
resultSet <- "WITH RESULT SETS((resultColumn varchar(MAX)))"
477480
query <- paste0(query
478481
,"\nEXEC sp_execute_external_script"
479482
,"\n@language = N'", languageName, "'"
480-
,"\n,@script = N'",script, "';"
483+
,"\n,@script = N'",script, "'"
484+
,"\n",resultSet, ";"
481485
)
482486

483487
if (!is.null(asuser))
@@ -818,7 +822,7 @@ sqlCheckPackageManagementVersion <- function(connectionString)
818822

819823
version <- sqlPackageManagementVersion(connectionString)
820824

821-
if (is.null(version) || is.na(version) || length(version) == 0)
825+
if (is.null(version) || any(is.na(version)) || length(version) == 0)
822826
{
823827
stop("Invalid SQL version is null or empty", call. = FALSE)
824828
}
@@ -1169,8 +1173,11 @@ getDependentPackagesToInstall <- function(pkgs, availablePackages, installedPack
11691173

11701174
#
11711175
# Determine if package is available as a binary package
1176+
# Utilize first of possibly many contributor URLs present
1177+
# in character vector contribWinBinaryUrl
11721178
#
1173-
packageProperties <- availablePackages[availablePackages$Package == package & availablePackages$Repository == contribWinBinaryUrl, ]
1179+
contributorURL <- contribWinBinaryUrl[1]
1180+
packageProperties <- availablePackages[availablePackages$Package == package & availablePackages$Repository == contributorURL, ]
11741181

11751182
#
11761183
# When only a source package is available, add LinkingTo dependencies

R/tests/testthat/helper-Setup.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ testthatDir <- getwd()
5151
R_Root <- file.path(testthatDir, "../..")
5252
scriptDirectory <- file.path(testthatDir, "scripts")
5353

54-
options(repos = c(CRAN="https://cran.microsoft.com", CRANextra = "https://mran.microsoft.com/snapshot/2019-02-01"))
54+
options(repos = c(CRAN="https://cran.microsoft.com/snapshot/2022-07-06"))
5555
cat("INFO: repos = ", getOption("repos"), sep="\n")
5656

5757
# Compute context specifications

R/tests/testthat/test.executeInSqlTests.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ test_that("Returning a function object",
9393
return(func2)
9494
}
9595

96-
expect_equal(executeFunctionInSQL(connection, func=func1), func2)
96+
# Result of executeFunctionInSQL() will have different environment than func2.
97+
expect_equal(executeFunctionInSQL(connection, func=func1), func2, check.environment=FALSE)
9798
})
9899

99100
test_that("Calling an object in the environment",

R/tests/testthat/test.sqlPackage.unit.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ test_that("Package management ExtLib", {
2222
versionClass <- sqlmlutils:::sqlCheckPackageManagementVersion(connectionString = helper_getSetting("connectionStringDBO"))
2323
expect_equal(versionClass, "ExtLib")
2424
})
25+
26+
test_that("GetServerVersion() Returns Server Version of R Successfully",{
27+
rversion <- sqlmlutils:::getserverVersion(connectionString = cnnstr, languageName = "R")
28+
# rversion value truncated, so R may be >= 3.5 (3.5.3) or >= 4.2
29+
expect_gte(as.double(rversion[['rversion']]), 3.5)
30+
})

0 commit comments

Comments
 (0)