Skip to content

Commit

Permalink
Fix lots and lots of tests
Browse files Browse the repository at this point in the history
Mostly as a result of me doing stuff that Ruff said, but doing it
wrongly.

A few instances of datetimes, now with timezones, that were out, but
which I *think* are now correct.

And a few instances where a templatetag was being called with an
arg instead of a kwarg
  • Loading branch information
philgyford committed Dec 12, 2023
1 parent e5dd68a commit 21788c7
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 41 deletions.
3 changes: 2 additions & 1 deletion ditto/core/templatetags/ditto_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.http import QueryDict
from django.urls import reverse
from django.utils.html import format_html
from django.utils.safestring import mark_safe

from ditto.core import app_settings
from ditto.core.apps import ditto_apps
Expand Down Expand Up @@ -142,7 +143,7 @@ def display_time(dt, *, link_to_day=False, granularity=0, case=None):
elif case == "capfirst":
visible_time = visible_time[0].upper() + visible_time[1:]

return format_html('<time datetime="{}">{}</time>', stamp, visible_time)
return format_html('<time datetime="{}">{}</time>', stamp, mark_safe(visible_time))


@register.simple_tag(takes_context=True)
Expand Down
2 changes: 1 addition & 1 deletion ditto/core/utils/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def make_filename(self, url, headers=None):
# Could be like 'attachment; filename=26897200312.avi'
headers["Content-Disposition"]
m = re.search(r"filename\=(.*?)$", headers["Content-Disposition"])
with contextlib.suppess(AttributeError, IndexError):
with contextlib.suppress(AttributeError, IndexError):
filename = m.group(1)
except KeyError:
pass
Expand Down
2 changes: 1 addition & 1 deletion ditto/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ def _date_from_string(
try:
return (
datetime.datetime.strptime(force_str(datestr), format)
.astimezone(datetime.UTC)
.astimezone(datetime.timezone.utc)
.date()
)
except ValueError as err:
Expand Down
6 changes: 3 additions & 3 deletions ditto/flickr/fetch/fetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def __init__(self, account):
# Maximum date of photos to return, if days or start are passed in:
# By default, set it before Flickr so we get everything.
self.min_date = datetime.datetime.strptime("2000-01-01", "%Y-%m-%d").astimezone(
datetime.UTC
datetime.timezone.utc
)

# Maximum date of photos to return, if end is passed in:
Expand Down Expand Up @@ -415,12 +415,12 @@ def fetch(self, days=None, start=None, end=None):
if start:
self.min_date = datetime.datetime.strptime(
f"{start} 00:00:00", "%Y-%m-%d %H:%M:%S"
).astimezone(datetime.UTC)
).astimezone(datetime.timezone.utc)

if end:
self.max_date = datetime.datetime.strptime(
f"{end} 23:59:59", "%Y-%m-%d %H:%M:%S"
).astimezone(datetime.UTC)
).astimezone(datetime.timezone.utc)

if (start and end) and (start > end):
msg = "Start date must be before the end date."
Expand Down
10 changes: 6 additions & 4 deletions ditto/flickr/fetch/filesfetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ def _fetch_and_save_file(self, photo, media_type):
with open(filepath, "rb") as reopened_file:
django_file = File(reopened_file)

if media_type == "video":
photo.video_original_file.save(os.path.basename(filepath), django_file)
else:
photo.original_file.save(os.path.basename(filepath), django_file)
if media_type == "video":
photo.video_original_file.save(
os.path.basename(filepath), django_file
)
else:
photo.original_file.save(os.path.basename(filepath), django_file)
2 changes: 1 addition & 1 deletion ditto/flickr/management/commands/fetch_flickr_originals.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def handle(self, *args, **options):
# We might be fetching for a specific account or all (None).
nsid = options["account"] if options["account"] else None

results = self.fetch_files(nsid, options["all"])
results = self.fetch_files(nsid, fetch_all=options["all"])
self.output_results(results, options.get("verbosity", 1))

