File size: 2,679 Bytes
8628def
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import pathlib
import shutil
import time
from pathlib import Path

import pytest
from click.testing import CliRunner

from ddgs import DDGS, __version__
from ddgs.cli import _download_results, _save_csv, _save_json, cli

runner = CliRunner()
TEXT_RESULTS = []
IMAGES_RESULTS = []


@pytest.fixture(autouse=True)
def pause_between_tests() -> None:
    time.sleep(2)


def test_version_command() -> None:
    result = runner.invoke(cli, ["version"])
    assert result.output.strip() == __version__


def test_text_command() -> None:
    result = runner.invoke(cli, ["text", "-q", "zebra"])
    assert "title" in result.output


def test_images_command() -> None:
    result = runner.invoke(cli, ["images", "-q", "fox"])
    assert "title" in result.output


def test_news_command() -> None:
    result = runner.invoke(cli, ["news", "-q", "deer"])
    assert "title" in result.output


def test_videos_command() -> None:
    result = runner.invoke(cli, ["videos", "-q", "pig"])
    assert "title" in result.output


def test_books_command() -> None:
    result = runner.invoke(cli, ["books", "-q", "bee"])
    assert "title" in result.output


@pytest.mark.dependency()
def test_get_text() -> None:
    global TEXT_RESULTS
    TEXT_RESULTS = DDGS().text("cow", max_results=5)
    assert TEXT_RESULTS


@pytest.mark.dependency()
def test_get_images() -> None:
    global IMAGES_RESULTS
    IMAGES_RESULTS = DDGS().images("horse", max_results=5)
    assert IMAGES_RESULTS


@pytest.mark.dependency(depends=["test_get_text"])
def test_save_csv(tmp_path: Path) -> None:
    temp_file = tmp_path / "test_csv.csv"
    _save_csv(temp_file, TEXT_RESULTS)
    assert temp_file.exists()


@pytest.mark.dependency(depends=["test_get_text"])
def test_save_json(tmp_path: Path) -> None:
    temp_file = tmp_path / "test_json.json"
    _save_json(temp_file, TEXT_RESULTS)
    assert temp_file.exists()


@pytest.mark.dependency(depends=["test_get_text"])
def test_text_download() -> None:
    pathname = pathlib.Path("text_downloads")
    _download_results(f"{test_text_download}", TEXT_RESULTS, function_name="text", pathname=str(pathname))
    assert pathname.is_dir() and pathname.iterdir()
    for file in pathname.iterdir():
        assert file.is_file()
    shutil.rmtree(str(pathname))


@pytest.mark.dependency(depends=["test_get_images"])
def test_images_download() -> None:
    pathname = pathlib.Path("images_downloads")
    _download_results(f"{test_images_download}", IMAGES_RESULTS, function_name="images", pathname=str(pathname))
    assert pathname.is_dir() and pathname.iterdir()
    for file in pathname.iterdir():
        assert file.is_file()
    shutil.rmtree(str(pathname))