|
| 1 | +from google_images_download import google_images_download |
| 2 | +import os |
| 3 | +import json |
| 4 | +import time |
| 5 | + |
| 6 | +from .test_google_images_download import silent_remove_of_file |
| 7 | + |
| 8 | + |
| 9 | +def test_metadata(): |
| 10 | + start_time = time.time() |
| 11 | + output_folder_path = os.path.join(os.path.realpath('.'), 'logs') |
| 12 | + |
| 13 | + keywords = ["Polar bears", "ほげ"] |
| 14 | + for keyword in keywords: |
| 15 | + argumnets = { |
| 16 | + "keywords": keyword, |
| 17 | + "limit": 5, |
| 18 | + "no_download": True, |
| 19 | + "extract_metadata": True |
| 20 | + } |
| 21 | + response = google_images_download.googleimagesdownload() |
| 22 | + response.download(argumnets) |
| 23 | + with open(os.path.join(output_folder_path, '{}.json'.format(keyword)), 'r') as fp: |
| 24 | + for item in json.load(fp): |
| 25 | + assert keyword.lower() in item['image_description'].lower() |
| 26 | + |
| 27 | + argumnets = { |
| 28 | + "keywords": "ほげ", |
| 29 | + "limit": 5, |
| 30 | + "no_download": True, |
| 31 | + "extract_metadata": True |
| 32 | + } |
| 33 | + response = google_images_download.googleimagesdownload() |
| 34 | + response.download(argumnets) |
| 35 | + |
| 36 | + files_modified_after_test_started = [name for name in os.listdir(output_folder_path) if os.path.isfile(os.path.join(output_folder_path, name)) and os.path.getmtime(os.path.join(output_folder_path, name)) > start_time] |
| 37 | + print(f"Cleaning up all files downloaded by test {__name__}...") |
| 38 | + for file in files_modified_after_test_started: |
| 39 | + if silent_remove_of_file(os.path.join(output_folder_path, file)): |
| 40 | + print(f"Deleted {os.path.join(output_folder_path, file)}") |
| 41 | + else: |
| 42 | + print(f"Failed to delete {os.path.join(output_folder_path, file)}") |
0 commit comments