Skip to content

Commit 89bc24c

Browse files
committed
Improve networked tests
1 parent a13b33f commit 89bc24c

File tree

6 files changed

+60
-60
lines changed

6 files changed

+60
-60
lines changed

test/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
config.before_record(callback=sanitize_cookies)
2424

2525
Betamax.register_serializer(PrettyJSONSerializer)
26-
session = Session()
27-
recorder = Betamax(session, default_cassette_options={'serialize_with': 'prettyjson'})
2826

2927

3028
class NetworkedTest(unittest.TestCase):
3129
"""Class to represent tests that perform network requests"""
3230

3331
@classmethod
3432
def setUpClass(cls):
35-
cls.gc = Geocaching(session=session)
33+
cls.session = Session()
34+
cls.recorder = Betamax(cls.session, default_cassette_options={'serialize_with': 'prettyjson'})
35+
cls.gc = Geocaching(session=cls.session)
3636
try:
3737
cls.gc.login(username, password)
3838
except Error:
@@ -43,4 +43,4 @@ def setUpClass(cls):
4343
# because if we are recording new cassettes this means we cannot get
4444
# properly logged in.
4545
cls.gc._logged_in = True # we're gonna trick it
46-
cls.gc._session = session # it got redefined; fix it
46+
cls.gc._session = cls.session # it got redefined; fix it

test/test_cache.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pycaching.geocaching import Geocaching
1010
from pycaching.log import Log, Type as LogType
1111
from pycaching.util import parse_date
12-
from . import recorder, NetworkedTest
12+
from . import NetworkedTest
1313

1414

1515
class TestProperties(unittest.TestCase):
@@ -169,49 +169,49 @@ class TestMethods(NetworkedTest):
169169
def setUpClass(cls):
170170
super().setUpClass()
171171
cls.c = Cache(cls.gc, "GC1PAR2")
172-
with recorder.use_cassette('cache_setup'):
172+
with cls.recorder.use_cassette('cache_setup'):
173173
cls.c.load()
174174

175175
def test_load(self):
176176
with self.subTest("normal (with explicit call of load())"):
177-
with recorder.use_cassette('cache_explicit_load'):
177+
with self.recorder.use_cassette('cache_explicit_load'):
178178
cache = Cache(self.gc, "GC4808G")
179179
cache.load()
180180
self.assertEqual("Nekonecne ticho", cache.name)
181181

182182
with self.subTest("normal"):
183-
with recorder.use_cassette('cache_normal_normal'):
183+
with self.recorder.use_cassette('cache_normal_normal'):
184184
cache = Cache(self.gc, "GC4808G")
185185
self.assertEqual("Nekonecne ticho", cache.name)
186186

187187
with self.subTest("non-ascii chars"):
188-
with recorder.use_cassette('cache_non-ascii'):
188+
with self.recorder.use_cassette('cache_non-ascii'):
189189
cache = Cache(self.gc, "GC5VJ0P")
190190
self.assertEqual("u parezové chaloupky", cache.hint)
191191

192192
with self.subTest("PM only"):
193-
with recorder.use_cassette('cache_PMO'):
193+
with self.recorder.use_cassette('cache_PMO'):
194194
with self.assertRaises(PMOnlyException):
195195
cache = Cache(self.gc, "GC3AHDM")
196196
cache.load()
197197

198198
with self.subTest("fail"):
199-
with recorder.use_cassette('cache_normal_fail'):
199+
with self.recorder.use_cassette('cache_normal_fail'):
200200
with self.assertRaises(LoadError):
201201
cache = Cache(self.gc, "GC123456")
202202
cache.load()
203203

204204
def test_load_quick(self):
205205
with self.subTest("normal"):
206-
with recorder.use_cassette('cache_quick_normal'):
206+
with self.recorder.use_cassette('cache_quick_normal'):
207207
cache = Cache(self.gc, "GC4808G")
208208
cache.load_quick()
209209
self.assertEqual(4, cache.terrain)
210210
self.assertEqual(Size.regular, cache.size)
211211
self.assertEqual(cache.guid, "15ad3a3d-92c1-4f7c-b273-60937bcc2072")
212212

