cubicweb/server/test/unittest_checkintegrity.py
changeset 11057 0b59724cb3f2
parent 10806 bd98cd3b7869
child 11269 73ac69970047
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 
       
    19 import sys
       
    20 
       
    21 from six import PY2
       
    22 if PY2:
       
    23     from StringIO import StringIO
       
    24 else:
       
    25     from io import StringIO
       
    26 
       
    27 from logilab.common.testlib import TestCase, unittest_main
       
    28 from cubicweb.devtools import get_test_db_handler, TestServerConfiguration
       
    29 
       
    30 
       
    31 from cubicweb.server.checkintegrity import check, reindex_entities
       
    32 
       
    33 class CheckIntegrityTC(TestCase):
       
    34 
       
    35     def setUp(self):
       
    36         handler = get_test_db_handler(TestServerConfiguration(apphome=self.datadir))
       
    37         handler.build_db_cache()
       
    38         self.repo, _cnx = handler.get_repo_and_cnx()
       
    39         sys.stderr = sys.stdout = StringIO()
       
    40 
       
    41     def tearDown(self):
       
    42         sys.stderr = sys.__stderr__
       
    43         sys.stdout = sys.__stdout__
       
    44         self.repo.shutdown()
       
    45 
       
    46     def test_checks(self):
       
    47         with self.repo.internal_cnx() as cnx:
       
    48             check(self.repo, cnx, ('entities', 'relations', 'text_index', 'metadata'),
       
    49                   reindex=False, fix=True, withpb=False)
       
    50 
       
    51     def test_reindex_all(self):
       
    52         with self.repo.internal_cnx() as cnx:
       
    53             cnx.execute('INSERT Personne X: X nom "toto", X prenom "tutu"')
       
    54             cnx.commit()
       
    55             self.assertTrue(cnx.execute('Any X WHERE X has_text "tutu"'))
       
    56             reindex_entities(self.repo.schema, cnx, withpb=False)
       
    57             self.assertTrue(cnx.execute('Any X WHERE X has_text "tutu"'))
       
    58 
       
    59     def test_reindex_etype(self):
       
    60         with self.repo.internal_cnx() as cnx:
       
    61             cnx.execute('INSERT Personne X: X nom "toto", X prenom "tutu"')
       
    62             cnx.execute('INSERT Affaire X: X ref "toto"')
       
    63             cnx.commit()
       
    64             reindex_entities(self.repo.schema, cnx, withpb=False,
       
    65                              etypes=('Personne',))
       
    66             self.assertTrue(cnx.execute('Any X WHERE X has_text "tutu"'))
       
    67             self.assertTrue(cnx.execute('Any X WHERE X has_text "toto"'))
       
    68 
       
    69 if __name__ == '__main__':
       
    70     unittest_main()