Skip to content

Commit 5ade2ee

Browse files
committed
Initial commit
0 parents  commit 5ade2ee

35 files changed

+1671
-0
lines changed

.Rbuildignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$
3+
^monetdb$

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
monetdb
2+
*.Rproj
3+
.Rhistory
4+
.Rproj.user

DESCRIPTION

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Package: useR2015
2+
Title: pakiet na konferencje useR
3+
Description: Pakiet na konferencje useR.
4+
Version: 0.0.1
5+
Author:
6+
Mateusz Zoltak <[email protected]>,
7+
Maintainer: Mateusz Zoltak <[email protected]>
8+
Depends:
9+
Imports:
10+
DBI,
11+
testthat
12+
Suggests:
13+
knitr,
14+
MonetDB.R,
15+
ODB,
16+
RJDBC,
17+
RMySQL,
18+
RPostgreSQL,
19+
RSQLite
20+
License: MIT + file LICENSE
21+
NeedsCompilation: no
22+
Encoding: UTF-8

LICENSE

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2015 Mateusz Żółtak
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
The above copyright notice and this permission notice shall be included in all
10+
copies or substantial portions of the Software.
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17+
SOFTWARE.

NAMESPACE

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Generated by roxygen2 (4.1.1): do not edit by hand
2+
3+
export(create_schemas)
4+
export(tidy_up)
5+
import(DBI)
6+
import(testthat)

R/create_schemas.R

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#' @title creates database objects required for testing
2+
#' @param conn connection to the database opend using DBI::dbConnect()
3+
#' @import DBI
4+
#' @export
5+
create_schemas = function(conn){
6+
try(dbSendQuery(conn, "CREATE SCHEMA foo"), silent = TRUE)
7+
8+
try(dbSendQuery(conn, "CREATE TABLE bar (foo varchar(255))"), silent = TRUE)
9+
try(dbSendQuery(conn, "CREATE TABLE foo.bar (foo int)"), silent = TRUE)
10+
11+
try(dbSendQuery(conn, "CREATE VIEW bar_view AS SELECT * FROM bar"), silent = TRUE)
12+
try(dbSendQuery(conn, "CREATE VIEW foo.bar_view AS SELECT * FROM foo.bar"), silent = TRUE)
13+
14+
try(dbSendQuery(conn, "INSERT INTO bar VALUES ('a'), ('b')"), silent = TRUE)
15+
try(dbSendQuery(conn, "INSERT INTO foo.bar VALUES (1), (2)"), silent = TRUE)
16+
try(dbSendQuery(conn, "INSERT INTO bar_tmp VALUES (true), (false)"), silent = TRUE)
17+
18+
try(dbSendQuery(conn, "CREATE TEMPORARY TABLE bar_tmp AS SELECT * FROM bar"), silent = TRUE)
19+
}

R/test_dbGetQuery.R

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#' @title tests dbGetQuery() compatibility
2+
#' @param conn connection to the database opend using DBI::dbConnect()
3+
#' @return TRUE or FALSE indicating test pass/fail
4+
#' @import DBI
5+
#' @import testthat
6+
test_dbGetQuery = function(conn){
7+
on.exit(tidy_up(conn))
8+
create_schemas(conn)
9+
try({
10+
model = data.frame(foo = letters[1:2], stringsAsFactors = FALSE)
11+
expect_equal(dbGetQuery(conn, "SELECT * FROM bar ORDER BY 1"), model)
12+
expect_equal(dbGetQuery(conn, "SELECT * FROM bar_view ORDER BY 1"), model)
13+
expect_equal(dbGetQuery(conn, "SELECT * FROM bar_tmp ORDER BY 1"), model)
14+
15+
model = data.frame(foo = 1:2, stringsAsFactors = FALSE)
16+
expect_equal(dbGetQuery(conn, "SELECT * FROM foo.bar ORDER BY 1"), model)
17+
expect_equal(dbGetQuery(conn, "SELECT * FROM foo.bar_view ORDER BY 1"), model)
18+
19+
return(TRUE)
20+
})
21+
return(FALSE)
22+
}

R/test_dbListTables.R

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#' @title tests behaviour of the dbListTables()
2+
#' @description
3+
#' As the dbListTables() specification in the DBI is simply wrong (if you want
4+
#' to support schemas you can not return a character vector as a dbListTables()
5+
#' result because it is impossible to correctly escape schema and table name
6+
#' then), we can not provide a tests giving simple pass/fail results.
7+
#'
8+
#' Instead of that we can only check out and describe some aspects of the
9+
#' dbListTables() like if views are listed, if temporary tables are listed, etc.
10+
#' @param conn connection to the database opend using DBI::dbConnect()
11+
#' @return numeric vector describing dbListTables() behaviour
12+
#' @import DBI
13+
#' @import testthat
14+
test_dbListTables = function(conn){
15+
on.exit(tidy_up(conn))
16+
create_schemas(conn)
17+
18+
tables = dbListTables(conn)
19+
results = c(
20+
length = length(tables),
21+
otherSchemas = any(duplicated(tables)),
22+
temporary = any(tables %in% 'bar_tmp'),
23+
views = any(tables %in% 'bar_view')
24+
)
25+
return(results)
26+
}