213213
with self.subTest("fail"):
214-
with recorder.use_cassette('cache_quickload_fail'):
214+
with self.recorder.use_cassette('cache_quickload_fail'):
215215
with self.assertRaises(LoadError):
216216
cache = Cache(self.gc, "GC123456")
217217
cache.load_quick()
@@ -221,7 +221,7 @@ def test_load_quick(self):
221221
def test_load_by_guid(self, mock_load_quick, mock_load):
222222
with self.subTest("normal"):
223223
cache = Cache(self.gc, "GC2WXPN", guid="5f45114d-1d79-4fdb-93ae-8f49f1d27188")
224-
with recorder.use_cassette('cache_guidload_normal'):
224+
with self.recorder.use_cassette('cache_guidload_normal'):
225225
cache.load_by_guid()
226226
self.assertEqual(cache.name, "Der Schatz vom Luftschloss")
227227
self.assertEqual(cache.location, Point("N 49° 57.895' E 008° 12.988'"))
@@ -247,25 +247,25 @@ def test_load_by_guid(self, mock_load_quick, mock_load):
247247

248248
with self.subTest("PM-only"):
249249
cache = Cache(self.gc, "GC6MKEF", guid="53d34c4d-12b5-4771-86d3-89318f71efb1")
250-
with recorder.use_cassette('cache_guidload_PMO'):
250+
with self.recorder.use_cassette('cache_guidload_PMO'):
251251
with self.assertRaises(PMOnlyException):
252252
cache.load_by_guid()
253253

254254
with self.subTest("calls load_quick if no guid"):
255255
cache = Cache(self.gc, "GC2WXPN")
256-
with recorder.use_cassette('cache_guidload_fallback'):
256+
with self.recorder.use_cassette('cache_guidload_fallback'):
257257
with self.assertRaises(Exception):
258258
cache.load_by_guid() # Raises error since we mocked load_quick()
259259
self.assertTrue(mock_load_quick.called)
260260

261261
def test_load_trackables(self):
262262
cache = Cache(self.gc, "GC26737") # TB graveyard - will surelly have some trackables
263-
with recorder.use_cassette('cache_trackables'):
263+
with self.recorder.use_cassette('cache_trackables'):
264264
trackable_list = list(cache.load_trackables(limit=10))
265265
self.assertTrue(isinstance(trackable_list, list))
266266

267267
def test_load_logbook(self):
268-
with recorder.use_cassette('cache_logbook'):
268+
with self.recorder.use_cassette('cache_logbook'):
269269
# limit over 100 tests pagination
270270
log_authors = list(map(lambda log: log.author, self.c.load_logbook(limit=200)))
271271
for expected_author in ["Dudny-1995", "Sopdet Reviewer", "donovanstangiano83"]:
@@ -274,7 +274,7 @@ def test_load_logbook(self):
274274
def test_load_log_page(self):
275275
expected_types = {t.value for t in (LogType.found_it, LogType.didnt_find_it, LogType.note)}
276276

277-
with recorder.use_cassette('cache_logpage'):
277+
with self.recorder.use_cassette('cache_logpage'):
278278
# make request
279279
valid_types, hidden_inputs = self.c._load_log_page()
280280

test/test_geo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pycaching.errors import GeocodeError, BadBlockError
1313
from pycaching.geo import Point, Polygon, Rectangle, Tile, UTFGridPoint, Block
1414
from pycaching.geo import to_decimal
15-
from . import recorder, NetworkedTest
15+
from . import NetworkedTest
1616

1717
_sample_caches_file = path.join(path.dirname(__file__), "sample_caches.csv")
1818
_sample_utfgrid_file = path.join(path.dirname(__file__), "sample_utfgrid.json")
@@ -69,18 +69,18 @@ def test_from_location(self):
6969
ref_point = Point(50.08746, 14.42125)
7070

