Blame tests/test_pagure_lib_model_delete_project.py

Pierre-Yves Chibon b4a226
# -*- coding: utf-8 -*-
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
"""
Pierre-Yves Chibon b4a226
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
 Authors:
Pierre-Yves Chibon b4a226
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
"""
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon b4a226
import json
Pierre-Yves Chibon b4a226
import unittest
Pierre-Yves Chibon b4a226
import sys
Pierre-Yves Chibon b4a226
import os
Pierre-Yves Chibon b4a226
import uuid
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon b4a226
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
import pagure.lib.model as model
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
import tests
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
class DeleteProjectTests(tests.Modeltests):
Pierre-Yves Chibon b4a226
    """ Tests for flask issues controller of pagure """
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
    def test_delete_project_with_group(self):
Pierre-Yves Chibon b4a226
        """ Test the model when we delete a project with a group. """
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create a project
Pierre-Yves Chibon b4a226
        item = model.Project(
Pierre-Yves Chibon b4a226
            user_id=1,  # pingou
Pierre-Yves Chibon b4a226
            name='test',
Pierre-Yves Chibon b4a226
            description='test project #1',
Pierre-Yves Chibon b4a226
            hook_token='aaabbbiii',
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(item)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create a group
Pierre-Yves Chibon b4a226
        grp = model.PagureGroup(
Pierre-Yves Chibon b4a226
            group_name='testgrp',
Pierre-Yves Chibon b4a226
            display_name='Test group',
Pierre-Yves Chibon b4a226
            description=None,
Pierre-Yves Chibon b4a226
            group_type='user',
Pierre-Yves Chibon b4a226
            user_id=1,  # pingou
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(grp)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Add group to project
Pierre-Yves Chibon b4a226
        project_group = model.ProjectGroup(
Pierre-Yves Chibon b4a226
            project_id=1,
Pierre-Yves Chibon b4a226
            group_id=1,
Pierre-Yves Chibon b4a226
            access='admin',
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(project_group)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Project).count(), 1)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.ProjectGroup).count(), 1)
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        project = self.session.query(
Pierre-Yves Chibon b4a226
            model.Project
Pierre-Yves Chibon b4a226
        ).filter(
Pierre-Yves Chibon b4a226
            model.Project.id==1
Pierre-Yves Chibon b4a226
        ).one()
Pierre-Yves Chibon b4a226
        self.session.delete(project)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Project).count(), 0)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.ProjectGroup).count(), 0)
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
    def test_delete_project_with_user(self):
Pierre-Yves Chibon b4a226
        """ Test the model when we delete a project with users. """
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create a project
Pierre-Yves Chibon b4a226
        item = model.Project(
Pierre-Yves Chibon b4a226
            user_id=1,  # pingou
Pierre-Yves Chibon b4a226
            name='test',
Pierre-Yves Chibon b4a226
            description='test project #1',
Pierre-Yves Chibon b4a226
            hook_token='aaabbbiii',
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(item)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Add user #2 to project
Pierre-Yves Chibon b4a226
        project_user = model.ProjectUser(
Pierre-Yves Chibon b4a226
            project_id=1,
Pierre-Yves Chibon b4a226
            user_id=2,
Pierre-Yves Chibon b4a226
            access='admin',
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(project_user)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Project).count(), 1)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.ProjectUser).count(), 1)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.User).count(), 2)
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        project = self.session.query(
Pierre-Yves Chibon b4a226
            model.Project
Pierre-Yves Chibon b4a226
        ).filter(
Pierre-Yves Chibon b4a226
            model.Project.id==1
Pierre-Yves Chibon b4a226
        ).one()
Pierre-Yves Chibon b4a226
        self.session.delete(project)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Project).count(), 0)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.ProjectUser).count(), 0)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.User).count(), 2)
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
    def test_delete_project_with_coloredtags(self):
