Skip to content

Commit

Permalink
Fixes #62 Removed pin delete option from dt command
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFriendlyCoder committed Apr 18, 2018
1 parent 4ce39e5 commit 3538cec
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 94 deletions.
7 changes: 1 addition & 6 deletions src/friendlypins/scripts/fpins.py
Expand Up @@ -14,7 +14,7 @@ def _download_thumbnails(args):
:returns: zero on success, non-zero on failure
:rtype: :class:`int`
"""
return download_thumbnails(args.token, args.board, args.path, args.delete)
return download_thumbnails(args.token, args.board, args.path)

def _edit_board(args):
"""Callback for manipulating a Pinterest board
Expand Down Expand Up @@ -80,11 +80,6 @@ def get_args(args):
required=True,
help="Path to the folder where thumbnails are to be downloaded",
)
thumbnails_cmd.add_argument(
'--delete', '-d',
action="store_true",
help="Deletes each pin as it's thumbnail is downloaded"
)

# Board manipulation sub-command
desc = 'Manipulates boards owned by the authenticated user'
Expand Down
5 changes: 1 addition & 4 deletions src/friendlypins/utils/console_actions.py
Expand Up @@ -48,13 +48,12 @@ def _download_pin(pin, folder):
return 0


def download_thumbnails(api_token, board_name, output_folder, delete):
def download_thumbnails(api_token, board_name, output_folder,):
"""Downloads thumbnails of all pins on a board
:param str api_token: Authentication token for accessing the Pinterest API
:param str board_name: name of the board containing the pins to process
:param str output_folder: path where the thumbnails are to be downloaded
:param bool delete: flag to delete pins as their thumbnails are downloaded
:returns:
status code describing the result of the action
zero on success, non-zero on failure
Expand Down Expand Up @@ -89,8 +88,6 @@ def download_thumbnails(api_token, board_name, output_folder, delete):
retval = _download_pin(cur_pin, output_folder)
if retval:
return retval
if delete:
cur_pin.delete()
pbar.update()

return 0
Expand Down
88 changes: 4 additions & 84 deletions unit_tests/test_console_actions.py
Expand Up @@ -74,7 +74,7 @@ def test_download_thumbnails(rest_io, action_requests, mock_open, mock_os):
mock_os.path.exists.return_value = False

# Flex our code
result = download_thumbnails("1234abcd", expected_board_name, "/tmp", False)
result = download_thumbnails("1234abcd", expected_board_name, "/tmp")

# Make sure the call was successful, and that our mock APIs
# that must have executed as part of the process were called
Expand All @@ -85,86 +85,6 @@ def test_download_thumbnails(rest_io, action_requests, mock_open, mock_os):
mock_open.assert_called()


@mock.patch("friendlypins.utils.console_actions.os")
@mock.patch("friendlypins.utils.console_actions.open")
@mock.patch("friendlypins.utils.console_actions.requests")
@mock.patch("friendlypins.api.RestIO")
def test_download_thumbnails_with_delete(rest_io, action_requests, mock_open, mock_os):

# Fake user data for the user authenticating to Pinterest
expected_user_data = {
'data': {
'url': 'https://www.pinterest.com/MyUserName/',
'first_name': "John",
'last_name': "Doe",
'id': "12345678"
}
}

# Fake board data for the boards owned by the fake authenticated user
expected_board_name = "MyBoard"
expected_board_data = {
"data": [{
"id": "6789",
"name": expected_board_name,
"url": "https://www.pinterest.ca/MyName/MyBoard/",
"counts": {
"pins": 1
}
}]
}

# Fake pin data for the fake board, with fake thumbnail metadata
expected_thumbnail_url = "https://i.pinimg.com/originals/1/2/3/abcd.jpg"
expected_pin_data = {
"data": [{
"id": "1234",
"url": "https://www.pinterest.ca/MyName/MyPin/",
"note": "My Pin descriptive text",
"link": "http://www.mysite.com/target",
"media": {
"type": "image"
},
"image": {
"original": {
"url": expected_thumbnail_url,
"width": "800",
"height": "600"
}
}
}],
"page": {
"cursor": None
}
}

# fake our Pinterest API data to flex our implementation logic
mock_response = mock.MagicMock()
mock_response.get.side_effect = [
expected_user_data,
]
mock_response.get_pages.side_effect = [
[expected_board_data],
[expected_pin_data]
]
rest_io.return_value = mock_response

# Make sure the code think's the output file where the
# thumbnail is to be downloaded doesn't already exist
mock_os.path.exists.return_value = False

# Flex our code
result = download_thumbnails("1234abcd", expected_board_name, "/tmp", True)

# Make sure the call was successful, and that our mock APIs
# that must have executed as part of the process were called
assert result == 0
action_requests.get.assert_called_once_with(expected_thumbnail_url, stream=True)
mock_os.makedirs.assert_called()
mock_os.path.exists.assert_called()
mock_open.assert_called()
mock_response.delete.assert_called_once()

@mock.patch("friendlypins.utils.console_actions.os")
@mock.patch("friendlypins.utils.console_actions.open")
@mock.patch("friendlypins.utils.console_actions.requests")
Expand Down Expand Up @@ -239,7 +159,7 @@ def test_download_thumbnails_error(rest_io, action_requests, mock_open, mock_os)
action_requests.get.return_value = mock_action_response

# Flex our code
result = download_thumbnails("1234abcd", expected_board_name, "/tmp", False)
result = download_thumbnails("1234abcd", expected_board_name, "/tmp")

# Make sure the call was successful, and that our mock APIs
# that must have executed as part of the process were called
Expand Down Expand Up @@ -318,7 +238,7 @@ def test_download_thumbnails_missing_board(rest_io, action_requests, mock_open,
mock_os.path.exists.return_value = False

# Flex our code
result = download_thumbnails("1234abcd", "FuBar", "/tmp", False)
result = download_thumbnails("1234abcd", "FuBar", "/tmp")

# Make sure the call was successful, and that our mock APIs
# that must have executed as part of the process were called
Expand Down Expand Up @@ -401,7 +321,7 @@ def test_download_thumbnails_exists(rest_io, action_requests, mock_open, mock_os

# Flex our code
output_folder = "/tmp"
result = download_thumbnails("1234abcd", expected_board_name, output_folder, False)
result = download_thumbnails("1234abcd", expected_board_name, output_folder)

# Make sure the call was successful, and that our mock APIs
# that must have executed as part of the process were called
Expand Down

0 comments on commit 3538cec

Please sign in to comment.