7171
with self.subTest("existing location"):
72-
with recorder.use_cassette('geo_location_existing'):
72+
with self.recorder.use_cassette('geo_location_existing'):
7373
self.assertLess(great_circle(Point.from_location(self.gc, "Prague"), ref_point).miles, 10)
7474
self.assertLess(great_circle(Point.from_location(self.gc, "Praha"), ref_point).miles, 10)
7575
self.assertLess(great_circle(Point.from_location(self.gc, "praha"), ref_point).miles, 10)
7676

7777
with self.subTest("non-existing location"):
78-
with recorder.use_cassette('geo_location_nonexisting'):
78+
with self.recorder.use_cassette('geo_location_nonexisting'):
7979
with self.assertRaises(GeocodeError):
8080
Point.from_location(self.gc, "qwertzuiop")
8181

8282
with self.subTest("empty request"):
83-
with recorder.use_cassette('geo_location_empty'):
83+
with self.recorder.use_cassette('geo_location_empty'):
8484
with self.assertRaises(GeocodeError):
8585
Point.from_location(self.gc, "")
8686

@@ -186,7 +186,7 @@ def setUp(self):
186186

187187
def test_download_utfgrid(self):
188188
"""Test if downloading a UTFGrid passes without errors"""
189-
with recorder.use_cassette('geo_point_utfgrid'):
189+
with self.recorder.use_cassette('geo_point_utfgrid'):
190190
with self.subTest("not getting .png tile first"):
191191
self.tile._download_utfgrid()
192192

test/test_geocaching.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
import pycaching
1414
from pycaching import Geocaching, Point, Rectangle
1515
from pycaching.errors import NotLoggedInException, LoginFailedException, PMOnlyException
16-
from . import username as _username, password as _password, recorder, session, NetworkedTest
16+
from . import username as _username, password as _password, NetworkedTest
1717

1818

1919
class TestMethods(NetworkedTest):
2020
def test_search(self):
2121
with self.subTest("normal"):
2222
tolerance = 2
2323
expected = {"GC5VJ0P", "GC41FJC", "GC17E8Y", "GC14AV5", "GC50AQ6", "GC167Y7"}
24-
with recorder.use_cassette('geocaching_search'):
24+
with self.recorder.use_cassette('geocaching_search'):
2525
found = {cache.wp for cache in self.gc.search(Point(49.733867, 13.397091), 20)}
2626
self.assertGreater(len(expected & found), len(expected) - tolerance)
2727

2828
with self.subTest("pagging"):
29-
with recorder.use_cassette('geocaching_search_pagination'):
29+
with self.recorder.use_cassette('geocaching_search_pagination'):
3030
caches = list(self.gc.search(Point(49.733867, 13.397091), 100))
3131
self.assertNotEqual(caches[0], caches[50])
3232

@@ -39,7 +39,7 @@ def test_search_quick(self):
3939
rect = Rectangle(Point(49.73, 13.38), Point(49.74, 13.40))
4040

4141
with self.subTest("normal"):
42-
with recorder.use_cassette('geocaching_quick_normal'):
42+
with self.recorder.use_cassette('geocaching_quick_normal'):
4343
# Once this feature is fixed, the corresponding cassette will have to be deleted
4444
# and re-recorded.
4545
res = [c.wp for c in self.gc.search_quick(rect)]
@@ -50,13 +50,13 @@ def test_search_quick(self):
5050
self.assertGreater(len(res), 90)
5151

5252
with self.subTest("strict handling of cache coordinates"):
53-
with recorder.use_cassette('geocaching_quick_strictness'):
53+
with self.recorder.use_cassette('geocaching_quick_strictness'):
5454
res = list(self.gc.search_quick(rect, strict=True))
5555
self.assertLess(len(res), expected_cache_num + tolerance)
5656
self.assertGreater(len(res), expected_cache_num - tolerance)
5757

