tests/testthat/test-report.R

suppressPackageStartupMessages(library(testthat))

test_that("list_report returns valid report names", {
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    
    # Test with db file directly
    reports <- list_report(db_file)
    expect_is(reports, "character")
    expect_true(length(reports) > 0)
    
    # Test with apsimx file (should convert to db)
    apsimx_file <- system.file("extdata/wheat.apsimx", package = "rapsimng")
    reports_from_apsimx <- list_report(apsimx_file)
    expect_equal(reports, reports_from_apsimx)
})

test_that("list_report errors on invalid inputs", {
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    
    # Test multiple files
    expect_error(list_report(c(db_file, db_file)), "only support single file")
    
    # Test missing file
    expect_error(list_report("nonexistent.db"), "File does not exist")
    
    # Test invalid extension
    expect_error(list_report("file.txt"), "Require a db file")
})

test_that("read_report returns data frame with correct structure", {
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    
    # First get available reports
    reports <- list_report(db_file)
    
    if (length(reports) > 0) {
        # Read the first available report
        result <- read_report(db_file, reports[1])
        expect_is(result, "tbl_df")
        expect_true(nrow(result) > 0)
        expect_true(ncol(result) > 0)
    }
})

test_that("read_report opens database in readonly mode", {
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    reports <- list_report(db_file)
    
    if (length(reports) > 0) {
        # Read the report (opens in readonly mode)
        result <- read_report(db_file, reports[1])
        
        # Verify data was read successfully
        expect_true(is.data.frame(result))
        expect_true(nrow(result) > 0)
    }
})

test_that("read_report with apsimx file (converts to db)", {
    apsimx_file <- system.file("extdata/wheat.apsimx", package = "rapsimng")
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    
    # Get reports from apsimx file
    reports <- list_report(apsimx_file)
    
    if (length(reports) > 0) {
        # Read from apsimx file (should convert to db internally)
        result_from_apsimx <- read_report(apsimx_file, reports[1])
        
        # Read directly from db file for comparison
        result_from_db <- read_report(db_file, reports[1])
        
        # Results should be identical
        expect_equal(nrow(result_from_apsimx), nrow(result_from_db))
        expect_equal(ncol(result_from_apsimx), ncol(result_from_db))
    }
})

test_that("read_report errors on invalid inputs", {
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    
    # Test with multiple files
    expect_error(read_report(c(db_file, db_file), "report"), "only support single file")
    
    # Test with multiple report names
    expect_error(read_report(db_file, c("report1", "report2")), "Only support single table")
    
    # Test missing file
    expect_error(read_report("nonexistent.db", "report"), "File does not exist")
    
    # Test invalid extension
    expect_error(read_report("file.txt", "report"), "Require a db file")
})

test_that("read_report errors on nonexistent report", {
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    
    # Try to read a report that doesn't exist
    expect_error(read_report(db_file, "NonexistentReport"), 
                 "Cannot find table NonexistentReport")
})

test_that("No extra db-shm and db-wal files created after read_report", {
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    db_dir <- dirname(db_file)
    db_basename <- basename(db_file)
    
    # Get list of files before reading
    files_before <- list.files(db_dir, pattern = "wheat", full.names = FALSE)
    
    # Read a report
    reports <- list_report(db_file)
    if (length(reports) > 0) {
        result <- read_report(db_file, reports[1])
    }
    
    # Give system a moment to clean up
    Sys.sleep(0.5)
    
    # Get list of files after reading
    files_after <- list.files(db_dir, pattern = "wheat", full.names = FALSE)
    
    # Check that no extra .db-shm or .db-wal files were created
    shm_files <- files_after[grepl("\\.db-shm$", files_after)]
    wal_files <- files_after[grepl("\\.db-wal$", files_after)]
    
    expect_equal(length(shm_files), 0, 
                 info = paste("Found unexpected .db-shm files:", paste(shm_files, collapse = ", ")))
    expect_equal(length(wal_files), 0, 
                 info = paste("Found unexpected .db-wal files:", paste(wal_files, collapse = ", ")))
})

test_that("list_report also does not create extra db files", {
    db_file <- system.file("extdata/wheat.db", package = "rapsimng")
    db_dir <- dirname(db_file)
    db_basename <- basename(db_file)
    
    # Get list of files before reading
    files_before <- list.files(db_dir, pattern = "wheat", full.names = FALSE)
    
    # List reports
    reports <- list_report(db_file)
    
    # Give system a moment to clean up
    Sys.sleep(0.5)
    
    # Get list of files after reading
    files_after <- list.files(db_dir, pattern = "wheat", full.names = FALSE)
    
    # Check that no extra .db-shm or .db-wal files were created
    shm_files <- files_after[grepl("\\.db-shm$", files_after)]
    wal_files <- files_after[grepl("\\.db-wal$", files_after)]
    
    expect_equal(length(shm_files), 0, 
                 info = paste("Found unexpected .db-shm files:", paste(shm_files, collapse = ", ")))
    expect_equal(length(wal_files), 0, 
                 info = paste("Found unexpected .db-wal files:", paste(wal_files, collapse = ", ")))
})

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.