Using the .setUp() and .tearDown() function in RUnit testsuite

183 views Asked by At

I have the following issue with my R tests. I have test functions that need to alter the database, compute the results, check if these results are equal to the test values and clean the database. I am trying to do using transactions from MySQL and dbBegin(con) and dbRollback(con) functions from RMySQL.

I was trying to run the following code:

.setUp <- function() {
dbBegin(con)
}

.tearDown <- function() {
dbRollback(con)
}

test.function1 <- function() {
....
}

test.function2 <- function() {
....
}

with the test suite

test.suite <- defineTestSuite("example",
                              dirs = file.path("tests"),
                              testFileRegexp = '*.r')

test.result <- runTestSuite(test.suite)
printTextProtocol(test.result)

However, when I run multiple functions I get the

Error in .local(conn, statement, ...) : 
  could not run statement: Duplicate entry '-1' for key 'PRIMARY'

Which means that I never rollback what I write in my database.

Could anyone indicate what is wrong in the code above and/or how to write tests in R + RMySQL to test inside transactions?

Thanks, Vladimir

1

There are 1 answers

0
Volodymyr Kruglov On BEST ANSWER

This is a proper way of testing R functions that alter the MySQL database. I was getting the error because of a typo.