# Path: crosscompute/exceptions.py
# class CrossComputeDataError(CrossComputeError):
#     pass
# 
# Path: crosscompute/routines/variable.py
# def update_variable_data(target_path, data_by_id):
#     try:
#         if exists(target_path):
#             with open(target_path, 'r+t') as f:
#                 d = json.load(f)
#                 d.update(data_by_id)
#                 f.seek(0)
#                 f.truncate()
#                 json.dump(d, f)
#         else:
#             with open(target_path, 'wt') as f:
#                 d = data_by_id
#                 json.dump(d, f)
#     except (json.JSONDecodeError, OSError) as e:
#         raise CrossComputeDataError(e)


import json
from crosscompute.exceptions import CrossComputeDataError
from crosscompute.routines.variable import (
    update_variable_data)
from pytest import raises


def test_update_variable_data(tmp_path):
    target_path = tmp_path / 'variables.dictionary'
    update_variable_data(target_path, {'a': 1})
    with target_path.open('r') as f:
        d = json.load(f)
    assert d['a'] == 1

# Path: crosscompute/exceptions.py
# class CrossComputeDataError(CrossComputeError):
#     pass
# 
# Path: crosscompute/routines/variable.py
# def update_variable_data(target_path, data_by_id):
#     try:
#         if exists(target_path):
#             with open(target_path, 'r+t') as f:
#                 d = json.load(f)
#                 d.update(data_by_id)
#                 f.seek(0)
#                 f.truncate()
#                 json.dump(d, f)
#         else:
#             with open(target_path, 'wt') as f:
#                 d = data_by_id
#                 json.dump(d, f)
#     except (json.JSONDecodeError, OSError) as e:
#         raise CrossComputeDataError(e)


# Path: tests/test_routines_variable.py
import json
from crosscompute.exceptions import CrossComputeDataError
from crosscompute.routines.variable import (
    update_variable_data)
from pytest import raises



def test_update_variable_data(tmp_path):
    target_path = tmp_path / 'variables.dictionary'
    update_variable_data(target_path, {'a': 1})
    with target_path.open('r') as f:
        d = json.load(f)
    assert d['a'] == 1

    update_variable_data(target_path, {'b': 2})