def fetch_files(self, nsid, *, fetch_all=False):
Expand Down
4 changes: 2 additions & 2 deletions ditto/flickr/templates/flickr/photo_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ <h4 class="h6 card-title">Albums</h4>
<div class="card mb-3 flickr-user">
<ul class="list-group list-group-flush">
<li class="list-group-item text-muted">
<small>Last updated on Flickr at {% display_time photo.last_update_time True %}</small>
<small>Last updated on Flickr at {% display_time photo.last_update_time link_to_day=True %}</small>
</li>
<li class="list-group-item text-muted">
<small>As of {% display_time photo.fetch_time True %}</small>
<small>As of {% display_time photo.fetch_time link_to_day=True %}</small>
</li>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion ditto/lastfm/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _send_request(self):
)
response.raise_for_status() # Raises an exception on HTTP error.
except requests.exceptions.RequestException as err:
msg = "Error when fetching Scrobbles (page {self.page_number}): {err}"
msg = f"Error when fetching Scrobbles (page {self.page_number}): {err}"
raise FetchError(msg) from err

response.encoding = "utf-8"
Expand Down
4 changes: 2 additions & 2 deletions ditto/lastfm/templatetags/ditto_lastfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def get_period_times(date, period):
# `date` is a datetime.date
min_time = datetime.datetime.combine(
date, datetime.datetime.min.time()
).replace(tzinfo=datetime.UTC)
).replace(tzinfo=datetime.timezone.utc)
max_time = datetime.datetime.combine(
date, datetime.datetime.max.time()
).replace(tzinfo=datetime.UTC)
).replace(tzinfo=datetime.timezone.utc)

