Skip to content

Commit c2a40c0

Browse files
committed
Mocking TestShortcuts for Geocaching
1 parent 4e4e97e commit c2a40c0

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

test/test_geocaching.py

+23-14
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from subprocess import CalledProcessError
1010
from unittest.mock import patch
1111
from tempfile import NamedTemporaryFile
12-
from geopy.distance import great_circle
13-
from pycaching import Geocaching, Point, Rectangle
12+
from pycaching import Geocaching, Point, Rectangle, Cache
1413
from pycaching.errors import NotLoggedInException, LoginFailedException, PMOnlyException
1514

1615

@@ -246,24 +245,34 @@ class TestShortcuts(unittest.TestCase):
246245

247246
@classmethod
248247
def setUpClass(cls):
249-
cls.g = Geocaching()
250-
cls.g.login(_username, _password)
248+
cls.gc = Geocaching()
251249

252250
def test_login(self):
253-
pycaching.login(_username, _password)
251+
with patch.object(Geocaching, "login") as login:
252+
pycaching.login(_username, _password)
253+
self.assertTrue(login.called)
254254

255255
def test_geocode(self):
256-
ref_point = Point(49.74774, 13.37752)
257-
self.assertLess(great_circle(self.g.geocode("Pilsen"), ref_point).miles, 10)
256+
with patch.object(Point, "from_location") as from_location:
257+
self.gc.geocode("Pilsen")
258+
self.assertTrue(from_location.called)
258259

259260
def test_get_cache(self):
260-
c = self.g.get_cache("GC4808G")
261-
self.assertEqual("Nekonecne ticho", c.name)
261+
with patch("pycaching.cache.Cache.__init__", return_value=None) as Cache:
262+
self.gc.get_cache("GC4808G")
263+
self.assertTrue(Cache.called)
262264

263265
def test_get_trackable(self):
264-
t = self.g.get_trackable("TB1KEZ9")
265-
self.assertEqual("Lilagul #2: SwedenHawk Geocoin", t.name)
266+
with patch("pycaching.trackable.Trackable.__init__", return_value=None) as Trackable:
267+
self.gc.get_trackable("TB1KEZ9")
268+
self.assertTrue(Trackable.called)
269+
270+
@patch.object(Geocaching, "get_cache")
271+
@patch.object(Cache, "post_log")
272+
def test_post_log(self, mock_post_log, mock_get_cache):
273+
mock_get_cache.return_value = Cache(self.gc)
274+
275+
self.gc.post_log("GC4808G", "Found it, thank you!")
266276

267-
def test_post_log(self):
268-
# I refuse to write 30 lines of tests (mocking etc.) because of 4 simple lines of code.
269-
pass
277+
self.assertTrue(mock_post_log.called)
278+
self.assertTrue(mock_get_cache.called)

0 commit comments

Comments
 (0)