"Fossies" - the Fresh Open Source Software Archive 
Member "googler-4.3.2/tests/test_googler.py" (21 Jan 2021, 3623 Bytes) of package /linux/misc/googler-4.3.2.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the latest
Fossies "Diffs" side-by-side code changes report for "test_googler.py":
4.3.1_vs_4.3.2.
1 import json
2 import os
3 import pathlib
4 import re
5 import subprocess
6
7 import pytest
8
9
10 ROOT = pathlib.Path(__file__).parent.parent
11 GOOGLER = ROOT / "googler"
12
13 # Load preset options from the environment, in case the testing
14 # environment need options like --ipv4 or --proxy to connect.
15 PRESET_OPTIONS = os.getenv("GOOGLER_PRESET_OPTIONS", "").split()
16
17
18 class GooglerResults:
19 def __init__(self, argv):
20 self.argv = argv
21 json_output = subprocess.check_output(
22 [str(GOOGLER), *PRESET_OPTIONS, "--debug", "--json", *argv]
23 ).decode("utf-8")
24 self.results = json.loads(json_output)
25 assert self.results, "no results"
26
27 def all_should(self, predicate):
28 # Using a loop for better error reporting.
29 for result in self.results:
30 assert predicate(result)
31
32 def some_should(self, predicate):
33 assert any(map(predicate, self.results))
34
35
36 GR = GooglerResults
37
38
39 @pytest.mark.parametrize("query", ["english", "中文"])
40 def test_default_search(query):
41 def have_url_title_and_abstract(result):
42 return bool(
43 result.get("url") and result.get("title") and ("abstract" in result)
44 )
45
46 def have_matched_keywords(result):
47 return bool(result.get("matches"))
48
49 gr = GR([query])
50 gr.all_should(have_url_title_and_abstract)
51 gr.some_should(have_matched_keywords)
52
53
54 def test_news_search():
55 def have_metadata(result):
56 return bool(result.get("metadata"))
57
58 def have_time_info_in_metadata(result):
59 return re.search(r"(hour|days)? ago", result.get("metadata", "")) is not None
60
61 gr = GR(["--news", "--lang=en", "google"])
62 gr.all_should(have_metadata)
63 gr.some_should(have_time_info_in_metadata)
64
65
66 def test_videos_search():
67 def be_from_youtube(result):
68 return re.match(r"https://(www\.)?youtube.com/", result["url"]) is not None
69
70 def have_uploader_in_metadata(result):
71 return "Uploaded by" in result.get("metadata", "")
72
73 gr = GR(["--videos", "--lang=en", "olympics youtube"])
74 gr.some_should(be_from_youtube)
75 gr.some_should(have_uploader_in_metadata)
76
77
78 def test_site_search():
79 def be_from_wikipedia(result):
80 return result["url"].startswith("https://en.wikipedia.org")
81
82 GR(["--site=en.wikipedia.org", "google"]).all_should(be_from_wikipedia)
83 GR(["site:en.wikipedia.org google"]).all_should(be_from_wikipedia)
84
85
86 @pytest.mark.parametrize("tld", ["in", "de"])
87 def test_tld_option(tld):
88 # Just a lame test to make sure there are results.
89 GR(["--tld", tld, "google"])
90
91
92 def test_exact_option():
93 def have_gogole_in_title_or_abstract(result):
94 return (
95 "gogole" in result["title"].lower()
96 or "gogole" in result["abstract"].lower()
97 )
98
99 gr = GR(["--exact", "gogole"])
100 gr.some_should(have_gogole_in_title_or_abstract)
101
102
103 def test_time_option():
104 def have_time_in_metadata(result):
105 return (
106 re.search(r"hours?|days?|(?P<year>\b\d{4}\b)", result.get("metadata", ""))
107 is not None
108 )
109
110 gr = GR(["--time=y1", "--lang=en", "google"])
111 gr.some_should(have_time_in_metadata)
112
113
114 def test_from_to_options():
115 def have_year_2019_in_metadata(result):
116 return "2019" in result.get("metadata", "")
117
118 gr = GR(["--from=01/01/2019", "--to=12/31/2019", "--lang=en", "google"])
119 # One would expect all results to have 2019 in metadata, but
120 # sometimes some results just don't have that line for whatever reason:
121 # https://github.com/zmwangx/googler/runs/704110651?check_suite_focus=true
122 gr.some_should(have_year_2019_in_metadata)