# -*- coding: utf-8 -*-
"""
    vep subcommand tests
    ~~~~~~~~~~~~~~~~~~~~

"""
# (c) 2015-2020 Wibowo Arindrarto <bow@bow.web.id>

import json

import pytest
from click.testing import CliRunner

from crimson.cli import main
from .utils import get_test_path, getattr_nested


@pytest.fixture(scope="module")
def vep_fail():
    runner = CliRunner()
    in_file = get_test_path("vep_nope.txt")
    result = runner.invoke(main, ["vep", in_file])
    return result


@pytest.fixture(scope="module")
def vep_v77_01():
    runner = CliRunner()
    in_file = get_test_path("vep_v77_01.txt")
    result = runner.invoke(main, ["vep", in_file])
    result.json = json.loads(result.output)
    return result


def test_vep_fail_exit_code(vep_fail):
    assert vep_fail.exit_code != 0


def test_vep_fail_output(vep_fail):
    err_msg = "Unexpected file structure. No contents parsed."
    assert err_msg in vep_fail.output


@pytest.mark.parametrize("attrs, exp", [
    (["VEP run statistics", "Cache/Database"],
     "/home/user/.vep/homo_sapiens/77_GRCh38"),
    (["Variant classes", "insertion"], 35),
    (["Variant classes", "deletion"], 18),
    (["Variant classes", "SNV"], 448),
    (["Variants by chromosome", "1"], 42),
    (["Variants by chromosome", "21"], 45),
    (["Position in protein", "90-100%"], 7),
    (["Distribution of variants on chromosome 21", 46], 0),
])
def test_vep_v77_01(vep_v77_01, attrs, exp):
    assert getattr_nested(vep_v77_01.json, attrs) == exp, \
        ", ".join([repr(x) for x in attrs])

@@ -271,16 +271,53 @@
 nested%0A%0A
+from crimson.vep import group2entry%0A%0A
 %0A@pytest
@@ -808,17 +808,16 @@
 e != 0%0A%0A
-%0A
 def test
@@ -1509,16 +1509,16 @@
  exp, %5C%0A
-
         
@@ -1553,8 +1553,245 @@
 attrs%5D)%0A
+%0A%0Adef test_vep_group2entry():%0A    group = %22%22%22 %5BVariant classes%5D%0A                deletion%5Ct18%0A                insertion%5Ct35%0A                SNV%5Ct448%0A    %22%22%22%0A    key, value_dict = group2entry(group)%0A    assert value_dict%5B'deletion'%5D == 18%0A

Add test for group2entry