# Test suite for ddbs_crs S3 generic and methods testthat::skip_on_cran() test_that("ddbs_crs works for different input types", { skip_if_not_installed("sf") skip_if_not_installed("duckdb") # Setup conn <- ddbs_create_conn() on.exit(ddbs_stop_conn(conn)) nc_sf <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)[1:5,] # 1. SF Object method expect_equal(ddbs_crs(nc_sf), sf::st_crs(nc_sf)) # 2. duckspatial_df method ddbs_write_table(conn, nc_sf, "nc_data") # Use default retrieval options nc_ds <- as_duckspatial_df("nc_data", conn, crs = sf::st_crs(nc_sf)) expect_equal(ddbs_crs(nc_ds), sf::st_crs(nc_sf)) # 3. tbl_duckdb_connection (lazy) method # Plain lazy table without duckspatial_df wrapper # nc_lazy <- dplyr::tbl(conn, "nc_data") nc_lazy <- as_duckspatial_df("nc_data", conn) # ddbs_crs should fallback to looking up the table in the DB if it is a simple table ref # Our implementation for tbl_duckdb_connection tries to detect from VIEW SQL or ST_Read # BUT "nc_data" is a table with 'crs_duckspatial' column? # ddbs_crs.tbl_duckdb_connection implementation ONLY looks for ST_Read views # It falls back to NA if not found. # Wait, if I pass a tbl(conn, "table"), it's a tbl_duckdb_connection. # Does logic support finding CRS from 'crs_duckspatial' column for lazy tables? # ddbs_crs.tbl_duckdb_connection logic I wrote prioritizes auto-detection from SQL. # If that fails, it returns NA. # Ideally it should check for 'crs_duckspatial' column too? # Currently it prints a warning and returns NA. # Let's verifying assumption - it might be NA for plain tables. # Let's test the specific feature: Auto-detection from view path <- system.file("shape/nc.shp", package = "sf") DBI::dbExecute(conn, glue::glue("CREATE VIEW nc_view AS SELECT * FROM ST_Read('{path}')")) # nc_view_lazy <- dplyr::tbl(conn, "nc_view") nc_view_lazy <- as_duckspatial_df("nc_view", conn) crs_auto <- ddbs_crs(nc_view_lazy) expect_false(is.na(crs_auto)) expect_equal(crs_auto$epsg, 4267) # NC shapefile has 4267 # 4. Character method # Uses 'crs_duckspatial' column by default crs_char <- ddbs_crs("nc_data", conn = conn) expect_true(crs_char == sf::st_crs(nc_sf)) # 5. Connection method (Backward Compatibility) # ddbs_crs(conn, name) crs_compat <- ddbs_crs(conn, "nc_data") expect_true(crs_compat == sf::st_crs(nc_sf)) # 6. Default/Error expect_error(ddbs_crs(NULL)) expect_error(ddbs_crs(123)) })