R/test_dbReadTable.R

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#' @title tests behaviour of the dbReadTable()
2+
#' @param conn connection to the database opend using DBI::dbConnect()
3+
#' @return TRUE or FALSE indicating test pass/fail
4+
#' @import DBI
5+
#' @import testthat
6+
test_dbReadTable = function(conn){
7+
on.exit(tidy_up(conn))
8+
create_schemas(conn)
9+
10+
try({
11+
model = data.frame(foo = letters[1:2], stringsAsFactors = FALSE)
12+
expect_equal(dbReadTable(conn, 'bar'), model)
13+
expect_equal(dbReadTable(conn, 'bar_view'), model)
14+
expect_equal(dbReadTable(conn, 'bar_tmp'), model)
15+
16+
return(TRUE)
17+
})
18+
return(FALSE)
19+
}

R/test_dbReadTable_another_schema.R

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#' @title tests behaviour of the dbReadTable() regarging tables in non-default
2+
#' schema
3+
#' @description
4+
#' Behaviour of the dbReadTable() regaring relations in schema other the default
5+
#' one is not described. Due to that we can only describe the way the driver
6+
#' behaves but we can not test if it meets the DBI interface or not.
7+
#' @param conn connection to the database opend using DBI::dbConnect()
8+
#' @return logical vector describing dbReadTable() behaviour
9+
#' @import DBI
10+
#' @import testthat
11+
test_dbReadTable_another_schema = function(conn){
12+
on.exit(tidy_up(conn))
13+
create_schemas(conn)
14+
15+
results = c(unescapedName = FALSE, schemaParameter = FALSE, nameAsVector = FALSE)
16+
try({
17+
model = data.frame(foo = 1:2, stringsAsFactors = FALSE)
18+
try({
19+
expect_equal(dbReadTable(conn, 'foo.bar'), model)
20+
results['unescapedName'] = TRUE
21+
}, silent = TRUE)
22+
try({
23+
expect_equal(dbReadTable(conn, 'bar', schema = 'foo'), model)
24+
results['schemaParameter'] = TRUE
25+
}, silent = TRUE)
26+
try({
27+
expect_equal(dbReadTable(conn, c('foo', 'bar')), model)
28+
results['nameAsVector'] = TRUE
29+
}, silent = TRUE)
30+
})
31+
return(results)
32+
}

R/test_dbSendQuery.R

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' @title tests dbSendQuery() compatibility
2+
#' @param conn connection to the database opend using DBI::dbConnect()
3+
#' @return TRUE or FALSE indicating test pass/fail
4+
#' @import DBI
5+
#' @import testthat
6+
test_dbSendQuery = function(conn){
7+
on.exit(tidy_up(conn))
8+
create_schemas(conn)
9+
try({
10+
dbSendQuery(conn, "UPDATE bar SET foo = 'z'")
11+
model = data.frame(foo = c('z', 'z'), stringsAsFactors = FALSE)
12+
expect_equal(dbGetQuery(conn, "SELECT * FROM bar ORDER BY 1"), model)
13+
14+
dbSendQuery(conn, "INSERT INTO bar VALUES ('x')")
15+
model = data.frame(foo = c('x', 'z', 'z'), stringsAsFactors = FALSE)
16+
expect_equal(dbGetQuery(conn, "SELECT * FROM bar ORDER BY 1"), model)
17+
18+
dbSendQuery(conn, "CREATE OR REPLACE VIEW bar_view AS SELECT * FROM bar")
19+
20+
# check if dbSendQuery will free unreaded results
21+
dbSendQuery(conn, "SELECT * FROM bar")
22+
dbSendQuery(conn, "UPDATE bar SET foo = 'z'")
23+
24+
return(TRUE)
25+
})
26+
return(FALSE)
27+
}

R/test_dbi_driver.R

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#' @title tests a given dbi impelementation
2+
#' @description
3+
#' Given a handler to the database tests capabilities of the DBI driver.
4+
#' @param conn connection to the database opend using DBI::dbConnect()
5+
#' @return list describing driver capabilities - see test_...() functions
6+
#' description
7+
#' @import DBI
8+
#' @import testthat
9+
#' @examples
10+
#' \dontrun{
11+
#' library(DBI)
12+
#' system('monetdbd start monetdb')
13+
#' handlers = list(
14+
#' SQLite = dbConnect(RSQLite::SQLite(), ":memory:"),
15+
#' MySQL = dbConnect(RMySQL::MySQL(), dbname = 'myDb'),
16+
#' PostgreSQL = dbConnect(RPostgreSQL::PostgreSQL(), dbname = 'myDb'),
17+
#' MonetDB = dbConnect(MonetDB.R::MonetDB.R(), 'pathToMyMonetDb')
18+
#' )
19+
#' sapply(handlers, test_dbi_driver)
20+
#' }
21+
test_dbi_driver = function(conn){
22+
result = list(
23+
dbGetQuery = test_dbGetQuery(conn),
24+
dbReadTable = test_dbReadTable(conn),
25+
dbReadTable_another_schema = test_dbReadTable_another_schema(conn),
26+
dbListTables = test_dbListTables(conn),
27+
dbSendQuery = test_dbSendQuery(conn)
28+
)
29+
return(result)
30+
}

