def test_write_index_file(self): """ http://stackoverflow.com/questions/977491/comparing-2-txt-files-using-difflib-in-python http://doughellmann.com/2007/10/pymotw-difflib.html """ img_urls = logpuzzle.read_urls('animal_code.google.com') dest_dir = './puzzle_images' logpuzzle.download_images(img_urls, dest_dir) logpuzzle.write_index_file(dest_dir) index_file = open('./index.html', 'r') result_lines = index_file.readlines() index_file.close() index_expected_file = open('./index_expected.html', 'r') expected_result_lines = index_expected_file.readlines() index_expected_file.close() # put expected_result_lines first, to make diff +- clearer diff_unified = difflib.unified_diff(expected_result_lines, result_lines, lineterm='') result = ('\n'.join(list(diff_unified))) expected_result = '' self.assertEqual(expected_result, result, 'write_index_file() expected {} but got {}'.format(expected_result, result)) self.clean_up_image_dir()
def test_read_urls_place(self): results = logpuzzle.read_urls('place_code.google.com') expected_result = 200 self.assertEqual(expected_result, len(results), 'expected {} but got {}'.format( expected_result, len(results))) test_index = 0 expected_result_index = 1 test_datas = [ [0, 'http://code.google.com/edu/languages/google-python-class/images/puzzle/p-baea-baaa.jpg'], [199, 'http://code.google.com/edu/languages/google-python-class/images/puzzle/p-bhdf-bbjj.jpg'], ] for test_data in test_datas: result = results[test_data[test_index]] expected_result = test_data[expected_result_index] self.assertEqual(expected_result, result, 'read_urls()[{}] expected {} but got {}'.format(test_index, expected_result, result))
def test_z_download_images(self): """ Choose test name alphabetically after test_write_index_file to run after it. """ #img_urls = logpuzzle.read_urls('place_code.google.com') img_urls = logpuzzle.read_urls('animal_code.google.com') dest_dir = './puzzle_images' logpuzzle.download_images(img_urls, dest_dir) result = os.listdir(dest_dir) expected_result = ['img0.jpg', 'img1.jpg', 'img10.jpg', 'img11.jpg', 'img12.jpg', 'img13.jpg', 'img14.jpg', 'img15.jpg', 'img16.jpg', 'img17.jpg', 'img18.jpg', 'img19.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg', 'img5.jpg', 'img6.jpg', 'img7.jpg', 'img8.jpg', 'img9.jpg'] self.assertEqual(expected_result, result, 'write_index_file() expected {} but got {}'.format(expected_result, result))