Commit f55fdeda authored by Sander Bollen's avatar Sander Bollen
Browse files

Merge branch 'feature-core_testing' into 'develop'

Added unit tests

see also #51 

See merge request !278
parents b5500137 1c21dd07
......@@ -43,10 +43,12 @@ trait CommandLineResources extends CommandLineFunction with Configurable {
* @return number of threads
*/
private def getThreads(default: Int): Int = {
val maxThreads: Int = config("maxthreads", default = 24)
val maxThreads: Option[Int] = config("maxthreads")
val threads: Int = config("threads", default = default)
if (maxThreads > threads) threads
else maxThreads
maxThreads match {
case Some(max) => if (max > threads) threads else max
case _ => threads
}
}
def setResources(): Unit = {
......@@ -79,7 +81,7 @@ trait CommandLineResources extends CommandLineFunction with Configurable {
if (vmem.isDefined) jobResourceRequests = jobResourceRequests.filterNot(_.contains("h_vmem="))
if (retry > 0) logger.info("Auto raise memory on retry")
retry += 1
this.freeze()
this.freezeFieldValues()
}
var threadsCorrection = 0
......
......@@ -31,9 +31,11 @@ trait PipelineCommand extends MainCommand with GatkLogging with ImplicitConversi
* Gets location of compiled class of pipeline
* @return path from classPath to class file
*/
def pipeline = "/" + getClass.getName.stripSuffix("$").replaceAll("\\.", "/") + ".class"
def pipeline = "/" + getClass.getName.takeWhile(_ != '$').replaceAll("\\.", "/") + ".class"
def pipelineName = getClass.getSimpleName.takeWhile(_ != '$').toLowerCase
def pipelineName = getClass.getName.takeWhile(_ != '$').split("\\.").last.toLowerCase
protected val globalConfig = Config.global
/** Class can be used directly from java with -cp option */
def main(args: Array[String]): Unit = {
......@@ -41,7 +43,7 @@ trait PipelineCommand extends MainCommand with GatkLogging with ImplicitConversi
for (t <- 0 until argsSize) {
if (args(t) == "-config" || args(t) == "--config_file") {
if (args.length <= (t + 1)) throw new IllegalStateException("-config needs a value: <file>")
Config.global.loadConfigFile(new File(args(t + 1)))
globalConfig.loadConfigFile(new File(args(t + 1)))
}
if (args(t) == "-cv" || args(t) == "--config_value") {
......@@ -52,7 +54,7 @@ trait PipelineCommand extends MainCommand with GatkLogging with ImplicitConversi
val p = v(0).split(":")
val key = p.last
val path = p.dropRight(1).toList
Config.global.addValue(key, value, path)
globalConfig.addValue(key, value, path)
}
if (args(t) == "--logging_level" || args(t) == "-l") {
......@@ -74,8 +76,8 @@ trait PipelineCommand extends MainCommand with GatkLogging with ImplicitConversi
val logFile = {
val pipelineName = this.getClass.getSimpleName.toLowerCase.split("""\$""").head
val pipelineConfig = Config.global.map.getOrElse(pipelineName, Map()).asInstanceOf[Map[String, Any]]
val pipelineOutputDir = new File(Config.global.map.getOrElse("output_dir", pipelineConfig.getOrElse("output_dir", "./")).toString)
val pipelineConfig = globalConfig.map.getOrElse(pipelineName, Map()).asInstanceOf[Map[String, Any]]
val pipelineOutputDir = new File(globalConfig.map.getOrElse("output_dir", pipelineConfig.getOrElse("output_dir", "./")).toString)
val logDir: File = new File(pipelineOutputDir, ".log")
logDir.mkdirs()
new File(logDir, "biopet." + BiopetQCommandLine.timestamp + ".log")
......@@ -91,7 +93,7 @@ trait PipelineCommand extends MainCommand with GatkLogging with ImplicitConversi
argv ++= List("--log_to_file", new File(logFile.getParentFile, "queue." + BiopetQCommandLine.timestamp + ".log").getAbsolutePath)
}
if (!args.contains("-retry") && !args.contains("--retry_failed")) {
val retry: Int = Config.global(pipelineName, Nil, "retry", default = 5)
val retry: Int = globalConfig(pipelineName, Nil, "retry", default = 5)
logger.info("No retry flag found, ")
argv ++= List("-retry", retry.toString)
}
......
......@@ -17,9 +17,4 @@ trait ToolCommandFunction extends BiopetJavaCommandLineFunction with Version {
javaMainClass = toolObject.getClass.getName.takeWhile(_ != '$')
super.beforeGraph()
}
override def freezeFieldValues(): Unit = {
javaMainClass = toolObject.getClass.getName.takeWhile(_ != '$')
super.freezeFieldValues()
}
}
package nl.lumc.sasc.biopet.core
import nl.lumc.sasc.biopet.utils.config.{ Config, Configurable }
import org.broadinstitute.gatk.queue.function.CommandLineFunction
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
import scala.language.reflectiveCalls
/**
* Created by pjvanthof on 17/11/15.
*/
class CommandLineResourcesTest extends TestNGSuite with Matchers {
class CommandLineFunctionMock(c: Map[String, Any] = Map()) extends CommandLineFunction with Configurable {
override def freezeFieldValues() {}
def commandLine = "command"
val root = null
override def globalConfig = new Config(c)
}
@Test
def testDefaults(): Unit = {
val cmd = new CommandLineFunctionMock with CommandLineResources
cmd.coreMemory shouldBe 2.0
cmd.residentFactor shouldBe 1.2
cmd.vmemFactor shouldBe 1.4
cmd.retry shouldBe 0
cmd.threads shouldBe 1
cmd.setResources()
cmd.memoryLimit shouldBe Some(cmd.coreMemory * cmd.threads)
cmd.residentLimit shouldBe Some(cmd.coreMemory * cmd.residentFactor)
cmd.vmem shouldBe Some((cmd.coreMemory * cmd.vmemFactor) + "G")
cmd.jobResourceRequests shouldBe empty
cmd.freezeFieldValues()
cmd.jobResourceRequests should contain("h_vmem=" + cmd.vmem.get)
cmd.setupRetry()
cmd.retry shouldBe 1
cmd.setupRetry()
cmd.retry shouldBe 2
cmd.setupRetry()
cmd.retry shouldBe 3
}
@Test
def testMaxThreads(): Unit = {
val cmd = new CommandLineFunctionMock(Map("maxthreads" -> 5, "threads" -> 10)) with CommandLineResources
cmd.threads shouldBe 5
}
@Test
def testCombine(): Unit = {
val cmd1 = new CommandLineFunctionMock with CommandLineResources
val cmd2 = new CommandLineFunctionMock with CommandLineResources
val mainCmd = new CommandLineFunctionMock with CommandLineResources {
def combine(functions: List[CommandLineResources]) = combineResources(functions)
}
mainCmd.combine(List(cmd1, cmd2))
mainCmd.coreMemory shouldBe 2.0
mainCmd.residentFactor shouldBe 1.2
mainCmd.vmemFactor shouldBe 1.4
mainCmd.memoryLimit shouldBe Some(4.0)
mainCmd.retry shouldBe 0
mainCmd.threads shouldBe 2
}
}
package nl.lumc.sasc.biopet.core
import nl.lumc.sasc.biopet.utils.config.Config
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
/**
* Created by pjvanthof on 17/11/15.
*/
class PipelineCommandTest extends TestNGSuite with Matchers {
@Test
def testPipelineCommand: Unit = {
val pipeline = new PipelineCommand {
override val globalConfig = new Config(Map())
def getConfig = globalConfig
}
pipeline.pipelineName shouldBe this.getClass.getSimpleName.toLowerCase
pipeline.pipeline shouldBe s"/${this.getClass.getName.stripSuffix("$").replaceAll("\\.", "/")}.class"
// Config should be emty if the main method is not yet touched
pipeline.getConfig.map shouldBe empty
//TODO: Main method testing
}
}
package nl.lumc.sasc.biopet.core
import nl.lumc.sasc.biopet.utils.config.{ Config, Configurable }
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
/**
* Created by pjvanthof on 17/11/15.
*/
class SampleLibraryTagTest extends TestNGSuite with Matchers {
@Test
def testDefault: Unit = {
val o = new SampleLibraryTag {
override def root: Configurable = null
override def globalConfig = new Config(Map())
}
o.sampleId shouldBe None
o.libId shouldBe None
}
@Test
def testInherit: Unit = {
val o1 = new SampleLibraryTag {
override def root: Configurable = null
override def globalConfig = new Config(Map())
}
o1.sampleId = Some("sampleName")
o1.libId = Some("libName")
o1.sampleId shouldBe Some("sampleName")
o1.libId shouldBe Some("libName")
val o2 = new SampleLibraryTag {
override def root: Configurable = o1
override def globalConfig = new Config(Map())
}
o2.sampleId shouldBe o1.sampleId
o2.libId shouldBe o1.libId
}
}
package nl.lumc.sasc.biopet.core
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
import nl.lumc.sasc.biopet.FullVersion
/**
* Created by pjvanthof on 16/11/15.
*/
class ToolCommandTest extends TestNGSuite with Matchers {
@Test
def testToolCommand: Unit = {
val tool = new ToolCommandFunction {
val root = null
val toolObject = ToolCommandTest
}
tool.versionCommand shouldBe empty
tool.versionRegex.toString() shouldBe empty
tool.getVersion shouldBe Some("Biopet " + FullVersion)
tool.beforeGraph
tool.javaMainClass shouldBe ToolCommandTest.getClass.getName.takeWhile(_ != '$')
}
}
object ToolCommandTest {
}
\ No newline at end of file
......@@ -20,7 +20,7 @@ import nl.lumc.sasc.biopet.utils.ConfigUtils.ImplicitConversions
trait Configurable extends ImplicitConversions {
/** Should be object of parant object */
val root: Configurable
def root: Configurable
def globalConfig: Config = if (root != null) root.globalConfig else Config.global
/** suffix to the path */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment