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")
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")
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.')
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
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)
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()
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()
def test_cli_version(capsys, flag): with pytest.raises(SystemExit): Packager.from_cli([flag]) captured = capsys.readouterr() assert __version__ in captured.out
def test_no_args(): """Works without arguments.""" assert DEFAULT_OPTIONS == Packager.from_cli([]).options