Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_valid_json(self):
# Arrange
package = "pip"
mocked_url = "https://pypistats.org/api/packages/pip/recent?&period=day"
mocked_response = """
{"data": {"last_day": 1956060}, "package": "pip", "type": "recent_downloads"}
"""
# Act
with requests_mock.Mocker() as m:
m.get(mocked_url, text=mocked_response)
output = pypistats.recent(package, period="day", format="json")
# Assert
# Should not raise any errors eg. TypeError
json.loads(output)
self.assertEqual(json.loads(output), json.loads(mocked_response))
mocked_url = "https://pypistats.org/api/packages/pip/recent"
mocked_response = """{
"data":
{"last_day": 2295765, "last_month": 67759913, "last_week": 15706750},
"package": "pip", "type": "recent_downloads"
}"""
expected_output = """
| last_day | last_month | last_week |
|----------:|-----------:|-----------:|
| 2,295,765 | 67,759,913 | 15,706,750 |
"""
# Act
with requests_mock.Mocker() as m:
m.get(mocked_url, text=mocked_response)
output = pypistats.recent(package)
# Assert
self.assertEqual(output.strip(), expected_output.strip())
import argparse
from pprint import pprint # noqa: F401
import pypistats
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Example use of pypistats",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
args = parser.parse_args()
# Example use
# Call the API
print(pypistats.recent("pillow"))
# print(pypistats.recent("pillow", "day", format="markdown"))
def recent(args): # pragma: no cover
print(
pypistats.recent(
args.package, period=args.period, format=args.format, verbose=args.verbose
)