コード例 #1
0
ファイル: conf.py プロジェクト: ixc/glamkit-smartlinks
    def testDotNotationInSearchedFields(self):
        dot_conf = SmartLinkConf(
            Teacher.objects,
            searched_fields=(
                'person.name',
            )
        )

        p = Person.objects.create(
            name = 'Mark'
        )

        t = Teacher.objects.create(
            position = 'None',
            person = p
        )

        dot_conf.update_index_for_object(
            Teacher,
            t,
            created=True
        )

        self.assertEqual(
            dot_conf.find_object('mark'),
            t
        )
コード例 #2
0
    def testDotNotationInSearchedFields(self):
        dot_conf = SmartLinkConf(Teacher.objects,
                                 searched_fields=('person.name', ))

        p = Person.objects.create(name='Mark')

        t = Teacher.objects.create(position='None', person=p)

        dot_conf.update_index_for_object(Teacher, t, created=True)

        self.assertEqual(dot_conf.find_object('mark'), t)
コード例 #3
0
    def testRegisterLinks(self):
        conf = SmartLinkConf(queryset=Event.objects)
        smartlinks_conf = register((('e', 'event'), conf),
                                   (('m', 'movie'), conf))

        self.assertEqual(smartlinks_conf, {
            'e': conf,
            'event': conf,
            'm': conf,
            'movie': conf
        })
コード例 #4
0
ファイル: conf.py プロジェクト: ixc/glamkit-smartlinks
    def setUp(self):
        self.ae = self.assertEqual

        self.m = Movie.objects.create(
            title="Mad Max",
            slug="mad-max",
            year=1984
        )

        self.secret_movie = Movie.objects.create(
            title="Secret Movie",
            slug='secret-movie',
            public=False,
            year=2000
        )

        self.harry1 = Movie.objects.create(
            title="Dirty Harry",
            slug="dirty-harry",
            year=1971
        )

        self.harry2 = Movie.objects.create(
            title="Dirty Harry",
            slug='dirty-harry',
            year=1976
        )

        self.movie_conf = SmartLinkConf(
            Movie.objects,
            searched_fields=(
                'title',
                'slug',
                ('title', 'year',),
                '__unicode__',
                'pk'
            )
        )

        self.movie_conf.update_index_for_object(Movie,
            self.m,
            created=True)

        self.movie_conf.update_index_for_object(Movie,
            self.harry1,
            created=True)

        self.movie_conf.update_index_for_object(Movie,
            self.harry2,
            created=True)

        self.movie_conf.update_index_for_object(Movie,
            self.secret_movie,
            created=True)
コード例 #5
0
    def setUp(self):
        self.ae = self.assertEqual

        self.m = Movie.objects.create(title="Mad Max",
                                      slug="mad-max",
                                      year=1984)

        self.secret_movie = Movie.objects.create(title="Secret Movie",
                                                 slug='secret-movie',
                                                 public=False,
                                                 year=2000)

        self.harry1 = Movie.objects.create(title="Dirty Harry",
                                           slug="dirty-harry",
                                           year=1971)

        self.harry2 = Movie.objects.create(title="Dirty Harry",
                                           slug='dirty-harry',
                                           year=1976)

        self.movie_conf = SmartLinkConf(Movie.objects,
                                        searched_fields=('title', 'slug', (
                                            'title',
                                            'year',
                                        ), '__unicode__', 'pk'))

        self.movie_conf.update_index_for_object(Movie, self.m, created=True)

        self.movie_conf.update_index_for_object(Movie,
                                                self.harry1,
                                                created=True)

        self.movie_conf.update_index_for_object(Movie,
                                                self.harry2,
                                                created=True)

        self.movie_conf.update_index_for_object(Movie,
                                                self.secret_movie,
                                                created=True)
コード例 #6
0
    def testSanityChecks(self):
        # No such field on the model => exception.
        self.assertRaises(
            IncorrectlyConfiguredSmartlinkException, register_smart_link,
            ('e', ), SmartLinkConf(Event.objects, searched_fields=('blah', )))

        # Searched field is a function with too many argument => exception.
        self.assertRaises(
            IncorrectlyConfiguredSmartlinkException, register_smart_link,
            ('e'),
            SmartLinkConf(Event.objects,
                          searched_fields=('my_favourite_func')))

        # While registering the callable with no args should work just fine.
        register_smart_link(
            ('zzz', ),
            SmartLinkConf(Event.objects,
                          searched_fields=('my_func_without_args', )))

        # We also can't register the model with same shortcut twice.
        self.assertRaises(AlreadyRegisteredSmartlinkException,
                          register_smart_link, ('zzz', ),
                          SmartLinkConf(Event.objects))
