From d4e2e8f447d3bfad085ef84a6f55d6c98480f915 Mon Sep 17 00:00:00 2001 From: Macartur Sousa Date: Wed, 26 Aug 2015 19:31:00 -0300 Subject: [PATCH] Adding group integration and refactored tests --- colab/plugins/gitlab/fixtures/__init__.py | 0 colab/plugins/gitlab/fixtures/test_gitlab_data.json | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ colab/plugins/gitlab/models.py | 13 +++++++++++++ colab/plugins/gitlab/tests/test_gitlab.py | 89 ++++++++++++++++------------------------------------------------------------------------- 4 files changed, 135 insertions(+), 73 deletions(-) create mode 100644 colab/plugins/gitlab/fixtures/__init__.py create mode 100644 colab/plugins/gitlab/fixtures/test_gitlab_data.json diff --git a/colab/plugins/gitlab/fixtures/__init__.py b/colab/plugins/gitlab/fixtures/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/colab/plugins/gitlab/fixtures/__init__.py diff --git a/colab/plugins/gitlab/fixtures/test_gitlab_data.json b/colab/plugins/gitlab/fixtures/test_gitlab_data.json new file mode 100644 index 0000000..b1683de --- /dev/null +++ b/colab/plugins/gitlab/fixtures/test_gitlab_data.json @@ -0,0 +1,106 @@ +[ +{ + "fields": { + "last_name": "COLAB", + "webpage": null, + "twitter": "usertestcolab", + "is_staff": false, + "user_permissions": [], + "date_joined": "2015-08-26T21:53:25.638Z", + "google_talk": null, + "first_name": "USERtestCoLaB", + "is_superuser": false, + "last_login": "2015-08-26T21:53:25.638Z", + "verification_hash": null, + "role": null, + "email": "usertest@colab.com.br", + "username": "", + "bio": null, + "needs_update": true, + "is_active": true, + "facebook": "usertestcolab", + "groups": [], + "password": "pbkdf2_sha256$15000$d4aHrZW0B4vI$gQImAx0W62PIl/uFtR8/XW2n9R9wC2qgbfb1dKIfkFA=", + "institution": null, + "github": null, + "modified": "2015-08-26T21:54:38.592Z" + }, + "model": "accounts.user", + "pk": 1 +}, +{ + "fields": { + "last_activity_at": "2015-08-26T22:01:46.401Z", + "description": "", + "name_with_namespace": "Software Public / Colab", + "created_at": "2015-08-26T22:01:42.569Z", + "path_with_namespace": "softwarepublico/colab", + "public": true, + "name": "colab" + }, + "model": "gitlab.gitlabproject", + "pk": 1 +}, +{ + "fields": { + "path": "softwarepublico", + "name": "softwarepublico", + "owner_id": 1 + }, + "model": "gitlab.gitlabgroup", + "pk": 1 +}, +{ + "fields": { + "target_branch": "", + "description": "Merge request for plugin support", + "source_branch": "", + "created_at": "2015-08-26T22:02:22.960Z", + "title": "Include plugin support", + "project": 1, + "iid": 1, + "state": "Closed", + "user": 1 + }, + "model": "gitlab.gitlabmergerequest", + "pk": 1 +}, +{ + "fields": { + "description": "Issue reported to colab", + "title": "Issue for colab", + "created_at": "2015-08-26T22:03:09.410Z", + "project": 1, + "state": "Open", + "user": 1 + }, + "model": "gitlab.gitlabissue", + "pk": 1 +}, +{ + "fields": { + "body": "Comment to merge request", + "parent_id": 1, + "created_at": "2015-08-26T22:03:50.590Z", + "issue_comment": false, + "project": 1, + "iid": 1, + "user": 1 + }, + "model": "gitlab.gitlabcomment", + "pk": 1 +}, +{ + "fields": { + "body": "Comment to issue", + "parent_id": 1, + "created_at": "2015-08-26T22:04:33.063Z", + "issue_comment": true, + "project": 1, + "iid": 2, + "user": 1 + }, + "model": "gitlab.gitlabcomment", + "pk": 2 +} +] diff --git a/colab/plugins/gitlab/models.py b/colab/plugins/gitlab/models.py index 49511a5..3edfd17 100644 --- a/colab/plugins/gitlab/models.py +++ b/colab/plugins/gitlab/models.py @@ -16,6 +16,10 @@ class GitlabProject(models.Model, HitCounterModelMixin): path_with_namespace = models.TextField(blank=True, null=True) @property + def group(self): + return self.path_with_namespace.split('/')[0] + + @property def url(self): return u'/gitlab/{}'.format(self.path_with_namespace) @@ -34,6 +38,15 @@ class GitlabGroup(models.Model): return u'{}'.format(self.path) @property + def projects(self): + projects = GitlabProject.objects.all() + result = list() + for project in projects: + if self.name.lower() in project.group: + result.append(project) + return result + + @property def url(self): return u'/gitlab/groups/{}'.format(self.id) diff --git a/colab/plugins/gitlab/tests/test_gitlab.py b/colab/plugins/gitlab/tests/test_gitlab.py index d732f64..b8550f7 100644 --- a/colab/plugins/gitlab/tests/test_gitlab.py +++ b/colab/plugins/gitlab/tests/test_gitlab.py @@ -2,37 +2,44 @@ Test User class. Objective: Test parameters, and behavior. """ -from datetime import datetime - - from django.test import TestCase, Client -from colab.accounts.models import User -from colab.plugins.gitlab.models import GitlabProject, \ - GitlabIssue, GitlabComment, GitlabMergeRequest +from colab.plugins.gitlab.models import (GitlabProject, GitlabGroup, + GitlabIssue, GitlabComment, + GitlabMergeRequest) class GitlabTest(TestCase): + fixtures = ["test_gitlab_data"] + def setUp(self): - self.user = self.create_user() self.client = Client() - self.create_gitlab_data() - super(GitlabTest, self).setUp() def tearDown(self): pass def test_data_integrity(self): + self.assertEqual(GitlabGroup.objects.all().count(), 1) self.assertEqual(GitlabProject.objects.all().count(), 1) self.assertEqual(GitlabMergeRequest.objects.all().count(), 1) self.assertEqual(GitlabIssue.objects.all().count(), 1) self.assertEqual(GitlabComment.objects.all().count(), 2) + def test_group_projects(self): + group = GitlabGroup.objects.get(id=1) + self.assertEqual(len(group.projects), 1) + self.assertEqual(group.projects[0].name, 'colab') + def test_project_url(self): self.assertEqual(GitlabProject.objects.get(id=1).url, '/gitlab/softwarepublico/colab') + def test_project_group(self): + project = GitlabProject.objects.get(id=1) + self.assertEqual(project.name, 'colab') + self.assertEqual(project.group, 'softwarepublico') + def test_merge_request_url(self): self.assertEqual(GitlabMergeRequest.objects.get(iid=1).url, '/gitlab/softwarepublico/colab/merge_requests/1') @@ -48,67 +55,3 @@ class GitlabTest(TestCase): def test_comment_on_issue_url(self): self.assertEqual(GitlabComment.objects.get(id=2).url, '/gitlab/softwarepublico/colab/issues/1#notes_2') - - def create_gitlab_data(self): - g = GitlabProject() - g.id = 1 - g.name = "colab" - g.name_with_namespace = "Software Public / Colab" - g.path_with_namespace = "softwarepublico/colab" - g.created_at = datetime.now() - g.last_activity_at = datetime.now() - g.save() - - mr = GitlabMergeRequest() - mr.iid = 1 - mr.project = g - mr.title = "Include plugin support" - mr.description = "Merge request for plugin support" - mr.state = "Closed" - mr.created_at = datetime.now() - mr.update_user(self.user.username) - mr.save() - - i = GitlabIssue() - i.id = 1 - i.project = g - i.title = "Issue for colab" - i.description = "Issue reported to colab" - i.created_at = datetime.now() - i.state = "Open" - i.update_user(self.user.username) - i.save() - - c1 = GitlabComment() - c1.iid = 1 - c1.parent_id = mr.iid - c1.project = g - c1.body = "Comment to merge request" - c1.created_at = datetime.now() - c1.issue_comment = False - c1.update_user(self.user.username) - c1.save() - - c2 = GitlabComment() - c2.iid = 2 - c2.parent_id = i.id - c2.project = g - c2.body = "Comment to issue" - c2.created_at = datetime.now() - c2.issue_comment = True - c2.update_user(self.user.username) - c2.save() - - def create_user(self): - user = User() - user.username = "USERtestCoLaB" - user.set_password("123colab4") - user.email = "usertest@colab.com.br" - user.id = 1 - user.twitter = "usertestcolab" - user.facebook = "usertestcolab" - user.first_name = "USERtestCoLaB" - user.last_name = "COLAB" - user.save() - - return user -- libgit2 0.21.2