Beispiel #1
0
 def admin_group_fixture(self):
     """ Create an admin group test fixture in the database."""
     try:
         from anuket.models.auth import AuthGroup
         group = AuthGroup()
         group.groupname = u'admins'
         self.DBSession.add(group)
         self.DBSession.flush()
         return group
     except:  # pragma: no cover
         self.DBSession.rollback()
         raise AssertionError
Beispiel #2
0
 def admin_group_fixture(self):
     """ Create an admin group test fixture in the database."""
     try:
         from anuket.models.auth import AuthGroup
         group = AuthGroup()
         group.groupname = u'admins'
         self.DBSession.add(group)
         self.DBSession.flush()
         return group
     except:  # pragma: no cover
         self.DBSession.rollback()
         raise AssertionError
Beispiel #3
0
    def initialize_db(self):
        """ Initialize the database schema and insert default values.

        :return: 0 (OK) or 1 (abnormal termination error)
        """
        config_uri = self.args.config_uri
        settings = get_appsettings(config_uri)
        engine = engine_from_config(settings, 'sqlalchemy.')
        DBSession.configure(bind=engine)

        # check if there is already a versioned database
        revision = get_alembic_revision(config_uri)
        if revision:
            print("This database is versioned. "
                  "Use the upgrade script instead!")
            return 1

        # create the tables (except alembic_version)
        Base.metadata.create_all(engine)

        # add default user & group values
        with transaction.manager:
            admins_group = AuthGroup()
            admins_group.groupname = u'admins'
            admin_user = AuthUser()
            admin_user.username = u'admin'
            admin_user.password = u'admin'
            admin_user.group = admins_group
            try:
                DBSession.add(admins_group)
                DBSession.add(admin_user)
                DBSession.flush()
            except IntegrityError:
                DBSession.rollback()
                print("There is already a database. "
                      "Use the upgrade script instead!")
                return 1

        # stamp the database with the most recent revision
        # (and create alembic_version table)
        try:
            alembic_cfg = get_alembic_settings(config_uri)
            stamp(alembic_cfg, 'head')
        except (AttributeError, ImportError):  # pragma: no cover
            # alembic is missing or not configured
            pass

        print("Database initialization done.")
        return 0
Beispiel #4
0
    def initialize_db(self):
        """ Initialize the database schema and insert default values.

        :return: 0 (OK) or 1 (abnormal termination error)
        """
        config_uri = self.args.config_uri
        settings = get_appsettings(config_uri)
        engine = engine_from_config(settings, 'sqlalchemy.')
        DBSession.configure(bind=engine)

        # check if there is already a versioned database
        revision = get_alembic_revision(config_uri)
        if revision:
            print("This database is versioned. "
                  "Use the upgrade script instead!")
            return 1

        # create the tables (except alembic_version)
        Base.metadata.create_all(engine)

        # add default user & group values
        with transaction.manager:
            admins_group = AuthGroup()
            admins_group.groupname = u'admins'
            admin_user = AuthUser()
            admin_user.username = u'admin'
            admin_user.password = u'admin'
            admin_user.group = admins_group
            try:
                DBSession.add(admins_group)
                DBSession.add(admin_user)
                DBSession.flush()
            except IntegrityError:
                DBSession.rollback()
                print("There is already a database. "
                      "Use the upgrade script instead!")
                return 1

        # stamp the database with the most recent revision
        # (and create alembic_version table)
        try:
            alembic_cfg = get_alembic_settings(config_uri)
            stamp(alembic_cfg, 'head')
        except (AttributeError, ImportError):  # pragma: no cover
            # alembic is missing or not configured
            pass

        print("Database initialization done.")
        return 0
Beispiel #5
0
 def test_AuthGroup_groupname_unique_constraint(self):
     """ Test `groupname` uniqueness in the ``AuthGroup`` model class."""
     self.dummy_group_fixture()
     from anuket.models.auth import AuthGroup
     from sqlalchemy.exc import IntegrityError
     duplicate = AuthGroup(groupname=u'groupname')
     self.DBSession.add(duplicate)
     self.assertRaises(IntegrityError, self.DBSession.flush)
    def test_initialize_db_integrity_error(self):
        """ Test than the `initialize_db` method fail if an ``IntegrityError``
        occur because there is already an `admins` group in the database.
        """
        import transaction
        from anuket.models.auth import AuthGroup
        Base.metadata.create_all()
        with transaction.manager:
            admins_group = AuthGroup()
            admins_group.groupname = u'admins'
            self.DBSession.add(admins_group)
        self.DBSession.remove()

        command = self._makeOne()
        command.args.config_uri = self.config_uri
        result = command.initialize_db()
        self.assertEqual(result, 1)
        self.assertEqual(self.output.getvalue().rstrip("\n"),
                         "There is already a database. "
                         "Use the upgrade script instead!")
    def test_initialize_db_integrity_error(self):
        """ Test than the `initialize_db` method fail if an ``IntegrityError``
        occur because there is already an `admins` group in the database.
        """
        import transaction
        from anuket.models.auth import AuthGroup
        Base.metadata.create_all()
        with transaction.manager:
            admins_group = AuthGroup()
            admins_group.groupname = u'admins'
            self.DBSession.add(admins_group)
        self.DBSession.remove()

        command = self._makeOne()
        command.args.config_uri = self.config_uri
        result = command.initialize_db()
        self.assertEqual(result, 1)
        self.assertEqual(
            self.output.getvalue().rstrip("\n"),
            "There is already a database. "
            "Use the upgrade script instead!")
Beispiel #8
0
 def test_AuthGroup_get_by_id(self):
     """ Test the `get_by_id` method of the ``AuthGroup`` model class."""
     group = self.dummy_group_fixture()
     from anuket.models.auth import AuthGroup
     self.assertTrue(AuthGroup.get_by_id(1))
     self.assertEqual(group, AuthGroup.get_by_id(1))
Beispiel #9
0
 def test_AuthGroup_get_by_id(self):
     """ Test the `get_by_id` method of the ``AuthGroup`` model class."""
     group = self.dummy_group_fixture()
     from anuket.models.auth import AuthGroup
     self.assertTrue(AuthGroup.get_by_id(1))
     self.assertEqual(group, AuthGroup.get_by_id(1))