コード例 #7
0
    def testIndexRecreation(self):
        register_smart_link(('m', 'movie'),
                            SmartLinkConf(Movie.objects,
                                          searched_fields=('title', )))

        self.m1 = Movie.objects.create(title="Mad Max",
                                       slug="mad-max",
                                       year="1986")
        self.m2 = Movie.objects.create(title="Once upon a time in the West",
                                       slug="once",
                                       year="1990")

        # Now let's tinker with the smartlink index and see whether it would
        # be able to restore itself to the initial state.
        IndexEntry.objects.all().delete()

        IndexEntry.objects.create(value="bogus",
                                  content_type=ContentType.objects.all()[0],
                                  object_id=200)

        IndexEntry.objects.create(value="bogus2",
                                  content_type=ContentType.objects.all()[0],
                                  object_id=200)

        # ...and see whether it can re-create itself properly.
        call_command('reset_smartlink_index')

        self.assertEqual(IndexEntry.objects.count(), 2)

        # As only one field is searched, it should create one entry per
        # :py:class:`Movie` object.
        self.assertEqual(
            IndexEntry.objects.filter(object_id=self.m1.pk).count(), 1)

        self.assertEqual(
            IndexEntry.objects.filter(object_id=self.m2.pk).count(), 1)
コード例 #8
0
    def testRegisterLink(self):
        conf = SmartLinkConf(queryset=Event.objects)

        smartlinks_conf = register_smart_link(('e', 'event'), conf)

        self.assertEqual(smartlinks_conf, {'e': conf, 'event': conf})
コード例 #9
0
ファイル: conf.py プロジェクト: ixc/glamkit-smartlinks
class ConfTest(TestCase):
    def setUp(self):
        self.ae = self.assertEqual

        self.m = Movie.objects.create(
            title="Mad Max",
            slug="mad-max",
            year=1984
        )

        self.secret_movie = Movie.objects.create(
            title="Secret Movie",
            slug='secret-movie',
            public=False,
            year=2000
        )

        self.harry1 = Movie.objects.create(
            title="Dirty Harry",
            slug="dirty-harry",
            year=1971
        )

        self.harry2 = Movie.objects.create(
            title="Dirty Harry",
            slug='dirty-harry',
            year=1976
        )

        self.movie_conf = SmartLinkConf(
            Movie.objects,
            searched_fields=(
                'title',
                'slug',
                ('title', 'year',),
                '__unicode__',
                'pk'
            )
        )

        self.movie_conf.update_index_for_object(Movie,
            self.m,
            created=True)

        self.movie_conf.update_index_for_object(Movie,
            self.harry1,
            created=True)

        self.movie_conf.update_index_for_object(Movie,
            self.harry2,
            created=True)

        self.movie_conf.update_index_for_object(Movie,
            self.secret_movie,
            created=True)

    def tearDown(self):
        IndexEntry.objects.all().delete()

    def testTemplates(self):
        # Test normal template.
        obj = type("MyObj", (object,), dict(
            __unicode__ = lambda self: u"obj",
            get_absolute_url = lambda self: "google.com"
        ))

        self.assertEqual(
            self.movie_conf.template.render(
                Context({
                    'obj': obj,
                    "verbose_text": u"verbose_text"
                })
            ),
            "".join(
                ['<a href="google.com" title="obj">',
                 'verbose_text',
                 '</a>']
        ))


        # Unresolved template.
        self.assertEqual(
            self.movie_conf.unresolved_template.render(
                Context({
                    "verbose_text": "verbose_text"
                })
            ),
            '<span class="smartlinks-unresolved">verbose_text</span>'
        )

        # Ambigous template.
        self.assertEqual(
            self.movie_conf.ambiguous_template.render(
                Context({
                    "verbose_text": "verbose_text"
                })
            ),
            '<span class="smartlinks-ambiguous">verbose_text</span>'
        )

        # Disallowed embeds template.
        self.assertEqual(
            self.movie_conf.disallowed_embed_template.render(
                Context({
                    "smartlink_text": "smartlink text"
                })
            ),
            '<span class="smartlinks-unallowed">smartlink text</span>'
        )


        # No model found template
        self.assertEqual(
            self.movie_conf.unresolved_template.render(
                Context({
                    "verbose_text": "smartlink_text"
                })
            ),
            '<span class="smartlinks-unresolved">smartlink_text</span>'
        )

        
    def testFindObject(self):
        self.assertEqual(
            self.movie_conf.find_object(
                query="Mad Max"
            ),
            self.m
        )

        self.assertEqual(
            self.movie_conf.find_object(
                query="Mad Max 1984"
            ),
            self.m
        )

        self.assertEqual(
            self.movie_conf.find_object(
                query=unicode(self.m.pk)
            ),
            self.m
        )

        self.assertEqual(
            self.movie_conf.find_object(
                query="mad-max-1984"
            ),
            self.m
        )

        # Falls back to __startswith if the object
        # can not be found.
        self.assertEqual(
            self.movie_conf.find_object(
                query="mad-max-1984"
            ),
            self.m
        )

        # Non-public movies can not be smartlinked to.
        self.assertRaises(
            IndexEntry.DoesNotExist,
            lambda: self.movie_conf.find_object(
                "Secret Movie"
            )
        )

        # We don't want the smartlink resolved
        # when there is an ambiguity.
        self.assertRaises(
            IndexEntry.MultipleObjectsReturned,
            lambda: self.movie_conf.find_object(
                "Dirty Harry"
            )
        )

        self.assertEqual(
            self.movie_conf.find_object(
                query="Dirty Harry: 1976"
            ),
            self.harry2
        )

    def testUpdateIndexForObject(self):
        # Dirty Harry 1971 would have:
        expected_entries = (
            u'dirtyharry',
            u'dirtyharry1971',
            unicode(self.harry1.pk),
            u'dirtyharryreleasedin1971',
        )

        # Let's check items first.
        indexed_entries = lambda: IndexEntry.objects.filter(
            content_type = ContentType.objects.get_for_model(Movie),
            object_id = self.harry1.pk
        )

        self.assertItemsEqual(
            expected_entries,
            [i.value for i in indexed_entries()]
        )

        # Signal for deleting the object.
        self.movie_conf.update_index_for_object(Movie,
            self.harry1,
            created='deleteme')

        # Now nothing should be stored in the index.
        self.assertItemsEqual(
            [],
            indexed_entries()
        )

        # And let's test creation.
        self.movie_conf.update_index_for_object(Movie,
            self.harry1,
            created=True
        )

        # Now they should be equal back again.
        self.assertItemsEqual(
            expected_entries,
            [i.value for i in indexed_entries()]
        )

        # And let's test editing.
        self.harry1.title = "Awesome Harry"
        self.harry1.slug = "awesome-harry"

        expected_entries = (
            u'awesomeharry',
            u'awesomeharry1971',
            unicode(self.harry1.pk),
            u'awesomeharryreleasedin1971',
        )

        # ...and the editing should work as well.
        self.movie_conf.update_index_for_object(Movie,
            self.harry1,
            created=False
        )


        self.assertItemsEqual(
            expected_entries,
            [i.value for i in indexed_entries()]
        )

    def testDotNotationInSearchedFields(self):
        dot_conf = SmartLinkConf(
            Teacher.objects,
            searched_fields=(
                'person.name',
            )
        )

        p = Person.objects.create(
            name = 'Mark'
        )

        t = Teacher.objects.create(
            position = 'None',
            person = p
        )

        dot_conf.update_index_for_object(
            Teacher,
            t,
            created=True
        )

        self.assertEqual(
            dot_conf.find_object('mark'),
            t
        )

    def testTrimming(self):
        # Trimming should work properly.
        m = Movie.objects.create(
            title="Amelie" * 300,
            slug='amelie',
            public=True,
            year=2000
        )

        self.movie_conf.update_index_for_object(
            Movie,
            m,
            created=True
        )

        self.assertEqual(
            self.movie_conf.find_object('Amelie'),
            m
        )
