def test_create_event_request_with_missing_contiguity_metrics():
    app = app_helper.setup_app_state('quast', 'missing_g75_values')
    event = post.create_event_request(app, post.list_outputs(app))
    nose.assert_equal(event["success"], True)
    nose.assert_in("nga50", event["metrics"])
    nose.assert_equal(event["metrics"]["nga50"], 17378.0)
    nose.assert_equal(event["metrics"]["nga75"], 0.0)
def test_assembly_benchmark_unsuccessful_event():
    app = app_helper.setup_app_state('quast', 'task')  # No tmp/biobox.yaml
    event = post.create_event_request(app, post.list_outputs(app))
    nose.assert_equal(event, {
        "task": 6,
        "success": False,
        "files": [],
        "metrics": {}
    })
def test_short_read_assembler_successful_event_with_incomplete_cgroup_data():
    app  = app_helper.setup_app_state('sra', 'incomplete_cgroup')
    outputs = [{
        "type"     : "contig_fasta",
        "location" : "/local/path",
        "sha256"   : "digest_1",
        "url"      : "s3://url/dir/file"}]
    event = post.create_event_request(app, outputs)
    nose.assert_equal(event['metrics']['total_rss_in_mibibytes'], 0.0)
def test_create_event_request_with_a_successful_quast_event():
    app = app_helper.setup_app_state('quast', 'outputs')
    event = post.create_event_request(app, post.list_outputs(app))
    nose.assert_equal(event["task"], 6)
    nose.assert_equal(event["success"], True)
    nose.assert_equal(event["files"][0]["type"], "assembly_metrics")
    nose.assert_in("total_aligned_length", event["metrics"])
    nose.assert_in("lga75", event["metrics"])
    nose.assert_equal(event["metrics"]["lga75"], 70.0)
    nose.assert_equal(event["metrics"]["total_aligned_length"], 679979.0)
def test_create_event_request_with_non_numeric_quast_values():
    app = app_helper.setup_app_state('quast', 'outputs')

    import fileinput
    for line in fileinput.input(app['path'] +
                                '/outputs/assembly_metrics/67ba437ffa',
                                inplace=True):
        if 'NGA50' in line:
            print line.replace("6456", "-"),
        else:
            print line.replace("\n", "")

    event = post.create_event_request(app, post.list_outputs(app))
    nose.assert_equal(event["success"], True)
    nose.assert_in("nga50", event["metrics"])
    nose.assert_equal(event["metrics"]["nga50"], 0.0)
def test_create_event_request_with_a_successful_gaet_event():
    app = app_helper.setup_app_state('gaet', 'outputs')
    event = post.create_event_request(app, post.list_outputs(app))
    nose.assert_equal(event["task"], 6)
    nose.assert_equal(event["success"], True)
    nose.assert_equal(event["files"][0]["type"], "assembly_metrics")

    nose.assert_in("comparison.gene_type_distance.cds.n_symmetric_difference",
                   event["metrics"])
    nose.assert_equal(
        event["metrics"]
        ["comparison.gene_type_distance.cds.n_symmetric_difference"], 7.0)

    nose.assert_in("assembly.minimum_gene_set.single_copy", event["metrics"])
    nose.assert_equal(
        event["metrics"]["assembly.minimum_gene_set.single_copy"], 0.0)
def test_short_read_assembler_successful_event_without_cgroup_data():
    """
    It is possible that an assembler could finish before the first set of cgroup
    metrics are collected. In this case, we would not want the task to be considered
    failed as long as contig files have been produced.
    """
    app  = app_helper.setup_app_state('sra', 'missing_cgroup')
    outputs = [{
        "type"     : "contig_fasta",
        "location" : "/local/path",
        "sha256"   : "digest_1",
        "url"      : "s3://url/dir/file"}]
    event = post.create_event_request(app, outputs)
    nose.assert_equal({
        "task" : 4,
        "success" : True,
        "metrics" : {},
        "files" : [
            {"url"    : "s3://url/dir/file",
             "sha256" : "digest_1",
             "type"   : "contig_fasta"}]}, event)
def test_short_read_assembler_successful_event_with_cgroup_data():
    app  = app_helper.setup_app_state('sra', 'outputs')
    outputs = [{
        "type"     : "contig_fasta",
        "location" : "/local/path",
        "sha256"   : "digest_1",
        "url"      : "s3://url/dir/file"}]
    event = post.create_event_request(app, outputs)
    nose.assert_equal({
        "task" : 4,
        "success" : True,
        "metrics" : {
            "total_cpu_usage_in_seconds"               : 53.546,
            "total_cpu_usage_in_seconds_in_kernelmode" : 1.75,
            "total_cpu_usage_in_seconds_in_usermode"   : 11.11,
            "total_memory_usage_in_mibibytes"          : 175.348,
            "total_rss_in_mibibytes"                   : 80.543,
            "total_read_io_in_mibibytes"               : 38.641,
            "total_write_io_in_mibibytes"              : 0.0,
            "total_wall_clock_time_in_seconds"         : 0.0},
        "files" : [
            {"url"    : "s3://url/dir/file",
             "sha256" : "digest_1",
             "type"   : "contig_fasta"}]}, event)
def test_create_event_request_with_missing_alignment_values():
    app = app_helper.setup_app_state('quast', 'missing_alignment')
    event = post.create_event_request(app, post.list_outputs(app))
    nose.assert_equal(event["success"], False)
    nose.assert_equal(event["metrics"], {})
def test_short_read_assembler_unsuccessful_event():
    app  = app_helper.setup_app_state('sra', 'task')
    outputs = []
    event = post.create_event_request(app, outputs)
    nose.assert_equal(event, {"task" : 4, "success" : False, "files" : [], "metrics" : {}})