Reputation: 33625
In Django what is the best way to test that an object can correctly save a relationship?
This is what I have, which works, but I am not 100% sure it is the right way to go.
class ModelCase(TestCase):
def setUp(self):
self.company = mommy.make(Company)
noz.assert_true(isinstance(self.company, Company))
self.campaign = mommy.make(Campaign)
def test_assign_campaign_to_company(self):
"""
Test if a campaign can be assigned to a company
"""
self.campaign.company = self.company
self.campaign.save()
noz.assert_true(self.campaign.company.name)
Does this really test the actually condition or should I be doing something else?
Upvotes: 0
Views: 460
Reputation: 5249
This assertion doesn't look up the database. It asserts on the objects in the memory.
To query the database best use:
(Campaign.objects
.filter(
pk=self.campaign.pk,
company=self.company)
.exists())
Upvotes: 2