"""Test suite for fmbiopy.io."""
from uuid import uuid4

import pandas as pd
from pytest import fixture, mark

from fmbiopy.io import *


@fixture
def list_data():
    return [["n1", "n2", "n3"], ["a", "b", "c"], ["1", "2", "3"]]


@mark.parametrize("delimiter", [(","), ("\t")])
def test_list_to_csv(list_data, sandbox, delimiter):
    if delimiter == ",":
        suffix = ".csv"
    else:
        suffix = ".tsv"
    output_file = sandbox / (uuid4().hex + suffix)
    list_to_csv(list_data, output_file, delimiter)
    df = pd.read_csv(output_file, sep=delimiter)
    for i, row in enumerate(df.itertuples()):
        assert tuple(row)[1:] == tuple(list_data[i + 1])


@fixture(params=["#", "/"])
def file_with_header(sandbox, request):
    comment_char = request.param
    filename = sandbox / "file_with_header.txt"
    with filename.open("w") as f:
        f.write("{} foo\n".format(comment_char))
        f.write("\n")
        f.write("bar")
    return {"filename": filename, "comment_char": comment_char}


def test_read_header(file_with_header):
    expected = ["{} foo\n".format(file_with_header["comment_char"]), "\n"]
    assert read_header(**file_with_header) == expected


def test_write_table_with_header(sandbox, dataframe, dataframe_header):
    tmpfile = sandbox / "write_table_with_header" / "table.csv"
    expected = ["# foo\n", "\n", "A,B\n", "0,0\n", "0,0\n"]
    write_table_with_header(dataframe, dataframe_header, tmpfile, sep=",")
    with tmpfile.open("r") as f:
        lines = f.readlines()
    assert lines == expected

3e689c0f840ccc425934b2c65492366ee2b59b79
<mask token>


@fixture
def list_data():
    return [['n1', 'n2', 'n3'], ['a', 'b', 'c'], ['1', '2', '3']]


<mask token>


@fixture(params=['#', '/'])
def file_with_header(sandbox, request):
    comment_char = request.param
    filename = sandbox / 'file_with_header.txt'
    with filename.open('w') as f:
        f.write('{} foo\n'.format(comment_char))
        f.write('\n')
        f.write('bar')
    return {'filename': filename, 'comment_char': comment_char}


<mask token>


def test_write_table_with_header(sandbox, dataframe, dataframe_header):
    tmpfile = sandbox / 'write_table_with_header' / 'table.csv'
    expected = ['# foo\n', '\n', 'A,B\n', '0,0\n', '0,0\n']
    write_table_with_header(dataframe, dataframe_header, tmpfile, sep=',')
    with tmpfile.open('r') as f:
        lines = f.readlines()
    assert lines == expected

<mask token>


@fixture
def list_data():
    return [['n1', 'n2', 'n3'], ['a', 'b', 'c'], ['1', '2', '3']]


@mark.parametrize('delimiter', [',', '\t'])
def test_list_to_csv(list_data, sandbox, delimiter):
    if delimiter == ',':
        suffix = '.csv'
    else:
        suffix = '.tsv'
    output_file = sandbox / (uuid4().hex + suffix)
    list_to_csv(list_data, output_file, delimiter)
    df = pd.read_csv(output_file, sep=delimiter)
    for i, row in enumerate(df.itertuples()):
        assert tuple(row)[1:] == tuple(list_data[i + 1])


@fixture(params=['#', '/'])
def file_with_header(sandbox, request):
    comment_char = request.param
    filename = sandbox / 'file_with_header.txt'
    with filename.open('w') as f:
        f.write('{} foo\n'.format(comment_char))
        f.write('\n')
        f.write('bar')
    return {'filename': filename, 'comment_char': comment_char}


<mask token>


def test_write_table_with_header(sandbox, dataframe, dataframe_header):
    tmpfile = sandbox / 'write_table_with_header' / 'table.csv'
    expected = ['# foo\n', '\n', 'A,B\n', '0,0\n', '0,0\n']
    write_table_with_header(dataframe, dataframe_header, tmpfile, sep=',')
    with tmpfile.open('r') as f:
        lines = f.readlines()
    assert lines == expected

<mask token>


@fixture
def list_data():
    return [['n1', 'n2', 'n3'], ['a', 'b', 'c'], ['1', '2', '3']]


@mark.parametrize('delimiter', [',', '\t'])
def test_list_to_csv(list_data, sandbox, delimiter):
    if delimiter == ',':
        suffix = '.csv'
    else:
        suffix = '.tsv'
    output_file = sandbox / (uuid4().hex + suffix)
    list_to_csv(list_data, output_file, delimiter)
    df = pd.read_csv(output_file, sep=delimiter)
    for i, row in enumerate(df.itertuples()):
        assert tuple(row)[1:] == tuple(list_data[i + 1])


