tests/testthat/test-report-readonly.R

suppressPackageStartupMessages(library(testthat))

test_that("readonly mode works with existing database", {
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    
    # Test list_report with readonly
    reports <- list_report(db_file)
    expect_is(reports, "character")
    expect_true(length(reports) > 0)
    
    # Test read_report with readonly
    result <- read_report(db_file, reports[1])
    expect_is(result, "tbl_df")
    expect_true(nrow(result) > 0)
})

test_that("No WAL files created in temp directory after operations", {
    # Create a temporary directory for this test
    temp_dir <- tempdir()
    
    # Copy the test database to temp directory
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    temp_db <- file.path(temp_dir, "test_readonly.db")
    
    if (file.exists(temp_db)) file.remove(temp_db)
    file.copy(db_file, temp_db, overwrite = TRUE)
    
    # Verify no WAL files exist before operations
    files_before <- list.files(temp_dir, pattern = "test_readonly", full.names = FALSE)
    shm_before <- files_before[grepl("\\.db-shm$", files_before)]
    wal_before <- files_before[grepl("\\.db-wal$", files_before)]
    
    expect_equal(length(shm_before), 0, info = "SHM file should not exist before")
    expect_equal(length(wal_before), 0, info = "WAL file should not exist before")
    
    # Perform list_report operation
    reports <- list_report(temp_db)
    
    # Give system a moment to clean up
    Sys.sleep(0.5)
    
    # Verify no WAL files after list_report
    files_after_list <- list.files(temp_dir, pattern = "test_readonly", full.names = FALSE)
    shm_after_list <- files_after_list[grepl("\\.db-shm$", files_after_list)]
    wal_after_list <- files_after_list[grepl("\\.db-wal$", files_after_list)]
    
    expect_equal(length(shm_after_list), 0, 
                 info = "SHM file should not be created after list_report")
    expect_equal(length(wal_after_list), 0, 
                 info = "WAL file should not be created after list_report")
    
    # Perform read_report operation
    if (length(reports) > 0) {
        result <- read_report(temp_db, reports[1])
        
        # Give system a moment to clean up
        Sys.sleep(0.5)
        
        # Verify no WAL files after read_report
        files_after_read <- list.files(temp_dir, pattern = "test_readonly", full.names = FALSE)
        shm_after_read <- files_after_read[grepl("\\.db-shm$", files_after_read)]
        wal_after_read <- files_after_read[grepl("\\.db-wal$", files_after_read)]
        
        expect_equal(length(shm_after_read), 0, 
                     info = "SHM file should not be created after read_report")
        expect_equal(length(wal_after_read), 0, 
                     info = "WAL file should not be created after read_report")
    }
    
    # Clean up
    file.remove(temp_db)
})

test_that("readonly mode prevents modifications", {
    temp_dir <- tempdir()
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    temp_db <- file.path(temp_dir, "test_readonly_write.db")
    
    if (file.exists(temp_db)) file.remove(temp_db)
    file.copy(db_file, temp_db, overwrite = TRUE)
    
    # Open in readonly mode
    con <- rapsimng:::.open_db(temp_db)
    
    # Attempt to write should fail
    expect_error(
        DBI::dbExecute(con, "INSERT INTO _Simulations (ID, Name) VALUES (999, 'test')"),
        "readonly"
    )
    
    rapsimng:::.close_db(con)
    file.remove(temp_db)
})

test_that("readonly mode works with multiple consecutive operations", {
    temp_dir <- tempdir()
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    temp_db <- file.path(temp_dir, "test_readonly_multi.db")
    
    if (file.exists(temp_db)) file.remove(temp_db)
    file.copy(db_file, temp_db, overwrite = TRUE)
    
    # Perform multiple operations
    for (i in 1:3) {
        reports <- list_report(temp_db)
        expect_true(length(reports) > 0)
        
        if (length(reports) > 0) {
            result <- read_report(temp_db, reports[1])
            expect_true(nrow(result) > 0)
        }
        
        Sys.sleep(0.2)
    }
    
    # Verify no WAL files after multiple operations
    files_final <- list.files(temp_dir, pattern = "test_readonly_multi", full.names = FALSE)
    shm_final <- files_final[grepl("\\.db-shm$", files_final)]
    wal_final <- files_final[grepl("\\.db-wal$", files_final)]
    
    expect_equal(length(shm_final), 0, 
                 info = "SHM file should not exist after multiple operations")
    expect_equal(length(wal_final), 0, 
                 info = "WAL file should not exist after multiple operations")
    
    # Clean up
    file.remove(temp_db)
})

test_that("readonly mode handles concurrent reads", {
    temp_dir <- tempdir()
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    temp_db <- file.path(temp_dir, "test_readonly_concurrent.db")
    
    if (file.exists(temp_db)) file.remove(temp_db)
    file.copy(db_file, temp_db, overwrite = TRUE)
    
    # Open two connections simultaneously
    con1 <- rapsimng:::.open_db(temp_db)
    con2 <- rapsimng:::.open_db(temp_db)
    
    # Read from both connections
    tbls1 <- RSQLite::dbListTables(con1)
    tbls2 <- RSQLite::dbListTables(con2)
    
    expect_equal(tbls1, tbls2)
    
    # Close both connections
    rapsimng:::.close_db(con1)
    rapsimng:::.close_db(con2)
    
    # Verify no WAL files after concurrent reads
    Sys.sleep(0.5)
    files_final <- list.files(temp_dir, pattern = "test_readonly_concurrent", full.names = FALSE)
    shm_final <- files_final[grepl("\\.db-shm$", files_final)]
    wal_final <- files_final[grepl("\\.db-wal$", files_final)]
    
    expect_equal(length(shm_final), 0, 
                 info = "SHM file should not exist after concurrent reads")
    expect_equal(length(wal_final), 0, 
                 info = "WAL file should not exist after concurrent reads")
    
    # Clean up
    file.remove(temp_db)
})

test_that("readonly mode works with paths containing spaces and special chars", {
    temp_dir <- tempdir()
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    
    # Create a subdirectory with spaces in the name
    special_dir <- file.path(temp_dir, "test dir with spaces")
    if (!dir.exists(special_dir)) dir.create(special_dir)
    
    temp_db <- file.path(special_dir, "test_db.db")
    
    if (file.exists(temp_db)) file.remove(temp_db)
    file.copy(db_file, temp_db, overwrite = TRUE)
    
    # Test operations with special paths
    reports <- list_report(temp_db)
    expect_true(length(reports) > 0)
    
    if (length(reports) > 0) {
        result <- read_report(temp_db, reports[1])
        expect_true(nrow(result) > 0)
    }
    
    # Verify no WAL files
    Sys.sleep(0.5)
    files_final <- list.files(special_dir, pattern = "test_db", full.names = FALSE)
    shm_final <- files_final[grepl("\\.db-shm$", files_final)]
    wal_final <- files_final[grepl("\\.db-wal$", files_final)]
    
    expect_equal(length(shm_final), 0, 
                 info = "SHM file should not exist with special paths")
    expect_equal(length(wal_final), 0, 
                 info = "WAL file should not exist with special paths")
    
    # Clean up
    file.remove(temp_db)
    unlink(special_dir, recursive = TRUE)
})

Try the rapsimng package in your browser

Any scripts or data that you put into this service are public.

rapsimng documentation built on Nov. 30, 2026, 5:08 p.m.