def test_1to1_forward_deletion(self): cust = models.Customer(name='test 1-1 deletion') cust.save() extra = models.CustomerExtraJunk(customer=cust) extra.save() models.signal_log.clear() extra.delete() # No signals on `cust` self.assert_sent_exact('extra predelete', 'extra postdelete')
def test_1to1_forward_direct_assignment_obviously_fires(self): customer = models.Customer(name='1to1 forward test', company=self.company) customer.save() models.signal_log.clear() extra = models.CustomerExtraJunk() extra.customer = customer extra.save() self.assert_sent_exact('extra junk presave', 'extra junk postsave')
def test_1to1_reverse_deletion_not_allowed(self): # This was actually a bug as of django 1.8 # and was fixed in 1.9. https://code.djangoproject.com/ticket/14368 # So this test fails on 1.9! cust = models.Customer(name='test 1-1 deletion') cust.save() extra = models.CustomerExtraJunk(customer=cust) extra.save() models.signal_log.clear() with self.assertRaises(AttributeError): cust.extrajunk = None cust.save()
def test_1to1_reverse_direct_assignment_does_not_fire(self): customer = models.Customer(name='1to1 reverse test', company=self.company) customer.save() extra = models.CustomerExtraJunk() extra.save() models.signal_log.clear() customer.extrajunk = extra customer.save() self.assertIn('customer presave', models.signal_log.keys()) # Passes on both 1.8 and 1.9 self.assertNotIn('extra junk presave', models.signal_log.keys())
def test_1to1_reverse_direct_assignment_if_child_unsaved(self): customer = models.Customer(name='1to1 unsaved forward test', company=self.company) customer.save() extra = models.CustomerExtraJunk() # NOT saving yet customer.extrajunk = extra customer.save() # This is allowed even though extra not saved! self.assertEqual(extra.customer, customer) # 'extra presave' not sent because extra isn't actually saved yet self.assert_sent_exact('customer presave', 'customer postsave') # reloading customer proves we didn't actually save the relationship yet customer = models.Customer.objects.get(pk=customer.pk) with self.assertRaises(Exception): customer.extrajunk # saving extra does the trick extra.save() customer = models.Customer.objects.get(pk=customer.pk) self.assertEqual(customer.extrajunk, extra) self.assert_sent_exact( 'customer presave', 'customer postsave', 'extra junk postsave', 'extra junk presave')
def test_1to1_forward_direct_assignment_not_allowed_if_child_unsaved(self): customer = models.Customer(name='1to1 unsaved forward test', company=self.company) extra = models.CustomerExtraJunk() extra.customer = customer with self.assertRaises(ValueError): extra.save()