def test_errors(): logging.configure('DEBUG') with pytest.raises(KnownError): with tempfile.TemporaryDirectory() as temp_dir: load_page(INCORRECT_URL, temp_dir) with pytest.raises(KnownError): load_page(TEST_URL, INCORRECT_PASS)
def test_create_file(): logging.configure('DEBUG') with tempfile.TemporaryDirectory() as temp_dir: with requests_mock.Mocker() as m: m.get(TEST_URL, text='test') load_page(TEST_URL, temp_dir) full_path = os.path.join(temp_dir, TEMP_FILE_NAME) assert os.path.exists(full_path) == True
def test_create_dir(): logging.configure('DEBUG') with tempfile.TemporaryDirectory() as temp_dir: with requests_mock.Mocker() as m: m.get(TEST_URL, text='test') load_page(TEST_URL, temp_dir) resource_dir = os.path.join(temp_dir, CORRECT_DIR) assert os.path.isdir(resource_dir) == True
def test_html(): logging.configure('DEBUG') with open(CORRECT_FILE, 'r') as correct_file: correct_result = correct_file.read() with tempfile.TemporaryDirectory() as temp_dir: load_page(TEST_URL, temp_dir) full_path = os.path.join(temp_dir, TEMP_FILE_NAME) with open(full_path, 'r') as test_file: test_result = test_file.read() assert correct_result == test_result
def main(): parser = argparse.ArgumentParser(description='Page loader') parser.add_argument('url', type=str) parser.add_argument('-o', '--output', help='set path for saving', type=str) parser.add_argument( '-l', '--log_level', help='set logging level from DEBUG, INFO, WARNING, ERROR, CRITICAL', type=str, choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], default='WARNING') args = parser.parse_args() logging.configure(args.log_level) try: load_page(args.url, args.output) except KnownError: sys.exit(1)