Use aware UTC datetimes internally (#887)

In lieu of Python v3.12 deprecation warnings
This commit is contained in:
Ville Skyttä
2023-06-14 23:39:34 +03:00
committed by GitHub
parent 3346977297
commit 3d36108446
12 changed files with 84 additions and 54 deletions

View File

@ -39,6 +39,7 @@ from unittest import mock
from json import dumps
from datetime import datetime
from datetime import timedelta
from datetime import timezone
# Disable logging for a cleaner testing output
import logging
@ -250,7 +251,7 @@ def test_plugin_reddit_general(mock_post):
}
# Epoch time:
epoch = datetime.utcfromtimestamp(0)
epoch = datetime.fromtimestamp(0, timezone.utc)
good_response = mock.Mock()
good_response.content = dumps({
@ -267,7 +268,8 @@ def test_plugin_reddit_general(mock_post):
})
good_response.status_code = requests.codes.ok
good_response.headers = {
'X-RateLimit-Reset': (datetime.utcnow() - epoch).total_seconds(),
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-Remaining': 1,
}
@ -296,7 +298,8 @@ def test_plugin_reddit_general(mock_post):
# Force a case where there are no more remaining posts allowed
good_response.headers = {
'X-RateLimit-Reset': (datetime.utcnow() - epoch).total_seconds(),
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-Remaining': 0,
}
# behind the scenes, it should cause us to update our rate limit
@ -305,7 +308,8 @@ def test_plugin_reddit_general(mock_post):
# This should cause us to block
good_response.headers = {
'X-RateLimit-Reset': (datetime.utcnow() - epoch).total_seconds(),
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-Remaining': 10,
}
assert obj.send(body="test") is True
@ -319,7 +323,8 @@ def test_plugin_reddit_general(mock_post):
# Reset our variable back to 1
good_response.headers = {
'X-RateLimit-Reset': (datetime.utcnow() - epoch).total_seconds(),
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-Remaining': 1,
}
# Handle cases where our epoch time is wrong
@ -328,7 +333,8 @@ def test_plugin_reddit_general(mock_post):
# Return our object, but place it in the future forcing us to block
good_response.headers = {
'X-RateLimit-Reset': (datetime.utcnow() - epoch).total_seconds() + 1,
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds() + 1,
'X-RateLimit-Remaining': 0,
}
@ -337,7 +343,8 @@ def test_plugin_reddit_general(mock_post):
# Return our object, but place it in the future forcing us to block
good_response.headers = {
'X-RateLimit-Reset': (datetime.utcnow() - epoch).total_seconds() - 1,
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds() - 1,
'X-RateLimit-Remaining': 0,
}
assert obj.send(body="test") is True
@ -348,7 +355,8 @@ def test_plugin_reddit_general(mock_post):
# Invalid JSON
response = mock.Mock()
response.headers = {
'X-RateLimit-Reset': (datetime.utcnow() - epoch).total_seconds(),
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-Remaining': 1,
}
response.content = '{'
@ -393,7 +401,8 @@ def test_plugin_reddit_general(mock_post):
})
good_response.status_code = requests.codes.ok
good_response.headers = {
'X-RateLimit-Reset': (datetime.utcnow() - epoch).total_seconds(),
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-Remaining': 1,
}