def test_exceptions(model):
    t_file = os.path.join(base_dir, 'temp.nc')
    spill_pair = model.spills

    def _del_temp_file():
        # clean up temporary file from previous run
        try:
            print 'remove temporary file {0}'.format(t_file)
            os.remove(t_file)
        except:
            pass

    # begin tests
    _del_temp_file()
    netcdf = NetCDFOutput(t_file, which_data='all')

    with raises(TypeError):
        # need to pass in model start time
        netcdf.prepare_for_model_run(num_time_steps=4)

    with raises(TypeError):
        # need to pass in model start time and spills
        netcdf.prepare_for_model_run()

    with raises(ValueError):
        # need a cache object
        netcdf.write_output(0)

    with raises(ValueError):
        netcdf.which_data = 'some random string'

    with raises(ValueError):
        # raise error because file 'temp.nc' should exist after 1st call
        netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                     spills=spill_pair,
                                     num_time_steps=4)
        netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                     spills=spill_pair,
                                     num_time_steps=4)

    with raises(AttributeError):
        'cannot change after prepare_for_model_run has been called'
        netcdf.which_data = 'all'

    _del_temp_file()
Ejemplo n.º 2
0
def test_exceptions(output_filename):
    spill_pair = SpillContainerPair()

    print "output_filename:", output_filename
    # begin tests
    netcdf = NetCDFOutput(output_filename, which_data='all')
    netcdf.rewind()  # delete temporary files

    with raises(TypeError):
        # need to pass in model start time
        netcdf.prepare_for_model_run(num_time_steps=4)

    with raises(TypeError):
        # need to pass in model start time and spills
        netcdf.prepare_for_model_run()

    with raises(ValueError):
        # need a cache object
        netcdf.write_output(0)

    with raises(ValueError):
        netcdf.which_data = 'some random string'

    # changed renderer and netcdf ouputter to delete old files in
    # prepare_for_model_run() rather than rewind()
    # -- rewind() was getting called a lot
    # -- before there was time to change the ouput file names, etc.
    # So for this unit test, there should be no exception if we do it twice.
    netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                 spills=spill_pair,
                                 num_time_steps=4)
    netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                 spills=spill_pair,
                                 num_time_steps=4)

    with raises(AttributeError):
        'cannot change after prepare_for_model_run has been called'
        netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                     spills=spill_pair,
                                     num_time_steps=4)
        netcdf.which_data = 'most'
Ejemplo n.º 3
0
def test_exceptions(output_filename):
    spill_pair = SpillContainerPair()

    print "output_filename:", output_filename
    # begin tests
    netcdf = NetCDFOutput(output_filename, which_data='all')
    netcdf.rewind()  # delete temporary files

    with raises(TypeError):
        # need to pass in model start time
        netcdf.prepare_for_model_run(num_time_steps=4)

    with raises(TypeError):
        # need to pass in model start time and spills
        netcdf.prepare_for_model_run()

    with raises(ValueError):
        # need a cache object
        netcdf.write_output(0)

    with raises(ValueError):
        netcdf.which_data = 'some random string'

    # changed renderer and netcdf ouputter to delete old files in
    # prepare_for_model_run() rather than rewind()
    # -- rewind() was getting called a lot
    # -- before there was time to change the ouput file names, etc.
    # So for this unit test, there should be no exception if we do it twice.
    netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                 spills=spill_pair,
                                 num_time_steps=4)
    netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                 spills=spill_pair,
                                 num_time_steps=4)

    with raises(AttributeError):
        'cannot change after prepare_for_model_run has been called'
        netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                     spills=spill_pair,
                                     num_time_steps=4)
        netcdf.which_data = 'most'
Ejemplo n.º 4
0
def test_exceptions():
    t_file = os.path.join(base_dir, 'temp.nc')

    # clean up temporary file from previos run
    if os.path.exists(t_file):
        print 'remove temporary file {0}'.format(t_file)
        os.remove(t_file)

    netcdf = NetCDFOutput(t_file, which_data='all')

    with raises(TypeError):
        # need to pass in model start time
        netcdf.prepare_for_model_run(num_time_steps=4)

    with raises(TypeError):
        # need to pass in model start time
        netcdf.prepare_for_model_run()

    with raises(ValueError):
        # needs a spills object for "all"
        netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                     num_time_steps=4,)

    with raises(ValueError):
        # need a cache object
        netcdf.write_output(0)

    with raises(ValueError):
        # raise error because file 'temp.nc' should already exist
        netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                     num_time_steps=4)

    with raises(ValueError):
        # which_data is 'all' but spills are not provided so raise an error
        netcdf.rewind()
        netcdf.which_data = 'all'
        netcdf.prepare_for_model_run(model_start_time=datetime.now(),
                                     num_time_steps=4)
    with raises(ValueError):
        netcdf.which_data = 'some random string'