cubicweb/server/test/unittest_serverctl.py
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Thu, 19 Dec 2019 12:15:29 +0100
branch3.23
changeset 12839 1dfe1cd9946a
parent 11355 47b0b08fbb4b
child 12002 26453d9467f6
permissions -rw-r--r--
closing branch 3.23
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     1
import os.path as osp
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     2
import shutil
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     3
11355
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
     4
from cubicweb import ExecutionError
10960
9e64fddebc89 merge with 3.21.3
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10825
diff changeset
     5
from cubicweb.devtools import testlib, ApptestConfiguration
11355
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
     6
from cubicweb.server.serverctl import _local_dump, DBDumpCommand, SynchronizeSourceCommand
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     7
from cubicweb.server.serverconfig import ServerConfiguration
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     8
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     9
class ServerCTLTC(testlib.CubicWebTC):
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    10
    def setUp(self):
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    11
        super(ServerCTLTC, self).setUp()
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    12
        self.orig_config_for = ServerConfiguration.config_for
11269
73ac69970047 [devtools] Simplify test configuration's init
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    13
        config_for = lambda appid: ApptestConfiguration(appid, __file__)
10960
9e64fddebc89 merge with 3.21.3
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10825
diff changeset
    14
        ServerConfiguration.config_for = staticmethod(config_for)
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    15
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    16
    def tearDown(self):
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    17
        ServerConfiguration.config_for = self.orig_config_for
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    18
        super(ServerCTLTC, self).tearDown()
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    19
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    20
    def test_dump(self):
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    21
        DBDumpCommand(None).run([self.appid])
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    22
        shutil.rmtree(osp.join(self.config.apphome, 'backup'))
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    23
11355
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    24
    def test_source_sync(self):
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    25
        with self.admin_access.repo_cnx() as cnx:
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    26
            cnx.create_entity('CWSource', name=u'success_feed', type=u'datafeed',
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    27
                              parser=u'test_source_parser_success',
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    28
                              url=u'ignored')
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    29
            cnx.create_entity('CWSource', name=u'fail_feed', type=u'datafeed',
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    30
                              parser=u'test_source_parser_fail',
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    31
                              url=u'ignored')
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    32
            cnx.commit()
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    33
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    34
            cmd = SynchronizeSourceCommand(None)
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    35
            cmd.config.force = 1
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    36
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    37
            # Should sync all sources even if one failed
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    38
            with self.assertRaises(ExecutionError) as exc:
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    39
                cmd.run([self.appid])
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    40
            self.assertEqual(len(cnx.find('Card', title=u'success')), 1)
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    41
            self.assertEqual(len(cnx.find('Card', title=u'fail')), 0)
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    42
            self.assertEqual(str(exc.exception), 'All sources where not synced')
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    43
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    44
            # call with named sources
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    45
            cmd.run([self.appid, u'success_feed'])
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    46
            self.assertEqual(len(cnx.find('Card', title=u'success')), 2)
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    47
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    48
            with self.assertRaises(ExecutionError) as exc:
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    49
                cmd.run([self.appid, u'fail_feed'])
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    50
            self.assertEqual(str(exc.exception), 'All sources where not synced')
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    51
            self.assertEqual(len(cnx.find('Card', title=u'fail')), 0)
47b0b08fbb4b [serverctl] allow to sync multiple and all sources in source-sync command
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11269
diff changeset
    52
10825
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    53
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    54
if __name__ == '__main__':
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    55
    from unittest import main
56ca5f6e0533 [server/test] add test for db-dump command
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    56
    main()