Project

General

Profile

Revision 5011664c

ID5011664c15df2bfe7bdf35c4b408d53411c74312
Parent b8500d1a
Child d3c47597

Added by Francois POIROTTE over 14 years ago

  • Migration des tests de vigi(core|board) concernant le modèle dans le dossier idoine.
  • Suppression de code mort dans les init des tests de ces 2 composants.
    Le code supprimé était commenté auparavant et consistait à modifier les variables
    d'environnement pour utiliser le module de settings réservé aux tests.
    Cette fonctionnalité est désormais fournie par le Makefile de ces composants.
  • Suppression du dossier de modèles spécifiques à vigicore (tous les modèles sont
    partagés avec le reste de Vigilo à présent).
    Vigiboard aura droit au même traitement bientôt.

git-svn-id: https://vigilo-dev.si.c-s.fr/svn@622 b22e2e97-25c9-44ff-b637-2e5ceca36478

View differences:

Makefile
26 26
	$(PYTHON) ./pylint_vigiboard.py $(NAME)
27 27

  
28 28
tests: $(PYTHON)
29
	PYTHONPATH=$(BUILDENV) $(BUILDENV)/bin/runtests-$(NAME)
29
	VIGILO_SETTINGS_MODULE=settings_tests \
30
		PYTHONPATH=$(BUILDENV) $(BUILDENV)/bin/runtests-$(NAME)
30 31

  
31 32

  
32 33
.PHONY: all clean buildclean apidoc lint tests
vigiboard/tests/__init__.py
66 66

  
67 67

  
68 68
def runtests():
69
    # XXX This is a hack, some import gets there first.
70
    #environ.setdefault('VIGILO_SETTINGS_MODULE', 'settings_tests')
71 69
    # XXX hard-coded path.
