Exemple #1
0
def test_spydata_import_missing_file():
    """
    Test that import fails properly when file is missing, and resets the cwd.
    """
    original_cwd = os.getcwd()
    path = os.path.join(LOCATION, 'non_existant_path_2019-01-23.spydata')
    try:
        iofuncs.load_dictionary(path)
    except IOError:
        pass
    else:
        # Fail if exception did not occur when it should
        assert False
    assert os.getcwd() == original_cwd
Exemple #2
0
def test_spydata_export(input_namespace, expected_namespace,
                        filename):
    """
    Test spydata export and re-import.

    This test saves the variables in ``spydata`` format and then
    reloads and checks them to make sure they save/restore properly
    and no errors occur during the process.
    """
    path = os.path.join(LOCATION, filename + '.spydata')
    expected_error = None
    if 'expected_error_string' in input_namespace:
        expected_error = input_namespace['expected_error_string']
        del input_namespace['expected_error_string']
    cwd_original = os.getcwd()

    try:
        export_error = iofuncs.save_dictionary(input_namespace, path)
        assert export_error == expected_error
        if expected_namespace is None:
            assert not os.path.isfile(path)
        else:
            data_actual, import_error = iofuncs.load_dictionary(path)
            assert import_error is None
            print(data_actual.keys())
            print(expected_namespace.keys())
            assert are_namespaces_equal(data_actual, expected_namespace)
        assert cwd_original == os.getcwd()
    finally:
        if os.path.isfile(path):
            try:
                os.remove(path)
            except (IOError, OSError, PermissionError):
                pass
Exemple #3
0
def test_spydata_export(spydata_values):
    """
    Test spydata export and re-import.

    This test saves the variables in ``spydata`` format and then
    reloads and checks them to make sure they save/restore properly
    and no errors occur during the process.
    """
    path = os.path.join(LOCATION, 'export_data_copy.spydata')
    export_error = iofuncs.save_dictionary(spydata_values, path)
    assert export_error is None
    data, import_error = iofuncs.load_dictionary(path)
    assert import_error is None
    valid = True
    for var in sorted(spydata_values.keys()):
        try:
            valid = valid and bool(np.mean(spydata_values[var] == data[var]))
        except ValueError:
            valid = valid and all([
                np.all(obj1 == obj2)
                for obj1, obj2 in zip(spydata_values[var], data[var])
            ])
    assert valid
    try:
        os.remove(path)
    except (IOError, OSError, PermissionError):
        pass
Exemple #4
0
def test_spydata_import_witherror():
    """
    Test that import fails gracefully with a fn not present in the namespace.

    Checks that the error is caught, the message is passed back,
    and the current working directory is restored afterwards.
    """
    original_cwd = os.getcwd()
    path = os.path.join(LOCATION, 'export_data_withfunction.spydata')
    data, error = iofuncs.load_dictionary(path)
    assert error and is_text_string(error)
    assert data is None
    assert os.getcwd() == original_cwd
Exemple #5
0
def test_spydata_import(spydata_file_name, spydata_values):
    """
    Test spydata handling and variable importing.

    This test loads all the variables contained inside a spydata tar
    container and compares them against their static values.
    It tests both a file with the original name, and one that has been renamed
    in order to catch Issue #9 .
    """
    path = os.path.join(LOCATION, spydata_file_name)
    data, error = iofuncs.load_dictionary(path)
    assert error is None
    assert are_namespaces_equal(data, spydata_values)
def test_spydata_import(spydata_values):
    """
    Test spydata handling and variable importing.

    This test loads all the variables contained inside a spydata tar
    container and compares them against their static values.
    """
    path = os.path.join(LOCATION, 'export_data.spydata')
    data, error = iofuncs.load_dictionary(path)
    assert error is None
    valid = True
    for var in sorted(spydata_values.keys()):
        try:
            valid = valid and bool(np.mean(spydata_values[var] == data[var]))
        except ValueError:
            valid = valid and all([np.all(obj1 == obj2) for obj1, obj2 in
                                   zip(spydata_values[var], data[var])])
    assert valid