test_news.py
2.51 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
# -*- 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, with_context
from pybossa.news import get_news, notify_news_admins
from pybossa.core import sentinel
from factories import UserFactory
from redis import StrictRedis
try:
import cPickle as pickle
except ImportError: # pragma: no cover
import pickle
myset = 'scifabricnews'
class TestNews(Test):
def setUp(self):
super(TestNews, self).setUp()
self.connection = StrictRedis()
self.connection.flushall()
news = dict(updated='2015-01-01')
@with_context
def test_get_news_empty(self):
news = get_news()
assert len(news) == 0, len(news)
@with_context
def test_get_news_with_score_empty(self):
news = get_news(score=1)
assert len(news) == 0, len(news)
@with_context
def test_get_news(self):
sentinel.master.zadd(myset, 0, pickle.dumps(self.news))
news = get_news()
assert len(news) == 1, len(news)
news[0]['updated'] == self.news['updated'], news
@with_context
def test_get_news_with_score(self):
sentinel.master.zadd(myset, 0, pickle.dumps(self.news))
news = get_news(score=1)
assert len(news) == 0, len(news)
@with_context
def test_notify_news_admins(self):
user = UserFactory.create(admin=True)
notify_news_admins()
key = "notify:admin:%s" % user.id
value = sentinel.slave.get(key)
err_msg = "Key should exist"
assert value == str(1), err_msg
@with_context
def test_notify_news_admins(self):
user = UserFactory.create(admin=False)
user2 = UserFactory.create(admin=False)
notify_news_admins()
key = "notify:admin:%s" % user2.id
value = sentinel.slave.get(key)
err_msg = "Key should not exist"
assert value is None, err_msg