tests/testthat/test-s3-upload-location.R

context("S3 upload location")

test_that("Check if the S3 upload location is correctly built",{
  skip_if_no_boto()
  skip_if_no_env()
  # Test connection is using AWS CLI to set profile_name 
  conn <- dbConnect(RAthena::athena(),
                    s3_staging_dir = Sys.getenv("rathena_s3_query"))
  
  # schema and name not s3 location 
  name <- "dummy_table"
  s3.location <- "s3://bucket/path/to/file"
  partition <- c("YEAR"= 2000)
  s3_1 <- RAthena:::s3_upload_location(conn, s3.location, name, partition)
  s3_2 <- RAthena:::s3_upload_location(conn, s3.location, name, NULL)
  
  # schema in s3 location 
  s3.location <- "s3://bucket/path/to/file/schema"
  name <- "schema.dummy_table"
  s3_3 <- RAthena:::s3_upload_location(conn, s3.location, name, partition)
  s3_4 <- RAthena:::s3_upload_location(conn, s3.location, name, NULL)
  
  # name in s3 location 
  s3.location <- "s3://bucket/path/to/file/dummy_table"
  name <- "schema.dummy_table"
  s3_3 <- RAthena:::s3_upload_location(conn, s3.location, name, partition)
  s3_4 <- RAthena:::s3_upload_location(conn, s3.location, name, NULL)
  
  # schema different s3 location
  s3.location <- "s3://bucket/path/schema/to/file"
  name <- "schema.dummy_table"
  s3_5 <- RAthena:::s3_upload_location(conn, s3.location, name, partition)
  s3_6 <- RAthena:::s3_upload_location(conn, s3.location, name, NULL)
  
  # schema and table in s3 location
  s3.location <- "s3://bucket/path/to/file/schema/dummy_table"
  name <- "schema.dummy_table"
  s3_7 <- RAthena:::s3_upload_location(conn, s3.location, name, partition)
  s3_8 <- RAthena:::s3_upload_location(conn, s3.location, name, NULL)
  
  # s3 location for existing table (should ignore schema/name/ partition)
  s3.location <- "s3://bucket/path/to/file/dummy_table"
  name <- "schema.dummy_table"
  s3_9 <- RAthena:::s3_upload_location(conn, s3.location, name, partition, TRUE)
  s3_10 <- RAthena:::s3_upload_location(conn, s3.location, name, NULL, TRUE)
  
  expect_equal(s3_1, list(Bucket = "bucket", Key = "path/to/file", Schema = "default", Name = "dummy_table", Partition = "YEAR=2000"))
  expect_equal(s3_2, list(Bucket = "bucket", Key = "path/to/file", Schema = "default", Name = "dummy_table", Partition = NULL))
  expect_equal(s3_3, list(Bucket = "bucket", Key = "path/to/file/dummy_table", Schema = "schema", Name = NULL, Partition = "YEAR=2000"))
  expect_equal(s3_4, list(Bucket = "bucket", Key = "path/to/file/dummy_table", Schema = "schema", Name = NULL, Partition = NULL))
  expect_equal(s3_5, list(Bucket = "bucket", Key = "path/schema/to/file", Schema = NULL, Name = "dummy_table", Partition = "YEAR=2000"))
  expect_equal(s3_6, list(Bucket = "bucket", Key = "path/schema/to/file", Schema = NULL, Name = "dummy_table", Partition = NULL))
  expect_equal(s3_7, list(Bucket = "bucket", Key = "path/to/file/schema/dummy_table", Schema = NULL, Name = NULL, Partition = "YEAR=2000"))
  expect_equal(s3_8, list(Bucket = "bucket", Key = "path/to/file/schema/dummy_table", Schema = NULL, Name = NULL, Partition = NULL))
  expect_equal(s3_9, list(Bucket = "bucket", Key = "path/to/file/dummy_table", Schema = NULL, Name = NULL, Partition = "YEAR=2000"))
  expect_equal(s3_10, list(Bucket = "bucket", Key = "path/to/file/dummy_table", Schema = NULL, Name = NULL, Partition = NULL))
})

Try the RAthena package in your browser

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

RAthena documentation built on Dec. 28, 2022, 1:19 a.m.