Thursday, February 25, 2021

Re: Question about models. test

On Thu, Feb 25, 2021 at 01:06:00AM -0500, dupakoor kannan wrote:
> I have the following model
>
> class Publication(models.Model):
> name = models.CharField(max_length=25, blank=True, null=False)
> publication_url = models.TextField(blank=True)

Once you clean out all the bad data, you might want to migrate from
TextField to URLField here. That would help prevent data entry
mistakes assuming some of the errors are invalid URLs.

One other suggestion, even though you didn't ask :) I'd rename
publication_url to just url since it's a part of the Publication model,
the "publication" part is redundant.

> and observed there are some inactive URLs on the production server (due to
> data entry mistakes). Is there any way I can write a test for the inactive
> URLs from the database (local copy)?.

I assume "inactive" here means that the URLs are returning HTTP 404.
For the test, you can run this in `python manage.py shell`:

import requests # you'll need to pip install requests

for publication in Publication.objects.all():
response = requests.get(publication.publication_url)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
# do something here with your "inactive" URL
pass


If you want to make this easier to run in the future, I suggest making a
custom management command[1]. If you want this test to run anytime
Publication.save() is called, add a validator[2] to publication_url.

[1] https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/
[2] https://docs.djangoproject.com/en/3.1/ref/validators/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20210225225044.GE19963%40fattuba.com.

No comments:

Post a Comment