Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Arrange
package = "pip"
mocked_url = "https://pypistats.org/api/packages/pip/overall"
mocked_response = SAMPLE_RESPONSE_OVERALL
expected_output = """
| category | downloads |
|-----------------|----------:|
| without_mirrors | 2,297,591 |
Date range: 2018-11-02 - 2018-11-02
"""
# Act
with requests_mock.Mocker() as m:
m.get(mocked_url, text=mocked_response)
output = pypistats.overall(package, mirrors=False, start_date="2018-11-02")
# Assert
self.assertEqual(output.strip(), expected_output.strip())
"package": "pip",
"type": "overall_downloads"
}"""
expected_output = """
| category | downloads |
|-----------------|----------:|
| without_mirrors | 2,295,765 |
"""
# Act
with requests_mock.Mocker() as m:
m.get(mocked_url, text=mocked_response)
# First time to save to cache
pypistats.overall(package)
# Second time to read from cache
output = pypistats.overall(package)
# Assert
self.assertEqual(output.strip(), expected_output.strip())
# Arrange
package = "pip"
mocked_url = "https://pypistats.org/api/packages/pip/overall"
mocked_response = SAMPLE_RESPONSE_OVERALL
expected_output = """
| category | downloads |
|-----------------|----------:|
| without_mirrors | 2,295,765 |
Date range: 2018-11-01 - 2018-11-01
"""
# Act
with requests_mock.Mocker() as m:
m.get(mocked_url, text=mocked_response)
output = pypistats.overall(package, mirrors=False, end_date="2018-11-01")
# Assert
self.assertEqual(output.strip(), expected_output.strip())
{"category": "without_mirrors", "date": "2018-11-01", "downloads": 2295765}
],
"package": "pip",
"type": "overall_downloads"
}"""
expected_output = """
| category | downloads |
|-----------------|----------:|
| without_mirrors | 2,295,765 |
"""
# Act
with requests_mock.Mocker() as m:
m.get(mocked_url, text=mocked_response)
# First time to save to cache
pypistats.overall(package)
# Second time to read from cache
output = pypistats.overall(package)
# Assert
self.assertEqual(output.strip(), expected_output.strip())
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())
def test__paramify_string(self):
# Arrange
period = "day"
# Act
param = pypistats._paramify("period", period)
# Assert
self.assertEqual(param, "&period=day")
def test__paramify_float(self):
# Arrange
version = 3.7
# Act
param = pypistats._paramify("version", version)
# Assert
self.assertEqual(param, "&version=3.7")
def test__paramify_none(self):
# Arrange
period = None
# Act
param = pypistats._paramify("period", period)
# Assert
self.assertEqual(param, "")
def test__paramify_bool(self):
# Arrange
mirrors = True
# Act
param = pypistats._paramify("mirrors", mirrors)
# Assert
self.assertEqual(param, "&mirrors=true")