示例#1
0
def test_cli_plus_defaults(mock_zip_file):
    """Test CLI args + defaults."""

    option_subset = {'zip_path': str(mock_zip_file)}
    result = Packager.from_cli(['-z', str(mock_zip_file)]).options
    assert_dict_contains_subset(option_subset, result)

    option_subset = {'fields': ['kDefinition']}
    result = Packager.from_cli(['-f', 'kDefinition']).options
    assert_dict_contains_subset(option_subset, result)

    option_subset = {'fields': ['kDefinition', 'kXerox']}
    result = Packager.from_cli(['-f', 'kDefinition', 'kXerox']).options
    assert_dict_contains_subset(option_subset,
                                result,
                                msg="fields -f allows multiple fields.")

    option_subset = {
        'fields': ['kDefinition', 'kXerox'],
        'destination': 'data/ha.csv'
    }
    result = Packager.from_cli(
        ['-f', 'kDefinition', 'kXerox', '-d', 'data/ha.csv']).options
    assert_dict_contains_subset(option_subset,
                                result,
                                msg="fields -f allows additional arguments.")

    result = Packager.from_cli(['--format', 'json']).options
    option_subset = {'format': 'json'}
    assert_dict_contains_subset(option_subset,
                                result,
                                msg="format argument works")
示例#2
0
def test_cli_plus_defaults(mock_zip_file):
    """Test CLI args + defaults."""

    option_subset = {'zip_path': str(mock_zip_file)}
    result = Packager.from_cli(['-z', str(mock_zip_file)]).options
    assert_dict_contains_subset(option_subset, result)

    option_subset = {'fields': ['kDefinition']}
    result = Packager.from_cli(['-f', 'kDefinition']).options
    assert_dict_contains_subset(option_subset, result)

    option_subset = {'fields': ['kDefinition', 'kXerox']}
    result = Packager.from_cli(['-f', 'kDefinition', 'kXerox']).options
    assert_dict_contains_subset(
        option_subset, result, msg="fields -f allows multiple fields."
    )

    option_subset = {'fields': ['kDefinition', 'kXerox'], 'destination': 'data/ha.csv'}
    result = Packager.from_cli(
        ['-f', 'kDefinition', 'kXerox', '-d', 'data/ha.csv']
    ).options
    assert_dict_contains_subset(
        option_subset, result, msg="fields -f allows additional arguments."
    )

    result = Packager.from_cli(['--format', 'json']).options
    option_subset = {'format': 'json'}
    assert_dict_contains_subset(option_subset, result, msg="format argument works")
示例#3
0
def test_cli_exit_emessage_to_stderr():
    """Sends exception .message to stderr on exit."""

    # SystemExit print's to stdout by default
    with pytest.raises(SystemExit) as excinfo:
        Packager.from_cli(['-d', 'data/output.csv', '-f', 'sdfa'])

    excinfo.match('Field sdfa not found in file list.')
示例#4
0
def test_cli_exit_emessage_to_stderr():
    """Sends exception .message to stderr on exit."""

    # SystemExit print's to stdout by default
    with pytest.raises(SystemExit) as excinfo:
        Packager.from_cli(['-d', 'data/output.csv', '-f', 'sdfa'])

    excinfo.match('Field sdfa not found in file list.')
示例#5
0
def test_cli_version(capsys, flag):
    with pytest.raises(SystemExit):
        Packager.from_cli([flag])
    captured = capsys.readouterr()

    if PY2:  # todo: why does python 2.x return -v in error?
        assert __version__ in captured.err
    else:
        assert __version__ in captured.out
示例#6
0
def test_cli_version(capsys, flag):
    with pytest.raises(SystemExit):
        Packager.from_cli([flag])
    captured = capsys.readouterr()

    if PY2:  # todo: why does python 2.x return -v in error?
        assert __version__ in captured.err
    else:
        assert __version__ in captured.out
示例#7
0
def TestPackager(mock_test_dir, mock_zip_file):
    # monkey-patching builder
    options = {
        'work_dir': str(mock_test_dir),
        'zip_path': str(mock_zip_file),
        'destination': str(mock_test_dir.join('unihan.csv')),
    }
    return Packager(options)
示例#8
0
def test_download_mock(tmpdir, mock_zip, mock_zip_file, mock_test_dir, test_options):
    data_path = tmpdir.join('data')
    dest_path = data_path.join('data', 'hey.zip')

    def urlretrieve(url, filename, url_retrieve, reporthook=None):
        mock_zip_file.copy(dest_path)

    p = Packager(
        merge_dict(
            test_options.copy,
            {
                'fields': ['kDefinition'],
                'zip_path': str(dest_path),
                'work_dir': str(mock_test_dir.join('downloads')),
                'destination': str(data_path.join('unihan.csv')),
            },
        )
    )
    p.download(urlretrieve_fn=urlretrieve)
    assert os.path.exists(str(dest_path))
    p.export()
示例#9
0
def test_download_mock(tmpdir, mock_zip, mock_zip_file, mock_test_dir,
                       test_options):
    data_path = tmpdir.join('data')
    dest_path = data_path.join('data', 'hey.zip')

    def urlretrieve(url, filename, url_retrieve, reporthook=None):
        mock_zip_file.copy(dest_path)

    p = Packager(
        merge_dict(
            test_options.copy,
            {
                'fields': ['kDefinition'],
                'zip_path': str(dest_path),
                'work_dir': str(mock_test_dir.join('downloads')),
                'destination': str(data_path.join('unihan.csv')),
            },
        ))
    p.download(urlretrieve_fn=urlretrieve)
    assert os.path.exists(str(dest_path))
    p.export()
示例#10
0
def test_cli_version(capsys, flag):
    with pytest.raises(SystemExit):
        Packager.from_cli([flag])
    captured = capsys.readouterr()
    assert __version__ in captured.out
示例#11
0
def test_no_args():
    """Works without arguments."""

    assert DEFAULT_OPTIONS == Packager.from_cli([]).options
示例#12
0
def test_no_args():
    """Works without arguments."""

    assert DEFAULT_OPTIONS == Packager.from_cli([]).options