1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Maybe not every model needs a test, but some models have special
18 # methods, and so it makes sense to test them here.
20 from nose
.tools
import assert_equal
22 from mediagoblin
.db
.base
import Session
23 from mediagoblin
.db
.models
import MediaEntry
25 from mediagoblin
.tests
.tools
import get_app
, \
31 class FakeUUID(object):
32 hex = 'testtest-test-test-test-testtesttest'
34 UUID_MOCK
= mock
.Mock(return_value
=FakeUUID())
37 class TestMediaEntrySlugs(object):
39 self
.test_app
= get_app(dump_old_app
=True)
40 self
.chris_user
= fixture_add_user(u
'chris')
41 self
.emily_user
= fixture_add_user(u
'emily')
42 self
.existing_entry
= self
._insert
_media
_entry
_fixture
(
43 title
=u
"Beware, I exist!",
44 slug
=u
"beware-i-exist")
46 def _insert_media_entry_fixture(self
, title
=None, slug
=None, this_id
=None,
47 uploader
=None, save
=True):
49 entry
.title
= title
or u
"Some title"
52 entry
.uploader
= uploader
or self
.chris_user
.id
53 entry
.media_type
= u
'image'
60 def test_unique_slug_from_title(self
):
61 entry
= self
._insert
_media
_entry
_fixture
(u
"Totally unique slug!", save
=False)
63 assert entry
.slug
== u
'totally-unique-slug'
65 def test_old_good_unique_slug(self
):
66 entry
= self
._insert
_media
_entry
_fixture
(
67 u
"A title here", u
"a-different-slug-there", save
=False)
69 assert entry
.slug
== u
"a-different-slug-there"
71 def test_old_weird_slug(self
):
72 entry
= self
._insert
_media
_entry
_fixture
(
73 slug
=u
"wowee!!!!!", save
=False)
75 assert entry
.slug
== u
"wowee"
77 def test_existing_slug_use_id(self
):
78 entry
= self
._insert
_media
_entry
_fixture
(
79 u
"Beware, I exist!!", this_id
=9000, save
=False)
81 assert entry
.slug
== u
"beware-i-exist-9000"
83 @mock.patch('uuid.uuid4', UUID_MOCK
)
84 def test_existing_slug_cant_use_id(self
):
85 # This one grabs the nine thousand slug
86 self
._insert
_media
_entry
_fixture
(
87 slug
=u
"beware-i-exist-9000")
89 entry
= self
._insert
_media
_entry
_fixture
(
90 u
"Beware, I exist!!", this_id
=9000, save
=False)
92 assert entry
.slug
== u
"beware-i-exist-test"
94 @mock.patch('uuid.uuid4', UUID_MOCK
)
95 def test_existing_slug_cant_use_id_extra_junk(self
):
96 # This one grabs the nine thousand slug
97 self
._insert
_media
_entry
_fixture
(
98 slug
=u
"beware-i-exist-9000")
100 # This one grabs makes sure the annoyance doesn't stop
101 self
._insert
_media
_entry
_fixture
(
102 slug
=u
"beware-i-exist-test")
104 entry
= self
._insert
_media
_entry
_fixture
(
105 u
"Beware, I exist!!", this_id
=9000, save
=False)
106 entry
.generate_slug()
107 assert entry
.slug
== u
"beware-i-exist-testtest"
109 def test_garbage_slug(self
):
111 Titles that sound totally like Q*Bert shouldn't have slugs at
112 all. We'll just reference them by id.
129 qbert_entry
= self
._insert
_media
_entry
_fixture
(
130 u
"@!#?@!", save
=False)
131 qbert_entry
.generate_slug()
132 assert qbert_entry
.slug
is None
135 def test_media_data_init():
136 get_app() # gotta init the db and etc
140 media
.media_type
= u
"mediagoblin.media_types.image"
141 assert media
.media_data
is None
142 media
.media_data_init()
143 assert media
.media_data
is not None
145 for obj
in Session():
148 assert_equal(obj_in_session
, 0)