aboutsummaryrefslogtreecommitdiff
blob: 94e9d9f99c1ce0231dcb3adcdf04899e6e5857cc (plain)
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
# -*- coding: utf-8 -*-
"""
    grumpy.testsuite.favorites
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    Unittests for "favorite" user packages unittests.

    :copyright: (c) by 2010 Priit Laes.
    :license: BSD, see LICENSE for details.
"""
from . import GrumpyTestCase

from grumpy.models import Ebuild, Favorite, Package, User
from sqlalchemy.exc import IntegrityError

import time, unittest

class FavoritesTestCase(GrumpyTestCase):

    def make_users(self):
        u = [User('user1@gentoo.org', 'http://example.net/openid1'),
             User('user2@gentoo.org', 'http://example.net/openid2')]
        self.db.session.add_all(u)
        self.db.session.commit()
        return u

    def make_packages(self):
        p = []
        p.append(Package('sys-misc', 'testpkg', 'Test Package', \
                         'Long Description', 'http://example.com/test', \
                         time.time()))
        p[0].ebuilds.append(Ebuild(p[0], '1.0', 2, 0, 'x86,amd64', 'doc', ''))
        p[0].ebuilds.append(Ebuild(p[0], '1.1', 2, 0, '~x86,~amd64', 'doc', ''))
        p.append(Package('sys-devel', 'testpkg', 'Test Package 2', \
                         'Long Description 2', 'http://example.com/test2', \
                         time.time()))
        p[1].ebuilds.append(Ebuild(p[1], '0.1', 2, 0, 'x86,amd64', 'doc', ''))
        self.db.session.add_all(p)
        self.db.session.commit()
        return p

    def test_favorites(self):
        with self.app.test_request_context():
            _u = self.make_users()
            _p = self.make_packages()
            assert User.query.count() == 2
            assert Package.query.count() == 2

            u = _u[0]
            u.favorites.append(Favorite(_p[0]))
            u.favorites.append(Favorite(_p[1]))
            self.db.session.commit()

            assert Favorite.query.count() == 2
            assert len(u.favorites) == 2

    def test_favorites_uniqueness(self):
        with self.app.test_request_context():
            _u = self.make_users()
            _p = self.make_packages()

            u = _u[0]
            u.favorites.append(Favorite(_p[0]))
            self.db.session.commit()

            u.favorites.append(Favorite(_p[0]))
            self.assertRaises(IntegrityError, self.db.session.commit)
            self.db.session.rollback()
            assert len(u.favorites) == 1
            for i in u.favorites:
                assert u == i.user

def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(FavoritesTestCase))
    return suite