@fixture(params=['#', '/'])
def file_with_header(sandbox, request):
    comment_char = request.param
    filename = sandbox / 'file_with_header.txt'
    with filename.open('w') as f:
        f.write('{} foo\n'.format(comment_char))
        f.write('\n')
        f.write('bar')
    return {'filename': filename, 'comment_char': comment_char}


def test_read_header(file_with_header):
    expected = ['{} foo\n'.format(file_with_header['comment_char']), '\n']
    assert read_header(**file_with_header) == expected


def test_write_table_with_header(sandbox, dataframe, dataframe_header):
    tmpfile = sandbox / 'write_table_with_header' / 'table.csv'
    expected = ['# foo\n', '\n', 'A,B\n', '0,0\n', '0,0\n']
    write_table_with_header(dataframe, dataframe_header, tmpfile, sep=',')
    with tmpfile.open('r') as f:
        lines = f.readlines()
    assert lines == expected

<mask token>
from uuid import uuid4
import pandas as pd
from pytest import fixture, mark
from fmbiopy.io import *


@fixture
def list_data():
    return [['n1', 'n2', 'n3'], ['a', 'b', 'c'], ['1', '2', '3']]


@mark.parametrize('delimiter', [',', '\t'])
def test_list_to_csv(list_data, sandbox, delimiter):
    if delimiter == ',':
        suffix = '.csv'
    else:
        suffix = '.tsv'
    output_file = sandbox / (uuid4().hex + suffix)
    list_to_csv(list_data, output_file, delimiter)
    df = pd.read_csv(output_file, sep=delimiter)
    for i, row in enumerate(df.itertuples()):
        assert tuple(row)[1:] == tuple(list_data[i + 1])


@fixture(params=['#', '/'])
def file_with_header(sandbox, request):
    comment_char = request.param
    filename = sandbox / 'file_with_header.txt'
    with filename.open('w') as f:
        f.write('{} foo\n'.format(comment_char))
        f.write('\n')
        f.write('bar')
    return {'filename': filename, 'comment_char': comment_char}


def test_read_header(file_with_header):
    expected = ['{} foo\n'.format(file_with_header['comment_char']), '\n']
    assert read_header(**file_with_header) == expected


def test_write_table_with_header(sandbox, dataframe, dataframe_header):
    tmpfile = sandbox / 'write_table_with_header' / 'table.csv'
    expected = ['# foo\n', '\n', 'A,B\n', '0,0\n', '0,0\n']
    write_table_with_header(dataframe, dataframe_header, tmpfile, sep=',')
    with tmpfile.open('r') as f:
        lines = f.readlines()
    assert lines == expected

"""Test suite for fmbiopy.io."""
from uuid import uuid4

import pandas as pd
from pytest import fixture, mark

from fmbiopy.io import *


@fixture
def list_data():
    return [["n1", "n2", "n3"], ["a", "b", "c"], ["1", "2", "3"]]


@mark.parametrize("delimiter", [(","), ("\t")])
def test_list_to_csv(list_data, sandbox, delimiter):
    if delimiter == ",":
        suffix = ".csv"
    else:
        suffix = ".tsv"
    output_file = sandbox / (uuid4().hex + suffix)
    list_to_csv(list_data, output_file, delimiter)
    df = pd.read_csv(output_file, sep=delimiter)
    for i, row in enumerate(df.itertuples()):
        assert tuple(row)[1:] == tuple(list_data[i + 1])


@fixture(params=["#", "/"])
def file_with_header(sandbox, request):
    comment_char = request.param
    filename = sandbox / "file_with_header.txt"
    with filename.open("w") as f:
        f.write("{} foo\n".format(comment_char))
        f.write("\n")
        f.write("bar")
    return {"filename": filename, "comment_char": comment_char}


def test_read_header(file_with_header):
    expected = ["{} foo\n".format(file_with_header["comment_char"]), "\n"]
    assert read_header(**file_with_header) == expected


def test_write_table_with_header(sandbox, dataframe, dataframe_header):
    tmpfile = sandbox / "write_table_with_header" / "table.csv"
    expected = ["# foo\n", "\n", "A,B\n", "0,0\n", "0,0\n"]
    write_table_with_header(dataframe, dataframe_header, tmpfile, sep=",")
    with tmpfile.open("r") as f:
        lines = f.readlines()
    assert lines == expected