Pierre-Yves Chibon b4a226
        """ Test the model when we delete a project with Colored tags. """
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create a project
Pierre-Yves Chibon b4a226
        item = model.Project(
Pierre-Yves Chibon b4a226
            user_id=1,  # pingou
Pierre-Yves Chibon b4a226
            name='test',
Pierre-Yves Chibon b4a226
            description='test project #1',
Pierre-Yves Chibon b4a226
            hook_token='aaabbbiii',
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(item)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create two ColoredTags
Pierre-Yves Chibon b4a226
        tagobj = model.TagColored(
Pierre-Yves Chibon b4a226
            tag='Tag#1',
Pierre-Yves Chibon b4a226
            project_id=1
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(tagobj)
Pierre-Yves Chibon b4a226
        self.session.flush()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        tagobj = model.TagColored(
Pierre-Yves Chibon b4a226
            tag='Tag#2',
Pierre-Yves Chibon b4a226
            project_id=1
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(tagobj)
Pierre-Yves Chibon b4a226
        self.session.flush()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Project).count(), 1)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.TagColored).count(), 2)
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        project = self.session.query(
Pierre-Yves Chibon b4a226
            model.Project
Pierre-Yves Chibon b4a226
        ).filter(
Pierre-Yves Chibon b4a226
            model.Project.id==1
Pierre-Yves Chibon b4a226
        ).one()
Pierre-Yves Chibon b4a226
        self.session.delete(project)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Project).count(), 0)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.TagColored).count(), 0)
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
    def test_delete_project_with_coloredtags_and_issues(self):
Pierre-Yves Chibon b4a226
        """ Test the model when we delete a project with Colored tags and
Pierre-Yves Chibon b4a226
        issues. """
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create a project
Pierre-Yves Chibon b4a226
        item = model.Project(
Pierre-Yves Chibon b4a226
            user_id=1,  # pingou
Pierre-Yves Chibon b4a226
            name='test',
Pierre-Yves Chibon b4a226
            description='test project #1',
Pierre-Yves Chibon b4a226
            hook_token='aaabbbiii',
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(item)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create two ColoredTags
Pierre-Yves Chibon b4a226
        tagobj = model.TagColored(
Pierre-Yves Chibon b4a226
            tag='Tag#1',
Pierre-Yves Chibon b4a226
            project_id=1
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(tagobj)
Pierre-Yves Chibon b4a226
        self.session.flush()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        tagobj = model.TagColored(
Pierre-Yves Chibon b4a226
            tag='Tag#2',
Pierre-Yves Chibon b4a226
            project_id=1
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(tagobj)
Pierre-Yves Chibon b4a226
        self.session.flush()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create issues
Pierre-Yves Chibon b4a226
        issue = model.Issue(
Pierre-Yves Chibon 930073
            id=pagure.lib.query.get_next_id(self.session, 1),
Pierre-Yves Chibon b4a226
            project_id=1,
Pierre-Yves Chibon b4a226
            title='Issue #1',
Pierre-Yves Chibon b4a226
            content='Description #1',
Pierre-Yves Chibon b4a226
            user_id=1,
Pierre-Yves Chibon b4a226
            uid=uuid.uuid4().hex,
Pierre-Yves Chibon b4a226
            private=False,
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(issue)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        issue = model.Issue(
Pierre-Yves Chibon 930073
            id=pagure.lib.query.get_next_id(self.session, 1),
Pierre-Yves Chibon b4a226
            project_id=1,
Pierre-Yves Chibon b4a226
            title='Issue #2',
Pierre-Yves Chibon b4a226
            content='Description #2',
Pierre-Yves Chibon b4a226
            user_id=1,
Pierre-Yves Chibon b4a226
            uid=uuid.uuid4().hex,
Pierre-Yves Chibon b4a226
            private=False,
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(issue)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Project).count(), 1)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.TagColored).count(), 2)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Issue).count(), 2)
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        project = self.session.query(
Pierre-Yves Chibon b4a226
            model.Project
Pierre-Yves Chibon b4a226
        ).filter(
Pierre-Yves Chibon b4a226
            model.Project.id==1
Pierre-Yves Chibon b4a226
        ).one()
