From de904d5736dca027515e263b631a53d35e9b3795 Mon Sep 17 00:00:00 2001 From: Chung-hong Chan Date: Wed, 9 Jun 2021 17:15:06 +0200 Subject: [PATCH] use httptest to mock API request; add test suite for get_all_tweets; update test for set_bearer #115 #78 --- DESCRIPTION | 3 +- R/utils.R | 5 ++ .../2/tweets/search/all-45a673.R | 31 +++++++ tests/testthat/setup.R | 1 + tests/testthat/test-get_all_tweets.R | 87 +++++++++++++++++++ tests/testthat/test-set_bearer.R | 13 +++ 6 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/api.twitter.com/2/tweets/search/all-45a673.R create mode 100644 tests/testthat/setup.R create mode 100644 tests/testthat/test-get_all_tweets.R diff --git a/DESCRIPTION b/DESCRIPTION index 7ca58e0..b2a3c4d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -40,6 +40,7 @@ Suggests: knitr, rmarkdown, devtools, - testthat (>= 3.0.0) + testthat (>= 3.0.0), + httptest VignetteBuilder: knitr Config/testthat/edition: 3 diff --git a/R/utils.R b/R/utils.R index bd3ae5e..a89b36c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -224,3 +224,8 @@ create_storage_dir <- function(data_path, export_query, built_query, start_tweet } } } + + +.gen_random_dir <- function() { + paste0(tempdir(), "/", paste0(sample(letters, 20), collapse = "")) +} diff --git a/tests/testthat/api.twitter.com/2/tweets/search/all-45a673.R b/tests/testthat/api.twitter.com/2/tweets/search/all-45a673.R new file mode 100644 index 0000000..50c72b5 --- /dev/null +++ b/tests/testthat/api.twitter.com/2/tweets/search/all-45a673.R @@ -0,0 +1,31 @@ +structure(list(url = "https://api.twitter.com/2/tweets/search/all?query=%23commtwitter&max_results=500&start_time=2021-06-01T00%3A00%3A00Z&end_time=2021-06-05T00%3A00%3A00Z&tweet.fields=attachments%2Cauthor_id%2Ccontext_annotations%2Cconversation_id%2Ccreated_at%2Centities%2Cgeo%2Cid%2Cin_reply_to_user_id%2Clang%2Cpublic_metrics%2Cpossibly_sensitive%2Creferenced_tweets%2Csource%2Ctext%2Cwithheld&user.fields=created_at%2Cdescription%2Centities%2Cid%2Clocation%2Cname%2Cpinned_tweet_id%2Cprofile_image_url%2Cprotected%2Cpublic_metrics%2Curl%2Cusername%2Cverified%2Cwithheld&expansions=author_id%2Centities.mentions.username%2Cgeo.place_id%2Cin_reply_to_user_id%2Creferenced_tweets.id%2Creferenced_tweets.id.author_id&place.fields=contained_within%2Ccountry%2Ccountry_code%2Cfull_name%2Cgeo%2Cid%2Cname%2Cplace_type", + status_code = 200L, headers = structure(list(date = "Wed, 09 Jun 2021 12:30:13 UTC", + server = "tsa_o", `content-type` = "application/json; charset=utf-8", + `cache-control` = "no-cache, no-store, max-age=0", `content-length` = "3519", + `x-access-level` = "read", `x-frame-options` = "SAMEORIGIN", + `content-encoding` = "gzip", `x-xss-protection` = "0", + `x-rate-limit-limit` = "300", `x-rate-limit-reset` = "1623242334", + `content-disposition` = "attachment; filename=json.json", + `x-content-type-options` = "nosniff", `x-rate-limit-remaining` = "283", + `strict-transport-security` = "max-age=631138519", `x-connection-hash` = "1d3bb70e897c85d76fb89a675b55db991a8c66a1d3b9aa7b410db16493fb6f10"), class = c("insensitive", + "list")), all_headers = list(list(status = 200L, version = "HTTP/2", + headers = structure(list(date = "Wed, 09 Jun 2021 12:30:13 UTC", + server = "tsa_o", `content-type` = "application/json; charset=utf-8", + `cache-control` = "no-cache, no-store, max-age=0", + `content-length` = "3519", `x-access-level` = "read", + `x-frame-options` = "SAMEORIGIN", `content-encoding` = "gzip", + `x-xss-protection` = "0", `x-rate-limit-limit` = "300", + `x-rate-limit-reset` = "1623242334", `content-disposition` = "attachment; filename=json.json", + `x-content-type-options` = "nosniff", `x-rate-limit-remaining` = "283", + `strict-transport-security` = "max-age=631138519", + `x-connection-hash` = "1d3bb70e897c85d76fb89a675b55db991a8c66a1d3b9aa7b410db16493fb6f10"), class = c("insensitive", + "list")))), cookies = structure(list(domain = c(".twitter.com", + ".twitter.com"), flag = c(TRUE, TRUE), path = c("/", "/"), + secure = c(TRUE, TRUE), expiration = structure(c(1686308276, + 1686308276), class = c("POSIXct", "POSIXt")), name = c("personalization_id", + "guest_id"), value = c("REDACTED", "REDACTED")), row.names = c(NA, + -2L), class = "data.frame"), content = charToRaw("{\"data\":[{\"lang\":\"en\",\"public_metrics\":{\"retweet_count\":6,\"reply_count\":0,\"like_count\":0,\"quote_count\":0},\"author_id\":\"45648666\",\"text\":\"RT @NatComm: Are you interested in a career outside of academia? Check out NCA’s “#AltAC in COM” YouTube playlist, which includes videos fr…\",\"entities\":{\"hashtags\":[{\"start\":82,\"end\":88,\"tag\":\"AltAC\"}],\"mentions\":[{\"start\":3,\"end\":11,\"username\":\"NatComm\"}]},\"referenced_tweets\":[{\"type\":\"retweeted\",\"id\":\"1391807748346683397\"}],\"source\":\"Twitter Web App\",\"id\":\"1400199391961620488\",\"conversation_id\":\"1400199391961620488\",\"possibly_sensitive\":false,\"created_at\":\"2021-06-02T21:15:24.000Z\",\"context_annotations\":[{\"domain\":{\"id\":\"65\",\"name\":\"Interests and Hobbies Vertical\",\"description\":\"Top level interests and hobbies groupings, like Food or Travel\"},\"entity\":{\"id\":\"847544972781826048\",\"name\":\"Careers\",\"description\":\"Careers\"}},{\"domain\":{\"id\":\"46\",\"name\":\"Brand Category\",\"description\":\"Categories within Brand Verticals that narrow down the scope of Brands\"},\"entity\":{\"id\":\"781974596752842752\",\"name\":\"Services\"}},{\"domain\":{\"id\":\"47\",\"name\":\"Brand\",\"description\":\"Brands and Companies\"},\"entity\":{\"id\":\"10029382357\",\"name\":\"YouTube\"}}]},{\"lang\":\"en\",\"public_metrics\":{\"retweet_count\":2,\"reply_count\":0,\"like_count\":0,\"quote_count\":0},\"author_id\":\"956624418\",\"entities\":{\"annotations\":[{\"start\":13,\"end\":19,\"probability\":0.486,\"type\":\"Organization\",\"normalized_text\":\"Comm Ph\"},{\"start\":65,\"end\":67,\"probability\":0.6134,\"type\":\"Organization\",\"normalized_text\":\"NCA\"}],\"mentions\":[{\"start\":3,\"end\":11,\"username\":\"NatComm\"}]},\"text\":\"RT @NatComm: Comm Ph.D. students: It’s not too late to apply for NCA’s virtual Doctoral Honors Seminar! The seminar offers students a speci…\",\"referenced_tweets\":[{\"type\":\"retweeted\",\"id\":\"1399780280710221834\"}],\"source\":\"Twitter for Android\",\"id\":\"1400103340495810561\",\"conversation_id\":\"1400103340495810561\",\"possibly_sensitive\":false,\"created_at\":\"2021-06-02T14:53:43.000Z\"},{\"lang\":\"en\",\"public_metrics\":{\"retweet_count\":2,\"reply_count\":0,\"like_count\":0,\"quote_count\":0},\"author_id\":\"1145936974064177152\",\"entities\":{\"annotations\":[{\"start\":13,\"end\":19,\"probability\":0.486,\"type\":\"Organization\",\"normalized_text\":\"Comm Ph\"},{\"start\":65,\"end\":67,\"probability\":0.6134,\"type\":\"Organization\",\"normalized_text\":\"NCA\"}],\"mentions\":[{\"start\":3,\"end\":11,\"username\":\"NatComm\"}]},\"text\":\"RT @NatComm: Comm Ph.D. students: It’s not too late to apply for NCA’s virtual Doctoral Honors Seminar! The seminar offers students a speci…\",\"referenced_tweets\":[{\"type\":\"retweeted\",\"id\":\"1399780280710221834\"}],\"source\":\"Twitter Web App\",\"id\":\"1399797349296197635\",\"conversation_id\":\"1399797349296197635\",\"possibly_sensitive\":false,\"created_at\":\"2021-06-01T18:37:49.000Z\"},{\"lang\":\"en\",\"public_metrics\":{\"retweet_count\":2,\"reply_count\":0,\"like_count\":5,\"quote_count\":0},\"author_id\":\"17880989\",\"entities\":{\"annotations\":[{\"start\":0,\"end\":6,\"probability\":0.5457,\"type\":\"Organization\",\"normalized_text\":\"Comm Ph\"},{\"start\":52,\"end\":54,\"probability\":0.6734,\"type\":\"Organization\",\"normalized_text\":\"NCA\"}],\"hashtags\":[{\"start\":266,\"end\":278,\"tag\":\"CommTwitter\"}],\"urls\":[{\"start\":242,\"end\":265,\"url\":\"https://t.co/stoYToSHfz\",\"expanded_url\":\"http://Natcom.org/DHS\",\"display_url\":\"Natcom.org/DHS\",\"images\":[{\"url\":\"https://pbs.twimg.com/news_img/1399780289744760838/MfDN54Vm?format=png&name=orig\",\"width\":400,\"height\":400},{\"url\":\"https://pbs.twimg.com/news_img/1399780289744760838/MfDN54Vm?format=png&name=150x150\",\"width\":150,\"height\":150}],\"status\":200,\"title\":\"NCA Doctoral Honors Seminar\",\"description\":\"The NCA Doctoral Honors Seminars (DHS) bring together promising doctoral students and distinguished faculty members from across the discipline and around the nation to discuss current topics in Communication.\",\"unwound_url\":\"https://www.natcom.org/convention-events/nca-sponsored-events/nca-doctoral-honors-seminar\"}]},\"text\":\"Comm Ph.D. students: It’s not too late to apply for NCA’s virtual Doctoral Honors Seminar! The seminar offers students a special opportunity to work with distinguished Communication faculty members. Application deadline: June 4. Apply today! https://t.co/stoYToSHfz #CommTwitter\",\"source\":\"Twitter for Advertisers\",\"id\":\"1399780280710221834\",\"conversation_id\":\"1399780280710221834\",\"possibly_sensitive\":false,\"created_at\":\"2021-06-01T17:30:00.000Z\"},{\"lang\":\"en\",\"public_metrics\":{\"retweet_count\":0,\"reply_count\":0,\"like_count\":0,\"quote_count\":0},\"author_id\":\"17880989\",\"entities\":{\"annotations\":[{\"start\":74,\"end\":76,\"probability\":0.785,\"type\":\"Organization\",\"normalized_text\":\"NCA\"}],\"hashtags\":[{\"start\":256,\"end\":268,\"tag\":\"CommTwitter\"}],\"urls\":[{\"start\":232,\"end\":255,\"url\":\"https://t.co/AQhHUXbAhH\",\"expanded_url\":\"http://Natcom.org/CSI\",\"display_url\":\"Natcom.org/CSI\",\"images\":[{\"url\":\"https://pbs.twimg.com/news_img/1398021208306860037/Ks49f37T?format=png&name=orig\",\"width\":430,\"height\":400},{\"url\":\"https://pbs.twimg.com/news_img/1398021208306860037/Ks49f37T?format=png&name=150x150\",\"width\":150,\"height\":150}],\"status\":200,\"title\":\"NCA Chairs' Summer Institute\",\"description\":\"2021 VIRTUAL \",\"unwound_url\":\"https://www.natcom.org/convention-events/nca-sponsored-events/nca-chairs-summer-institute\"}]},\"text\":\"Today is the deadline for Communication department chairs to register for NCA’s virtual Chairs’ Summer Institute! Themed, “Leadership Challenges in a Changing World,” it will be held June 25-26, 2021. Registration deadline: June 1. https://t.co/AQhHUXbAhH #CommTwitter\",\"source\":\"Twitter Web App\",\"id\":\"1399765322509557763\",\"conversation_id\":\"1399765322509557763\",\"possibly_sensitive\":false,\"created_at\":\"2021-06-01T16:30:33.000Z\"}],\"includes\":{\"users\":[{\"profile_image_url\":\"https://pbs.twimg.com/profile_images/1394300766823895040/fS-tV4kA_normal.jpg\",\"description\":\"I study env. organizing, CSR #orgcomm #envcomm. Assoc Prof @WayneState @WSU_COM, Dir. #RISElab #DetroitWaterStories, @LifeatPurdue alum. ❤ #publicscholarship\",\"location\":\"Detroit,MI\",\"entities\":{\"url\":{\"urls\":[{\"start\":0,\"end\":23,\"url\":\"https://t.co/T5l6L9BrrE\",\"expanded_url\":\"http://www.rahulmitraphd.com\",\"display_url\":\"rahulmitraphd.com\"}]},\"description\":{\"hashtags\":[{\"start\":29,\"end\":37,\"tag\":\"orgcomm\"},{\"start\":38,\"end\":46,\"tag\":\"envcomm\"},{\"start\":86,\"end\":94,\"tag\":\"RISElab\"},{\"start\":95,\"end\":115,\"tag\":\"DetroitWaterStories\"},{\"start\":139,\"end\":157,\"tag\":\"publicscholarship\"}],\"mentions\":[{\"start\":59,\"end\":70,\"username\":\"WayneState\"},{\"start\":71,\"end\":79,\"username\":\"WSU_COM\"},{\"start\":117,\"end\":130,\"username\":\"LifeatPurdue\"}]}},\"verified\":false,\"username\":\"rahulmitra\",\"pinned_tweet_id\":\"1360333484846444554\",\"created_at\":\"2009-06-08T19:35:34.000Z\",\"name\":\"Dr. Rahul Mitra \\uD83C\\uDF08\",\"public_metrics\":{\"followers_count\":1941,\"following_count\":2671,\"tweet_count\":16464,\"listed_count\":105},\"id\":\"45648666\",\"protected\":false,\"url\":\"https://t.co/T5l6L9BrrE\"},{\"profile_image_url\":\"https://pbs.twimg.com/profile_images/1110927896435150849/M16LTNRp_normal.png\",\"description\":\"The National Communication Association is a scholarly society that enables & supports its members’ Communication research & teaching.\",\"location\":\"Washington, DC\",\"entities\":{\"url\":{\"urls\":[{\"start\":0,\"end\":22,\"url\":\"http://t.co/nPhK8jTwuD\",\"expanded_url\":\"http://www.natcom.org\",\"display_url\":\"natcom.org\"}]}},\"verified\":true,\"username\":\"NatComm\",\"created_at\":\"2008-12-04T22:04:19.000Z\",\"name\":\"NCA\",\"public_metrics\":{\"followers_count\":12249,\"following_count\":1053,\"tweet_count\":11993,\"listed_count\":417},\"id\":\"17880989\",\"protected\":false,\"url\":\"http://t.co/nPhK8jTwuD\"},{\"profile_image_url\":\"https://pbs.twimg.com/profile_images/1081676548443439104/1XgFdpFl_normal.jpg\",\"description\":\"Sharing organizational communication cases in the news, and celebrating #OrgComm teachers and our awesome #OrgCommStudents. \\uD83D\\uDC69\\uD83C\\uDFFD‍\\uD83C\\uDF93\\uD83D\\uDC68\\uD83C\\uDFFB‍\\uD83C\\uDF93\",\"entities\":{\"url\":{\"urls\":[{\"start\":0,\"end\":23,\"url\":\"https://t.co/GvEJcYb4fr\",\"expanded_url\":\"http://www.orgcominthenews.com\",\"display_url\":\"orgcominthenews.com\"}]},\"description\":{\"hashtags\":[{\"start\":72,\"end\":80,\"tag\":\"OrgComm\"},{\"start\":106,\"end\":122,\"tag\":\"OrgCommStudents\"}]}},\"verified\":false,\"username\":\"OrgComInTheNews\",\"created_at\":\"2012-11-19T02:52:07.000Z\",\"name\":\"Org Com In The News\",\"public_metrics\":{\"followers_count\":481,\"following_count\":277,\"tweet_count\":959,\"listed_count\":11},\"id\":\"956624418\",\"protected\":false,\"url\":\"https://t.co/GvEJcYb4fr\"},{\"profile_image_url\":\"https://pbs.twimg.com/profile_images/1261391537675530240/R4id22hu_normal.jpg\",\"description\":\"IIS\\nIndian Information Service \\nBureau of Outreach and Communication \\n(DAVP)\\nMinistry of I&B\\nFr IIFO/ PRO ICMR \\nMinistry of Health and Family Welfare\\nNew Delhi\",\"location\":\"Neu-Delhi, Republik Indien\",\"entities\":{\"url\":{\"urls\":[{\"start\":0,\"end\":23,\"url\":\"https://t.co/Zyf8RHx0rW\",\"expanded_url\":\"http://davp.nic.in\",\"display_url\":\"davp.nic.in\"}]}},\"verified\":false,\"username\":\"adil_iis\",\"created_at\":\"2019-07-02T06:07:19.000Z\",\"name\":\"Syed Adil Shamim Andrabi\",\"public_metrics\":{\"followers_count\":223,\"following_count\":142,\"tweet_count\":1289,\"listed_count\":2},\"id\":\"1145936974064177152\",\"protected\":false,\"url\":\"https://t.co/Zyf8RHx0rW\"}],\"tweets\":[{\"lang\":\"en\",\"public_metrics\":{\"retweet_count\":6,\"reply_count\":0,\"like_count\":13,\"quote_count\":3},\"author_id\":\"17880989\",\"entities\":{\"annotations\":[{\"start\":84,\"end\":90,\"probability\":0.3373,\"type\":\"Organization\",\"normalized_text\":\"YouTube\"}],\"hashtags\":[{\"start\":69,\"end\":75,\"tag\":\"AltAC\"},{\"start\":211,\"end\":223,\"tag\":\"CommTwitter\"},{\"start\":224,\"end\":231,\"tag\":\"career\"},{\"start\":232,\"end\":240,\"tag\":\"phdchat\"}],\"urls\":[{\"start\":187,\"end\":210,\"url\":\"https://t.co/vO3Sy7smj4\",\"expanded_url\":\"https://bit.ly/2Q5qMuV\",\"display_url\":\"bit.ly/2Q5qMuV\",\"images\":[{\"url\":\"https://pbs.twimg.com/news_img/1391807750804582404/VX9Wp9Sn?format=jpg&name=orig\",\"width\":1280,\"height\":720},{\"url\":\"https://pbs.twimg.com/news_img/1391807750804582404/VX9Wp9Sn?format=jpg&name=150x150\",\"width\":150,\"height\":150}],\"status\":200,\"title\":\"#AltAC in COM: Day 1: From Academy to Industry presented by Dr. Christine E. Kiesinger\",\"description\":\"#AltAC in COM: Exploring Career Options Beyond the Academy for Communication PhDsWorkshop Title: Leaving Home: Finding Place, Purpose and Possibilities Outsi...\",\"unwound_url\":\"https://www.youtube.com/watch?v=5yHsZcys44E&list=PLaETCkw7w_Q8qHrMC8fE--PU2VLSPpAWf\"}]},\"text\":\"Are you interested in a career outside of academia? Check out NCA’s “#AltAC in COM” YouTube playlist, which includes videos from all three sessions of the recent virtual workshop series! https://t.co/vO3Sy7smj4 #CommTwitter #career #phdchat\",\"source\":\"Twitter for Advertisers\",\"id\":\"1391807748346683397\",\"conversation_id\":\"1391807748346683397\",\"possibly_sensitive\":false,\"created_at\":\"2021-05-10T17:30:00.000Z\",\"context_annotations\":[{\"domain\":{\"id\":\"65\",\"name\":\"Interests and Hobbies Vertical\",\"description\":\"Top level interests and hobbies groupings, like Food or Travel\"},\"entity\":{\"id\":\"847544972781826048\",\"name\":\"Careers\",\"description\":\"Careers\"}},{\"domain\":{\"id\":\"46\",\"name\":\"Brand Category\",\"description\":\"Categories within Brand Verticals that narrow down the scope of Brands\"},\"entity\":{\"id\":\"781974596752842752\",\"name\":\"Services\"}},{\"domain\":{\"id\":\"47\",\"name\":\"Brand\",\"description\":\"Brands and Companies\"},\"entity\":{\"id\":\"10029382357\",\"name\":\"YouTube\"}},{\"domain\":{\"id\":\"65\",\"name\":\"Interests and Hobbies Vertical\",\"description\":\"Top level interests and hobbies groupings, like Food or Travel\"},\"entity\":{\"id\":\"847546154439000064\",\"name\":\"Education\",\"description\":\"Education through the lens of those passionate about resources and news. This topic excludes Education policy and reform.\"}},{\"domain\":{\"id\":\"66\",\"name\":\"Interests and Hobbies Category\",\"description\":\"A grouping of interests and hobbies entities, like Novelty Food or Destinations\"},\"entity\":{\"id\":\"852247095461371904\",\"name\":\"Graduate school\",\"description\":\"Graduate school\"}}]},{\"lang\":\"en\",\"public_metrics\":{\"retweet_count\":2,\"reply_count\":0,\"like_count\":5,\"quote_count\":0},\"author_id\":\"17880989\",\"entities\":{\"annotations\":[{\"start\":0,\"end\":6,\"probability\":0.5457,\"type\":\"Organization\",\"normalized_text\":\"Comm Ph\"},{\"start\":52,\"end\":54,\"probability\":0.6734,\"type\":\"Organization\",\"normalized_text\":\"NCA\"}],\"hashtags\":[{\"start\":266,\"end\":278,\"tag\":\"CommTwitter\"}],\"urls\":[{\"start\":242,\"end\":265,\"url\":\"https://t.co/stoYToSHfz\",\"expanded_url\":\"http://Natcom.org/DHS\",\"display_url\":\"Natcom.org/DHS\",\"images\":[{\"url\":\"https://pbs.twimg.com/news_img/1399780289744760838/MfDN54Vm?format=png&name=orig\",\"width\":400,\"height\":400},{\"url\":\"https://pbs.twimg.com/news_img/1399780289744760838/MfDN54Vm?format=png&name=150x150\",\"width\":150,\"height\":150}],\"status\":200,\"title\":\"NCA Doctoral Honors Seminar\",\"description\":\"The NCA Doctoral Honors Seminars (DHS) bring together promising doctoral students and distinguished faculty members from across the discipline and around the nation to discuss current topics in Communication.\",\"unwound_url\":\"https://www.natcom.org/convention-events/nca-sponsored-events/nca-doctoral-honors-seminar\"}]},\"text\":\"Comm Ph.D. students: It’s not too late to apply for NCA’s virtual Doctoral Honors Seminar! The seminar offers students a special opportunity to work with distinguished Communication faculty members. Application deadline: June 4. Apply today! https://t.co/stoYToSHfz #CommTwitter\",\"source\":\"Twitter for Advertisers\",\"id\":\"1399780280710221834\",\"conversation_id\":\"1399780280710221834\",\"possibly_sensitive\":false,\"created_at\":\"2021-06-01T17:30:00.000Z\"}]},\"meta\":{\"newest_id\":\"1400199391961620488\",\"oldest_id\":\"1399765322509557763\",\"result_count\":5}}"), + date = structure(1623241813, class = c("POSIXct", "POSIXt" + ), tzone = "GMT"), times = c(redirect = 0, namelookup = 2.9e-05, + connect = 3e-05, pretransfer = 0.000115, starttransfer = 0.410948, + total = 0.411086)), class = "response") diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R new file mode 100644 index 0000000..07b642c --- /dev/null +++ b/tests/testthat/setup.R @@ -0,0 +1 @@ +library(httptest) diff --git a/tests/testthat/test-get_all_tweets.R b/tests/testthat/test-get_all_tweets.R new file mode 100644 index 0000000..36669e7 --- /dev/null +++ b/tests/testthat/test-get_all_tweets.R @@ -0,0 +1,87 @@ +## NOTE: This test will only test various params of get_all_tweets, also the behaviours before and after the httr::GET (wrapped in fetch_data, which in turn wrapped in get_tweet) + +## Also, we cannot test params: query, page_n, start_time, end_time with mock_api because these params are related to the GET request. + +## Github secret has not been set yet! +Sys.setenv("TWITTER_BEARER" = "ABC") + +test_that("defensive programming", { + expect_error(capture_warnings(get_all_tweets(query = "#commtwitter", end_tweets = "2021-06-05T00:00:00Z"))) + expect_error(capture_warnings(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-05T00:00:00Z"))) +}) + + +with_mock_api({ + test_that("params: default", { + emptydir <- academictwitteR:::.gen_random_dir() + ## "Normal" usage; at least the default + expect_error(w0 <- capture_warnings(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", )), NA) + unlink(emptydir) + }) + test_that("params: data_path", { + emptydir <- academictwitteR:::.gen_random_dir() + expect_error(w1 <- capture_warnings(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, data_path = emptydir)), NA) + expect_match(w1, "Tweets will be bound", all = FALSE) + expect_true(length(list.files(emptydir)) != 0) ### side effect is there + ## warning message when the data_path is not empty + w2 <- capture_warnings(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, data_path = emptydir)) + expect_match(w2, "Tweets will be bound", all = FALSE) + expect_match(w2, "Directory already exists", all = FALSE) + unlink(emptydir) + }) + test_that("param: bind_tweets", { + emptydir <- academictwitteR:::.gen_random_dir() + + ## bind_tweets is FALSE and data_path is NULL: error + expect_error(capture_warnings({ + get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, bind_tweets = FALSE) + })) + ## "NORMAL" USAGE + expect_error(w3 <- capture_warnings({ + z1 <- get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, data_path = emptydir, bind_tweets = FALSE) + }), NA) + expect_equal(z1, NULL) ## nothing is returned; only side effect + expect_true(identical(w3, character(0))) ## no warning + unlink(emptydir) + ## No data_path; no side effect + emptydir <- academictwitteR:::.gen_random_dir() + expect_error({ + w4 <- capture_warnings(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, bind_tweets = TRUE)) + }, NA) + expect_true(length(list.files(emptydir)) == 0) ## No side effect + expect_false(identical(w4, character(0))) ## tons of warnings + unlink(emptydir) + }) + test_that("param: verbose", { + ## verbose should also be tested; but there is still message in all cases + + ## emptydir <- academictwitteR:::.gen_random_dir() + ## expect_silent(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = FALSE, data_path = emptydir, bind_tweets = FALSE)) + ## unlink(emptydir) + ## test for output + emptydir <- academictwitteR:::.gen_random_dir() + expect_output(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, data_path = emptydir, bind_tweets = FALSE)) + unlink(emptydir) + }) + ## Until issue #144 is resolved + ## test_that("param: file", { + ## emptydir <- academictwitteR:::.gen_random_dir() + ## temp_RDS <- "aaa.RDS" + ## expect_error(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, data_path = emptydir, bind_tweets = FALSE, file = temp_RDS), NA) + ## expect_true(file.exists(temp_RDS)) + ## x <- readRDS(temp_RDS) + ## expect_true("data.frame" %in% class(x)) + ## }) + + test_that("param: export_query", { + emptydir <- academictwitteR:::.gen_random_dir() + capture_warnings(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, data_path = emptydir, bind_tweets = FALSE, export_query = TRUE)) + expect_true(file.exists(paste0(emptydir, "/query"))) + unlink(emptydir) + + emptydir <- academictwitteR:::.gen_random_dir() + capture_warnings(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, data_path = emptydir, bind_tweets = FALSE, export_query = FALSE)) + expect_false(file.exists(paste0(emptydir, "/query"))) + unlink(emptydir) + }) +}) diff --git a/tests/testthat/test-set_bearer.R b/tests/testthat/test-set_bearer.R index b31f695..5ef9f50 100644 --- a/tests/testthat/test-set_bearer.R +++ b/tests/testthat/test-set_bearer.R @@ -7,3 +7,16 @@ test_that("get_bearer", { expect_equal(get_bearer(), "ABC") Sys.setenv("TWITTER_BEARER" = ORI_BEARER) }) + +with_mock_api({ + test_that("integration with get_all_tweets", { + emptydir <- academictwitteR:::.gen_random_dir() + ORI_BEARER <- Sys.getenv("TWITTER_BEARER") + Sys.setenv("TWITTER_BEARER" = "") + expect_error(w1 <- capture_warnings(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, data_path = emptydir))) + Sys.setenv("TWITTER_BEARER" = ORI_BEARER) + expect_error(w1 <- capture_warnings(get_all_tweets(query = "#commtwitter", start_tweets = "2021-06-01T00:00:00Z", end_tweets = "2021-06-05T00:00:00Z", verbose = TRUE, data_path = emptydir)), NA) + unlink(emptydir) + Sys.setenv("TWITTER_BEARER" = ORI_BEARER) + }) +})