def test_percentage(self, mock_from_file, mock_Pool, mock_cpu_count): files = ['file%d' % i for i in range(5)] out = six.StringIO() mock_Pool.return_value.map.return_value = [] mock_from_file.side_effect = self.make_results() file_indexer.main(files, out, percentages=True) self.assertEqual( out.getvalue(), 'Total number of words: 750\n' 'Total distinct words: 5\n' '\n' 'Top 5 word(s):\n' ' word1: 33.33%\n' ' word2: 26.67%\n' ' word3: 20.00%\n' ' word4: 13.33%\n' ' word5: 6.67%\n') self.assertFalse(mock_cpu_count.called) self.assertFalse(mock_Pool.called) mock_from_file.assert_has_calls([mock.call(fname) for fname in files]) self.assertEqual(mock_from_file.call_count, len(files))
def test_workers_specified(self, mock_from_file, mock_Pool, mock_cpu_count): files = ['file%d' % i for i in range(5)] out = six.StringIO() mock_Pool.return_value.map.return_value = self.make_results() mock_from_file.side_effect = lambda x: file_indexer.Histogram() file_indexer.main(files, out, workers=7) self.assertEqual( out.getvalue(), 'Total number of words: 750\n' 'Total distinct words: 5\n' '\n' 'Top 5 word(s):\n' ' word1: 250\n' ' word2: 200\n' ' word3: 150\n' ' word4: 100\n' ' word5: 50\n') self.assertFalse(mock_cpu_count.called) mock_Pool.assert_called_once_with(7) mock_Pool.return_value.map.assert_called_once_with( file_indexer.histogram_from_file, files) self.assertFalse(mock_from_file.called)
def test_with_stdin(self, mock_from_file, mock_Pool, mock_cpu_count): files = ['file0', '-', 'file2', 'file3', 'file4'] expected = ['file0', None, 'file2', 'file3', 'file4'] out = six.StringIO() mock_Pool.return_value.map.return_value = [] mock_from_file.side_effect = self.make_results() file_indexer.main(files, out) self.assertEqual( out.getvalue(), 'Total number of words: 750\n' 'Total distinct words: 5\n' '\n' 'Top 5 word(s):\n' ' word1: 250\n' ' word2: 200\n' ' word3: 150\n' ' word4: 100\n' ' word5: 50\n') self.assertFalse(mock_cpu_count.called) self.assertFalse(mock_Pool.called) mock_from_file.assert_has_calls( [mock.call(fname) for fname in expected]) self.assertEqual(mock_from_file.call_count, len(expected))