tests/testthat/test-export-gtfs.R

# Tests for GTFS export functionality (export_path parameter)

test_that("download_feed() accepts export_path parameter", {
  skip_if_not_installed("tidytransit")
  skip_if_not_installed("httptest2")
  skip_if_not(mobdb_has_key(), "API key not configured")
  skip_if_not_installed("gtfsio")

  # This test verifies the parameter is accepted
  # We skip the actual download/export
  skip("Requires full GTFS download - parameter acceptance tested manually")

  httptest2::with_mock_dir("download_feed_export", {
    # Test that export_path parameter is accepted without error
    expect_no_error(
      download_feed("mdb-247", export_path = tempfile(fileext = ".zip"))
    )
  }, simplify = FALSE)
})

test_that("download_feed() requires gtfsio package for export", {
  skip_if_not_installed("tidytransit")

  # Mock a gtfs object
  mock_gtfs <- structure(list(), class = "gtfs")

  # Mock download_feed to return mock_gtfs without actually downloading
  # We'll test the export logic separately
  skip("Requires mocking download_feed internals - tested via integration")
})

test_that("export_path creates parent directory if it doesn't exist", {
  skip_if_not_installed("tidytransit")
  skip_if_not_installed("gtfsio")

  # This tests directory creation behavior
  # We test this via integration since the logic is embedded in download_feed
  skip("Directory creation tested via integration test")
})

test_that("download_feed() returns gtfs object even when exporting", {
  skip_if_not_installed("tidytransit")
  skip_if_not_installed("httptest2")
  skip_if_not(mobdb_has_key(), "API key not configured")
  skip_if_not_installed("gtfsio")

  # This test verifies that gtfs object is still returned after export
  skip("Requires full GTFS download - return value tested manually")

  httptest2::with_mock_dir("download_feed_export_return", {
    result <- download_feed("mdb-247", export_path = tempfile(fileext = ".zip"))
    expect_s3_class(result, "gtfs")
  }, simplify = FALSE)
})

test_that("download_best_feed() passes export_path to download_feed()", {
  skip_if_not_installed("tidytransit")
  skip_if_not_installed("httptest2")
  skip_if_not(mobdb_has_key(), "API key not configured")
  skip_if_not_installed("gtfsio")

  # This test verifies parameter is passed through correctly
  skip("Requires full GTFS download - parameter passing tested manually")

  httptest2::with_mock_dir("download_best_feed_export", {
    expect_no_error(
      download_best_feed(
        provider = "TriMet",
        export_path = tempfile(fileext = ".zip"),
        interactive = FALSE
      )
    )
  }, simplify = FALSE)
})

# Integration tests with mocked gtfsio
test_that("export_path functionality works with mocked gtfsio", {
  skip_if_not_installed("tidytransit")
  skip_if_not_installed("withr")

  # Create a temporary directory for testing
  temp_dir <- tempdir()
  export_file <- file.path(temp_dir, "test_export", "feed.zip")

  # Mock gtfsio::export_gtfs to avoid actual file writing
  mock_export_called <- FALSE
  mock_export_path <- NULL

  with_mocked_bindings(
    export_gtfs = function(gtfs, path) {
      mock_export_called <<- TRUE
      mock_export_path <<- path
      # Create the file to simulate export
      dir.create(dirname(path), recursive = TRUE, showWarnings = FALSE)
      file.create(path)
    },
    .package = "gtfsio"
  , {
    # This would need the full download_feed to work
    # For now, we're testing the logic in isolation
    skip("Requires mocking full download_feed flow")
  })
})

# Test error handling when gtfsio is not installed
test_that("export_path gives helpful error when gtfsio not installed", {
  skip_if_not_installed("tidytransit")

  # This test would need to temporarily "uninstall" gtfsio
  # which is complex in R testing
  skip("Package availability check tested via manual testing")

  # If we could mock requireNamespace:
  # expect_error(
  #   download_feed("mdb-247", export_path = "test.zip"),
  #   "gtfsio.*package is required"
  # )
})

# Test export_path = NULL (default behavior)
test_that("download_feed() works normally when export_path is NULL", {
  skip_if_not_installed("tidytransit")
  skip_if_not_installed("httptest2")
  skip_if_not(mobdb_has_key(), "API key not configured")

  # This verifies backward compatibility - NULL export_path should not change behavior
  skip("Requires full GTFS download - default behavior tested manually")

  httptest2::with_mock_dir("download_feed_no_export", {
    result <- download_feed("mdb-247", export_path = NULL)
    expect_s3_class(result, "gtfs")
  }, simplify = FALSE)
})

# Test with various export path formats
test_that("export_path accepts various path formats", {
  skip_if_not_installed("tidytransit")
  skip_if_not_installed("httptest2")
  skip_if_not(mobdb_has_key(), "API key not configured")
  skip_if_not_installed("gtfsio")

  skip("Path format validation tested via integration")

  # Relative path
  # Absolute path
  # Path with spaces
  # Path with nested directories
})

# Tests for raw export (raw = TRUE on export_path)

test_that("download_feed() with raw = TRUE and export_path skips tidytransit", {
  skip_if_not_installed("httptest2")
  skip_if_not(mobdb_has_key(), "API key not configured")

  # raw = TRUE should not require tidytransit
  # This test verifies the parameter combination is accepted
  skip("Requires full GTFS download - raw download tested manually")
})

test_that("download_feed() export_path uses gtfs_to_spec_format for GTFS compliance", {
  skip_if_not_installed("tidytransit")
  skip_if_not_installed("httptest2")
  skip_if_not(mobdb_has_key(), "API key not configured")
  skip_if_not_installed("gtfsio")

  # When export_path is used (without raw = TRUE), dates and times
  # are converted back to GTFS-spec-compliant format via gtfs_to_spec_format()
  # before writing with gtfsio::export_gtfs()
  skip("Requires full GTFS download - spec compliance tested manually")
})

# Test that exported file is valid ZIP
test_that("exported GTFS feed is a valid ZIP file", {
  skip_if_not_installed("tidytransit")
  skip_if_not_installed("httptest2")
  skip_if_not(mobdb_has_key(), "API key not configured")
  skip_if_not_installed("gtfsio")

  skip("ZIP file validation tested via integration")

  httptest2::with_mock_dir("download_feed_export_valid", {
    temp_path <- tempfile(fileext = ".zip")
    result <- download_feed("mdb-247", export_path = temp_path)

    # Verify file exists
    expect_true(file.exists(temp_path))

    # Verify it's a valid ZIP
    zip_contents <- zip::zip_list(temp_path)
    expect_true(nrow(zip_contents) > 0)

    # Cleanup
    unlink(temp_path)
  }, simplify = FALSE)
})

Try the mobdb package in your browser

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

mobdb documentation built on Aug. 29, 2026, 1:07 a.m.