72 70
    sys.argv[1:0] = ['--with-pylons', '../vigiboard/test.ini', 
73 71
                     '--with-coverage', '--cover-inclusive',
vigiboard/tests/models/__init__.py
1
# -*- coding: utf-8 -*-
2
"""Unit test suite for the models of the application."""
3
from nose.tools import assert_equals
4

  
5
from vigiboard.model import DBSession
6
from vigiboard.tests import setup_db, teardown_db
7

  
8
__all__ = ['ModelTest']
9

  
10
#Create an empty database before we start our tests for this module
11
def setup():
12
    """Function called by nose on module load"""
13
    setup_db()
14
    
15
#Teardown that database 
16
def teardown():
17
    """Function called by nose after all tests in this module ran"""
18
    teardown_db()
19
    
20
class ModelTest(object):
21
    """Base unit test case for the models."""
22
    
23
    klass = None
24
    attrs = {}
25

  
26
    def setup(self):
27
        try:
28
            new_attrs = {}
29
            new_attrs.update(self.attrs)
30
            new_attrs.update(self.do_get_dependencies())
31
            self.obj = self.klass(**new_attrs)
32
            DBSession.add(self.obj)
33
            DBSession.flush()
34
            return self.obj
35
        except:
36
            DBSession.rollback()
37
            raise 
38

  
39
    def tearDown(self):
40
        DBSession.rollback()
41
    
42
    def do_get_dependencies(self):
43
        """Use this method to pull in other objects that need to be created for this object to be build properly"""
44
        return {}
45

  
46
    def test_create_obj(self):
47
        pass
48

  
49
    def test_query_obj(self):
50
        obj = DBSession.query(self.klass).one()
51
        for key, value in self.attrs.iteritems():
52
            assert_equals(getattr(obj, key), value)
vigiboard/tests/models/test_auth.py
1
# -*- coding: utf-8 -*-
2
"""Test suite for the TG app's models"""
3
from nose.tools import eq_
4

  
5
from vigiboard import model
6
from vigiboard.tests.models import ModelTest
7

  
8
class TestGroup(ModelTest):
9
    """Unit test case for the ``Group`` model."""
10
    klass = model.Group
11
    attrs = dict(
12
        group_name = u"test_group",
13
        display_name = u"Test Group"
14
        )
15

  
16

  
17
class TestUser(ModelTest):
18
    """Unit test case for the ``User`` model."""
19
    
20
    klass = model.User
21
    attrs = dict(
22
        user_name = u"ignucius",
23
        email_address = u"ignucius@example.org"
24
        )
25

  
26
    def test_obj_creation_username(self):
27
        """The obj constructor must set the user name right"""
28
        eq_(self.obj.user_name, u"ignucius")
29

  
30
    def test_obj_creation_email(self):
31
        """The obj constructor must set the email right"""
32
        eq_(self.obj.email_address, u"ignucius@example.org")
33

  
34
    def test_no_permissions_by_default(self):
35
        """User objects should have no permission by default."""
36
        eq_(len(self.obj.permissions), 0)
37

  
38
    def test_getting_by_email(self):
39
        """Users should be fetcheable by their email addresses"""
40
        him = model.User.by_email_address(u"ignucius@example.org")
41
        eq_(him, self.obj)
42

  
43

  
44
class TestPermission(ModelTest):
45
    """Unit test case for the ``Permission`` model."""
46
    
47
    klass = model.Permission
48
    attrs = dict(
49
        permission_name = u"test_permission",
50
        description = u"This is a test Description"
51
        )
vigiboard/tests/models/test_vigiboard.py
1
# -*- coding: utf-8 -*-
2
# vim:set expandtab tabstop=4 shiftwidth=4:
3
"""Test des modèle de Vigiboard"""
4

  
5
from nose.tools import assert_true
6
import re
7
from vigiboard.model import DBSession, Host, Service, Events, Graph, \
8
    GraphGroups, Groups, ServiceHautNiveau, EventHistory, GraphToGroups, \
9
    GroupPermissions, HostGroups, PerfDataSource, ServiceGroups, ServiceTopo
10
from vigiboard.tests.models import ModelTest
11

  
12

  
13
class TestEventHistory(ModelTest):
14
    """Test de la table EventHistory"""
15

  
16
    klass = EventHistory
17
    attrs = dict(type_action = 'Nagios update state', value = '',
18
            text = '', username = 'manager')
19

  
20
    def do_get_dependencies(self):
21
        """Generate some data for the test"""
22

  
23
        DBSession.add(Host(name = "monhost"))
24
        DBSession.add(Service(name = "monservice"))
25
        DBSession.flush()
26
        DBSession.add(Events(hostname = "monhost", servicename = "monservice"))
27
        DBSession.flush()
28
        return dict(idevent = DBSession.query(Events.idevent)[0].idevent)
29

  
30
class TestEvent(ModelTest):
31
    """Test de la table Events"""
32

  
33
    klass = Events
34
    attrs = {}
35

  
36
    def do_get_dependencies(self):
37
        """Generate some data for the test"""
38

  
39
        DBSession.add(Host(name = "monhost"))
40
        DBSession.add(Service(name = "monservice"))
41
        DBSession.flush()
42
        return dict(hostname = "monhost", servicename = "monservice")
43

  
44
    def test_get_date(self):
45
        """La fonction GetDate doit renvoyer un objet formaté"""
46
        form1 = re.compile("^\w* \w* \d*:\d*:\d*$")
47
        form2 = re.compile("^\w* \d*:\d*:\d*$")
48
        assert_true(form1.match(self.obj.get_date("timestamp_active")) \
49
                or form2.match(self.obj.get_date("timestamp_active")))
50

  
51
    def test_get_since_date(self):
52
        """La fonction GetSinceDate doit renvoyer un objet formaté"""
53
        assert_true(re.compile("^\d*d \d*h \d'$").match(
54
            self.obj.get_since_date("timestamp_active")))
55
        
56
class TestGraph(ModelTest):
57
    """Test de la table GraphGroups"""
58

  
59
    klass = GraphGroups
60
    attrs = dict(name = "mongraph")
61

  
62
class TestGraphToGroups(ModelTest):
63
    """Test de la table GraphToGroups"""
64

  
65
    klass = GraphToGroups
66
    attrs = {}
67

  
68
    def do_get_dependencies(self):
69
        """Generate some data for the test"""
70

  
71
        DBSession.add(Graph(name = "mongraph"))
72
        DBSession.add(GraphGroups(name = "mongraphgroup"))
73
        DBSession.flush()
74
        return dict(graphname = "mongraph", groupname = "mongraphgroup")
75

  
76
class TestGraphGroups(ModelTest):
77
    """Test de la table GraphGroups"""
78

  
79
    klass = GraphGroups
80
    attrs = dict(name = "mongraphgroup")
81

  
82
class TestGroupPermissions(ModelTest):
83
    """Test de la table GroupPermissions"""
84

  
85
    klass = GroupPermissions
86
    attrs = {}
87

  
88
    def do_get_dependencies(self):
89
        """Generate some data for the test"""
90

  
91
        DBSession.add(Groups(name = "mongroup"))
92
        DBSession.flush()
93
        return dict(groupname = "mongroup")
94

  
95
class TestGroups(ModelTest):
96
    """Test de la table Groups"""
97

  
98
    klass = Groups
99
    attrs = dict(name = "mongroup")
100

  
101
class TestHost(ModelTest):
102
    """Test de la table host"""
103

  
104
    klass = Host
105
    attrs = dict(name = "monhost")
106

  
107
class TestHostGroups(ModelTest):
108
    """Test de la table hostgroup"""
109

  
110
    klass = HostGroups
111
    attrs = {}
112

  
113
    def do_get_dependencies(self):
114
        """Generate some data for the test"""
115

  
116
        DBSession.add(Host(name = "monhost"))
117
        DBSession.add(Groups(name = "mongroup"))
118
        DBSession.flush()
119
        return dict(hostname = "monhost", groupname = "mongroup")
120

  
121
class TestPerfDataSource(ModelTest):
122
    """Test de la table perfdatasource"""
123

  
124
    klass = PerfDataSource
125
    attrs = {}
126

  
127
    def do_get_dependencies(self):
128
        """Generate some data for the test"""
129

  
130
        DBSession.add(Host(name = "monhost"))
131
        DBSession.add(Service(name = "monservice"))
132
        DBSession.add(Graph(name = "mongraph"))
133
        DBSession.flush()
134
        return dict(hostname = "monhost", servicename = "monservice",
135
                graphname = "mongraph")
136

  
137
class TestServiceGroups(ModelTest):
138
    """Test de la table servicegroups"""
139

  
140
    klass = ServiceGroups
141
    attrs = {}
142

  
143
    def do_get_dependencies(self):
144
        """Generate some data for the test"""
145

  
146
        DBSession.add(Service(name = "monservice"))
147
        DBSession.add(Groups(name = "mongroupe"))
148
        DBSession.flush()
149
        return dict(servicename = "monservice", groupname = "mongroupe")
150

  
151
class TestServiceHautNiveau(ModelTest):
152
    """Test de la table servicehautniveau"""
153

  
154
    klass = ServiceHautNiveau
155
    attrs = {}
156

  
157
    def do_get_dependencies(self):
158
        """Generate some data for the test"""
159
        
160
        DBSession.add(Service(name = "monservice"))
161
        DBSession.flush()
162
        return dict(servicename = "monservice", servicename_dep = "monservice")
163

  
164
class TestService(ModelTest):
165
    """Test de la table service"""
166

  
167
    klass = Service
168
    attrs = dict(name = "monservice")
169

  
170
class TestServiceTopo(ModelTest):
171
    """Test de la table servicetopo"""
172

  
173
    klass = ServiceTopo
174
    attrs = {}
175

  
176
    def do_get_dependencies(self):
177
        """Generate some data for the test"""
178

  
179
        DBSession.add(Service(name = "monservice"))
180
        DBSession.add(ServiceHautNiveau(servicename = "monservice",
181
            servicename_dep = "monservice"))
182
        DBSession.flush()
183
        return dict(servicename = "monservice")

Also available in: Unified diff