import pytest
from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell
from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion


def test_raise_on_different_metadata():
    ref = new_notebook(metadata={'kernelspec': {'language': 'python', 'name': 'python', 'display_name': 'Python'}},
                       cells=[new_markdown_cell('Cell one')])
    test = new_notebook(metadata={'kernelspec': {'language': 'R', 'name': 'R', 'display_name': 'R'}},
                        cells=[new_markdown_cell('Cell one')])
    with pytest.raises(NotebookDifference):
        compare_notebooks(ref, test, 'md')


@pytest.mark.parametrize('raise_on_first_difference', [True, False])
def test_raise_on_different_cell_type(raise_on_first_difference):
    ref = new_notebook(cells=[new_markdown_cell('Cell one'), new_code_cell('Cell two')])
    test = new_notebook(cells=[new_markdown_cell('Cell one'), new_raw_cell('Cell two')])
    with pytest.raises(NotebookDifference):
        compare_notebooks(ref, test, 'md', raise_on_first_difference=raise_on_first_difference)


@pytest.mark.parametrize('raise_on_first_difference', [True, False])
def test_raise_on_different_cell_content(raise_on_first_difference):
    ref = new_notebook(cells=[new_markdown_cell('Cell one'), new_code_cell('Cell two')])
    test = new_notebook(cells=[new_markdown_cell('Cell one'), new_code_cell('Modified cell two')])
    with pytest.raises(NotebookDifference):
        compare_notebooks(ref, test, 'md', raise_on_first_difference=raise_on_first_difference)


def test_raise_on_incomplete_markdown_cell():
    ref = new_notebook(cells=[new_markdown_cell('Cell one\n\n\nsecond line')])
    test = new_notebook(cells=[new_markdown_cell('Cell one')])
    with pytest.raises(NotebookDifference):
        compare_notebooks(ref, test, 'md')


def test_does_raise_on_split_markdown_cell():
    ref = new_notebook(cells=[new_markdown_cell('Cell one\n\n\nsecond line')])
    test = new_notebook(cells=[new_markdown_cell('Cell one'),
                               new_markdown_cell('second line')])
    with pytest.raises(NotebookDifference):
        compare_notebooks(ref, test, 'md')


def test_raise_on_different_cell_metadata():
    ref = new_notebook(cells=[new_code_cell('1+1')])
    test = new_notebook(cells=[new_code_cell('1+1', metadata={'metakey': 'value'})])
    with pytest.raises(NotebookDifference):
        compare_notebooks(ref, test, 'py:light')


@pytest.mark.parametrize('raise_on_first_difference', [True, False])
def test_raise_on_different_cell_count(raise_on_first_difference):
    ref = new_notebook(cells=[new_code_cell('1')])
    test = new_notebook(cells=[new_code_cell('1'),
                               new_code_cell('2')])
    with pytest.raises(NotebookDifference):
        compare_notebooks(ref, test, 'py:light', raise_on_first_difference=raise_on_first_difference)

    with pytest.raises(NotebookDifference):
        compare_notebooks(test, ref, 'py:light', raise_on_first_difference=raise_on_first_difference)


def test_does_not_raise_on_blank_line_removed():
    ref = new_notebook(cells=[new_code_cell('1+1\n    ')])
    test = new_notebook(cells=[new_code_cell('1+1')])
    compare_notebooks(ref, test, 'py:light')


def test_strict_raise_on_blank_line_removed():
    ref = new_notebook(cells=[new_code_cell('1+1\n')])
    test = new_notebook(cells=[new_code_cell('1+1')])
    with pytest.raises(NotebookDifference):
        compare_notebooks(ref, test, 'py:light', allow_expected_differences=False)


def test_dont_raise_on_different_outputs():
    ref = new_notebook(cells=[new_code_cell('1+1')])
    test = new_notebook(cells=[new_code_cell('1+1', outputs=[
        {
            "data": {
                "text/plain": [
                    "2"
                ]
            },
            "execution_count": 1,
            "metadata": {},
            "output_type": "execute_result"
        }
    ])])
    compare_notebooks(ref, test, 'md')