if period == "week":
# Default is Sunday (0):
Expand Down
2 changes: 1 addition & 1 deletion ditto/pinboard/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def fetch(self, post_date, username=None):
FetchError if the date format is invalid.
"""
try:
dt = datetime.strptime(post_date, "%Y-%m-%d").astimezone(timezone.UTC)
dt = datetime.strptime(post_date, "%Y-%m-%d").astimezone(timezone.utc)
except ValueError as err:
raise FetchError("Invalid date format ('%s')" % post_date) from err
else:
Expand Down
4 changes: 2 additions & 2 deletions ditto/pinboard/templates/pinboard/includes/bookmark.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ <h2 class="{% if view == 'detail' %}h4{% else %}h6{% endif %} mb-0 pinboard-book
{% load ditto_core %}
Posted at
{% if view == 'day' %}
{% display_time bookmark.post_time False %}
{% display_time bookmark.post_time link_to_day=False %}
{% else %}
{% display_time bookmark.post_time True %}
{% display_time bookmark.post_time link_to_day=True %}
{% endif %}

{% if view != 'detail' %}
Expand Down
8 changes: 4 additions & 4 deletions ditto/twitter/fetch/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def _fetch_and_save_file(self, media_obj, media_type):
with open(filepath, "rb") as reopened_file:
django_file = File(reopened_file)

if media_type == "mp4":
media_obj.mp4_file.save(os.path.basename(filepath), django_file)
else:
media_obj.image_file.save(os.path.basename(filepath), django_file)
if media_type == "mp4":
media_obj.mp4_file.save(os.path.basename(filepath), django_file)
else:
media_obj.image_file.save(os.path.basename(filepath), django_file)
3 changes: 2 additions & 1 deletion ditto/twitter/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def _save_media(self, directory):
directory, "tweet_media", local_filename
)

with File(open(filepath, "rb")) as django_file:
with open(filepath, "rb") as f:
django_file = File(f)
if media_obj.media_type == "animated_gif":
# When we fetch GIFs we also fetch an image file
# for them. But their images aren't included in
Expand Down
4 changes: 2 additions & 2 deletions ditto/twitter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def __str__(self):
@property
def has_file(self):
"Do we have a file saved at all?"
return self.image_file.name or self.mp4_file.name
return bool(self.image_file.name or self.mp4_file.name)

@property
def thumbnail_w(self):
Expand Down Expand Up @@ -601,7 +601,7 @@ def get_previous(self):

@property
def is_reply(self):
return self.in_reply_to_screen_name == ""
return self.in_reply_to_screen_name != ""

@property
def in_reply_to_url(self):
Expand Down
4 changes: 2 additions & 2 deletions ditto/twitter/templates/twitter/includes/tweet.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
{% load ditto_core %}
{% if view == 'day' %}
{% display_time tweet.post_time False %}
{% display_time tweet.post_time link_to_day=False %}
{% else %}
{% display_time tweet.post_time True %}
{% display_time tweet.post_time link_to_day=True %}
{% endif %}

{% if tweet.is_reply %}
Expand Down
2 changes: 1 addition & 1 deletion tests/flickr/test_fetch_fetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def test_calls_fetch_pages(self, fetch_pages):
def test_fetches_recent_days(self, save_photo, fetch_extra):
"Should only ask for photos from recent days, if number of days is set."
self.expect_response(
"people.getPhotos", params={"min_upload_date": "1439294400"}
"people.getPhotos", params={"min_upload_date": "1439265600"}
)

with patch("time.sleep"):
Expand Down
21 changes: 13 additions & 8 deletions tests/pinboard/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ def make_success_body(
posts = []
for n in range(num_posts):
posts.append(
f'{"href":"http:\\/\\/example{n}.com\\/","description":"My description {n}","extended":"My extended {n}.","meta":"abcdef1234567890abcdef1234567890","hash":"1234567890abcdef1234567890abcdef","time":"{post_date}T09:48:31Z","shared":"yes","toread":"no","tags":"tag1 tag2 tag3"}' # noqa: E501
'{"href":"http:\\/\\/example%s.com\\/","description":"My description %s","extended":"My extended %s.","meta":"abcdef1234567890abcdef1234567890","hash":"1234567890abcdef1234567890abcdef","time":"%sT09:48:31Z","shared":"yes","toread":"no","tags":"tag1 tag2 tag3"}' # noqa: E501, UP031
% (n, n, n, post_date)
)

posts_json = "[%s]\t\n" % (",".join(posts))

if method == "all":
return posts_json
else:
return f'{"date":"{post_date}T09:48:31Z","user":"{username}","posts":{posts_json}}\t\n' # noqa: E501
return '{{"date":"{}T09:48:31Z","user":"{}","posts":{}}}\t\n'.format(
post_date,
username,
posts_json,
)

# Check that all interface methods return expected results on success.

Expand Down Expand Up @@ -324,8 +329,8 @@ def test_save_bookmarks(self):

self.assertEqual(
bookmarks[1].fetch_time,
datetime.strptime("2015-07-01 12:00:00", "%Y-%m-%d %H:%M:%S").replace(
tzinfo=timezone.utc
datetime.strptime("2015-07-01 04:00:00", "%Y-%m-%d %H:%M:%S").astimezone(
timezone.utc
),
)
self.assertEqual(
Expand All @@ -340,8 +345,8 @@ def test_save_bookmarks(self):
self.assertEqual(bookmarks[1].url, "http://fontello.com/")
self.assertEqual(
bookmarks[1].post_time,
datetime.strptime("2015-06-18T09:48:31Z", "%Y-%m-%dT%H:%M:%SZ").replace(
tzinfo=timezone.utc
datetime.strptime("2015-06-18T09:48:31Z", "%Y-%m-%dT%H:%M:%SZ").astimezone(
timezone.utc
),
)
self.assertEqual(
Expand Down Expand Up @@ -404,8 +409,8 @@ def test_update_bookmarks(self):
# This should be updated to now, as we've changed things:
self.assertEqual(
bookmarks[1].fetch_time,
datetime.strptime("2015-07-01 12:00:00", "%Y-%m-%d %H:%M:%S").replace(
tzinfo=timezone.utc
datetime.strptime("2015-07-01 04:00:00", "%Y-%m-%d %H:%M:%S").astimezone(
timezone.utc
),
)

Expand Down
6 changes: 3 additions & 3 deletions tests/twitter/test_ingest_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def test_opens_all_files(self):
ingester.ingest(directory="/good/dir")
m.assert_has_calls(
[
call("/good/dir/2015_01.js", "r"),
call("/good/dir/2015_02.js", "r"),
call("/good/dir/2015_03.js", "r"),
call("/good/dir/2015_01.js"),
call("/good/dir/2015_02.js"),
call("/good/dir/2015_03.js"),
],
any_order=True,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/twitter/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def test_update_user_returns_error_message(self):
result = account.update_user_from_twitter()
self.assertEqual(1, len(responses.calls))
self.assertEqual(
f"{self.api_url}/account/verify_credentials.json.json",
f"{self.api_url}/account/verify_credentials.json",
responses.calls[0].request.url,
)
self.assertFalse(result["success"])
Expand Down

0 comments on commit 21788c7

Please sign in to comment.