Browse Source

Fixtures management

Bastien Sevajol 9 years ago
parent
commit
b2a8c1d9cc

+ 39 - 0
tracim/tracim/fixtures/__init__.py View File

1
+import transaction
2
+
3
+from tracim.model import DBSession
4
+
5
+
6
+class Fixture(object):
7
+
8
+    """ Fixture classes (list) required for this fixtures"""
9
+    require = NotImplemented
10
+
11
+    def __init__(self, session):
12
+        self._session = session
13
+
14
+    def insert(self):
15
+        raise NotImplementedError()
16
+
17
+
18
+class FixturesLoader(object):
19
+    """
20
+    Fixtures loader. Load each fixture once.
21
+    """
22
+
23
+    def __init__(self, loaded=None):
24
+        loaded = [] if loaded is None else loaded
25
+        self._loaded = loaded
26
+
27
+    def loads(self, fixtures_classes):
28
+        for fixture_class in fixtures_classes:
29
+            for required_fixture_class in fixture_class.require:
30
+                self._load(required_fixture_class)
31
+            self._load(fixture_class)
32
+
33
+    def _load(self, fixture_class):
34
+        if fixture_class not in self._loaded:
35
+            fixture = fixture_class(DBSession)
36
+            fixture.insert()
37
+            self._loaded.append(fixture_class)
38
+            DBSession.flush()
39
+            transaction.commit()

+ 49 - 0
tracim/tracim/fixtures/users_and_groups.py View File

1
+# -*- coding: utf-8 -*-
2
+from tracim import model
3
+from tracim.fixtures import Fixture
4
+
5
+
6
+class Base(Fixture):
7
+    require = []
8
+
9
+    def insert(self):
10
+        u = model.User()
11
+        u.display_name = 'Global manager'
12
+        u.email = 'admin@admin.admin'
13
+        u.password = 'admin@admin.admin'
14
+        self._session.add(u)
15
+
16
+        g1 = model.Group()
17
+        g1.group_id = 1
18
+        g1.group_name = 'users'
19
+        g1.display_name = 'Users'
20
+        g1.users.append(u)
21
+        self._session.add(g1)
22
+
23
+        g2 = model.Group()
24
+        g2.group_id = 2
25
+        g2.group_name = 'managers'
26
+        g2.display_name = 'Global Managers'
27
+        g2.users.append(u)
28
+        self._session.add(g2)
29
+
30
+        g3 = model.Group()
31
+        g3.group_id = 3
32
+        g3.group_name = 'administrators'
33
+        g3.display_name = 'Administrators'
34
+        g3.users.append(u)
35
+        self._session.add(g3)
36
+
37
+
38
+class Test(Fixture):
39
+    require = [Base, ]
40
+
41
+    def insert(self):
42
+        g2 = self._session.query(model.Group).filter(model.Group.group_name == 'managers').one()
43
+
44
+        lawrence = model.User()
45
+        lawrence.display_name = 'Lawrence L.'
46
+        lawrence.email = 'lawrence-not-real-email@fsf.org'
47
+        lawrence.password = 'foobarbaz'
48
+        self._session.add(lawrence)
49
+        g2.users.append(lawrence)

+ 12 - 1
tracim/tracim/tests/__init__.py View File

25
 from webtest import TestApp as BaseTestApp, AppError
25
 from webtest import TestApp as BaseTestApp, AppError
26
 from who_ldap import make_connection
26
 from who_ldap import make_connection
27
 
27
 
28
-from tracim.command import BaseCommand
28
+from tracim.fixtures import FixturesLoader
29
+from tracim.fixtures.users_and_groups import Base as BaseFixture
29
 from tracim.lib.base import logger
30
 from tracim.lib.base import logger
30
 from tracim.model import DBSession
31
 from tracim.model import DBSession
31
 
32
 
149
 class TestStandard(object):
150
 class TestStandard(object):
150
 
151
 
151
     application_under_test = application_name
152
     application_under_test = application_name
153
+    fixtures = [BaseFixture, ]
152
 
154
 
153
     def setUp(self):
155
     def setUp(self):
154
         self.app = load_app(self.application_under_test)
156
         self.app = load_app(self.application_under_test)
