Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the equivalent for assertDatabaseHas in Django

Coming from Laravel background learning Django. Forgive me if my question is naive.

response = self.client.delete(reverse('details'), kwargs={'pk':pk}, format="json")

self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

The above test passes, and I would like to go a little further and try to check if the database actually has this item, of course I can query the db and try to match the count. But in Laravel I have used these methods to check for the existence/nonexistence of records.

assertDatabaseHas('users', ['id' => 10]); //asserts true if record is present in users table.

assertDatabaseMissing('users', ['id' => 10]); // asserts true if record is not present in users table

Is there something similar present in Django?

like image 473
Shobi Avatar asked Feb 01 '26 04:02

Shobi


1 Answers

There's no specific assertion for this.

You could perhaps use a general assertTrue along with the exists filter:

self.assertTrue(User.objects.filter(id=10).exists())

(Normally you'd use get() with an id query, but that doesn't allow exists() and you'd have to catch the DoesNotExist exception if it's not found.)

like image 103
Daniel Roseman Avatar answered Feb 02 '26 18:02

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!