|
9 | 9 | from subprocess import CalledProcessError
|
10 | 10 | from unittest.mock import patch
|
11 | 11 | 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 |
14 | 13 | from pycaching.errors import NotLoggedInException, LoginFailedException, PMOnlyException
|
15 | 14 |
|
16 | 15 |
|
@@ -246,24 +245,34 @@ class TestShortcuts(unittest.TestCase):
|
246 | 245 |
|
247 | 246 | @classmethod
|
248 | 247 | def setUpClass(cls):
|
249 |
| - cls.g = Geocaching() |
250 |
| - cls.g.login(_username, _password) |
| 248 | + cls.gc = Geocaching() |
251 | 249 |
|
252 | 250 | 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) |
254 | 254 |
|
255 | 255 | 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) |
258 | 259 |
|
259 | 260 | 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) |
262 | 264 |
|
263 | 265 | 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!") |
266 | 276 |
|
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