# Path: crosscompute/exceptions.py
# class CrossComputeExecutionError(CrossComputeError):
#     pass


import json
from collections import defaultdict
from pytest import raises
from crosscompute.exceptions import (
    CrossComputeExecutionError)
from crosscompute.routines import (
    load_image_png,
    load_map_geojson,
    load_markdown_md,
    load_number_json,
    load_table_csv,
    load_text_json,
    load_text_txt,
    load_value_json,
    render_object,
    save_image_png,
    save_map_geojson,
    save_markdown_md,
    save_number_json,
    save_table_csv,
    save_text_json,
    save_text_txt)
    assert load_image_png(target_path, variable_id) == value


def test_save_number_json(tmpdir):
    target_path = tmpdir.join('variables.json').strpath
    variable_id = 'a'
    value_by_id_by_path = defaultdict(dict)
    with raises(CrossComputeExecutionError):
        save_number_json(target_path, 'a', variable_id, value_by_id_by_path)
    save_number_json(target_path, '1', variable_id, value_by_id_by_path)
    assert value_by_id_by_path[target_path][variable_id] == 1


def test_save_markdown_md(tmpdir):
    target_path = tmpdir.join('book.md').strpath
    value = '# whee'
    variable_id = 'a'
    value_by_id_by_path = defaultdict(dict)
    save_markdown_md(target_path, value, variable_id, value_by_id_by_path)
    assert load_markdown_md(target_path, variable_id) == value


def test_save_table_csv(tmpdir):
    target_path = tmpdir.join('table.csv').strpath
    value = {
        'columns': ['x', 'y'],
        'rows': [[1, 2], [2, 3], [3, 4]],
    }
    variable_id = 'a'
    value_by_id_by_path = defaultdict(dict)

# Path: crosscompute/exceptions.py
# class CrossComputeExecutionError(CrossComputeError):
#     pass


# Path: experiments/0.8/tests/test_routines_serialization.py
import json
from collections import defaultdict
from pytest import raises
from crosscompute.exceptions import (
    CrossComputeExecutionError)
from crosscompute.routines import (
    load_image_png,
    load_map_geojson,
    load_markdown_md,
    load_number_json,
    load_table_csv,
    load_text_json,
    load_text_txt,
    load_value_json,
    render_object,
    save_image_png,
    save_map_geojson,
    save_markdown_md,
    save_number_json,
    save_table_csv,
    save_text_json,
    save_text_txt)

    assert load_image_png(target_path, variable_id) == value


def test_save_number_json(tmpdir):
    target_path = tmpdir.join('variables.json').strpath
    variable_id = 'a'
    value_by_id_by_path = defaultdict(dict)
    with raises(CrossComputeExecutionError):
        save_number_json(target_path, 'a', variable_id, value_by_id_by_path)
    save_number_json(target_path, '1', variable_id, value_by_id_by_path)
    assert value_by_id_by_path[target_path][variable_id] == 1


def test_save_markdown_md(tmpdir):
    target_path = tmpdir.join('book.md').strpath
    value = '# whee'
    variable_id = 'a'
    value_by_id_by_path = defaultdict(dict)
    save_markdown_md(target_path, value, variable_id, value_by_id_by_path)
    assert load_markdown_md(target_path, variable_id) == value


def test_save_table_csv(tmpdir):
    target_path = tmpdir.join('table.csv').strpath
    value = {
        'columns': ['x', 'y'],
        'rows': [[1, 2], [2, 3], [3, 4]],
    }
    variable_id = 'a'
    value_by_id_by_path = defaultdict(dict)

    with raises(CrossComputeExecutionError):