tests/testthat/test-pdtfToVertices.R

context("Function pdtfToVertices")
library(data.table)

test_that("ptdfToVertices works", {

  PTDF <- fread(system.file("testdata/PTDF6h.csv",package = "flowBasedClustering"))

  out1 <- ptdfToVertices(PTDF = system.file("testdata/PTDF6h.csv",package = "flowBasedClustering"),  nbCore = 1)
  
  
  expect_warning(ptdfToVertices(PTDF = system.file("testdata/PTDF6h.csv",package = "flowBasedClustering"),  nbCore = 1, maxDomainSize = 500))
  
  
  # output file should not be empty
  expect_false(nrow(out1) == 0)
  
  # Note : This test has been removed as it seems multithreading does not work with Rcmd check
  out2 <- ptdfToVertices(PTDF = system.file("testdata/PTDF6h.csv",package = "flowBasedClustering"),  nbCore = 2)
  # check that output are similare for nbCore = 1 and 2
  expect_true(all.equal(out1, out2))
  
  # check that output are equal to expected values 
  # (the comparison is based on a file generated by this very same function the 08.08.2017)
  out3 <-fread(system.file("testdata/vertices6h.csv",package = "flowBasedClustering"))
  expect_true(all.equal(out1, out3))
  
  PTDF$Date <- as.Date(PTDF$Date, "%d/%m/%Y")
  # check that the vertices are contained into the domains defined by the PTDF
  # AND
  # check that the vertices are at least on three planes defined by the PTDF 
  tol <- 1 #tolerance on bounds
  is_it_all_good <- sapply(1:nrow(out1), function(i){
      
      # to speed-up calculation, do not check all i :
      if(i %% 20 == 0){
      
        date <- out1[i, "Date"]
        date <- as.Date(date$Date)
        period <- as.numeric(out1[i, "Period"])
      
       in_domain <- sapply(1:nrow(PTDF[Date == date & Period == period,]), function(j){
        
          out1[i, BE] * PTDF[Date == date & Period == period,][j,BE] +
          out1[i, DE] *PTDF[Date == date & Period == period,][j,DE] +
          out1[i, FR] *PTDF[Date == date & Period == period,][j,FR] +
          (-sum(out1[i,c(BE, DE,FR)])) * PTDF[Date == date & Period == period,][j,NL] <= 
          PTDF[Date == date & Period == period,][j,RAM] + tol
        })
  
        expect_true(all(in_domain))
      
        on_plane <- sapply(1:nrow(PTDF[Date == date & Period == period,]), function(j){
        
            out1[i, BE] * PTDF[Date == date & Period == period,][j,BE] +
            out1[i, DE] *PTDF[Date == date & Period == period,][j,DE] +
            out1[i, FR] *PTDF[Date == date & Period == period,][j,FR] +
            (-sum(out1[i,c(BE, DE,FR)])) * PTDF[Date == date & Period == period,][j,NL] >= 
            PTDF[Date == date & Period == period,][j,RAM] - tol
        })
      
        expect_true(length(on_plane[on_plane]) >= 3)
      }
    })

})

test_that("ptdfToVertices returns error if file does not exist", {
  expect_error(ptdfToVertices("NotAFile"))
})

test_that("bad pdtf file", {
  expect_error(ptdfToVertices(system.file("testdata/testBadFile.csv",package = "flowBasedClustering")))
})


test_that("ptdfToVertices returns error if header is not correct", {
  expect_error(
    ptdfToVertices(PTDF = system.file("testdata/PTDF.csv",package = "flowBasedClustering"),  nbCore = 1))
  })
  
  
rte-antares-rpackage/flowBasedClustering documentation built on Nov. 21, 2020, 11:21 a.m.