1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# 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.base import Session
from mediagoblin.db.models import MediaEntry
from mediagoblin.tests.tools import 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.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, test_app):
self._setup()
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, test_app):
self._setup()
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, test_app):
self._setup()
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, test_app):
self._setup()
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"
def test_existing_slug_cant_use_id(self, test_app):
self._setup()
# Getting tired of dealing with test_app and this mock.patch
# thing conflicting, getting lazy.
@mock.patch('uuid.uuid4', UUID_MOCK)
def _real_test():
# 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"
_real_test()
def test_existing_slug_cant_use_id_extra_junk(self, test_app):
self._setup()
# Getting tired of dealing with test_app and this mock.patch
# thing conflicting, getting lazy.
@mock.patch('uuid.uuid4', UUID_MOCK)
def _real_test():
# 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"
_real_test()
def test_garbage_slug(self, test_app):
"""
Titles that sound totally like Q*Bert shouldn't have slugs at
all. We'll just reference them by id.
,
/ \ (@!#?@!)
|\,/| ,-, /
| |#| ( ")~
/ \|/ \ L L
|\,/|\,/|
| |#, |#|
/ \|/ \|/ \
|\,/|\,/|\,/|
| |#| |#| |#|
/ \|/ \|/ \|/ \
|\,/|\,/|\,/|\,/|
| |#| |#| |#| |#|
\|/ \|/ \|/ \|/
"""
self._setup()
qbert_entry = self._insert_media_entry_fixture(
u"@!#?@!", save=False)
qbert_entry.generate_slug()
assert qbert_entry.slug is None
def test_media_data_init(test_app):
Session.rollback()
Session.remove()
media = MediaEntry()
media.media_type = u"mediagoblin.media_types.image"
assert media.media_data is None
media.media_data_init()
assert media.media_data is not None
obj_in_session = 0
for obj in Session():
obj_in_session += 1
print repr(obj)
assert obj_in_session == 0
|