R/tidy_up.R

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#' @title deletes all database objects created by create_schemas()
2+
#' @param conn connection to the database opend using DBI::dbConnect()
3+
#' @import DBI
4+
#' @export
5+
tidy_up = function(conn){
6+
try(dbSendQuery(conn, "DROP TABLE bar_tmp"), silent = TRUE)
7+
try(dbSendQuery(conn, "DROP VIEW bar_view"), silent = TRUE)
8+
try(dbSendQuery(conn, "DROP TABLE bar"), silent = TRUE)
9+
try(dbSendQuery(conn, "DROP VIEW foo.bar_view"), silent = TRUE)
10+
try(dbSendQuery(conn, "DROP TABLE foo.bar"), silent = TRUE)
11+
try(dbSendQuery(conn, "DROP SCHEMA foo"), silent = TRUE)
12+
return(invisible(NULL))
13+
}

man/create_schemas.Rd

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/create_schemas.R
3+
\name{create_schemas}
4+
\alias{create_schemas}
5+
\title{creates database objects required for testing}
6+
\usage{
7+
create_schemas(conn)
8+
}
9+
\arguments{
10+
\item{conn}{connection to the database opend using DBI::dbConnect()}
11+
}
12+
\description{
13+
creates database objects required for testing
14+
}
15+

man/test_dbGetQuery.Rd

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/test_dbGetQuery.R
3+
\name{test_dbGetQuery}
4+
\alias{test_dbGetQuery}
5+
\title{tests dbGetQuery() compatibility}
6+
\usage{
7+
test_dbGetQuery(conn)
8+
}
9+
\arguments{
10+
\item{conn}{connection to the database opend using DBI::dbConnect()}
11+
}
12+
\value{
13+
TRUE or FALSE indicating test pass/fail
14+
}
15+
\description{
16+
tests dbGetQuery() compatibility
17+
}
18+

man/test_dbListTables.Rd

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/test_dbListTables.R
3+
\name{test_dbListTables}
4+
\alias{test_dbListTables}
5+
\title{tests behaviour of the dbListTables()}
6+
\usage{
7+
test_dbListTables(conn)
8+
}
9+
\arguments{
10+
\item{conn}{connection to the database opend using DBI::dbConnect()}
11+
}
12+
\value{
13+
numeric vector describing dbListTables() behaviour
14+
}
15+
\description{
16+
As the dbListTables() specification in the DBI is simply wrong (if you want
17+
to support schemas you can not return a character vector as a dbListTables()
18+
result because it is impossible to correctly escape schema and table name
19+
then), we can not provide a tests giving simple pass/fail results.
20+
21+
Instead of that we can only check out and describe some aspects of the
22+
dbListTables() like if views are listed, if temporary tables are listed, etc.
23+
}
24+

man/test_dbReadTable.Rd

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/test_dbReadTable.R
3+
\name{test_dbReadTable}
4+
\alias{test_dbReadTable}
5+
\title{tests behaviour of the dbReadTable()}
6+
\usage{
7+
test_dbReadTable(conn)
8+
}
9+
\arguments{
10+
\item{conn}{connection to the database opend using DBI::dbConnect()}
11+
}
12+
\value{
13+
TRUE or FALSE indicating test pass/fail
14+
}
15+
\description{
16+
tests behaviour of the dbReadTable()
17+
}
18+
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/test_dbReadTable_another_schema.R
3+
\name{test_dbReadTable_another_schema}
4+
\alias{test_dbReadTable_another_schema}
5+
\title{tests behaviour of the dbReadTable() regarging tables in non-default
6+
schema}
7+
\usage{
8+
test_dbReadTable_another_schema(conn)
9+
}
10+
\arguments{
11+
\item{conn}{connection to the database opend using DBI::dbConnect()}
12+
}
13+
\value{
14+
logical vector describing dbReadTable() behaviour
15+
}
16+
\description{
17+
Behaviour of the dbReadTable() regaring relations in schema other the default
18+
one is not described. Due to that we can only describe the way the driver
19+
behaves but we can not test if it meets the DBI interface or not.
20+
}
21+

man/test_dbSendQuery.Rd

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/test_dbSendQuery.R
3+
\name{test_dbSendQuery}
4+
\alias{test_dbSendQuery}
5+
\title{tests dbSendQuery() compatibility}
6+
\usage{
7+
test_dbSendQuery(conn)
8+
}
9+
\arguments{
10+
\item{conn}{connection to the database opend using DBI::dbConnect()}
11+
}
12+
\value{
13+
TRUE or FALSE indicating test pass/fail
14+
}
15+
\description{
16+
tests dbSendQuery() compatibility
17+
}
18+

0 commit comments

Comments
 (0)