Skip to content

Commit

Permalink
Merge e0f7700 into 1786e88
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFriendlyCoder committed Jul 28, 2020
2 parents 1786e88 + e0f7700 commit 81acfce
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 55 deletions.
3 changes: 2 additions & 1 deletion project.prop
Expand Up @@ -29,7 +29,8 @@
"colorama",
# pinning coverage package until bugs with coveralls plugin is fixed
# https://github.com/z4r/python-coveralls/issues/73
"coverage<5.0"
"coverage<5.0",
"pyyaml"
],
"DESCRIPTION" : "Python wrapper around the Pinterest developer APIs",
"KEYWORDS" : "pinterest api wrapper library",
Expand Down
44 changes: 34 additions & 10 deletions tests/conftest.py
@@ -1,11 +1,13 @@
import os
from copy import deepcopy
import json
from pathlib import Path
import yaml
import pytest

CUR_PATH = Path(__file__).parent
DEFAULT_KEY_FILE = CUR_PATH.parent.joinpath("key.txt")
REFERECE_DATA = CUR_PATH.joinpath("reference_data.json")
REFERENCE_DATA = CUR_PATH.joinpath("reference_data.yaml")


def scrub_response(secret):
Expand Down Expand Up @@ -56,8 +58,17 @@ def inner_scrub_response(response):

# Scrub our authentication token from the response headers
if "Location" in response["headers"]:
temp = response["headers"]["Location"][0]
response["headers"]["Location"][0] = temp.replace(secret, "DUMMY")
# The default location header field has an upper case L
loc_key = "Location"
elif "location" in response["headers"]:
# When the API rate limit has been reached the header field for
# location gets changed to a lower case l for some weird reason
loc_key = "location"
else:
loc_key = None
if loc_key:
temp = response["headers"][loc_key][0]
response["headers"][loc_key][0] = temp.replace(secret, "DUMMY")

# One final sanity check to make sure the token doesn't appear
# anywhere else in the response data structure
Expand All @@ -77,6 +88,13 @@ def vcr_config(request):
if key_file and os.path.exists(key_file):
with open(key_file) as fh:
secret = fh.read().strip()
else:
# If no auth key provided, let's try and load a default one
# This is needed when running in an IDE, and to make it easier
# to run the tests form the shell when one uses a consistent
# naming convention for the API key
if DEFAULT_KEY_FILE.exists():
secret = DEFAULT_KEY_FILE.read_text().strip()

retval = {
"filter_query_parameters": [("access_token", "DUMMY")],
Expand All @@ -94,29 +112,35 @@ def vcr_config(request):
if request.config.getoption("--record-mode") == "none":
retval["record_mode"] = "once"

return retval
return deepcopy(retval)


@pytest.fixture(scope="function")
def test_env(request):
if not REFERECE_DATA.exists():
if not REFERENCE_DATA.exists():
raise Exception("Reference data for integration tests must be stored in a file name " +
str(REFERECE_DATA.name))
str(REFERENCE_DATA.name))

key_file = request.config.getoption("--key-file")
key = "DUMMY"
key = None
if key_file:
if not os.path.exists(key_file):
raise Exception("API authentication token file not found")

with open(key_file) as fh:
key = fh.read().strip()
elif request.config.getoption("--record-mode") == "rewrite":
else:
# If no explicit auth token can be found, lets try loading a default one
if DEFAULT_KEY_FILE.exists():
key = DEFAULT_KEY_FILE.read_text().strip()

if request.config.getoption("--record-mode") == "rewrite" and not key:
raise Exception("Rewrite mode can only work with a valid auth key. "
"Use --key-file to run the tests.")

retval = json.loads(REFERECE_DATA.read_text())
retval["key"] = key
retval = yaml.safe_load(REFERENCE_DATA.read_text())
retval["key"] = key or "DUMMY" # If we have no auth token, provide a dummy value

yield retval


Expand Down
34 changes: 0 additions & 34 deletions tests/reference_data.json

This file was deleted.

