예제 #1
0
    def test_m2m_direct_forward_does_not_fire_related_presave(self):
        company = self.company
        customer = models.Customer(name='testing m2m direct forward', company=company)
        customer.save()
        cat1 = models.CustomerCategory(name='m2m cat 1')
        cat1.save()
        cat2 = models.CustomerCategory(name='m2m cat 2')
        cat2.save()

        models.signal_log.clear()

        customer.categories_direct = [cat1, cat2]
        customer.save()

        # The related model (categories) don't get presave
        self.assert_sent_exact('customer presave', 'customer postsave')
예제 #2
0
    def test_m2m_indirect_forward_fires_through_presave_but_not_rel_presave(self):
        company = self.company
        customer = models.Customer(name='testing m2m forward', company=company)
        customer.save()
        cat1 = models.CustomerCategory(name='m2m cat 1')
        cat1.save()
        cat2 = models.CustomerCategory(name='m2m cat 2')
        cat2.save()

        models.signal_log.clear()

        rel = models.CustomerCategoryRel(customer=customer, category=cat1)
        rel.save()

        # The 'through' model gets a presave, but not the related model (categories)
        self.assert_sent_exact('rel presave', 'rel postsave')
예제 #3
0
 def test_m2m_direct_assigment_does_not_allow_unsaved_children(self):
     customer = models.Customer(name='test unsaved m2m children')
     customer.save()
     cat1 = models.CustomerCategory(name='cat1')
     with self.assertRaises(ValueError):
         customer.categories_direct = [cat1]
     with self.assertRaises(ValueError):
         customer.categories_direct.add(cat1)
예제 #4
0
    def test_m2m_direct_reverse_does_not_fire_related_presave(self):
        company = self.company
        customer = models.Customer(name='testing m2m direct reverse', company=company)
        customer.save()
        cat1 = models.CustomerCategory(name='m2m cat 1')
        cat1.save()
        cat2 = models.CustomerCategory(name='m2m cat 2')
        cat2.save()

        models.signal_log.clear()

        cat1.customers_direct = [customer]
        cat2.customers_direct = [customer]
        cat1.save()
        cat2.save()

        # The customer is not saved.
        self.assert_sent_exact('category presave', 'category postsave')
예제 #5
0
    def test_m2m_indirect_cannot_set_attr_directly(self):
        company = self.company
        customer = models.Customer(name='testing m2m forward', company=company)
        customer.save()
        cat1 = models.CustomerCategory(name='m2m cat 1')
        cat1.save()
        cat2 = models.CustomerCategory(name='m2m cat 2')
        cat2.save()

        models.signal_log.clear()

        with self.assertRaises(AttributeError):
            # TIL you can't do this if there's a `through` model.
            customer.categories_indirect = [cat1, cat2]

        with self.assertRaises(AttributeError):
            # TIL you can't do this if there's a `through` model.
            cat1.customers_indirect = [customer]
예제 #6
0
    def test_MtoM_direct_reverse_deletion_does_not_send_related_signals(self):
        customer = models.Customer(name='cust')
        customer.save()
        cat1 = models.CustomerCategory()
        cat1.save()
        cat1.customers_direct = [customer]
        cat1.save()

        models.signal_log.clear()

        cat1.customers_direct = []
        cat1.save()

        self.assert_sent_exact('category presave', 'category postsave')
예제 #7
0
    def test_MtoM_indirect_deletion(self):
        company = self.company
        customer = models.Customer(name='testing m2m forward', company=company)
        customer.save()
        cat1 = models.CustomerCategory(name='m2m cat 1')
        cat1.save()

        rel = models.CustomerCategoryRel(customer=customer, category=cat1)
        rel.save()

        models.signal_log.clear()

        rel.delete()
        # The 'through' model gets a signal, but not the relateds
        self.assert_sent_exact('rel predelete', 'rel postdelete')