168
         setup_db()
170
         setup_db()
169
         logger.debug(self, 'Start Database Setup... -> done')
171
         logger.debug(self, 'Start Database Setup... -> done')
170
 
172
 
173
+        logger.debug(self, 'Load extra fixtures...')
174
+        fixtures_loader = FixturesLoader([BaseFixture])  # BaseFixture is already loaded in bootstrap
175
+        fixtures_loader.loads(self.fixtures)
176
+        logger.debug(self, 'Load extra fixtures... -> done')
177
+
171
         self.app.get('/_test_vars')  # Allow to create fake context
178
         self.app.get('/_test_vars')  # Allow to create fake context
172
         tg.i18n.set_lang('en')  # Set a default lang
179
         tg.i18n.set_lang('en')  # Set a default lang
173
 
180
 
207
     """
214
     """
208
 
215
 
209
     application_under_test = application_name
216
     application_under_test = application_name
217
+    fixtures = [BaseFixture, ]
210
 
218
 
211
     def setUp(self):
219
     def setUp(self):
212
         """Setup test fixture for each functional test method."""
220
         """Setup test fixture for each functional test method."""
220
         setup_app(section_name=self.application_under_test)
228
         setup_app(section_name=self.application_under_test)
221
         setup_db()
229
         setup_db()
222
 
230
 
231
+        fixtures_loader = FixturesLoader([BaseFixture])  # BaseFixture is already loaded in bootstrap
232
+        fixtures_loader.loads(self.fixtures)
233
+
223
 
234
 
224
     def tearDown(self):
235
     def tearDown(self):
225
         """Tear down test fixture for each functional test method."""
236
         """Tear down test fixture for each functional test method."""

+ 2 - 0
tracim/tracim/tests/functional/test_ldap_authentication.py View File

5
 from tracim.fixtures.ldap import ldap_test_server_fixtures
5
 from tracim.fixtures.ldap import ldap_test_server_fixtures
6
 from nose.tools import eq_, ok_
6
 from nose.tools import eq_, ok_
7
 
7
 
8
+from tracim.fixtures.users_and_groups import Test as TestFixture
8
 from tracim.model import DBSession, User
9
 from tracim.model import DBSession, User
9
 from tracim.tests import LDAPTest, TracimTestController
10
 from tracim.tests import LDAPTest, TracimTestController
10
 
11
 
12
 class TestAuthentication(LDAPTest, TracimTestController):
13
 class TestAuthentication(LDAPTest, TracimTestController):
13
     application_under_test = 'ldap'
14
     application_under_test = 'ldap'
14
     ldap_server_data = ldap_test_server_fixtures
15
     ldap_server_data = ldap_test_server_fixtures
16
+    fixtures = [TestFixture, ]
15
 
17
 
16
     def test_ldap_auth_fail_no_account(self):
18
     def test_ldap_auth_fail_no_account(self):
17
         # User is unknown in tracim database
19
         # User is unknown in tracim database

+ 2 - 0
tracim/tracim/tests/functional/test_ldap_restrictions.py View File

5
 from nose.tools import eq_, ok_
5
 from nose.tools import eq_, ok_
6
 
6
 
7
 from tracim.fixtures.ldap import ldap_test_server_fixtures
7
 from tracim.fixtures.ldap import ldap_test_server_fixtures
8
+from tracim.fixtures.users_and_groups import Test as TestFixture
8
 from tracim.lib.base import current_user
9
 from tracim.lib.base import current_user
9
 from tracim.model import DBSession, User
10
 from tracim.model import DBSession, User
10
 from tracim.tests import LDAPTest, TracimTestController
11
 from tracim.tests import LDAPTest, TracimTestController
13
 class TestAuthentication(LDAPTest, TracimTestController):
14
 class TestAuthentication(LDAPTest, TracimTestController):
14
     application_under_test = 'ldap'
15
     application_under_test = 'ldap'
15
     ldap_server_data = ldap_test_server_fixtures
16
     ldap_server_data = ldap_test_server_fixtures
17
+    fixtures = [TestFixture, ]
16
 
18
 
17
     def test_password_disabled(self):
19
     def test_password_disabled(self):