5858
with self.subTest("larger zoom - more precise"):
59-
with recorder.use_cassette('geocaching_quick_zoom'):
59+
with self.recorder.use_cassette('geocaching_quick_zoom'):
6060
res1 = list(self.gc.search_quick(rect, strict=True, zoom=15))
6161
res2 = list(self.gc.search_quick(rect, strict=True, zoom=14))
6262
for res in res1, res2:
@@ -69,7 +69,7 @@ def test_search_quick(self):
6969
def test_search_quick_match_load(self):
7070
"""Test if quick search results matches exact cache locations."""
7171
rect = Rectangle(Point(49.73, 13.38), Point(49.74, 13.39))
72-
with recorder.use_cassette('geocaching_matchload'):
72+
with self.recorder.use_cassette('geocaching_matchload'):
7373
# at commit time, this test is an allowed failure. Once this feature is fixed, the
7474
# corresponding cassette will have to be deleted and re-recorded.
7575
caches = list(self.gc.search_quick(rect, strict=True, zoom=15))
@@ -83,7 +83,8 @@ def test_search_quick_match_load(self):
8383

8484
class TestLoginOperations(NetworkedTest):
8585
def setUp(self):
86-
self.gc = Geocaching(session=session)
86+
super().setUp()
87+
self.gc = Geocaching(session=self.session)
8788

8889
def test_request(self):
8990
with self.subTest("login needed"):
@@ -92,24 +93,24 @@ def test_request(self):
9293

9394
def test_login(self):
9495
with self.subTest("bad credentials"):
95-
with recorder.use_cassette('geocaching_badcreds'):
96-
session.cookies.clear()
96+
with self.recorder.use_cassette('geocaching_badcreds'):
97+
self.session.cookies.clear()
9798
with self.assertRaises(LoginFailedException):
9899
self.gc.login("0", "0")
99100

100101
with self.subTest("good credentials twice"):
101102
self.gc.logout()
102-
session.cookies.clear()
103-
self.gc._session = session # gotta reattach so we can keep listening
104-
with recorder.use_cassette('geocaching_2login'):
103+
self.session.cookies.clear()
104+
self.gc._session = self.session # gotta reattach so we can keep listening
105+
with self.recorder.use_cassette('geocaching_2login'):
105106
self.gc.login(_username, _password)
106107
self.gc.login(_username, _password)
107108

108109
with self.subTest("bad credentials automatic logout"):
109-
with recorder.use_cassette('geocaching_badcreds_logout'):
110+
with self.recorder.use_cassette('geocaching_badcreds_logout'):
110111
self.gc.logout()
111-
session.cookies.clear()
112-
self.gc._session = session # gotta reattach so we can keep listening
112+
self.session.cookies.clear()
113+
self.gc._session = self.session # gotta reattach so we can keep listening
113114
with self.assertRaises(LoginFailedException):
114115
self.gc.login("0", "0")
115116

@@ -144,16 +145,16 @@ def test_login(self):
144145
self.gc.login()
145146

146147
def test_get_logged_user(self):
147-
with recorder.use_cassette('geocaching_loggeduser'):
148+
with self.recorder.use_cassette('geocaching_loggeduser'):
148149
self.gc.login(_username, _password)
149150
self.assertEqual(self.gc.get_logged_user(), _username)
150151

151152
def test_logout(self):
152-
with recorder.use_cassette('geocaching_logout'):
153+
with self.recorder.use_cassette('geocaching_logout'):
153154
self.gc.login(_username, _password)
154155
self.gc.logout()
155-
session.cookies.clear()
156-
self.gc._session = session # gotta reattach so we can keep listening
156+
self.session.cookies.clear()
157+
self.gc._session = self.session # gotta reattach so we can keep listening
157158
self.assertIsNone(self.gc.get_logged_user())
158159

159160
def test_load_credentials(self):
@@ -262,25 +263,25 @@ def test_login(self):
262263
real_init = Geocaching.__init__
263264

264265
def fake_init(self_, unused_argument=None):
265-
real_init(self_, session=session)
266+
real_init(self_, session=self.session)
266267