46 changes: 46 additions & 0 deletions tests/reference_data.yaml
@@ -0,0 +1,46 @@
test_board:
# Sample board used for testing properties and attributes of boards
# Must contain at least 1 pin and 1 section (see the test_section node below)
description: Board used for unit testing
id: 485262997288961007
name: testing
pins:
# IDs of pins on this board
# NOTE: the Pinterest API will report all pins from all subsections of a board
# when listing the pins for the board itself
- 485262928605818444
- 485262928605821260
privacy: public

test_pin:
# Sample pin used for testing properties and attributes of boards
id: 485262928605818444
link: ''
note: Here is my note
thumbnail_height: 907
thumbnail_url: https://i.pinimg.com/originals/e7/f5/50/e7f550c0e688fcc24b0568415fe83a72.jpg
thumbnail_width: 735
type: image
url: https://www.pinterest.com/pin/485262928605818444/

test_section:
# Sample board subsection to use for testing sections
# Must contain at least 1 pin
id: 5111575990144913664
pins:
# IDs of pins in this section
# NOTE: the Pinterest API will report pins from board sections together with
# the pins of the parent board when querying the board itself
- 485262928605821260
title: Sample Section

test_user:
# Sample user to use for testing properties and attributes of Pinterest users
bio: Interesting reference links
first_name: Kevin
full_name: Kevin
id: 485263066007653091
last_name: ''
type: individual
url: https://www.pinterest.com/thefriendlycoder/
username: thefriendlycoder
10 changes: 4 additions & 6 deletions tests/test_board.py
Expand Up @@ -19,20 +19,18 @@ def test_board_properties(test_env):
assert isinstance(board.num_followers, int)
assert isinstance(board.num_collaborators, int)
assert isinstance(board.num_pins, int)
all_pins = test_env["test_board"]["pins"] + test_env["section_pins"]
assert board.num_pins == len(all_pins)
assert board.num_pins == len(test_env["test_board"]["pins"])


@pytest.mark.vcr()
def test_get_pins(test_env):
obj = API(test_env["key"])
board = obj.get_board_by_id(test_env["test_board"]["id"])
pins = list(board.pins)
all_pins = test_env["test_board"]["pins"] + test_env["section_pins"]
assert len(pins) == len(all_pins)
assert len(pins) == len(test_env["test_board"]["pins"])
for cur_pin in pins:
assert cur_pin.unique_id in all_pins
all_pins.remove(cur_pin.unique_id)
assert cur_pin.unique_id in test_env["test_board"]["pins"]
test_env["test_board"]["pins"].remove(cur_pin.unique_id)


def test_cache_refresh():
Expand Down
10 changes: 6 additions & 4 deletions update.py
Expand Up @@ -57,8 +57,10 @@ def load_report():
if not REPORT_FILE.exists():
raise Exception("pytest report file not found: " + REPORT_FILE.name)

return json.loads(REPORT_FILE.read_text())

retval = json.loads(REPORT_FILE.read_text())
# Reformat our JSON report to make it easier to read
REPORT_FILE.write_text(json.dumps(retval, indent=4))
return retval

def analyse_report(report):
"""Analyses a pytest report, and displays summary information to the console
Expand All @@ -77,7 +79,7 @@ def analyse_report(report):

current_failures = list()
for cur_test in report["tests"]:
if cur_test["outcome"] == "passed":
if cur_test["outcome"] in ("passed", "skipped"):
continue
if "RateLimitException" not in str(cur_test) and "Network is disabled" not in str(cur_test):
raise Exception("Unit test {0} has failed for unexpected reasons. See debug.log for details".format(
Expand All @@ -95,7 +97,7 @@ def analyse_report(report):
if PREVIOUS_REPORT:
fixed_tests = list()
for cur_test in PREVIOUS_REPORT["tests"]:
if cur_test["outcome"] == "passed":
if cur_test["outcome"] in ("passed", "skipped"):
continue
if cur_test["nodeid"] not in current_failures:
fixed_tests.append(cur_test["nodeid"])
Expand Down

0 comments on commit 81acfce

Please sign in to comment.