# test_scrape_books.R library(testthat) library(httr) library(rvest) library(dplyr) library(magrittr) library(stringr) library(parallel) # Mock data mock_book_ids <- c(1234, 5678) mock_html <- '

Test Book

Book details

Published 2023

300 pages, Paperback

Test Author
Fiction Drama
1000 ratings 500 reviews
4.5
500
' # Mock functions mock_GET <- function(url) { structure( list( status_code = 200, content = charToRaw(mock_html) ), class = "response" ) } test_that("scrape_books function works correctly", { skip_on_cran() # Create a temporary file with mock book IDs temp_file <- tempfile() writeLines(as.character(mock_book_ids), temp_file) # Mock the necessary functions with_mock( `httr::GET` = mock_GET, { result <- scrape_books(temp_file) expect_s3_class(result, "data.frame") expect_equal(nrow(result), length(mock_book_ids)) expect_equal(result$book_id, as.character(mock_book_ids)) expect_equal(result$book_title, rep("Test Book", length(mock_book_ids))) expect_equal(result$author, rep("Test Author", length(mock_book_ids))) expect_equal(result$num_pages, rep("300", length(mock_book_ids))) expect_equal(result$genres, rep("Fiction, Drama", length(mock_book_ids))) expect_equal(result$num_ratings, rep("1000", length(mock_book_ids))) expect_equal(result$num_reviews, rep("500", length(mock_book_ids))) expect_equal(result$average_rating, rep("4.5", length(mock_book_ids))) } ) # Clean up the temporary file unlink(temp_file) })