示例#1
0
    def test_can_access_tags_on_unsaved_instance(self):
        mission_burrito = Place(name='Mission Burrito')
        self.assertEqual(0, mission_burrito.tags.count())

        mission_burrito.tags.add('mexican', 'burrito')
        self.assertEqual(2, mission_burrito.tags.count())
        self.assertEqual(Tag, mission_burrito.tags.all()[0].__class__)
        self.assertTrue([tag for tag in mission_burrito.tags.all() if tag.name == 'mexican'])

        mission_burrito.save()
        self.assertEqual(2, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())

        mission_burrito.tags.remove('burrito')
        self.assertEqual(1, mission_burrito.tags.count())
        # should not affect database until we save
        self.assertEqual(2, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())
        mission_burrito.save()
        self.assertEqual(1, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())

        mission_burrito.tags.clear()
        self.assertEqual(0, mission_burrito.tags.count())
        # should not affect database until we save
        self.assertEqual(1, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())
        mission_burrito.save()
        self.assertEqual(0, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())

        mission_burrito.tags.set('mexican', 'burrito')
        self.assertEqual(2, mission_burrito.tags.count())
        self.assertEqual(0, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())
        mission_burrito.save()
        self.assertEqual(2, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())
    def test_can_access_tags_on_unsaved_instance(self):
        mission_burrito = Place(name='Mission Burrito')
        self.assertEqual(0, mission_burrito.tags.count())

        mission_burrito.tags.add('mexican', 'burrito')
        self.assertEqual(2, mission_burrito.tags.count())
        self.assertEqual(Tag, mission_burrito.tags.all()[0].__class__)
        self.assertTrue([tag for tag in mission_burrito.tags.all() if tag.name == 'mexican'])

        mission_burrito.save()
        self.assertEqual(2, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())

        mission_burrito.tags.remove('burrito')
        self.assertEqual(1, mission_burrito.tags.count())
        # should not affect database until we save
        self.assertEqual(2, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())
        mission_burrito.save()
        self.assertEqual(1, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())

        mission_burrito.tags.clear()
        self.assertEqual(0, mission_burrito.tags.count())
        # should not affect database until we save
        self.assertEqual(1, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())
        mission_burrito.save()
        self.assertEqual(0, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())

        mission_burrito.tags.set('mexican', 'burrito')
        self.assertEqual(2, mission_burrito.tags.count())
        self.assertEqual(0, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())
        mission_burrito.save()
        self.assertEqual(2, TaggedPlace.objects.filter(content_object_id=mission_burrito.id).count())
示例#3
0
    def test_tag_form_field(self):
        class PlaceForm(ClusterForm):
            class Meta:
                model = Place
                exclude_formsets = ['tagged_items', 'reviews']
                fields = ['name', 'tags']

        mission_burrito = Place(name='Mission Burrito')
        mission_burrito.tags.add('mexican', 'burrito')

        form = PlaceForm(instance=mission_burrito)
        self.assertEqual(2, len(form['tags'].value()))
        expected_instance = TaggedPlace if TAGGIT_VERSION < (1, ) else Tag
        self.assertEqual(expected_instance, form['tags'].value()[0].__class__)

        form = PlaceForm({
            'name': "Mission Burrito",
            'tags': "burrito, fajita"
        },
                         instance=mission_burrito)
        self.assertTrue(form.is_valid())
        mission_burrito = form.save(commit=False)
        self.assertTrue(
            Tag.objects.get(name='burrito') in mission_burrito.tags.all())
        self.assertTrue(
            Tag.objects.get(name='fajita') in mission_burrito.tags.all())
        self.assertFalse(
            Tag.objects.get(name='mexican') in mission_burrito.tags.all())
示例#4
0
 def test_integers(self):
     """Adding an integer as a tag should raise a ValueError"""
     mission_burrito = Place(name='Mission Burrito')
     with self.assertRaisesRegexp(ValueError, (
             r"Cannot add 1 \(<(type|class) 'int'>\). "
             r"Expected <class 'django.db.models.base.ModelBase'> or str.")):
         mission_burrito.tags.add(1)
示例#5
0
    def test_prefetch_tags_doesnt_break(self):
        mission_burrito = Place(name='Mission Burrito')
        mission_burrito.tags.add('mexican', 'burrito')
        mission_burrito.save()

        atomic_burger = Place(name='Atomic Burger')
        atomic_burger.tags.add('burger')
        atomic_burger.save()

        places = list(Place.objects.order_by('name').prefetch_related('tags'))
        self.assertEqual(places[0].name, 'Atomic Burger')
        self.assertEqual(places[0].tags.first().name, 'burger')
示例#6
0
    def test_render_tag_form(self):
        class PlaceForm(ClusterForm):
            class Meta:
                model = Place
                exclude_formsets = ['tagged_items', 'reviews']
                fields = ['name', 'tags']

        mission_burrito = Place(name="Mission Burrito")
        mission_burrito.tags.add('burrito', 'mexican')
        form = PlaceForm(instance=mission_burrito)
        form_html = form.as_p()
        self.assertInHTML(
            '<input type="text" name="tags" value="burrito, mexican" id="id_tags">',
            form_html)
示例#7
0
    def test_prefetch_tags_doesnt_break(self):
        mission_burrito = Place(name='Mission Burrito')
        mission_burrito.tags.add('mexican', 'burrito')
        mission_burrito.save()

        atomic_burger = Place(name='Atomic Burger')
        atomic_burger.tags.add('burger')
        atomic_burger.save()

        places = list(Place.objects.order_by('name').prefetch_related('tags'))
        self.assertEqual(places[0].name, 'Atomic Burger')
        self.assertEqual(places[0].tags.first().name, 'burger')
    def test_create_with_tags(self):
        class PlaceForm(ClusterForm):
            class Meta:
                model = Place
                exclude_formsets = ['tagged_items', 'reviews']
                fields = ['name', 'tags']

        form = PlaceForm({
            'name': "Mission Burrito",
            'tags': "burrito, fajita"
        }, instance=Place())
        self.assertTrue(form.is_valid())
        mission_burrito = form.save()
        reloaded_mission_burrito = Place.objects.get(pk=mission_burrito.pk)
        self.assertEqual(
            set(reloaded_mission_burrito.tags.all()),
            set([Tag.objects.get(name='burrito'), Tag.objects.get(name='fajita')])
        )
示例#9
0
    def test_case_insensitive_tags(self):
        mission_burrito = Place(name='Mission Burrito')
        mission_burrito.tags.add('burrito')
        mission_burrito.tags.add('Burrito')

        self.assertEqual(1, mission_burrito.tags.count())