267268
# patching with the fake init method above to insert our session into the Geocaching object for testing
268269
with patch.object(Geocaching, '__init__', new=fake_init):
269-
with recorder.use_cassette('geocaching_shortcut_login'):
270+
with self.recorder.use_cassette('geocaching_shortcut_login'):
270271
pycaching.login(_username, _password)
271272

272273
def test_geocode(self):
273274
ref_point = Point(50.08746, 14.42125)
274-
with recorder.use_cassette('geocaching_shortcut_geocode'):
275+
with self.recorder.use_cassette('geocaching_shortcut_geocode'):
275276
self.assertLess(great_circle(self.gc.geocode("Prague"), ref_point).miles, 10)
276277

277278
def test_get_cache(self):
278-
with recorder.use_cassette('geocaching_shortcut_getcache'):
279+
with self.recorder.use_cassette('geocaching_shortcut_getcache'):
279280
c = self.gc.get_cache("GC4808G")
280281
self.assertEqual("Nekonecne ticho", c.name)
281282

282283
def test_get_trackable(self):
283-
with recorder.use_cassette('geocaching_shortcut_gettrackable'):
284+
with self.recorder.use_cassette('geocaching_shortcut_gettrackable'):
284285
t = self.gc.get_trackable("TB1KEZ9")
285286
self.assertEqual("Lilagul #2: SwedenHawk Geocoin", t.name)
286287

test/test_trackable.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pycaching import Geocaching, Trackable
88
from pycaching.errors import ValueError as PycachingValueError, LoadError
99
from pycaching.log import Log, Type as LogType
10-
from . import recorder, NetworkedTest
10+
from . import NetworkedTest
1111

1212

1313
class TestProperties(unittest.TestCase):
@@ -53,19 +53,19 @@ class TestMethods(NetworkedTest):
5353
def setUpClass(cls):
5454
super().setUpClass()
5555
cls.t = Trackable(cls.gc, "TB1KEZ9")
56-
with recorder.use_cassette('trackable_setup'):
56+
with cls.recorder.use_cassette('trackable_setup'):
5757
cls.t.load()
5858

5959
def test_load(self):
6060
with self.subTest("tid"):
6161
trackable = Trackable(self.gc, "TB1KEZ9")
62-
with recorder.use_cassette('trackable_load_tid'):
62+
with self.recorder.use_cassette('trackable_load_tid'):
6363
self.assertEqual("Lilagul #2: SwedenHawk Geocoin", trackable.name)
6464

6565
with self.subTest("trackable url"):
6666
url = "http://www.geocaching.com/track/details.aspx?guid=cff00ac4-f562-486e-b303-32b2d01ed386"
6767
trackable = Trackable(self.gc, None, url=url)
68-
with recorder.use_cassette('trackable_load_url'):
68+
with self.recorder.use_cassette('trackable_load_url'):
6969
self.assertEqual("Lilagul #2: SwedenHawk Geocoin", trackable.name)
7070

7171
with self.subTest("fail lazyload"):
@@ -79,7 +79,7 @@ def test_load_log_page(self):
7979
expected_date_format = "M/d/yyyy" # if test is re-recorded, update for your testing account
8080

8181
# make request
82-
with recorder.use_cassette('trackable_load_page'):
82+
with self.recorder.use_cassette('trackable_load_page'):
8383
valid_types, hidden_inputs, user_date_format = self.t._load_log_page()
8484

8585
self.assertSequenceEqual(expected_types, valid_types)
@@ -126,7 +126,7 @@ def test_post_log(self, mock_request, mock_load_log_page):
126126
mock_request.assert_called_with(self.t._log_page_url, method="POST", data=expected_post_data)
127127

128128
def test_get_KML(self):
129-
with recorder.use_cassette('trackable_kml'):
129+
with self.recorder.use_cassette('trackable_kml'):
130130
kml = self.t.get_KML()
131131
self.assertTrue("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" in kml)
132132
self.assertTrue("<kml xmlns=\"http://earth.google.com/kml/2.2\">" in kml)

0 commit comments

Comments
 (0)