Skip to content

Implement property 'Personal Note' #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pycaching/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def _from_print_page(cls, geocaching, guid, soup):
cache_info["hint"] = hint.text.strip() if hint else None
cache_info["waypoints"] = Waypoint.from_html(content, table_id="Waypoints")
cache_info["log_counts"] = Cache._get_log_counts_from_print_page(soup)
cache_info["personal_note"] = content.find("h2", text="Cache Note").find_next("div").text
return Cache(geocaching, **cache_info)

def __init__(self, geocaching, wp, **kwargs):
Expand All @@ -169,7 +170,7 @@ def __init__(self, geocaching, wp, **kwargs):
known_kwargs = {"name", "type", "location", "original_location", "state", "found", "size",
"difficulty", "terrain", "author", "hidden", "attributes", "summary",
"description", "hint", "favorites", "pm_only", "url", "waypoints", "_logbook_token",
"_trackable_page_url", "guid", "visited", "log_counts"}
"_trackable_page_url", "guid", "visited", "log_counts", "personal_note"}

for name in known_kwargs:
if name in kwargs:
Expand Down Expand Up @@ -593,6 +594,19 @@ def pm_only(self):
def pm_only(self, pm_only):
self._pm_only = bool(pm_only)

@property
@lazy_loaded
def personal_note(self):
"""Personal note in the cache.

:type: :class:`str`
"""
return self._personal_note

@personal_note.setter
def personal_note(self, note):
self._personal_note = note

@property
@lazy_loaded
def _logbook_token(self):
Expand Down Expand Up @@ -738,6 +752,12 @@ def load(self):
else:
self.favorites = 0

personal_note_node = root.find(id="viewCacheNote")
if personal_note_node is not None:
self.personal_note = personal_note_node.text
else:
self.personal_note = ""

js_content = "\n".join(root.find_all(string=lambda i: isinstance(i, Script)))
self._logbook_token = re.findall("userToken\\s*=\\s*'([^']+)'", js_content)[0]
# find original location if any
Expand Down
179 changes: 179 additions & 0 deletions test/cassettes/cache_personal_note.json

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion test/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def setUp(self):
found=False, size=Size.micro, difficulty=1.5, terrain=5, author="human", hidden=date(2000, 1, 1),
attributes={"onehour": True, "kids": False, "available": True}, summary="text",
description="long text", hint="rot13", favorites=0, pm_only=False,
original_location=Point(), waypoints={}, guid="53d34c4d-12b5-4771-86d3-89318f71efb1")
original_location=Point(), waypoints={}, guid="53d34c4d-12b5-4771-86d3-89318f71efb1",
personal_note='You will find the final at N47 12.345 E015 54.321')

def test___str__(self):
self.assertEqual(str(self.c), "GC12345")
Expand Down Expand Up @@ -173,6 +174,9 @@ def test_favorites(self):
def test_pm_only(self):
self.assertEqual(self.c.pm_only, False)

def test_personal_note(self):
self.assertEqual(self.c.personal_note, 'You will find the final at N47 12.345 E015 54.321')


class TestMethods(NetworkedTest):
@classmethod
Expand Down Expand Up @@ -205,6 +209,11 @@ def test_load(self):
cache = Cache(self.gc, "GC3AHDM")
cache.load()

with self.subTest("Personal Note"):
with self.recorder.use_cassette('cache_personal_note'):
cache = Cache(self.gc, "GC1FPN1") # a location less cache
self.assertEqual("Wandern, Wandern, Wandern, ...", cache.personal_note)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit problematic, because the personal note will be different for everyone who is re-running this test (= re-recording a cassette).. I am not sure, how (and whether) we should test this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right Personal Note is a PM feature.

If I record the cassette for my test case I have the same question, what happens if some other records the cassette. D'am! I have no real idea how we can solve this problem.

By the way, I have a similar problem for a still existing test case with compares the number of founds. After I updated the cassette the test case fails.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not test it, since we don't know how.

Feel free to delete offending tests as well. But for example, testing number of founds can be re-written to assert num_of_founds >= 100 (which presumes non-descending tendency of this number and tests correct parsing at least). So please delete only those, which really cannot be fixed.


with self.subTest("fail"):
with self.recorder.use_cassette('cache_normal_fail'):
with self.assertRaises(LoadError):
Expand Down