tests/testthat/test-source-nytimes.R

context("NYTimesSource")

data(nytimes_appid)

test_that("NYTimesSource",{
	
	lengthcorp <- 200
	
	if(!exists(as.character(substitute(nytimes_appid)))){
		cat("No Variable nytimes_appid provided. Skipping Test...\n")
		return()
	}
	
	testcorp <- WebCorpus(NYTimesSource("Microsoft", appid = nytimes_appid, n = lengthcorp))
	# Check Corpus object
	expect_that(length(testcorp), equals(lengthcorp))
	expect_that(class(testcorp), equals(c("WebCorpus","VCorpus","Corpus")))
	
	# Check Content
	#expect_that(all(sapply(testcorp, nchar) > 0), is_true())
	contentlength <- sapply(testcorp, function(x) 
				if( length(content(x)) < 1) 0 else nchar(content(x)))	
	contentratio <- length(which(contentlength > 0)) / length(testcorp)
	expect_that(contentratio > 0.5, is_true())
	
	# Check Meta Data
	datetimestamp <- lapply(testcorp, function(x) meta(x, "datetimestamp"))
	expect_that(all(sapply(datetimestamp, function(x) class(x)[1] == "POSIXlt")), is_true())
	
	description <- lapply(testcorp, function(x) meta(x, "description"))
	expect_that(all(sapply(description, function(x) class(x)[1] == "character")), is_true())
	expect_that(all(sapply(description, nchar) > 0), is_true())
	
	heading <- lapply(testcorp, function(x) meta(x, "heading"))
	expect_that(all(sapply(heading, function(x) class(x)[1] == "character")), is_true())
	expect_that(all(sapply(heading, nchar) > 0), is_true())
	
	id <- lapply(testcorp, function(x) meta(x, "id"))
	expect_that(all(sapply(id, function(x) class(x)[1] == "character")), is_true())
	expect_that(all(sapply(id, nchar) > 0), is_true())
	
	language <- lapply(testcorp, function(x) meta(x, "language"))
	expect_that(all(sapply(language, function(x) class(x)[1] == "character")), is_true())
	expect_that(all(sapply(language, nchar) > 0), is_true())
	
	origin <- lapply(testcorp, function(x) meta(x, "origin"))
	expect_that(all(sapply(origin, function(x) class(x)[1] == "character")), is_true())
	expect_that(all(sapply(origin, nchar) > 0), is_true())
	
	testcorp <- testcorp[1:10]
	testcorp <- corpus.update(testcorp)
	expect_that(length(testcorp) >= lengthcorp, is_true())
	
	cat(" | Contentratio: ", sprintf("%.0f%%", contentratio * 100))
})

Try the tm.plugin.webmining package in your browser

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

tm.plugin.webmining documentation built on May 2, 2019, 1:10 p.m.