Pierre-Yves Chibon b4a226
        self.session.delete(project)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Project).count(), 0)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.TagColored).count(), 0)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Issue).count(), 0)
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
    def test_delete_project_with_coloredtags_and_tagged_issues(self):
Pierre-Yves Chibon b4a226
        """ Test the model when we delete a project with Colored tags and
Pierre-Yves Chibon b4a226
        tagged issues. """
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create a project
Pierre-Yves Chibon b4a226
        item = model.Project(
Pierre-Yves Chibon b4a226
            user_id=1,  # pingou
Pierre-Yves Chibon b4a226
            name='test',
Pierre-Yves Chibon b4a226
            description='test project #1',
Pierre-Yves Chibon b4a226
            hook_token='aaabbbiii',
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(item)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create two ColoredTags
Pierre-Yves Chibon b4a226
        tagobj = model.TagColored(
Pierre-Yves Chibon b4a226
            tag='Tag#1',
Pierre-Yves Chibon b4a226
            project_id=1
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(tagobj)
Pierre-Yves Chibon b4a226
        self.session.flush()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        tagobj = model.TagColored(
Pierre-Yves Chibon b4a226
            tag='Tag#2',
Pierre-Yves Chibon b4a226
            project_id=1
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(tagobj)
Pierre-Yves Chibon b4a226
        self.session.flush()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Create issues
Pierre-Yves Chibon b4a226
        issue = model.Issue(
Pierre-Yves Chibon 930073
            id=pagure.lib.query.get_next_id(self.session, 1),
Pierre-Yves Chibon b4a226
            project_id=1,
Pierre-Yves Chibon b4a226
            title='Issue #1',
Pierre-Yves Chibon b4a226
            content='Description #1',
Pierre-Yves Chibon b4a226
            user_id=1,
Pierre-Yves Chibon b4a226
            uid='Issue#1',
Pierre-Yves Chibon b4a226
            private=False,
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(issue)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        issue = model.Issue(
Pierre-Yves Chibon 930073
            id=pagure.lib.query.get_next_id(self.session, 1),
Pierre-Yves Chibon b4a226
            project_id=1,
Pierre-Yves Chibon b4a226
            title='Issue #2',
Pierre-Yves Chibon b4a226
            content='Description #2',
Pierre-Yves Chibon b4a226
            user_id=1,
Pierre-Yves Chibon b4a226
            uid='Issue#2',
Pierre-Yves Chibon b4a226
            private=False,
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(issue)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        # Tag the issue
Pierre-Yves Chibon b4a226
        tagissue = model.TagIssueColored(
Pierre-Yves Chibon b4a226
            issue_uid='Issue#1',
Pierre-Yves Chibon b4a226
            tag_id=1
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(tagissue)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        tagissue = model.TagIssueColored(
Pierre-Yves Chibon b4a226
            issue_uid='Issue#2',
Pierre-Yves Chibon b4a226
            tag_id=2
Pierre-Yves Chibon b4a226
        )
Pierre-Yves Chibon b4a226
        self.session.add(tagissue)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Project).count(), 1)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.TagColored).count(), 2)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Issue).count(), 2)
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        project = self.session.query(
Pierre-Yves Chibon b4a226
            model.Project
Pierre-Yves Chibon b4a226
        ).filter(
Pierre-Yves Chibon b4a226
            model.Project.id==1
Pierre-Yves Chibon b4a226
        ).one()
Pierre-Yves Chibon b4a226
        self.session.delete(project)
Pierre-Yves Chibon b4a226
        self.session.commit()
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Project).count(), 0)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.TagColored).count(), 0)
Pierre-Yves Chibon b4a226
        self.assertEqual(
Pierre-Yves Chibon b4a226
            self.session.query(model.Issue).count(), 0)
Pierre-Yves Chibon b4a226
Pierre-Yves Chibon b4a226
if __name__ == '__main__':
Pierre-Yves Chibon b4a226
    unittest.main(verbosity=2)