Blame tests/test_pagure_lib_model.py

Pierre-Yves Chibon 8b45f6
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 8b45f6
"""
Pierre-Yves Chibon b63f28
 (c) 2015-2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 8b45f6
 Authors:
Pierre-Yves Chibon 8b45f6
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 8b45f6
"""
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 8b45f6
import unittest
Pierre-Yves Chibon 8b45f6
import sys
Pierre-Yves Chibon 8b45f6
import os
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 8b45f6
from mock import patch
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 930073
import pagure.lib.model
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 8b45f6
import tests
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon fe5017
class PagureLibModeltests(tests.Modeltests):
Pierre-Yves Chibon fe5017
    """ Tests for pagure.lib.model """
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 8b45f6
    def test_user__repr__(self):
Pierre-Yves Chibon fe5017
        """ Test the User.__repr__ function of pagure.lib.model. """
Pierre-Yves Chibon 73d120
        item = pagure.lib.query.search_user(self.session, email="foo@bar.com")
Pierre-Yves Chibon 73d120
        self.assertEqual(str(item), "User: 2 - name foo")
Pierre-Yves Chibon 73d120
        self.assertEqual("foo", item.user)
Pierre-Yves Chibon 73d120
        self.assertEqual("foo", item.username)
Pierre-Yves Chibon 8b45f6
        self.assertEqual([], item.groups)
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon c6bb48
    def test_issue__repr__(self, p_send_email, p_ugt):
Pierre-Yves Chibon fe5017
        """ Test the Issue.__repr__ function of pagure.lib.model. """
Pierre-Yves Chibon c6bb48
        p_send_email.return_value = True
Pierre-Yves Chibon c6bb48
        p_ugt.return_value = True
Pierre-Yves Chibon c6bb48
Pierre-Yves Chibon c6bb48
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon c6bb48
Pierre-Yves Chibon c6bb48
        # Create an issue
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon c6bb48
            session=self.session,
Pierre-Yves Chibon c6bb48
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon c6bb48
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue")
Pierre-Yves Chibon c6bb48
Pierre-Yves Chibon 930073
        issues = pagure.lib.query.search_issues(self.session, repo)
Pierre-Yves Chibon c6bb48
        self.assertEqual(len(issues), 1)
Pierre-Yves Chibon c6bb48
        self.assertEqual(
Pierre-Yves Chibon c6bb48
            str(issues[0]),
Pierre-Yves Chibon 73d120
            "Issue(1, project:test, user:pingou, title:Test issue)",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon c6bb48
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 17d33a
    def test_pullrequest__repr__(self, p_send_email, p_ugt):
Pierre-Yves Chibon fe5017
        """ Test the PullRequest.__repr__ function of pagure.lib.model. """
Pierre-Yves Chibon 17d33a
        p_send_email.return_value = True
Pierre-Yves Chibon 17d33a
        p_ugt.return_value = True
Pierre-Yves Chibon 17d33a
Pierre-Yves Chibon 17d33a
        tests.create_projects(self.session)
Pierre-Yves Chibon 17d33a
        # Create a forked repo
Pierre-Yves Chibon fe5017
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 17d33a
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="test project #1",
farhaanbukhsh 907098
            is_fork=True,
Pierre-Yves Chibon 17d33a
            parent_id=1,
Pierre-Yves Chibon 73d120
            hook_token="aaabbbyyy",
Pierre-Yves Chibon 17d33a
        )
Pierre-Yves Chibon 17d33a
        self.session.commit()
Pierre-Yves Chibon 17d33a
        self.session.add(item)
Pierre-Yves Chibon 17d33a
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="pingou"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 17d33a
Pierre-Yves Chibon 17d33a
        # Create an pull-request
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 17d33a
            session=self.session,
