|
1 |
| -library(testthat) |
2 |
| -library(devtools) |
3 |
| -library(rJava) |
4 |
| - |
5 |
| - |
6 |
| -# For debugging: force reload of code & patterns: |
7 |
| -# load_all() |
8 |
| -# rJava::J('org.ohdsi.sql.SqlTranslate')$setReplacementPatterns('../../inst/csv/replacementPatterns.csv') |
9 |
| - |
10 |
| - |
11 |
| -expect_equal_ignore_spaces <- function(string1, string2) { |
12 |
| - string1 <- gsub("([;()'+-/|*\n])", " \\1 ", string1) |
13 |
| - string2 <- gsub("([;()'+-/|*\n])", " \\1 ", string2) |
14 |
| - string1 <- gsub(" +", " ", string1) |
15 |
| - string2 <- gsub(" +", " ", string2) |
16 |
| - expect_equivalent(string1, string2) |
17 |
| -} |
18 |
| - |
19 |
| -# tests wrt string concatenation |
20 |
| -test_that("translate sql server -> InterSystems IRIS string concatenation", { |
21 |
| - sql <- translate("SELECT CONCAT(a, 'b', c)", targetDialect = "iris") |
22 |
| - expect_equal_ignore_spaces(sql, "SELECT a || 'b' || c") |
23 |
| -}) |
24 |
| -test_that("translate sql server -> InterSystems IRIS string concatenation", { |
25 |
| - sql <- translate("SELECT CONCAT(a, 'b', c, d, e, e, f)", targetDialect = "iris") |
26 |
| - expect_equal_ignore_spaces(sql, "SELECT a || 'b' || c || d || e || e || f") |
27 |
| -}) |
28 |
| -test_that("translate sql server -> InterSystems IRIS string +", { |
29 |
| - sql <- translate("SELECT CAST(a AS VARCHAR) + CAST(b AS VARCHAR(10)) + CAST(c AS VARCHAR) + 'd'", targetDialect = "iris") |
30 |
| - expect_equal_ignore_spaces(sql, "SELECT CAST(a AS VARCHAR) || CAST(b AS varchar(10)) || CAST(c AS VARCHAR) || 'd'") |
31 |
| -}) |
32 |
| -test_that("translate sql server -> InterSystem IRIS string concatenation DOB", { |
33 |
| - sql <- translate("SELECT CONCAT(p.year_of_birth, 11, 11)", targetDialect = "iris") |
34 |
| - expect_equal_ignore_spaces(sql, "SELECT p.year_of_birth||'-'||11||'-'||11") |
35 |
| -}) |
36 |
| - |
37 |
| - |
38 |
| -# build date from parts |
39 |
| -test_that("translate sql server -> InterSystems IRIS DATEFROMPARTS()", { |
40 |
| - sql <- translate("SELECT DATEFROMPARTS(yyyy, mm, dd)", targetDialect = "iris") |
41 |
| - expect_equal_ignore_spaces(sql, "SELECT TO_DATE(TO_CHAR(yyyy,'FM0000')||'-'||TO_CHAR(mm,'FM00')||'-'||TO_CHAR(dd,'FM00'), 'YYYY-MM-DD')") |
42 |
| -}) |
43 |
| -test_that("translate sql server -> InterSystems IRIS DATETIMEFROMPARTS()", { |
44 |
| - sql <- translate("SELECT DATETIMEFROMPARTS(yyyy, mm, dd, hh, mi, ss, ms)", targetDialect = "iris") |
45 |
| - expect_equal_ignore_spaces(sql, "SELECT TO_TIMESTAMP(TO_CHAR(yyyy,'FM0000')||'-'||TO_CHAR(mm,'FM00')||'-'||TO_CHAR(dd,'FM00')||' '||TO_CHAR(hh,'FM00')||':'||TO_CHAR(mi,'FM00')||':'||TO_CHAR(ss,'FM00')||'.'||TO_CHAR(ms,'FM000'), 'YYYY-MM-DD HH24:MI:SS.FF')") |
46 |
| -}) |
47 |
| - |
48 |
| - |
49 |
| - |
50 |
| -# temp table handling |
51 |
| -test_that("translate sql server -> InterSystems IRIS implicit CTAS", { |
52 |
| - sql <- translate("SELECT a, b INTO t_new FROM t;", targetDialect = "iris") |
53 |
| - expect_equal_ignore_spaces(sql, "CREATE TABLE t_new AS SELECT a, b FROM t;") |
54 |
| -}) |
55 |
| -test_that("translate sql server -> InterSystems IRIS implicit CTTAS", { |
56 |
| - sql <- translate("SELECT a, b INTO #t_new FROM t;", targetDialect = "iris") |
57 |
| - expect_equal_ignore_spaces(sql, paste("CREATE GLOBAL TEMPORARY TABLE ", getTempTablePrefix(), "t_new AS SELECT a, b FROM t;", sep="")) |
58 |
| -}) |
59 |
| - |
60 |
| - |
61 |
| -# test DATEADD() flavours |
62 |
| -test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
63 |
| - sql <- translate("SELECT DATEADD(d, 1, '2007-07-28') AS dt", targetDialect = "iris") |
64 |
| - expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(d,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
65 |
| -}) |
66 |
| -test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
67 |
| - sql <- translate("SELECT DATEADD(dd, 1, '2007-07-28') AS dt", targetDialect = "iris") |
68 |
| - expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(dd,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
69 |
| -}) |
70 |
| -test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
71 |
| - sql <- translate("SELECT DATEADD(day, 1, '2007-07-28') AS dt", targetDialect = "iris") |
72 |
| - expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(day,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
73 |
| -}) |
74 |
| -test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
75 |
| - sql <- translate("SELECT DATEADD(m, 1, '2007-07-28') AS dt", targetDialect = "iris") |
76 |
| - expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(m,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
77 |
| -}) |
78 |
| -test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
79 |
| - sql <- translate("SELECT DATEADD(mm, 1, '2007-07-28') AS dt", targetDialect = "iris") |
80 |
| - expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(mm,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
81 |
| -}) |
82 |
| -test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
83 |
| - sql <- translate("SELECT DATEADD(yy, 1, '2007-07-28') AS dt", targetDialect = "iris") |
84 |
| - expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(yy,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
85 |
| -}) |
86 |
| -test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
87 |
| - sql <- translate("SELECT DATEADD(yyyy, 1, '2007-07-28') AS dt", targetDialect = "iris") |
88 |
| - expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(yyyy,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
89 |
| -}) |
90 |
| - |
91 |
| - |
92 |
| -# test reserved words |
93 |
| -test_that("translate sql server -> InterSystems IRIS reserved word DOMAIN", { |
94 |
| - sql <- translate("SELECT t.domain, 'domain' FROM omopcdm.domain AS t", targetDialect = "iris") |
95 |
| - expect_equal_ignore_spaces(sql, "SELECT t.\"DOMAIN\", 'domain' FROM omopcdm.\"DOMAIN\" AS t") |
96 |
| -}) |
97 |
| -test_that("translate sql server -> InterSystems IRIS reserved words for aggregates", { |
98 |
| - sql <- translate("SELECT MIN(x) AS min, MAX(x) AS max, COUNT(x) as COUNT FROM t", targetDialect = "iris") |
99 |
| - expect_equal_ignore_spaces(sql, "SELECT MIN(x) AS \"MIN\", MAX(x) AS \"MAX\", COUNT(x) as \"COUNT\" FROM t") |
100 |
| -}) |
101 |
| - |
102 |
| - |
103 |
| -# test function names |
104 |
| -test_that("translate sql server -> InterSystems IRIS function names", { |
105 |
| - sql <- translate("SELECT STDEV(x), STDEV_POP(x), STDEV_SAMP(x), EOMONTH(dt) FROM t", targetDialect = "iris") |
106 |
| - expect_equal_ignore_spaces(sql, "SELECT STDDEV(x), STDDEV_POP(x), STDDEV_SAMP(x), LAST_DAY(dt) FROM t") |
107 |
| -}) |
| 1 | +library(testthat) |
| 2 | +library(rJava) |
| 3 | + |
| 4 | + |
| 5 | +# For debugging: force reload of code & patterns: |
| 6 | +# load_all() |
| 7 | +# rJava::J('org.ohdsi.sql.SqlTranslate')$setReplacementPatterns('../../inst/csv/replacementPatterns.csv') |
| 8 | + |
| 9 | + |
| 10 | +expect_equal_ignore_spaces <- function(string1, string2) { |
| 11 | + string1 <- gsub("([;()'+-/|*\n])", " \\1 ", string1) |
| 12 | + string2 <- gsub("([;()'+-/|*\n])", " \\1 ", string2) |
| 13 | + string1 <- gsub(" +", " ", string1) |
| 14 | + string2 <- gsub(" +", " ", string2) |
| 15 | + expect_equivalent(string1, string2) |
| 16 | +} |
| 17 | + |
| 18 | +# tests wrt string concatenation |
| 19 | +test_that("translate sql server -> InterSystems IRIS string concatenation", { |
| 20 | + sql <- translate("SELECT CONCAT(a, 'b', c)", targetDialect = "iris") |
| 21 | + expect_equal_ignore_spaces(sql, "SELECT a || 'b' || c") |
| 22 | +}) |
| 23 | +test_that("translate sql server -> InterSystems IRIS string concatenation", { |
| 24 | + sql <- translate("SELECT CONCAT(a, 'b', c, d, e, e, f)", targetDialect = "iris") |
| 25 | + expect_equal_ignore_spaces(sql, "SELECT a || 'b' || c || d || e || e || f") |
| 26 | +}) |
| 27 | +test_that("translate sql server -> InterSystems IRIS string +", { |
| 28 | + sql <- translate("SELECT CAST(a AS VARCHAR) + CAST(b AS VARCHAR(10)) + CAST(c AS VARCHAR) + 'd'", targetDialect = "iris") |
| 29 | + expect_equal_ignore_spaces(sql, "SELECT CAST(a AS VARCHAR) || CAST(b AS varchar(10)) || CAST(c AS VARCHAR) || 'd'") |
| 30 | +}) |
| 31 | +test_that("translate sql server -> InterSystem IRIS string concatenation DOB", { |
| 32 | + sql <- translate("SELECT CONCAT(p.year_of_birth, 11, 11)", targetDialect = "iris") |
| 33 | + expect_equal_ignore_spaces(sql, "SELECT p.year_of_birth||'-'||11||'-'||11") |
| 34 | +}) |
| 35 | + |
| 36 | + |
| 37 | +# build date from parts |
| 38 | +test_that("translate sql server -> InterSystems IRIS DATEFROMPARTS()", { |
| 39 | + sql <- translate("SELECT DATEFROMPARTS(yyyy, mm, dd)", targetDialect = "iris") |
| 40 | + expect_equal_ignore_spaces(sql, "SELECT TO_DATE(TO_CHAR(yyyy,'FM0000')||'-'||TO_CHAR(mm,'FM00')||'-'||TO_CHAR(dd,'FM00'), 'YYYY-MM-DD')") |
| 41 | +}) |
| 42 | +test_that("translate sql server -> InterSystems IRIS DATETIMEFROMPARTS()", { |
| 43 | + sql <- translate("SELECT DATETIMEFROMPARTS(yyyy, mm, dd, hh, mi, ss, ms)", targetDialect = "iris") |
| 44 | + expect_equal_ignore_spaces(sql, "SELECT TO_TIMESTAMP(TO_CHAR(yyyy,'FM0000')||'-'||TO_CHAR(mm,'FM00')||'-'||TO_CHAR(dd,'FM00')||' '||TO_CHAR(hh,'FM00')||':'||TO_CHAR(mi,'FM00')||':'||TO_CHAR(ss,'FM00')||'.'||TO_CHAR(ms,'FM000'), 'YYYY-MM-DD HH24:MI:SS.FF')") |
| 45 | +}) |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +# temp table handling |
| 50 | +test_that("translate sql server -> InterSystems IRIS implicit CTAS", { |
| 51 | + sql <- translate("SELECT a, b INTO t_new FROM t;", targetDialect = "iris") |
| 52 | + expect_equal_ignore_spaces(sql, "CREATE TABLE t_new AS SELECT a, b FROM t;") |
| 53 | +}) |
| 54 | +test_that("translate sql server -> InterSystems IRIS implicit CTTAS", { |
| 55 | + sql <- translate("SELECT a, b INTO #t_new FROM t;", targetDialect = "iris") |
| 56 | + expect_equal_ignore_spaces(sql, paste("CREATE GLOBAL TEMPORARY TABLE ", getTempTablePrefix(), "t_new AS SELECT a, b FROM t;", sep="")) |
| 57 | +}) |
| 58 | + |
| 59 | + |
| 60 | +# test DATEADD() flavours |
| 61 | +test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
| 62 | + sql <- translate("SELECT DATEADD(d, 1, '2007-07-28') AS dt", targetDialect = "iris") |
| 63 | + expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(d,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
| 64 | +}) |
| 65 | +test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
| 66 | + sql <- translate("SELECT DATEADD(dd, 1, '2007-07-28') AS dt", targetDialect = "iris") |
| 67 | + expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(dd,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
| 68 | +}) |
| 69 | +test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
| 70 | + sql <- translate("SELECT DATEADD(day, 1, '2007-07-28') AS dt", targetDialect = "iris") |
| 71 | + expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(day,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
| 72 | +}) |
| 73 | +test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
| 74 | + sql <- translate("SELECT DATEADD(m, 1, '2007-07-28') AS dt", targetDialect = "iris") |
| 75 | + expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(m,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
| 76 | +}) |
| 77 | +test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
| 78 | + sql <- translate("SELECT DATEADD(mm, 1, '2007-07-28') AS dt", targetDialect = "iris") |
| 79 | + expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(mm,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
| 80 | +}) |
| 81 | +test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
| 82 | + sql <- translate("SELECT DATEADD(yy, 1, '2007-07-28') AS dt", targetDialect = "iris") |
| 83 | + expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(yy,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
| 84 | +}) |
| 85 | +test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", { |
| 86 | + sql <- translate("SELECT DATEADD(yyyy, 1, '2007-07-28') AS dt", targetDialect = "iris") |
| 87 | + expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(yyyy,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt") |
| 88 | +}) |
| 89 | + |
| 90 | + |
| 91 | +# test reserved words |
| 92 | +test_that("translate sql server -> InterSystems IRIS reserved word DOMAIN", { |
| 93 | + sql <- translate("SELECT t.domain, 'domain' FROM omopcdm.domain AS t", targetDialect = "iris") |
| 94 | + expect_equal_ignore_spaces(sql, "SELECT t.\"DOMAIN\", 'domain' FROM omopcdm.\"DOMAIN\" AS t") |
| 95 | +}) |
| 96 | +test_that("translate sql server -> InterSystems IRIS reserved words for aggregates", { |
| 97 | + sql <- translate("SELECT MIN(x) AS min, MAX(x) AS max, COUNT(x) as COUNT FROM t", targetDialect = "iris") |
| 98 | + expect_equal_ignore_spaces(sql, "SELECT MIN(x) AS \"MIN\", MAX(x) AS \"MAX\", COUNT(x) AS \"COUNT\" FROM t") |
| 99 | +}) |
| 100 | + |
| 101 | + |
| 102 | +# test function names |
| 103 | +test_that("translate sql server -> InterSystems IRIS function names", { |
| 104 | + sql <- translate("SELECT STDEV(x), STDEV_POP(x), STDEV_SAMP(x), EOMONTH(dt) FROM t", targetDialect = "iris") |
| 105 | + expect_equal_ignore_spaces(sql, "SELECT STDDEV(x), STDDEV_POP(x), STDDEV_SAMP(x), LAST_DAY(dt) FROM t") |
| 106 | +}) |
0 commit comments