18
         """
20
         """

+ 2 - 0
tracim/tracim/tests/library/test_ldap_without_ldap_groups.py View File

3
 from tg import config
3
 from tg import config
4
 
4
 
5
 from tracim.fixtures.ldap import ldap_test_server_fixtures
5
 from tracim.fixtures.ldap import ldap_test_server_fixtures
6
+from tracim.fixtures.users_and_groups import Test as TestFixtures
6
 from tracim.lib.auth.ldap import LDAPAuth
7
 from tracim.lib.auth.ldap import LDAPAuth
7
 from tracim.lib.helpers import ini_conf_to_bool
8
 from tracim.lib.helpers import ini_conf_to_bool
8
 from tracim.model import DBSession, User, Group
9
 from tracim.model import DBSession, User, Group
15
     """
16
     """
16
     application_under_test = 'ldap'
17
     application_under_test = 'ldap'
17
     ldap_server_data = ldap_test_server_fixtures
18
     ldap_server_data = ldap_test_server_fixtures
19
+    fixtures = [TestFixtures]
18
 
20
 
19
     def _check_db_user(self, email, count=1):
21
     def _check_db_user(self, email, count=1):
20
         eq_(count, DBSession.query(User).filter(User.email == email).count())
22
         eq_(count, DBSession.query(User).filter(User.email == email).count())

+ 6 - 43
tracim/tracim/websetup/bootstrap.py View File

2
 """Setup the tracim application"""
2
 """Setup the tracim application"""
3
 from __future__ import print_function
3
 from __future__ import print_function
4
 
4
 
5
-import logging
6
-from tg import config
7
-from tracim import model
8
 import transaction
5
 import transaction
9
 
6
 
7
+from tracim.fixtures import FixturesLoader
8
+from tracim.fixtures.users_and_groups import Base as BaseFixture
9
+
10
+
10
 def bootstrap(command, conf, vars):
11
 def bootstrap(command, conf, vars):
11
     """Place any commands to setup tracim here"""
12
     """Place any commands to setup tracim here"""
12
 
13
 
13
     # <websetup.bootstrap.before.auth
14
     # <websetup.bootstrap.before.auth
14
     from sqlalchemy.exc import IntegrityError
15
     from sqlalchemy.exc import IntegrityError
15
     try:
16
     try:
16
-        u = model.User()
17
-        u.display_name = 'Global manager'
18
-        u.email = 'admin@admin.admin'
19
-        u.password = 'admin@admin.admin'
20
-        model.DBSession.add(u)
21
-
22
-        g1 = model.Group()
23
-        g1.group_id = 1
24
-        g1.group_name = 'users'
25
-        g1.display_name = 'Users'
26
-        g1.users.append(u)
27
-        model.DBSession.add(g1)
28
-
29
-        g2 = model.Group()
30
-        g2.group_id = 2
31
-        g2.group_name = 'managers'
32
-        g2.display_name = 'Global Managers'
33
-        g2.users.append(u)
34
-        model.DBSession.add(g2)
35
-
36
-        g3 = model.Group()
37
-        g3.group_id = 3
38
-        g3.group_name = 'administrators'
39
-        g3.display_name = 'Administrators'
40
-        g3.users.append(u)
41
-        model.DBSession.add(g3)
42
-
43
-        # TODO: - B.S. - 20160212: Following fixture is LDAP tests specific, should make an little fixture management
44
-        # for tests
45
-        lawrence = model.User()
46
-        lawrence.display_name = 'Lawrence L.'
47
-        lawrence.email = 'lawrence-not-real-email@fsf.org'
48
-        lawrence.password = 'foobarbaz'
49
-        model.DBSession.add(lawrence)
50
-        g2.users.append(lawrence)
51
-
52
-        model.DBSession.flush()
53
-        transaction.commit()
54
-        pass
55
-
17
+        fixtures_loader = FixturesLoader()
18
+        fixtures_loader.loads([BaseFixture])
56
     except IntegrityError:
19
     except IntegrityError:
57
         print('Warning, there was a problem adding your auth data, it may have already been added:')
20
         print('Warning, there was a problem adding your auth data, it may have already been added:')
58
         import traceback
21
         import traceback