These tests need to have a MediaGoblin app setup so they can connect to the db!
[mediagoblin.git] / mediagoblin / tests / test_modelmethods.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
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.
8 #
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.
13 #
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/>.
16
17 # Maybe not every model needs a test, but some models have special
18 # methods, and so it makes sense to test them here.
19
20 from nose.tools import assert_equal
21
22 from mediagoblin.db.base import Session
23 from mediagoblin.db.models import MediaEntry
24
25 from mediagoblin.tests.tools import get_app, \
26 fixture_add_user
27
28 import mock
29
30
31 class FakeUUID(object):
32 hex = 'testtest-test-test-test-testtesttest'
33
34 UUID_MOCK = mock.Mock(return_value=FakeUUID())
35
36
37 class TestMediaEntrySlugs(object):
38 def setup(self):
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")
45
46 def _insert_media_entry_fixture(self, title=None, slug=None, this_id=None,
47 uploader=None, save=True):
48 entry = MediaEntry()
49 entry.title = title or u"Some title"
50 entry.slug = slug
51 entry.id = this_id
52 entry.uploader = uploader or self.chris_user.id
53 entry.media_type = u'image'
54
55 if save:
56 entry.save()
57
58 return entry
59
60 def test_unique_slug_from_title(self):
61 entry = self._insert_media_entry_fixture(u"Totally unique slug!", save=False)
62 entry.generate_slug()
63 assert entry.slug == u'totally-unique-slug'
64
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)
68 entry.generate_slug()
69 assert entry.slug == u"a-different-slug-there"
70
71 def test_old_weird_slug(self):
72 entry = self._insert_media_entry_fixture(
73 slug=u"wowee!!!!!", save=False)
74 entry.generate_slug()
75 assert entry.slug == u"wowee"
76
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)
80 entry.generate_slug()
81 assert entry.slug == u"beware-i-exist-9000"
82
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")
88
89 entry = self._insert_media_entry_fixture(
90 u"Beware, I exist!!", this_id=9000, save=False)
91 entry.generate_slug()
92 assert entry.slug == u"beware-i-exist-test"
93
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")
99
100 # This one grabs makes sure the annoyance doesn't stop
101 self._insert_media_entry_fixture(
102 slug=u"beware-i-exist-test")
103
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"
108
109 def test_garbage_slug(self):
110 """
111 Titles that sound totally like Q*Bert shouldn't have slugs at
112 all. We'll just reference them by id.
113
114 ,
115 / \ (@!#?@!)
116 |\,/| ,-, /
117 | |#| ( ")~
118 / \|/ \ L L
119 |\,/|\,/|
120 | |#, |#|
121 / \|/ \|/ \
122 |\,/|\,/|\,/|
123 | |#| |#| |#|
124 / \|/ \|/ \|/ \
125 |\,/|\,/|\,/|\,/|
126 | |#| |#| |#| |#|
127 \|/ \|/ \|/ \|/
128 """
129 qbert_entry = self._insert_media_entry_fixture(
130 u"@!#?@!", save=False)
131 qbert_entry.generate_slug()
132 assert qbert_entry.slug is None
133
134
135 def test_media_data_init():
136 get_app() # gotta init the db and etc
137 Session.rollback()
138 Session.remove()
139 media = MediaEntry()
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
144 obj_in_session = 0
145 for obj in Session():
146 obj_in_session += 1
147 print repr(obj)
148 assert_equal(obj_in_session, 0)