context("Get a summary statistics table")

test_that("Getting a summary table is identical to computing and exporting a summary table", {
		
	data <- data.frame(
		PARAM = c("a", "b", "a", "b"),
		TRT = c("A", "A", "B", "B"),
		AVAL = c(1, 2, 4, 5),
		USUBJID = seq.int(4)
	)		
	
	summaryTableOutput <- getSummaryStatisticsTable(
		data = data,	
		rowVar = "PARAM", colVar = "TRT",
		var = "AVAL"
	)
	
	summaryTable <- computeSummaryStatisticsTable(
		data = data,	
		rowVar = "PARAM", 
		colVar = "TRT",
		var = "AVAL"
	)
	summaryTableOutputSteps <- export(summaryTable)
			
	expect_identical(summaryTableOutput, summaryTableOutputSteps)
			
})

test_that("No output and a message is generated if the data is empty", {

	expect_message(
		res <- getSummaryStatisticsTable(data = data.frame())
	)
	expect_null(res)
	
})

test_that("A variable with subgroups and a flag variable are correctly combined when multiple statistics are set", {
			
	data <- data.frame(
		TRTEMFL = c("Y", "Y", "N", "N"),
		WORST = factor(
			c("Mild", "Moderate", "Mild", "Mild"),
			levels = c("Mild", "Moderate"),
		),
		TRT = c("A", "A", "A", "A"),
		USUBJID = c("1", "2", "3", "4"),
		stringsAsFactors = FALSE
	)
	ft <- getSummaryStatisticsTable(
		data = data,
		var = c("TRTEMFL", "WORST"),
		varFlag = "TRTEMFL",
		colVar = "TRT",
		stats = getStats(c("n", "%"))
	)
			
	dataRef <- 	data.frame(
		col1 = c(
			"TRTEMFL", "n", "%",
			"WORST", 
			"Mild", "n", "%",
			"Moderate", "n", "%"
		),
		col2 = c(
			NA_character_, "2", "50.0", 
			NA_character_, 
			NA_character_, "3", "75.0", 
			NA_character_, "1", "25.0"
		),
		stringsAsFactors = FALSE
	)
			
	expect_equal(
		object = ft$body$data, 
		expected = dataRef, 
		# rownames might differ
		check.attributes = FALSE
	)
			
})

test_that("The width of the columns is correctly set", {
			
	data <- data.frame(
		TRT = c("A", "B"),
		USUBJID = c("1", "2"),
		stringsAsFactors = FALSE
	)
	columnsWidth <- c(0.5, 2, 8)
	ft <- getSummaryStatisticsTable(data = data, colVar = "TRT", columnsWidth = columnsWidth)
			
	expect_equal(object = ft$body$colwidths, expected = columnsWidth)
			
})

test_that("The width of the columns specified as percentage is correctly set", {
  
  data <- data.frame(
    TRT = c("A", "B"),
    USUBJID = c("1", "2"),
    stringsAsFactors = FALSE
  )
  columnsWidth <- c(0.8, 0.1, 0.1)
  ft <- getSummaryStatisticsTable(data = data, colVar = "TRT", columnsWidth = columnsWidth)
  
  # widths are converted to inches for flextable
  expect_equal(
    object = ft$body$colwidths, 
    expected = columnsWidth * getDimPage(type = "width")
  )
  
})

test_that("The width of the columns specified for a subset of the columns is correctly set", {
  
  data <- data.frame(
    TRT = c("A", "B"),
    USUBJID = c("1", "2"),
    stringsAsFactors = FALSE
  )
  columnsWidth <- 2
  ft <- getSummaryStatisticsTable(data = data, colVar = "TRT", columnsWidth = columnsWidth)
  
  # width is set as specified for the first column
  expect_equal(object = ft$body$colwidths[1], expected = columnsWidth)
  # total width = page width
  expect_equal(object = sum(ft$body$colwidths), expected = getDimPage(type = "width"))
  
})

test_that("A warning is generated if the width of the columns is not correctly set", {
			
	data <- data.frame(
		TRT = c("A", "B"),
		USUBJID = c("1", "2"),
		stringsAsFactors = FALSE
	)
	columnsWidth <- c(2, 8, 4, 5)
	expect_warning(
		object = getSummaryStatisticsTable(
			data = data, 
			colVar = "TRT", 
			columnsWidth = columnsWidth
		)
	)

})
			
