import Bio.Phylo
from io import StringIO
from pathlib import Path
import pytest
import sys

# we assume (and assert) that this script is running from the tests/ directory
sys.path.append(str(Path(__file__).parent.parent.parent))

from augur.export_v2 import convert_tree_to_json_structure
from augur.validate import ValidateError
from augur.validate_export import ensure_no_duplicate_names


class TestValidateExport():
    def test_export_without_duplicate_names(self):
        # Create a tree with unique tip names.
        tree = Bio.Phylo.read(StringIO("root(A, internal(B, C))"), "newick")
        metadata = {"A": {}, "B": {}, "C": {}, "root": {}, "internal": {}}
        root = convert_tree_to_json_structure(tree.root, metadata)
        ensure_no_duplicate_names(root, ValidateError)

    def test_export_with_duplicate_names(self):
        # Create a tree with duplicate tip names.
        tree = Bio.Phylo.read(StringIO("root(A, internal(B, B))"), "newick")
        metadata = {"A": {}, "B": {}, "root": {}, "internal": {}}
        root = convert_tree_to_json_structure(tree.root, metadata)

        with pytest.raises(ValidateError):
            ensure_no_duplicate_names(root, ValidateError)

Add unit tests for ensure_no_duplicate_names
Add unit tests for ensure_no_duplicate_names

Adds minimal tests for validation of no duplicate names. Instead of
mocking up the tree JSON directly, these tests build a minimal BioPython
tree and use augur's own tree-to-JSON function to make the more verbose
JSON structure expected by the validation function.
