forked from joshcarty/google-searchconsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
215 lines (162 loc) · 7.45 KB
/
tests.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# encoding: utf-8
import unittest
import doctest
import datetime
import os
import searchconsole
from auth.creds import webproperty_uri
class TestAuthentication(unittest.TestCase):
""" Test whether authentication procedure works. Currently
uses client_secrets and credentials files saved in ./auth
directory of repository.
"""
def test_mappings(self):
""" Test whether a webmasters service can be created using
Google format client_config and credentials mappings. """
from auth.creds import client_secrets, credentials
account = searchconsole.authenticate(
client_config=client_secrets,
credentials=credentials
)
self.assertIsInstance(account, searchconsole.account.Account)
def test_files(self):
""" Test whether a webmasters service can be created using
a Google format client secrets and credentials file. """
account = searchconsole.authenticate(
client_config='auth/client_secrets.json',
credentials='auth/credentials.dat'
)
self.assertIsInstance(account, searchconsole.account.Account)
def test_serialize_credentials(self):
""" Test whether a credentials object can serialized."""
serialized_file = 'auth/webmasters.dat'
account = searchconsole.authenticate(
client_config='auth/client_secrets.json',
credentials='auth/credentials.dat',
serialize=serialized_file
)
serialized_file_exists = os.path.isfile(serialized_file)
self.assertTrue(serialized_file_exists)
serialized_account = searchconsole.authenticate(
client_config='auth/client_secrets.json',
credentials=serialized_file,
)
self.assertIsInstance(serialized_account, searchconsole.account.Account)
os.remove(serialized_file)
class AuthenticatedTestCase(unittest.TestCase):
"""Base test authenticated using file-based client secrets and
credentials."""
def setUp(self):
self.account = searchconsole.authenticate(
client_config='auth/client_secrets.json',
credentials='auth/credentials.dat'
)
self.webproperty = self.account[webproperty_uri]
self.query = self.webproperty.query
class TestAccount(AuthenticatedTestCase):
""" Test whether properties of a Search Console account can
be accessed: e.g. web properties. """
def test_indexing(self):
""" Test whether an account can be indexed by a number or
by the full URL of a web property. """
a = self.account[0]
b = self.account[a.url]
self.assertEqual(a, b)
class TestQuerying(AuthenticatedTestCase):
""" Test whether users can query Search Analytics from a web
property in Search Console. """
def test_query(self):
"""It should be able to run a query and return a report. """
q = self.query.dimension('date').range('yesterday', days=-7)
report = q.get()
self.assertTrue(report.rows)
def test_multiple_dimensions(self):
""" It should return more rows for multiple dimensions. This addresses issues
noted here: https://productforums.google.com/forum/#!msg/webmasters/PKNGqSo1t
Kc/lAE0hcdGCQAJ """
a = self.query.range('today', days=-7).dimension('query').get()
b = self.query.range('today', days=-7).dimension('query', 'date').get()
self.assertGreater(len(b), len(a))
def test_range(self):
""" It should handle different date types. """
a = self.query.range(start='2017-01-01', stop='2017-01-03')
b = self.query.range(start=datetime.date(2017, 1, 1), stop=datetime.date(2017, 1, 3))
self.assertEqual(a.raw['startDate'], '2017-01-01')
self.assertEqual(b.raw['startDate'], '2017-01-01')
self.assertEqual(a.raw['startDate'], b.raw['startDate'])
def test_range_days(self):
""" It should handle a day offset from a start or stop date. """
a = self.query.range(start='2017-01-01', stop='2017-01-03')
b = self.query.range(start='2017-01-01', days=3)
self.assertEqual(a.raw['endDate'], '2017-01-03')
self.assertEqual(b.raw['endDate'], '2017-01-03')
self.assertEqual(a.raw['endDate'], b.raw['endDate'])
def test_range_months(self):
""" It should handle a month offset from a start or stop date. """
a = self.query.range(start='2017-01-01', stop='2017-01-31')
b = self.query.range(start='2017-01-01', months=1)
self.assertEqual(a.raw['endDate'], '2017-01-31')
self.assertEqual(b.raw['endDate'], '2017-01-31')
self.assertEqual(a.raw['endDate'], b.raw['endDate'])
def test_descriptions(self):
""" It should handle some convenient date strings. """
yesterday = datetime.date.today() - datetime.timedelta(days=1)
a = self.query.range('yesterday', days=-1)
b = self.query.range(yesterday, days=-1)
self.assertEqual(a.raw['endDate'], b.raw['endDate'])
def test_search_type(self):
""" It should be able to filter for the specific search type. """
a = self.query.search_type('image')
self.assertEqual(a.raw['type'], 'image')
def test_search_type_metrics(self):
""" Certain search types should not return position """
a = self.query.range('yesterday', days=-7).get()
b = self.query.search_type('googleNews').range('yesterday', days=-7).get()
self.assertTrue(hasattr(a.Row, 'position'))
self.assertFalse(hasattr(b.Row, 'position'))
def test_immutable(self):
""" Queries should be refined by creating a new query instance not
by modifying the base query. """
a = self.query.dimension('date')
b = a.range('2017-11-01', '2017-11-03')
self.assertNotEqual(a, b)
self.assertNotEqual(a.raw, b.raw)
def test_limit(self):
""" It can limit the total amount of results. """
q = self.query.range('yesterday', days=-7).dimension('date')
full_report = q.get()
limited_report = q.limit(2).get()
self.assertEqual(len(limited_report.rows), 2)
self.assertEqual(len(limited_report), 2)
self.assertEqual(full_report.rows[:2], limited_report.rows[:2])
def test_start_limit(self):
""" It can limit the amount of results and the index at which
to start. """
q = self.query.range('yesterday', days=-7).dimension('date')
full_report = q.get()
limited_report = q.limit(2, 2).get()
self.assertEqual(len(limited_report), 2)
self.assertEqual(full_report[2:4], limited_report.rows)
def load_tests(loader, tests, ignore):
""" Many docstrings contain doctests. Instead of using a separate doctest
runner, we use doctest's Unittest API."""
account = searchconsole.authenticate(
client_config='auth/client_secrets.json',
credentials='auth/credentials.dat'
)
globs = {
'account': account,
'webproperty': account[webproperty_uri],
'www_webproperty_com': webproperty_uri,
'query': account[webproperty_uri].query
}
kwargs = {
'globs': globs,
'optionflags': doctest.ELLIPSIS
}
tests.addTests(doctest.DocTestSuite(searchconsole.auth, **kwargs))
tests.addTests(doctest.DocTestSuite(searchconsole.account, **kwargs))
tests.addTests(doctest.DocTestSuite(searchconsole.query, **kwargs))
return tests
if __name__ == '__main__':
unittest.main()