def test_natsort_keygen_splits_input_with_locale(): load_locale('en_US') strxfrm = get_strxfrm() with patch('natsort.compat.locale.dumb_sort', return_value=False): assert natsort_keygen(alg=ns.L)(INPUT) == ((null_string_locale, 6, strxfrm('A-'), 5, strxfrm('.'), 34, strxfrm('e+'), 1), (strxfrm('/Folder ('), 1, strxfrm(')/Foo')), (null_string_locale, 56.7)) with patch('natsort.compat.locale.dumb_sort', return_value=True): assert natsort_keygen(alg=ns.L)(INPUT) == ((null_string_locale, 6, strxfrm('aa--'), 5, strxfrm('..'), 34, strxfrm('eE++'), 1), (strxfrm('//ffoOlLdDeErR (('), 1, strxfrm('))//ffoOoO')), (null_string_locale, 56.7)) if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LA)(b'6A-5.034e+1') == (b'6A-5.034e+1',) locale.setlocale(locale.LC_ALL, str(''))
def test_natsort_keygen_splits_input_with_locale_and_capitalfirst(): load_locale('en_US') strxfrm = get_strxfrm() with patch('natsort.compat.locale.dumb_sort', return_value=False): assert natsort_keygen(alg=ns.LA | ns.C)(INPUT) == ((('',), (null_string_locale, 6, strxfrm('A-'), 5, strxfrm('.'), 34, strxfrm('e+'), 1)), (('/',), (strxfrm('/Folder ('), 1, strxfrm(')/Foo'))), (('',), (null_string_locale, 56.7))) if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LA | ns.C)(b'6A-5.034e+1') == (b'6A-5.034e+1',) locale.setlocale(locale.LC_ALL, str(''))
def test_sort_and_print_entries_excludes_paths_between_of_20_to_100_with_reverse_filter_option(): with patch(mock_print) as p: # tmp/a1/path1 # tmp/a1 (1)/path1 # tmp/a130/path1 sort_and_print_entries(entries, Args(None, [(20, 100)], False, True, False)) e = [call(entries[i]) for i in [2, 3, 4]] p.assert_has_calls(e)
def test_fetch(self): try: nds_buffer = mockutils.mock_nds2_buffer( 'X1:TEST', self.data, 1000000000, self.data.shape[0], 'm') except ImportError as e: self.skipTest(str(e)) nds_connection = mockutils.mock_nds2_connection([nds_buffer]) with mock.patch('nds2.connection') as mock_connection, \ mock.patch('nds2.buffer', nds_buffer): mock_connection.return_value = nds_connection # use verbose=True to hit more lines ts = TimeSeries.fetch('X1:TEST', 1000000000, 1000000001, verbose=True) nptest.assert_array_equal(ts.value, self.data) self.assertEqual(ts.sample_rate, self.data.shape[0] * units.Hz) self.assertTupleEqual(ts.span, (1000000000, 1000000001)) self.assertEqual(ts.unit, units.meter)
def test_sort_and_print_entries_keeps_only_paths_between_of_20_to_100_with_filter_option(): with patch(mock_print) as p: # tmp/a23/path1 # tmp/a57/path2 # tmp/a64/path1 # tmp/a64/path2 sort_and_print_entries(entries, Args([(20, 100)], None, False, False, False)) e = [call(entries[i]) for i in [1, 0, 5, 6]] p.assert_has_calls(e)
def test_natsort_keygen_splits_input_with_locale(): load_locale("en_US") strxfrm = get_strxfrm() with patch("natsort.compat.locale.dumb_sort", return_value=False): assert natsort_keygen(alg=ns.L)(INPUT) == ( (null_string, 6, strxfrm("A-"), 5, strxfrm("."), 34, strxfrm("e+"), 1), (strxfrm("/Folder ("), 1, strxfrm(")/Foo")), (null_string, 56.7), ) with patch("natsort.compat.locale.dumb_sort", return_value=True): assert natsort_keygen(alg=ns.L)(INPUT) == ( (null_string, 6, strxfrm("aa--"), 5, strxfrm(".."), 34, strxfrm("eE++"), 1), (strxfrm("//ffoOlLdDeErR (("), 1, strxfrm("))//ffoOoO")), (null_string, 56.7), ) if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LA)(b"6A-5.034e+1") == (b"6A-5.034e+1",) locale.setlocale(locale.LC_ALL, str(""))
def test_sort_and_print_entries_excludes_paths_between_of_20_to_100_with_reverse_filter_option( ): with patch(mock_print) as p: # tmp/a1/path1 # tmp/a1 (1)/path1 # tmp/a130/path1 sort_and_print_entries(entries, Args(None, [(20, 100)], False, True, False)) e = [call(entries[i]) for i in [2, 3, 4]] p.assert_has_calls(e)
def test_sort_and_print_entries_excludes_paths_23_or_130_with_exclude_option_list(): with patch(mock_print) as p: # tmp/a1/path1 # tmp/a1 (1)/path1 # tmp/a57/path2 # tmp/a64/path1 # tmp/a64/path2 sort_and_print_entries(entries, Args(None, None, [23, 130], True, False)) e = [call(entries[i]) for i in [2, 3, 0, 5, 6]] p.assert_has_calls(e)
def test__keys_are_being_cached(): natcmp.cached_keys = {} assert len(natcmp.cached_keys) == 0 natcmp(0, 0) assert len(natcmp.cached_keys) == 1 natcmp(0, 0) assert len(natcmp.cached_keys) == 1 with patch('natsort.compat.locale.dumb_sort', return_value=False): natcmp(0, 0, alg=ns.L) assert len(natcmp.cached_keys) == 2 natcmp(0, 0, alg=ns.L) assert len(natcmp.cached_keys) == 2 with patch('natsort.compat.locale.dumb_sort', return_value=True): natcmp(0, 0, alg=ns.L) assert len(natcmp.cached_keys) == 3 natcmp(0, 0, alg=ns.L) assert len(natcmp.cached_keys) == 3
def test_iter_channel_names(self): # maybe need something better? from types import GeneratorType names = gwf.iter_channel_names(TEST_GWF_FILE) self.assertIsInstance(names, GeneratorType) self.assertSequenceEqual(list(names), TEST_CHANNELS) with mock.patch('gwpy.utils.shell.call', mock_call): names = gwf.iter_channel_names(TEST_GWF_FILE) self.assertIsInstance(names, GeneratorType) self.assertSequenceEqual(list(names), TEST_CHANNELS)
def test_iter_channel_names(self): # maybe need something better? from types import GeneratorType names = datafind.iter_channel_names(TEST_GWF_FILE) self.assertIsInstance(names, GeneratorType) self.assertSequenceEqual(list(names), TEST_CHANNELS) with mock.patch('gwpy.utils.shell.call', mock_call): names = datafind.iter_channel_names(TEST_GWF_FILE) self.assertIsInstance(names, GeneratorType) self.assertSequenceEqual(list(names), TEST_CHANNELS)
def test_sort_and_print_entries_keeps_only_paths_between_of_20_to_100_with_filter_option( ): with patch(mock_print) as p: # tmp/a23/path1 # tmp/a57/path2 # tmp/a64/path1 # tmp/a64/path2 sort_and_print_entries(entries, Args([(20, 100)], None, False, False, False)) e = [call(entries[i]) for i in [1, 0, 5, 6]] p.assert_has_calls(e)
def test_sort_and_print_entries_excludes_paths_23_or_130_with_exclude_option_list( ): with patch(mock_print) as p: # tmp/a1/path1 # tmp/a1 (1)/path1 # tmp/a57/path2 # tmp/a64/path1 # tmp/a64/path2 sort_and_print_entries(entries, Args(None, None, [23, 130], True, False)) e = [call(entries[i]) for i in [2, 3, 0, 5, 6]] p.assert_has_calls(e)
def _mock_query_versionless(self, cm, result, *args, **kwargs): """Query for segments using a mock of the dqsegdb API """ try: return cm(*args, **kwargs) except (UnboundLocalError, AttributeError, URLError) as e: warnings.warn("Test query failed with %s: %s, " "rerunning with mock..." % (type(e).__name__, str(e))) with mock.patch('dqsegdb.apicalls.dqsegdbCascadedQuery', mockutils.mock_dqsegdb_cascaded_query(result)): return cm(*args, **kwargs)
def test_natsort_keygen_splits_input_with_locale_and_capitalfirst(): load_locale("en_US") strxfrm = get_strxfrm() with patch("natsort.compat.locale.dumb_sort", return_value=False): assert natsort_keygen(alg=ns.LA | ns.C)(INPUT) == ( (("",), (null_string, 6, strxfrm("A-"), 5, strxfrm("."), 34, strxfrm("e+"), 1)), (("/",), (strxfrm("/Folder ("), 1, strxfrm(")/Foo"))), (("",), (null_string, 56.7)), ) if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LA | ns.C)(b"6A-5.034e+1") == (b"6A-5.034e+1",) locale.setlocale(locale.LC_ALL, str(""))
def test_randomly_selects_correct_amount_of_items(self, mpopen, mpool): mpopen.return_value.poll.return_value = None mpopen.return_value.stdout.__iter__.return_value = self.samview_lines[1:] mpopen.return_value.stdout.readline.return_value = self.samview_lines[0] mpool.return_value.map = map with mock.patch('subsamplebam.sys') as msys: r = subsamplebam.subselect_from_bam( 'foo.bam', 10, 'chr1:1-10', ) self.assertEqual(set(self.samview_lines), r)
def test_sort_and_print_entries_reverses_order_with_reverse_option(): with patch(mock_print) as p: # tmp/a130/path1 # tmp/a64/path2 # tmp/a64/path1 # tmp/a57/path2 # tmp/a23/path1 # tmp/a1 (1)/path1 # tmp/a1/path1 sort_and_print_entries(entries, Args(None, None, False, True, True)) e = [call(entries[i]) for i in reversed([2, 3, 1, 0, 5, 6, 4])] p.assert_has_calls(e)
def test_sort_and_print_entries_uses_default_algorithm_with_all_options_false(): with patch(mock_print) as p: # tmp/a1 (1)/path1 # tmp/a1/path1 # tmp/a23/path1 # tmp/a57/path2 # tmp/a64/path1 # tmp/a64/path2 # tmp/a130/path1 sort_and_print_entries(entries, Args(None, None, False, False, False)) e = [call(entries[i]) for i in [3, 2, 1, 0, 5, 6, 4]] p.assert_has_calls(e)
def test_sort_and_print_entries_uses_PATH_algorithm_with_path_option_true_to_properly_sort_OS_generated_path_names(): with patch(mock_print) as p: # tmp/a1/path1 # tmp/a1 (1)/path1 # tmp/a23/path1 # tmp/a57/path2 # tmp/a64/path1 # tmp/a64/path2 # tmp/a130/path1 sort_and_print_entries(entries, Args(None, None, False, True, False)) e = [call(entries[i]) for i in [2, 3, 1, 0, 5, 6, 4]] p.assert_has_calls(e)
def test_handles_missing_depth_error(self, mpopen, mpool): mpopen.return_value.poll.return_value = None mpopen.return_value.stdout.__iter__.return_value = self.samview_lines[1:] mpopen.return_value.stdout.readline.return_value = self.samview_lines[0] mpool.return_value.map = map with mock.patch('subsamplebam.sys') as msys: r = subsamplebam.subselect_from_bam( 'foo.bam', 11, 'chr1:1-10', ) self.assertEqual(set([]), r) for i in range(1,11): msys.stderr.write.assert_has_call('Depth for chr1:{0}-{0} is only 10\n'.format(i))
def test_missing_samtools(self, margparser, msys, mpool): mpool.return_value.map = map args = mock.Mock() margparser.return_value.parse_args.return_value = args args.bamfile = 'foo.bam' args.subsample = 10 args.regionstr = 'chr1:1-10' with mock.patch('subsamplebam.samtools_is_available') as mocky: mocky.return_value = False msys.exit.side_effect = SystemExit self.assertRaises(SystemExit, subsamplebam.main)
def test_sort_and_print_entries_uses_PATH_algorithm_with_path_option_true_to_properly_sort_OS_generated_path_names( ): with patch(mock_print) as p: # tmp/a1/path1 # tmp/a1 (1)/path1 # tmp/a23/path1 # tmp/a57/path2 # tmp/a64/path1 # tmp/a64/path2 # tmp/a130/path1 sort_and_print_entries(entries, Args(None, None, False, True, False)) e = [call(entries[i]) for i in [2, 3, 1, 0, 5, 6, 4]] p.assert_has_calls(e)
def test_sort_and_print_entries_uses_default_algorithm_with_all_options_false( ): with patch(mock_print) as p: # tmp/a1 (1)/path1 # tmp/a1/path1 # tmp/a23/path1 # tmp/a57/path2 # tmp/a64/path1 # tmp/a64/path2 # tmp/a130/path1 sort_and_print_entries(entries, Args(None, None, False, False, False)) e = [call(entries[i]) for i in [3, 2, 1, 0, 5, 6, 4]] p.assert_has_calls(e)
def test_main_passes_default_arguments_with_no_command_line_options(): with patch('natsort.__main__.sort_and_print_entries') as p: sys.argv[1:] = ['num-2', 'num-6', 'num-1'] main() args = p.call_args[0][1] assert not args.paths assert args.filter is None assert args.reverse_filter is None assert args.exclude is None assert not args.reverse assert args.number_type == 'int' assert not args.signed assert args.exp assert not args.locale
def test_randomly_selects_correct_amount_of_items(self, mpopen, mpool): mpopen.return_value.poll.return_value = None mpopen.return_value.stdout.__iter__.return_value = self.samview_lines[ 1:] mpopen.return_value.stdout.readline.return_value = self.samview_lines[ 0] mpool.return_value.map = map with mock.patch('subsamplebam.sys') as msys: r = subsamplebam.subselect_from_bam( 'foo.bam', 10, 'chr1:1-10', ) self.assertEqual(set(self.samview_lines), r)
def test_handles_missing_depth_error(self, mpopen, mpool): mpopen.return_value.poll.return_value = None mpopen.return_value.stdout.__iter__.return_value = self.samview_lines[ 1:] mpopen.return_value.stdout.readline.return_value = self.samview_lines[ 0] mpool.return_value.map = map with mock.patch('subsamplebam.sys') as msys: r = subsamplebam.subselect_from_bam( 'foo.bam', 11, 'chr1:1-10', ) self.assertEqual(set([]), r) for i in range(1, 11): msys.stderr.write.assert_has_call( 'Depth for chr1:{0}-{0} is only 10\n'.format(i))
def test_main_passes_arguments_with_all_command_line_options(): with patch('natsort.__main__.sort_and_print_entries') as p: sys.argv[1:] = ['--paths', '--reverse', '--locale', '--filter', '4', '10', '--reverse-filter', '100', '110', '--number-type', 'float', '--noexp', '--sign', '--exclude', '34', '--exclude', '35', 'num-2', 'num-6', 'num-1'] main() args = p.call_args[0][1] assert args.paths assert args.filter == [(4.0, 10.0)] assert args.reverse_filter == [(100.0, 110.0)] assert args.exclude == [34, 35] assert args.reverse assert args.number_type == 'float' assert args.signed assert not args.exp assert args.locale
def test_main_passes_arguments_with_all_command_line_options(): with patch('natsort.__main__.sort_and_print_entries') as p: sys.argv[1:] = [ '--paths', '--reverse', '--locale', '--filter', '4', '10', '--reverse-filter', '100', '110', '--number-type', 'float', '--noexp', '--sign', '--exclude', '34', '--exclude', '35', 'num-2', 'num-6', 'num-1' ] main() args = p.call_args[0][1] assert args.paths assert args.filter == [(4.0, 10.0)] assert args.reverse_filter == [(100.0, 110.0)] assert args.exclude == [34, 35] assert args.reverse assert args.number_type == 'float' assert args.signed assert not args.exp assert args.locale