test_article.py 1.8 KB
Newer Older
luxcem's avatar
luxcem committed
1
2
3
4
5
6
7
8
9
10
11
12
from rp.models import Article
from rp.factories import ArticleFactory
from rp.apps import RpConfig


def test_init():
    assert RpConfig.name == "rp"


def test_article():
    article = ArticleFactory()
    assert type(article) == Article
luxcem's avatar
luxcem committed
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


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