Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
rp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
14
Issues
14
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
La Quadrature du Net
rpteam
rp
Commits
62698501
Commit
62698501
authored
Apr 23, 2017
by
luxcem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update tests for votes
parent
ef2a5922
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
101 additions
and
3 deletions
+101
-3
apps/rp/models/vote.py
apps/rp/models/vote.py
+4
-0
apps/rp/tests/test_article.py
apps/rp/tests/test_article.py
+55
-0
apps/rp/tests/test_votes_views.py
apps/rp/tests/test_votes_views.py
+39
-0
apps/rp/views/votes.py
apps/rp/views/votes.py
+2
-2
conftest.py
conftest.py
+1
-1
No files found.
apps/rp/models/vote.py
View file @
62698501
...
...
@@ -62,6 +62,8 @@ class UnDVotedMixin(models.Model):
vote
.
save
()
diff_score
=
1
self
.
und_score
+=
diff_score
# Update self score, use update and filter to avoid triggering signals
self
.
__class__
.
objects
.
filter
(
id
=
self
.
id
).
update
(
und_score
=
F
(
"und_score"
)
+
diff_score
)
...
...
@@ -84,6 +86,7 @@ class UnDVotedMixin(models.Model):
vote
.
save
()
diff_score
=
-
1
self
.
und_score
+=
diff_score
# Update self score, use update and filter to avoid triggering signals
self
.
__class__
.
objects
.
filter
(
id
=
self
.
id
).
update
(
und_score
=
F
(
"und_score"
)
+
diff_score
)
...
...
@@ -91,5 +94,6 @@ class UnDVotedMixin(models.Model):
def
update_und_score
(
self
):
"""Reset score to the correct count (should not be necessary)"""
score
=
self
.
und_votes
.
aggregate
(
Sum
(
"score"
))[
"score__sum"
]
or
0
self
.
und_score
=
score
# Update self score, use update and filter to avoid triggering signals
self
.
__class__
.
objects
.
filter
(
id
=
self
.
id
).
update
(
und_score
=
score
)
apps/rp/tests/test_article.py
View file @
62698501
...
...
@@ -10,3 +10,58 @@ def test_init():
def
test_article
():
article
=
ArticleFactory
()
assert
type
(
article
)
==
Article
def
test_votes
():
article
=
ArticleFactory
()
# Upvote
article
.
upvote
(
"test_user"
)
article_db
=
Article
.
objects
.
get
(
id
=
article
.
id
)
assert
article
.
und_score
==
1
assert
article_db
.
und_score
==
1
votes
=
article_db
.
und_votes
.
all
()
assert
len
(
votes
)
==
1
assert
str
(
votes
[
0
])
==
"{}:{}:{}"
.
format
(
"test_user"
,
str
(
article_db
),
1
)
# Upvote -> upvote
article
.
upvote
(
"test_user"
)
article_db
=
Article
.
objects
.
get
(
id
=
article
.
id
)
assert
article
.
und_score
==
0
assert
article_db
.
und_score
==
0
# 0 -> downvote
article
.
downvote
(
"test_user"
)
article_db
=
Article
.
objects
.
get
(
id
=
article
.
id
)
assert
article
.
und_score
==
-
1
assert
article_db
.
und_score
==
-
1
# Downvote -> Downvote
article
.
downvote
(
"test_user"
)
article_db
=
Article
.
objects
.
get
(
id
=
article
.
id
)
assert
article
.
und_score
==
0
assert
article_db
.
und_score
==
0
# Downvote -> Upvote
article
.
downvote
(
"test_user"
)
article_db
=
Article
.
objects
.
get
(
id
=
article
.
id
)
assert
article
.
und_score
==
-
1
assert
article_db
.
und_score
==
-
1
article
.
upvote
(
"test_user"
)
article_db
=
Article
.
objects
.
get
(
id
=
article
.
id
)
assert
article
.
und_score
==
1
assert
article_db
.
und_score
==
1
# Upvote -> Downwote
article
.
downvote
(
"test_user"
)
article_db
=
Article
.
objects
.
get
(
id
=
article
.
id
)
assert
article
.
und_score
==
-
1
assert
article_db
.
und_score
==
-
1
article
.
update_und_score
()
article_db
=
Article
.
objects
.
get
(
id
=
article
.
id
)
assert
article
.
und_score
==
-
1
assert
article_db
.
und_score
==
-
1
apps/rp/tests/test_votes_views.py
0 → 100644
View file @
62698501
from
django.urls
import
reverse
from
django.test
import
TestCase
from
django.contrib.contenttypes.models
import
ContentType
from
userprofile.factories
import
ProfileFactory
from
rp.factories
import
ArticleFactory
from
rp.models
import
Article
class
VoteViewTestCase
(
TestCase
):
def
setUp
(
self
):
self
.
article
=
ArticleFactory
()
self
.
content_type
=
ContentType
.
objects
.
get_for_model
(
self
.
article
).
pk
self
.
profile
=
ProfileFactory
()
self
.
user
=
self
.
profile
.
user
self
.
client
.
force_login
(
self
.
user
)
def
test_votes
(
self
):
url_upvote
=
reverse
(
"und-upvote"
,
kwargs
=
{
"content_type"
:
self
.
content_type
,
"object_id"
:
self
.
article
.
id
})
url_downvote
=
reverse
(
"und-downvote"
,
kwargs
=
{
"content_type"
:
self
.
content_type
,
"object_id"
:
self
.
article
.
id
})
response
=
self
.
client
.
get
(
url_upvote
)
assert
response
.
status_code
==
200
article_db
=
Article
.
objects
.
get
(
id
=
self
.
article
.
id
)
assert
article_db
.
und_score
==
1
response
=
self
.
client
.
get
(
url_downvote
)
assert
response
.
status_code
==
200
article_db
=
Article
.
objects
.
get
(
id
=
self
.
article
.
id
)
assert
article_db
.
und_score
==
-
1
apps/rp/views/votes.py
View file @
62698501
...
...
@@ -6,14 +6,14 @@ from django.http import HttpResponse
def
upvote
(
request
,
content_type
,
object_id
):
ct
=
ContentType
.
objects
.
get_for_id
(
content_type
)
obj
=
ct
.
get_object_for_this_type
(
pk
=
object_id
)
obj
.
upvote
(
user
=
request
.
user
)
obj
.
upvote
(
user
name
=
request
.
user
.
username
)
return
HttpResponse
({
"success"
})
def
downvote
(
request
,
content_type
,
object_id
):
ct
=
ContentType
.
objects
.
get_for_id
(
content_type
)
obj
=
ct
.
get_object_for_this_type
(
pk
=
object_id
)
obj
.
downvote
(
user
=
request
.
user
)
obj
.
downvote
(
user
name
=
request
.
user
.
username
)
return
HttpResponse
({
"success"
})
...
...
conftest.py
View file @
62698501
...
...
@@ -6,7 +6,7 @@ import django
from
django.conf
import
settings
os
.
environ
.
setdefault
(
'DJANGO_SETTINGS_MODULE'
,
'project.settings'
)
os
.
environ
.
setdefault
(
"DJANGO_SETTINGS_MODULE"
,
"project.settings"
)
@
pytest
.
fixture
(
autouse
=
True
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment