test_model_blogpost.py
5.71 KB
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
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa 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.
#
# PyBossa 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 PyBossa. If not, see <http://www.gnu.org/licenses/>.
from default import Test, db, with_context, assert_not_raises
from nose.tools import raises, assert_raises
from sqlalchemy.exc import IntegrityError, DataError
from pybossa.model.project import Project
from pybossa.model.user import User
from pybossa.model.category import Category
from pybossa.model.blogpost import Blogpost
class TestBlogpostModel(Test):
def setUp(self):
super(TestBlogpostModel, self).setUp()
with self.flask_app.app_context():
user = User(email_addr="john.doe@example.com",
name="johndoe",
fullname="John Doe",
locale="en")
category = Category(name=u'cat', short_name=u'cat', description=u'cat')
project = Project(name='Application', short_name='app', description='desc',
owner=user, category=category)
db.session.add(user)
db.session.add(project)
db.session.commit()
def configure_fixtures(self):
self.project = db.session.query(Project).first()
self.user = db.session.query(User).first()
@with_context
def test_blogpost_title_length(self):
"""Test BLOGPOST model title length has a limit"""
self.configure_fixtures()
valid_title = 'a' * 255
invalid_title = 'a' * 256
blogpost = Blogpost(title=valid_title, body="body", project=self.project)
db.session.add(blogpost)
assert_not_raises(DataError, db.session.commit)
blogpost.title = invalid_title
assert_raises(DataError, db.session.commit)
@with_context
def test_blogpost_title_presence(self):
"""Test BLOGPOST a blogpost must have a title"""
self.configure_fixtures()
blogpost = Blogpost(title=None, body="body", project=self.project)
db.session.add(blogpost)
assert_raises(IntegrityError, db.session.commit)
@with_context
def test_blogpost_body_presence(self):
"""Test BLOGPOST a blogpost must have a body"""
self.configure_fixtures()
blogpost = Blogpost(title='title', body=None, project=self.project)
db.session.add(blogpost)
assert_raises(IntegrityError, db.session.commit)
@with_context
def test_blogpost_belongs_to_project(self):
"""Test BLOGPOSTS must belong to a project"""
self.configure_fixtures()
blogpost = Blogpost(title='title', body="body", project=None)
@with_context
def test_blogpost_belongs_to_project(self):
"""Test BLOGPOSTS must belong to a project"""
self.configure_fixtures()
blogpost = Blogpost(title='title', project=None)
db.session.add(blogpost)
assert_raises(IntegrityError, db.session.commit)
@with_context
def test_blogpost_is_deleted_after_project_deletion(self):
"""Test BLOGPOST no blogposts can exist after its project has been removed"""
self.configure_fixtures()
blogpost = Blogpost(title='title', body="body", project=self.project)
db.session.add(blogpost)
db.session.commit()
assert self.project in db.session
assert blogpost in db.session
db.session.delete(self.project)
db.session.commit()
assert self.project not in db.session
assert blogpost not in db.session
@with_context
def test_blogpost_deletion_doesnt_delete_project(self):
"""Test BLOGPOST when deleting a blogpost its parent project is not affected"""
self.configure_fixtures()
blogpost = Blogpost(title='title', body="body", project=self.project)
db.session.add(blogpost)
db.session.commit()
assert self.project in db.session
assert blogpost in db.session
db.session.delete(blogpost)
db.session.commit()
assert self.project in db.session
assert blogpost not in db.session
@with_context
def test_blogpost_owner_is_nullable(self):
"""Test BLOGPOST a blogpost owner can be none
(if the user is removed from the system)"""
self.configure_fixtures()
blogpost = Blogpost(title='title', body="body", project=self.project, owner=None)
db.session.add(blogpost)
assert_not_raises(IntegrityError, db.session.commit)
@with_context
def test_blogpost_is_not_deleted_after_owner_deletion(self):
"""Test BLOGPOST a blogpost remains when its owner user is removed
from the system"""
self.configure_fixtures()
owner = User(
email_addr="john.doe2@example.com",
name="johndoe2",
fullname="John Doe2",
locale="en")
blogpost = Blogpost(title='title', body="body", project=self.project, owner=owner)
db.session.add(blogpost)
db.session.commit()
assert owner in db.session
assert blogpost in db.session
db.session.delete(owner)
db.session.commit()
assert owner not in db.session
assert blogpost in db.session
assert blogpost.owner == None, blogpost.owner