Pierre-Yves Chibon 17d33a
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 17d33a
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 17d33a
        )
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon f9c5f9
Pierre-Yves Chibon f9c5f9
        self.assertEqual(
Pierre-Yves Chibon f9c5f9
            str(req),
Pierre-Yves Chibon 73d120
            "PullRequest(1, project:test, user:pingou, "
Pierre-Yves Chibon 73d120
            "title:test pull-request)",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 17d33a
Pierre-Yves Chibon 73d120
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 17d33a
        self.assertEqual(
Pierre-Yves Chibon 17d33a
            str(request),
Pierre-Yves Chibon 73d120
            "PullRequest(1, project:test, user:pingou, "
Pierre-Yves Chibon 73d120
            "title:test pull-request)",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 17d33a
Pierre-Yves Chibon fe5017
    def test_paguregroup__repr__(self):
Pierre-Yves Chibon fe5017
        """ Test the PagureGroup.__repr__ function of pagure.lib.model. """
Pierre-Yves Chibon fe5017
        item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon 73d120
            group_name="admin",
Pierre-Yves Chibon 73d120
            display_name="admin group",
Pierre-Yves Chibon 73d120
            description="the local admin group",
Pierre-Yves Chibon d5efb0
            user_id=1,
Pierre-Yves Chibon e1c99a
        )
Pierre-Yves Chibon e1c99a
        self.session.add(item)
Pierre-Yves Chibon e1c99a
        self.session.commit()
Pierre-Yves Chibon e1c99a
Pierre-Yves Chibon 73d120
        self.assertEqual(str(item), "Group: 1 - name admin")
Pierre-Yves Chibon 8b45f6
Pierre-Yves Chibon 302953
    def test_tagissue__repr__(self):
Pierre-Yves Chibon 302953
        """ Test the TagIssue.__repr__ function of pagure.lib.model. """
Pierre-Yves Chibon 302953
        self.test_issue__repr__()
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issues = pagure.lib.query.search_issues(self.session, repo)
Pierre-Yves Chibon 302953
        self.assertEqual(len(issues), 1)
Pierre-Yves Chibon 302953
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.Tag(tag="foo")
Pierre-Yves Chibon 302953
        self.session.add(item)
Pierre-Yves Chibon 302953
        self.session.commit()
Pierre-Yves Chibon 302953
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.TagIssue(issue_uid=issues[0].uid, tag="foo")
Pierre-Yves Chibon 302953
        self.session.add(item)
Pierre-Yves Chibon 302953
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(str(item), "TagIssue(issue:1, tag:foo)")
Pierre-Yves Chibon 302953
Pierre-Yves Chibon a8f682
    def test_tagissuecolor__repr__(self):
Pierre-Yves Chibon a8f682
        """ Test the TagIssue.__repr__ function of pagure.lib.model. """
Pierre-Yves Chibon a8f682
        self.test_issue__repr__()
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issues = pagure.lib.query.search_issues(self.session, repo)
Pierre-Yves Chibon a8f682
        self.assertEqual(len(issues), 1)
Pierre-Yves Chibon a8f682
Pierre-Yves Chibon a8f682
        item = pagure.lib.model.TagColored(
Pierre-Yves Chibon 73d120
            tag="foo",
Pierre-Yves Chibon 73d120
            tag_description="bar",
Pierre-Yves Chibon 73d120
            tag_color="DeepSkyBlue",
Pierre-Yves Chibon 73d120
            project_id=repo.id,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon a8f682
        self.session.add(item)
Pierre-Yves Chibon a8f682
        self.session.commit()
Pierre-Yves Chibon a8f682
Pierre-Yves Chibon a8f682
        item = pagure.lib.model.TagIssueColored(
Pierre-Yves Chibon 73d120
            issue_uid=issues[0].uid, tag_id=item.id
Pierre-Yves Chibon a8f682
        )
Pierre-Yves Chibon a8f682
        self.session.add(item)
Pierre-Yves Chibon a8f682
        self.session.commit()
Pierre-Yves Chibon a8f682
        self.assertEqual(
Pierre-Yves Chibon 73d120
            str(item), "TagIssueColored(issue:1, tag:foo, project:test)"
Pierre-Yves Chibon a8f682
        )
Pierre-Yves Chibon a8f682
Pierre-Yves Chibon 830728
    def test_group_project_ordering(self):
Pierre-Yves Chibon 830728
        """ Test the ordering of project.groups. """
Pierre-Yves Chibon 830728
        # Create three projects
Pierre-Yves Chibon 830728
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 830728
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="aaa",
Pierre-Yves Chibon 73d120
            description="Project aaa",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbccc",
Pierre-Yves Chibon 830728
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Fixed", "Duplicate"]
Pierre-Yves Chibon 830728
        self.session.add(item)
Pierre-Yves Chibon 830728
Pierre-Yves Chibon 830728
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 830728
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="KKK",
Pierre-Yves Chibon 73d120
            description="project KKK",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbddd",
Pierre-Yves Chibon 830728
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Fixed", "Duplicate"]
Pierre-Yves Chibon 830728
        self.session.add(item)
Pierre-Yves Chibon 830728
Pierre-Yves Chibon 830728
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 830728
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="zzz",
Pierre-Yves Chibon 73d120
            description="Namespaced project zzz",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbeee",
Pierre-Yves Chibon 73d120
            namespace="somenamespace",
Pierre-Yves Chibon 830728
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Fixed", "Duplicate"]
Pierre-Yves Chibon 830728
        self.session.add(item)
Pierre-Yves Chibon 830728
Pierre-Yves Chibon 830728
        # Create a group
Pierre-Yves Chibon 830728
        group = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon 73d120
            group_name="testgrp",
Pierre-Yves Chibon 73d120
            display_name="Test group",
Pierre-Yves Chibon 830728
            description=None,
Pierre-Yves Chibon 73d120
            group_type="user",
Pierre-Yves Chibon 830728
            user_id=1,  # pingou
Pierre-Yves Chibon 830728
        )
Pierre-Yves Chibon 73d120
        item.close_status = ["Invalid", "Fixed", "Duplicate"]
Pierre-Yves Chibon 830728
        self.session.add(group)
Pierre-Yves Chibon 830728
Pierre-Yves Chibon 830728
        self.session.commit()
Pierre-Yves Chibon 830728
Pierre-Yves Chibon 830728
        # Add projects to group
Pierre-Yves Chibon 830728
        for ns, reponame in [
Pierre-Yves Chibon 73d120
            (None, "aaa"),
Pierre-Yves Chibon 73d120
            (None, "KKK"),
Pierre-Yves Chibon 73d120
            ("somenamespace", "zzz"),
Pierre-Yves Chibon 73d120
        ]:
Pierre-Yves Chibon 830728
Pierre-Yves Chibon 930073
            repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, reponame, namespace=ns
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 930073
            msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 830728
                self.session,
Pierre-Yves Chibon 830728
                project=repo,
Pierre-Yves Chibon 73d120
                new_group="testgrp",
Pierre-Yves Chibon 73d120
                user="pingou",
Pierre-Yves Chibon 830728
                create=False,
Pierre-Yves Chibon 73d120
                is_admin=False,
Pierre-Yves Chibon 830728
            )
Pierre-Yves Chibon 830728
            self.session.commit()
Pierre-Yves Chibon 73d120
            self.assertEqual(msg, "Group added")
Pierre-Yves Chibon 830728
Pierre-Yves Chibon 830728
        # Check the ordering
Pierre-Yves Chibon 73d120
        group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
            self.session, group_name="testgrp"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 022a29
        # Default PostgreSQL order
Pierre-Yves Chibon 73d120
        order = ["aaa", "KKK", "somenamespace/zzz"]
Pierre-Yves Chibon 022a29
        # Odd, SQLite order
Pierre-Yves Chibon 73d120
        if str(self.session.bind.engine.url).startswith("sqlite:"):
Pierre-Yves Chibon 73d120
            order = ["somenamespace/zzz", "aaa", "KKK"]
Pierre-Yves Chibon 830728
Pierre-Yves Chibon 73d120
        self.assertEqual([p.fullname for p in group.projects], order)
Pierre-Yves Chibon 830728
Pierre-Yves Chibon 302953
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)