# tests/testthat/test-data-import.Rlibrary(testthat)library(mypackage)# Test data import functionstest_that("import_csv works with valid file",{# Create temporary CSV filetemp_file<-tempfile(fileext=".csv")test_data<-data.frame(id=1:3,name=c("Alice","Bob","Charlie"),value=c(10.5,20.3,15.7))write.csv(test_data,temp_file,row.names=FALSE)# Test importresult<-import_csv(temp_file)# Assertionsexpect_s3_class(result,"data.frame")expect_equal(nrow(result),3)expect_equal(ncol(result),3)expect_equal(names(result),c("id","name","value"))# Clean upunlink(temp_file)})test_that("import_csv handles missing file",{expect_error(import_csv("nonexistent.csv"),"File not found")})test_that("import_csv handles empty file",{temp_file<-tempfile(fileext=".csv")writeLines("",temp_file)expect_error(import_csv(temp_file),"Empty file")unlink(temp_file)})
# tests/testthat/test-data-cleaning.Rlibrary(testthat)library(mypackage)# Test data cleaning functionstest_that("clean_data removes duplicates",{test_data<-data.frame(id=c(1,2,1,3),name=c("Alice","Bob","Alice","Charlie"),value=c(10,20,10,30))config<-list(remove_duplicates=TRUE,handle_missing=FALSE,standardize_text=FALSE,convert_types=FALSE,remove_outliers=FALSE)result<-clean_data(test_data,config)expect_equal(nrow(result),3)expect_equal(result$id,c(1,2,3))})test_that("clean_data handles missing values",{test_data<-data.frame(id=c(1,2,3),name=c("Alice",NA,"Charlie"),value=c(10,NA,30))config<-list(remove_duplicates=FALSE,handle_missing=TRUE,missing_strategy="impute_mean",standardize_text=FALSE,convert_types=FALSE,remove_outliers=FALSE)result<-clean_data(test_data,config)expect_false(any(is.na(result$value)))expect_equal(result$value[2],mean(c(10,30)))})test_that("clean_data standardizes text",{test_data<-data.frame(id=c(1,2,3),name=c(" ALICE ","bob","Charlie"),value=c(10,20,30))config<-list(remove_duplicates=FALSE,handle_missing=FALSE,standardize_text=TRUE,text_columns=c("name"),convert_types=FALSE,remove_outliers=FALSE)result<-clean_data(test_data,config)expect_equal(result$name,c("alice","bob","charlie"))})
# tests/testthat/test-analysis.Rlibrary(testthat)library(mypackage)# Test statistical functionstest_that("calculate_descriptive_stats works with normal data",{set.seed(123)test_data<-data.frame(x=rnorm(100,mean=10,sd=2),y=rnorm(100,mean=20,sd=3))result<-calculate_descriptive_stats(test_data)expect_s3_class(result,"data.frame")expect_equal(nrow(result),2)expect_equal(ncol(result),9)expect_equal(names(result),c("variable","mean","median","sd","min","max","q25","q75"))# Check that means are close to expected valuesexpect_true(abs(result$mean[result$variable=="x"]-10)<0.5)expect_true(abs(result$mean[result$variable=="y"]-20)<0.5)})test_that("calculate_correlations works with correlated data",{set.seed(123)x<-rnorm(100)y<-2*x+rnorm(100,sd=0.1)test_data<-data.frame(x=x,y=y)result<-calculate_correlations(test_data)expect_true(is.matrix(result))expect_equal(dim(result),c(2,2))expect_true(result[1,2]>0.9)# High correlation})test_that("perform_regression works with linear relationship",{set.seed(123)x<-rnorm(100)y<-2*x+rnorm(100)test_data<-data.frame(x=x,y=y)config<-list(formula="y ~ x",method="linear")result<-perform_regression(test_data,config)expect_s3_class(result$model,"lm")expect_true(result$r_squared>0.8)expect_equal(length(result$coefficients),2)})
# tests/testthat/test-integration.Rlibrary(testthat)library(mypackage)# Test complete data pipelinetest_that("complete analysis pipeline works",{# Create test datatest_data<-create_test_data(100)# Save to temporary filetemp_file<-tempfile(fileext=".csv")write.csv(test_data,temp_file,row.names=FALSE)# Run complete pipelineconfig<-create_test_config()results<-run_analysis(temp_file,config)# Assertionsexpect_true(is.list(results))expect_true("descriptive"%in%names(results))expect_true("correlation"%in%names(results))# Clean upunlink(temp_file)})# Test data pipeline with missing valuestest_that("pipeline handles missing values",{test_data<-create_test_data(100)test_data$value[sample(1:100,10)]<-NAtemp_file<-tempfile(fileext=".csv")write.csv(test_data,temp_file,row.names=FALSE)config<-create_test_config()config$cleaning_config$handle_missing<-TRUEconfig$cleaning_config$missing_strategy<-"impute_mean"results<-run_analysis(temp_file,config)expect_true(is.list(results))expect_false(any(is.na(results$descriptive)))unlink(temp_file)})
# tests/testthat/test-database-integration.Rlibrary(testthat)library(mypackage)# Test database connectiontest_that("database connection works",{skip_if_not_installed("DBI")skip_if_not_installed("RSQLite")# Create in-memory databasecon<-DBI::dbConnect(RSQLite::SQLite(),":memory:")on.exit(DBI::dbDisconnect(con))# Create test tabletest_data<-data.frame(id=1:3,name=c("Alice","Bob","Charlie"),value=c(10,20,30))DBI::dbWriteTable(con,"test_table",test_data)# Test importresult<-import_database(":memory:","SELECT * FROM test_table")expect_s3_class(result,"data.frame")expect_equal(nrow(result),3)expect_equal(names(result),c("id","name","value"))})
# tests/testthat/test-performance.Rlibrary(testthat)library(mypackage)library(microbenchmark)# Test function performancetest_that("calculate_descriptive_stats is fast",{set.seed(123)large_data<-data.frame(x=rnorm(10000),y=rnorm(10000),z=rnorm(10000))# Benchmark the functionbenchmark_result<-microbenchmark(calculate_descriptive_stats(large_data),times=10)# Check that median time is less than 1 secondexpect_true(median(benchmark_result$time)<1e9)# 1 second in nanoseconds})# Test memory usagetest_that("functions don't use excessive memory",{set.seed(123)large_data<-data.frame(x=rnorm(10000),y=rnorm(10000))# Measure memory usagememory_before<-gc()result<-calculate_descriptive_stats(large_data)memory_after<-gc()# Check that memory usage didn't increase significantlymemory_increase<-memory_after$used-memory_before$usedexpect_true(memory_increase<100)# Less than 100 MB increase})
# tests/helpers/mock-data.R#' Create test data for testing#'#' @param n Number of observations#' @param seed Random seed#' @return Test data framecreate_test_data<-function(n=100,seed=123){set.seed(seed)data.frame(id=1:n,name=paste("Person",1:n),value=rnorm(n,mean=10,sd=2),category=sample(c("A","B","C"),n,replace=TRUE),date=seq(as.Date("2023-01-01"),by="day",length.out=n))}#' Create test data with missing values#'#' @param n Number of observations#' @param missing_rate Rate of missing values#' @param seed Random seed#' @return Test data frame with missing valuescreate_test_data_with_missing<-function(n=100,missing_rate=0.1,seed=123){set.seed(seed)data<-create_test_data(n,seed)# Add missing valuesn_missing<-round(n*missing_rate)missing_indices<-sample(1:n,n_missing)data$value[missing_indices]<-NAreturn(data)}#' Create test data with outliers#'#' @param n Number of observations#' @param outlier_rate Rate of outliers#' @param seed Random seed#' @return Test data frame with outlierscreate_test_data_with_outliers<-function(n=100,outlier_rate=0.05,seed=123){set.seed(seed)data<-create_test_data(n,seed)# Add outliersn_outliers<-round(n*outlier_rate)outlier_indices<-sample(1:n,n_outliers)data$value[outlier_indices]<-data$value[outlier_indices]+10return(data)}
# tests/helpers/test-helpers.R#' Assert that two data frames are equal#'#' @param actual Actual data frame#' @param expected Expected data frame#' @param tolerance Numeric tolerance for comparisonsassert_data_frame_equal<-function(actual,expected,tolerance=1e-6){expect_equal(nrow(actual),nrow(expected))expect_equal(ncol(actual),ncol(expected))expect_equal(names(actual),names(expected))for(colinnames(actual)){if(is.numeric(actual[[col]])){expect_equal(actual[[col]],expected[[col]],tolerance=tolerance)}else{expect_equal(actual[[col]],expected[[col]])}}}#' Assert that a function throws an error with specific message#'#' @param expr Expression to evaluate#' @param expected_message Expected error messageassert_error_message<-function(expr,expected_message){expect_error(expr,expected_message,fixed=TRUE)}#' Assert that a function returns a specific class#'#' @param expr Expression to evaluate#' @param expected_class Expected classassert_class<-function(expr,expected_class){result<-exprexpect_s3_class(result,expected_class)}#' Assert that a numeric value is within a range#'#' @param value Numeric value#' @param min_val Minimum value#' @param max_val Maximum valueassert_in_range<-function(value,min_val,max_val){expect_true(value>=min_val&&value<=max_val)}
# 1. Set up testingusethis::use_testthat()# 2. Create test filesusethis::use_test("data-import")usethis::use_test("data-cleaning")usethis::use_test("analysis")# 3. Run teststestthat::test_dir("tests/testthat")# 4. Run specific testtestthat::test_file("tests/testthat/test-data-import.R")# 5. Run with coveragelibrary(covr)coverage<-package_coverage(".")report(coverage)
# Basic test structuretest_that("function works correctly",{# Arrangeinput<-create_test_input()# Actresult<-my_function(input)# Assertexpect_equal(result,expected_output)})# Test with multiple casestest_that("function handles different inputs",{test_cases<-list(normal=c(1,2,3,4,5),empty=numeric(0),single=42)for(case_nameinnames(test_cases)){result<-my_function(test_cases[[case_name]])expect_true(is.numeric(result))}})