@pytest.mark.parametrize('raise_on_first_difference', [True, False])
def test_raise_on_different_outputs(raise_on_first_difference):
    ref = new_notebook(cells=[new_code_cell('1+1')])
    test = new_notebook(cells=[new_code_cell('1+1', outputs=[
        {
            "data": {
                "text/plain": [
                    "2"
                ]
            },
            "execution_count": 1,
            "metadata": {},
            "output_type": "execute_result"
        }
    ])])
    with pytest.raises(NotebookDifference):
        compare_notebooks(ref, test, 'md', compare_outputs=True, raise_on_first_difference=raise_on_first_difference)


def test_test_round_trip_conversion():
    notebook = new_notebook(cells=[new_code_cell('1+1', outputs=[
        {
            "data": {
                "text/plain": [
                    "2"
                ]
            },
            "execution_count": 1,
            "metadata": {},
            "output_type": "execute_result"
        }
    ])], metadata={'main_language': 'python'})

    round_trip_conversion(notebook, {'extension': '.py'}, update=True)


def test_mutiple_cells_differ():
    nb1 = new_notebook(cells=[new_code_cell(''),
                              new_code_cell('2')])
    nb2 = new_notebook(cells=[new_code_cell('1+1'),
                              new_code_cell('2\n2')])
    with pytest.raises(NotebookDifference) as exception_info:
        compare_notebooks(nb1, nb2, raise_on_first_difference=False)
    assert 'Cells 1,2 differ' in exception_info.value.args[0]


def test_cell_metadata_differ():
    nb1 = new_notebook(cells=[new_code_cell('1'),
                              new_code_cell('2', metadata={'additional': 'metadata1'})])
    nb2 = new_notebook(cells=[new_code_cell('1'),
                              new_code_cell('2', metadata={'additional': 'metadata2'})])
    with pytest.raises(NotebookDifference) as exception_info:
        compare_notebooks(nb1, nb2, raise_on_first_difference=False)
    assert "Cell metadata 'additional' differ" in exception_info.value.args[0]


def test_notebook_metadata_differ():
    nb1 = new_notebook(cells=[new_code_cell('1'),
                              new_code_cell('2')])
    nb2 = new_notebook(cells=[new_code_cell('1'),
                              new_code_cell('2')],
                       metadata={'kernelspec': {'language': 'python', 'name': 'python', 'display_name': 'Python'}})
    with pytest.raises(NotebookDifference) as exception_info:
        compare_notebooks(nb1, nb2, raise_on_first_difference=False, )
    assert "Notebook metadata differ" in exception_info.value.args[0]

[(15, 'pytest.mark.parametrize', 'pytest.mark.parametrize', False, 'import pytest\n'), (23, 'pytest.mark.parametrize', 'pytest.mark.parametrize', False, 'import pytest\n'), (53, 'pytest.mark.parametrize', 'pytest.mark.parametrize', False, 'import pytest\n'), (95, 'pytest.mark.parametrize', 'pytest.mark.parametrize', False, 'import pytest\n'), (68, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (92, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (128, 'jupytext.compare.test_round_trip_conversion', 'round_trip_conversion', True, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (11, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (12, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (19, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (20, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (27, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (28, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (34, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (35, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (42, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (43, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (49, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (50, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (58, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (59, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (61, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (62, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (74, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (75, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (110, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (111, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (136, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (137, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (146, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (147, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (157, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (158, 'jupytext.compare.compare_notebooks', 'compare_notebooks', False, 'from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion\n'), (8, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (10, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (17, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (17, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (18, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (18, 'nbformat.v4.nbbase.new_raw_cell', 'new_raw_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (25, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (25, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (26, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (26, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (32, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (33, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (39, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (40, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (41, 'nbformat.v4.nbbase.new_markdown_cell', 'new_markdown_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (47, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (48, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (55, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (56, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (57, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (66, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (67, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (72, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (73, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (79, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (80, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (97, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (98, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (115, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (132, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (133, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (134, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (135, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (142, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (143, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (144, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (145, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (152, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (153, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (154, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n'), (155, 'nbformat.v4.nbbase.new_code_cell', 'new_code_cell', False, 'from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell\n')]