Skip to content

Commit

Permalink
Added note deletion and pytest modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mmshivesh committed Jul 20, 2020
1 parent b292ed3 commit 5c7ed88
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@

Python wrapper for the Snoonotes API

## Currently Supported Features:
## Supported Features:

1. Fetch notes for a particular user
2. Fetch Subreddit Notes - Caching implemented
3. Add notes for a user

## Not Supported Currently:

1. Removing usernotes
4. Removing usernotes for a user

## Depends On:

Expand Down Expand Up @@ -49,14 +46,18 @@ notes_for_usernames12 = sn.get_notes_for_user(["username1", "username2"])
subreddit_notes = sn.get_notes_for_subreddit("subreddit")
```

---
NOTE: This function caches queries using a pickle file to `./caches` directory to prevent repeated API requests. By default it automatically updates caches once a day. However, using `use_cache=False`, you can manually trigger a call that bypasses cache (this call will also update the cache)
*NOTE*: This function caches queries using a pickle file to `./caches` directory to prevent repeated API requests. By default it automatically updates caches once a day. However, using `use_cache=False`, you can manually trigger a call that bypasses cache (this call will also update the cache)

3. Add a new usernote for the user under a subreddit with a custom note and a link to the comment/post:

```python
sn.add_note_for_user("username", "note_type_id", "subreddit", "Reason for note", "www.reddit.com/r/subreddit/123abc/.../123abc")
```

4. Delete a usernote for a given username, given a note_id.

```python
sn.delete_note_for_user("username","note_id")
```

The above is also summarized in the `sample.py` script
16 changes: 16 additions & 0 deletions pysnoonotes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def _authed_request(self, request_type, endpoint, data=None):
return r.json()
else:
raise RequestFailedError(f"{request_type} request returned a non-ok code: {r.status_code}")
elif request_type == "DELETE":
r = requests.delete(self._endpoint_url(endpoint), headers={
"Authorization": f"{self.token_type} {self.access_token}"
})
if r.ok:
return
else:
raise RequestFailedError(f"{request_type} request returned a non-ok code: {r.status_code}")

def add_note_for_user(self, username, note_type, subreddit, message, url):
"""Add a Snoonote to the `username` under the specific `subreddit`.
Expand Down Expand Up @@ -82,6 +90,14 @@ def get_notes_for_user(self, usernames):
usernames = [usernames]
return self._authed_request("POST", "/api/Note/GetNotes", data=usernames)

def delete_note_for_user(self, username, note_id):
""" Deletes a note given a `note_id` for a given `user`
:param username: Username of the user to remove a note for
:param note_id: id of the note to remove.
"""
return self._authed_request("DELETE", f"/api/Note?id={note_id}")

def get_notes_for_subreddit(self, subreddit, use_cache=True):
"""Return usernotes supported by a subreddit. Also caches the data for a day to prevent repeated calls to the API. Should therefore be very fast for repeated access.
Expand Down
3 changes: 3 additions & 0 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@

# Add a new usernote to the user under the subreddit with a note with a link to the comment/post.
sn.add_note_for_user("username", "note_type", "subreddit", "Reason for note", "www.reddit.com/r/subreddit/123jks/.../jkas12")

# Delete a usernote for a given username, given a note_id.
sn.delete_note_for_user("username","note_id")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pysnoonotes",
version="1.0.1",
version="1.1.0",
author="mmshivesh",
author_email="",
description="A Python wrapper for the Snoonotes API",
Expand Down
Empty file added tests/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

import pytest

def pytest_addoption(parser):
parser.addoption("--key", action="store")

@pytest.fixture(scope='session')
def key(request):
key = request.config.option.key
return key
45 changes: 45 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from pysnoonotes.core import SnooNotes
import pytest
from pysnoonotes.errors import LoginFailedError


class TestSnooNotes:
def _sub_notes(self, subreddit, cache, key):
return SnooNotes("pysnoonotestest", key).get_notes_for_subreddit(subreddit, use_cache=cache)["SubName"] == subreddit

def test_sub_notes(self, key):
assert self._sub_notes("pysnoonotes_test", False, key)

def test_sub_notes_caching(self, key):
# Uses the previous cache
assert self._sub_notes("pysnoonotes_test", True, key)

# Usernotes check
def test_dictionary_return(self, key):
assert isinstance(SnooNotes("pysnoonotestest", key).get_notes_for_user("pysnoonotestest"), dict)

def test_multiple_users_dict_return(self, key):
assert isinstance(SnooNotes("pysnoonotestest", key).get_notes_for_user(["pysnoonotestest", "pysnoonotestest"]), dict)

# Login Fail check
def test_should_raise_login_error(self):
with pytest.raises(LoginFailedError):
SnooNotes("wrong_username", "wrong_password").get_access_token()

# Test note addition and deletion for a user.
def test_adding_note(self, key):
initial_notes = len(SnooNotes("pysnoonotestest", key).get_notes_for_user("pysnoonotestest")['pysnoonotestest'])
SnooNotes("pysnoonotestest", key).add_note_for_user("pysnoonotestest", "4763", "pysnoonotes_test", "pytest", "https://old.reddit.com/r/pysnoonotes_test/comments/hugnjr/test/")
assert len(SnooNotes("pysnoonotestest", key).get_notes_for_user("pysnoonotestest")['pysnoonotestest']) == initial_notes + 1

def test_deleting_note(self, key):
note_list = SnooNotes("pysnoonotestest", key).get_notes_for_user("pysnoonotestest")['pysnoonotestest']
initial_notes = len(note_list)
SnooNotes("pysnoonotestest", key).delete_note_for_user("pysnoonotestest", note_list[-1]["NoteID"])
assert len(SnooNotes("pysnoonotestest", key).get_notes_for_user("pysnoonotestest")['pysnoonotestest']) == initial_notes - 1

# Token shouldn't refresh if last refresh was less than an hour ago
def test_no_tokens_refresh(self, key):
sn = SnooNotes("pysnoonotestest", key)
sn.get_access_token()
assert sn.refresh_access_token() == 0

0 comments on commit 5c7ed88

Please sign in to comment.