コード例 #10
0
class ConfTest(TestCase):
    def setUp(self):
        self.ae = self.assertEqual

        self.m = Movie.objects.create(title="Mad Max",
                                      slug="mad-max",
                                      year=1984)

        self.secret_movie = Movie.objects.create(title="Secret Movie",
                                                 slug='secret-movie',
                                                 public=False,
                                                 year=2000)

        self.harry1 = Movie.objects.create(title="Dirty Harry",
                                           slug="dirty-harry",
                                           year=1971)

        self.harry2 = Movie.objects.create(title="Dirty Harry",
                                           slug='dirty-harry',
                                           year=1976)

        self.movie_conf = SmartLinkConf(Movie.objects,
                                        searched_fields=('title', 'slug', (
                                            'title',
                                            'year',
                                        ), '__unicode__', 'pk'))

        self.movie_conf.update_index_for_object(Movie, self.m, created=True)

        self.movie_conf.update_index_for_object(Movie,
                                                self.harry1,
                                                created=True)

        self.movie_conf.update_index_for_object(Movie,
                                                self.harry2,
                                                created=True)

        self.movie_conf.update_index_for_object(Movie,
                                                self.secret_movie,
                                                created=True)

    def tearDown(self):
        IndexEntry.objects.all().delete()

    def testTemplates(self):
        # Test normal template.
        obj = type(
            "MyObj", (object, ),
            dict(__unicode__=lambda self: u"obj",
                 get_absolute_url=lambda self: "google.com"))

        self.assertEqual(
            self.movie_conf.template.render(
                Context({
                    'obj': obj,
                    "verbose_text": u"verbose_text"
                })),
            "".join(
                ['<a href="google.com" title="obj">', 'verbose_text', '</a>']))

        # Unresolved template.
        self.assertEqual(
            self.movie_conf.unresolved_template.render(
                Context({"verbose_text": "verbose_text"})),
            '<span class="smartlinks-unresolved">verbose_text</span>')

        # Ambigous template.
        self.assertEqual(
            self.movie_conf.ambiguous_template.render(
                Context({"verbose_text": "verbose_text"})),
            '<span class="smartlinks-ambiguous">verbose_text</span>')

        # Disallowed embeds template.
        self.assertEqual(
            self.movie_conf.disallowed_embed_template.render(
                Context({"smartlink_text": "smartlink text"})),
            '<span class="smartlinks-unallowed">smartlink text</span>')

        # No model found template
        self.assertEqual(
            self.movie_conf.unresolved_template.render(
                Context({"verbose_text": "smartlink_text"})),
            '<span class="smartlinks-unresolved">smartlink_text</span>')

    def testFindObject(self):
        self.assertEqual(self.movie_conf.find_object(query="Mad Max"), self.m)

        self.assertEqual(self.movie_conf.find_object(query="Mad Max 1984"),
                         self.m)

        self.assertEqual(self.movie_conf.find_object(query=unicode(self.m.pk)),
                         self.m)

        self.assertEqual(self.movie_conf.find_object(query="mad-max-1984"),
                         self.m)

        # Falls back to __startswith if the object
        # can not be found.
        self.assertEqual(self.movie_conf.find_object(query="mad-max-1984"),
                         self.m)

        # Non-public movies can not be smartlinked to.
        self.assertRaises(IndexEntry.DoesNotExist,
                          lambda: self.movie_conf.find_object("Secret Movie"))

        # We don't want the smartlink resolved
        # when there is an ambiguity.
        self.assertRaises(IndexEntry.MultipleObjectsReturned,
                          lambda: self.movie_conf.find_object("Dirty Harry"))

        self.assertEqual(
            self.movie_conf.find_object(query="Dirty Harry: 1976"),
            self.harry2)

    def testUpdateIndexForObject(self):
        # Dirty Harry 1971 would have:
        expected_entries = (
            u'dirtyharry',
            u'dirtyharry1971',
            unicode(self.harry1.pk),
            u'dirtyharryreleasedin1971',
        )

        # Let's check items first.
        indexed_entries = lambda: IndexEntry.objects.filter(
            content_type=ContentType.objects.get_for_model(Movie),
            object_id=self.harry1.pk)

        self.assertItemsEqual(expected_entries,
                              [i.value for i in indexed_entries()])

        # Signal for deleting the object.
        self.movie_conf.update_index_for_object(Movie,
                                                self.harry1,
                                                created='deleteme')

        # Now nothing should be stored in the index.
        self.assertItemsEqual([], indexed_entries())

        # And let's test creation.
        self.movie_conf.update_index_for_object(Movie,
                                                self.harry1,
                                                created=True)

        # Now they should be equal back again.
        self.assertItemsEqual(expected_entries,
                              [i.value for i in indexed_entries()])

        # And let's test editing.
        self.harry1.title = "Awesome Harry"
        self.harry1.slug = "awesome-harry"

        expected_entries = (
            u'awesomeharry',
            u'awesomeharry1971',
            unicode(self.harry1.pk),
            u'awesomeharryreleasedin1971',
        )

        # ...and the editing should work as well.
        self.movie_conf.update_index_for_object(Movie,
                                                self.harry1,
                                                created=False)

        self.assertItemsEqual(expected_entries,
                              [i.value for i in indexed_entries()])

    def testDotNotationInSearchedFields(self):
        dot_conf = SmartLinkConf(Teacher.objects,
                                 searched_fields=('person.name', ))

        p = Person.objects.create(name='Mark')

        t = Teacher.objects.create(position='None', person=p)

        dot_conf.update_index_for_object(Teacher, t, created=True)

        self.assertEqual(dot_conf.find_object('mark'), t)

    def testTrimming(self):
        # Trimming should work properly.
        m = Movie.objects.create(title="Amelie" * 300,
                                 slug='amelie',
                                 public=True,
                                 year=2000)

        self.movie_conf.update_index_for_object(Movie, m, created=True)

        self.assertEqual(self.movie_conf.find_object('Amelie'), m)
コード例 #11
0
    def setUp(self):
        if not 'zzz' in conf.smartlinks_conf:
            register_smart_link(('zzz',), SmartLinkConf(queryset=Movie2.objects,
                searched_fields=('title', 'slug',),))

        self.l = LinkModel(link=u'[[ my awesome Movie2 | Terrible ]]')