New mediaentry slug tests now pass!

- fixed some issues with "whacking uuid junk on the slug"
 - uuid4() -> uuid.uuid4() so that mock will work right
 - added all the tests!
This commit is contained in:
Christopher Allan Webber 2013-01-23 16:49:54 -06:00
parent 394a4a37f7
commit a81082fcaf
2 changed files with 134 additions and 3 deletions

View File

@ -27,7 +27,7 @@ These functions now live here and get "mixed in" into the
real objects.
"""
from uuid import uuid4
import uuid
from werkzeug.utils import cached_property
@ -113,9 +113,10 @@ class MediaEntryMixin(object):
# okay, still no success;
# let's whack junk on there till it's unique.
self.slug += '-'
self.slug += '-' + uuid.uuid4().hex[:4]
# keep going if necessary!
while check_media_slug_used(self.uploader, self.slug, self.id):
self.slug += uuid4().hex[:4]
self.slug += uuid.uuid4().hex[:4]
@property
def description_html(self):

View File

@ -0,0 +1,130 @@
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Maybe not every model needs a test, but some models have special
# methods, and so it makes sense to test them here.
from mediagoblin.db.models import MediaEntry
from mediagoblin.tests.tools import get_test_app, \
fixture_add_user
import mock
class FakeUUID(object):
hex = 'testtest-test-test-test-testtesttest'
UUID_MOCK = mock.Mock(return_value=FakeUUID())
class TestMediaEntrySlugs(object):
def setUp(self):
self.test_app = get_test_app(dump_old_app=True)
self.chris_user = fixture_add_user(u'chris')
self.emily_user = fixture_add_user(u'emily')
self.existing_entry = self._insert_media_entry_fixture(
title=u"Beware, I exist!",
slug=u"beware-i-exist")
def _insert_media_entry_fixture(self, title=None, slug=None, this_id=None,
uploader=None, save=True):
entry = MediaEntry()
entry.title = title or u"Some title"
entry.slug = slug
entry.id = this_id
entry.uploader = uploader or self.chris_user.id
entry.media_type = u'image'
if save:
entry.save()
return entry
def test_unique_slug_from_title(self):
entry = self._insert_media_entry_fixture(u"Totally unique slug!", save=False)
entry.generate_slug()
assert entry.slug == u'totally-unique-slug'
def test_old_good_unique_slug(self):
entry = self._insert_media_entry_fixture(
u"A title here", u"a-different-slug-there", save=False)
entry.generate_slug()
assert entry.slug == u"a-different-slug-there"
def test_old_weird_slug(self):
entry = self._insert_media_entry_fixture(
slug=u"wowee!!!!!", save=False)
entry.generate_slug()
assert entry.slug == u"wowee"
def test_existing_slug_use_id(self):
entry = self._insert_media_entry_fixture(
u"Beware, I exist!!", this_id=9000, save=False)
entry.generate_slug()
assert entry.slug == u"beware-i-exist-9000"
@mock.patch('uuid.uuid4', UUID_MOCK)
def test_existing_slug_cant_use_id(self):
# This one grabs the nine thousand slug
self._insert_media_entry_fixture(
slug=u"beware-i-exist-9000")
entry = self._insert_media_entry_fixture(
u"Beware, I exist!!", this_id=9000, save=False)
entry.generate_slug()
assert entry.slug == u"beware-i-exist-test"
@mock.patch('uuid.uuid4', UUID_MOCK)
def test_existing_slug_cant_use_id_extra_junk(self):
# This one grabs the nine thousand slug
self._insert_media_entry_fixture(
slug=u"beware-i-exist-9000")
# This one grabs makes sure the annoyance doesn't stop
self._insert_media_entry_fixture(
slug=u"beware-i-exist-test")
entry = self._insert_media_entry_fixture(
u"Beware, I exist!!", this_id=9000, save=False)
entry.generate_slug()
assert entry.slug == u"beware-i-exist-testtest"
def test_garbage_slug(self):
"""
Titles that sound totally like Q*Bert shouldn't have slugs at
all. We'll just reference them by id.
,
/ \ (@!#?@!)
|\,/| ,-, /
| |#| ( ")~
/ \|/ \ L L
|\,/|\,/|
| |#, |#|
/ \|/ \|/ \
|\,/|\,/|\,/|
| |#| |#| |#|
/ \|/ \|/ \|/ \
|\,/|\,/|\,/|\,/|
| |#| |#| |#| |#|
\|/ \|/ \|/ \|/
"""
qbert_entry = self._insert_media_entry_fixture(
u"@!#?@!", save=False)
qbert_entry.generate_slug()
assert qbert_entry.slug is None