def test_invalid_chars_are_removed(self): print(inspect.stack()[0][3]) fake_file_path = "/fake/file/path" lines = ["%.,", ",,", "alpha", " \" " ] # the last empty line ensures that we have a final '\n' fake_file = io.StringIO('\n'.join(lines)) # Execute main and be sure you capture the output but use mocking to fake a file read by the program with io.StringIO() as buf, redirect_stdout(buf): with patch('word_index.open', return_value=fake_file, create=True): wi_main(fake_file_path) output = buf.getvalue() output_lines = list(filter( None, output.split('\n'))) # Split over '\n' but remove empty strings expected_n_lines = 1 expected_output_string = "alpha - 1\n" self.assertEqual(expected_n_lines, len(output_lines), msg="Output has wrong number of lines") self.assertEqual(expected_output_string, output, msg="Wrong output")
def test_program_with_few_lines(self): # Print name of this test print(inspect.stack()[0][3]) fake_file_path = "/fake/file/path" lines = ["foo", "bar", ""] # the last empty line ensures that we have a final '\n' fake_file = io.StringIO('\n'.join(lines)) # Create the expectation using the oracle_main with patch('monolithic_word_index.open', return_value=fake_file, create=True): expected_output = oracle_main(fake_file_path) # Create the actual output using the wi_main fake_file = io.StringIO('\n'.join(lines)) with io.StringIO() as buf, redirect_stdout(buf): with patch('word_index.open', return_value=fake_file, create=True): wi_main(fake_file_path) output = buf.getvalue() # Assert that the two output matches self.assertEqual(output, expected_output, msg="Output does not match")
def test_output_with_a_mocked_file(self): print(inspect.stack()[0][3]) fake_file_path = "/fake/file/path" lines = ["foo", "bar"] fake_file = io.StringIO('\n'.join(lines)) # Execute main and be sure you capture the output but use mocking to fake a file read by the program with io.StringIO() as buf, redirect_stdout(buf): # Moking in action with patch('word_index.open', return_value=fake_file, create=True): wi_main(fake_file_path) output = buf.getvalue() # Check the output is not None self.assertIsNotNone(output, msg="Output cannot be None") # Check the output is not empty self.assertNotEqual(output, '', msg="Output cannot be empty") # Check output is as we expect self.assertEqual(output, 'bar - 1\nfoo - 1\n', msg="Output cannot be empty")
def test_frequent_word_is_filtered(self): print(inspect.stack()[0][3]) # Execute main and be sure you capture the output with io.StringIO() as buf, redirect_stdout(buf): wi_main(os.path.join(TEST_DATA, './test.txt')) output = buf.getvalue() # Check the output is the expected one. # https://kapeli.com/cheat_sheets/Python_unittest_Assertions.docset/Contents/Resources/Documents/index self.assertNotIn("Line", output, msg="Output contains filtered word")
def test_output_with_another_one_line_file(self): print(inspect.stack()[0][3]) # Execute main and be sure you capture the output with io.StringIO() as buf, redirect_stdout(buf): wi_main(os.path.join(TEST_DATA, './one-line.txt')) output = buf.getvalue() # Check the output is the expected one. # Q: Do we care about the last '\n' ? self.assertEqual(output, "blablba - 1\n", msg="Output cannot be empty")
def test_output_with_an_empty_file_produces_not_output(self): print(inspect.stack()[0][3]) # Print the name of THIS function # Execute main and be sure you capture the output with io.StringIO() as buf, redirect_stdout(buf): wi_main(os.path.join(TEST_DATA, './empty.txt')) output = buf.getvalue() # Check the output is not None self.assertIsNotNone(output, msg="Output cannot be None") # Check the output is not empty self.assertEqual(output, '', msg="Output is not empty")
def test_output_is_not_none(self): # Execute wi_main() # Copy the value to a string. Note that at this point you cannot print anything to console, # use the debugger instead output = self.capturedOutput.getvalue() # Check the output is not None self.assertIsNotNone(output, msg="Output cannot be None") # Check the output is not empty self.assertNotEqual(output, '', msg="Output cannot be empty")
def test_output_with_another_existing_file(self): print(inspect.stack()[0][3]) # Execute main and be sure you capture the output with io.StringIO() as buf, redirect_stdout(buf): wi_main('./long.txt') output = buf.getvalue() # Check the output is not None self.assertIsNotNone(output, msg="Output cannot be None") # Check the output is not empty self.assertNotEqual(output, '', msg="Output cannot be empty")
def test_program_with_sample_file(self): # Print name of this test print(inspect.stack()[0][3]) real_file_path = "../test-data/sample.txt" expected_output = oracle_main(real_file_path) with io.StringIO() as buf, redirect_stdout(buf): wi_main(real_file_path) output = buf.getvalue() # Assert that the two output matches self.assertEqual(output, expected_output, msg="Output does not match")
def test_program_with_few_lines(self): # Print name of this test print(inspect.stack()[0][3]) temp_file = os.path.join(self.test_dir.name, "input.txt") with open(temp_file, 'w') as f: f.writelines('\n'.join(["foo", "bar"])) expected_output = '\n'.join(["bar - 1", "foo - 1", ""]) with io.StringIO() as buf, redirect_stdout(buf): # Invoke main passing the temp file wi_main(temp_file) output = buf.getvalue() # Make the assertions self.assertEqual(output, expected_output, msg="Output does not match")
def test_output_with_empty_file(self): print(inspect.stack()[0][3]) fake_file_path = "/fake/file/path" lines = [""] # the last empty line ensures that we have a final '\n' fake_file = io.StringIO('\n'.join(lines)) # Execute main and be sure you capture the output but use mocking to fake a file read by the program with io.StringIO() as buf, redirect_stdout(buf): with patch('word_index.open', return_value=fake_file, create=True): wi_main(fake_file_path) output = buf.getvalue() # Check the output is not None self.assertIsNotNone(output, msg="Output cannot be None") # Check the output is empty self.assertEqual(output, '', msg="Output is not empty")
def test_that_overly_recurrent_words_are_not_reported(self): # Print name of this test print(inspect.stack()[0][3]) temp_file = os.path.join(self.test_dir.name, "input.txt") with open(temp_file, 'w') as f: f.writelines('\n'.join(["foo", "bar", "bar", "bar", "bar"])) expected_output = '\n'.join(["foo - 1", ""]) with io.StringIO() as buf, redirect_stdout(buf): # Invoke main passing the temp file. wi_main(temp_file, STOP_FREQUENCY_LIMIT=3) output = buf.getvalue() # Make the assertions self.assertEqual(output, expected_output, msg="Output does not match")
def test_program_with_many_lines(self): # Print name of this test print(inspect.stack()[0][3]) fake_file_path = "/fake/file/path" lines = [] # This should appear on output for i in range(1, 100): lines.append("foo") # This should not appear on output for i in range(1, 101): lines.append("bar") lines.append("") fake_file = io.StringIO('\n'.join(lines)) # Create the expectation using the oracle_main with patch('monolithic_word_index.open', return_value=fake_file, create=True): expected_output = oracle_main(fake_file_path) # Create the actual output using the wi_main fake_file = io.StringIO('\n'.join(lines)) with io.StringIO() as buf, redirect_stdout(buf): with patch('word_index.open', return_value=fake_file, create=True): wi_main(fake_file_path) output = buf.getvalue() # Assert that the two output matches self.assertEqual(output, expected_output, msg="Output does not match")