From 358a0795ea8ffb4fd4f77a85812bfe9496b72b2b Mon Sep 17 00:00:00 2001 From: Matheus Fernandes Date: Tue, 25 Aug 2015 11:36:05 -0300 Subject: [PATCH] Added data importer tests --- colab/plugins/gitlab/fixtures/gitlab_associations.json | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ colab/plugins/gitlab/tests/data.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ colab/plugins/gitlab/tests/test_data_importer.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+), 0 deletions(-) create mode 100644 colab/plugins/gitlab/fixtures/gitlab_associations.json create mode 100644 colab/plugins/gitlab/tests/data.py create mode 100644 colab/plugins/gitlab/tests/test_data_importer.py diff --git a/colab/plugins/gitlab/fixtures/gitlab_associations.json b/colab/plugins/gitlab/fixtures/gitlab_associations.json new file mode 100644 index 0000000..86cafc5 --- /dev/null +++ b/colab/plugins/gitlab/fixtures/gitlab_associations.json @@ -0,0 +1,60 @@ +[ +{ + "fields": { + "last_name": "Administrator", + "webpage": "", + "twitter": "", + "is_staff": true, + "user_permissions": [], + "date_joined": "2015-01-28T12:34:58.770Z", + "google_talk": "", + "first_name": "Admin", + "is_superuser": true, + "last_login": "2015-08-24T14:33:28.802Z", + "verification_hash": null, + "role": "", + "email": "admin@mail.com", + "username": "admin", + "bio": "", + "needs_update": true, + "is_active": true, + "facebook": "", + "groups": [], + "password": "pbkdf2_sha256$15000$sWnvfYpB8ec4$7dEFg6vSSGPnpEmfRelJ12zkiwacGx9aXx8/8ZFWBSI=", + "institution": "", + "github": "", + "modified": "2015-01-28T12:45:27.375Z" + }, + "model": "accounts.user", + "pk": 1 +}, +{ + "fields": { + "last_name": "user", + "webpage": null, + "twitter": null, + "is_staff": false, + "user_permissions": [], + "date_joined": "2015-08-24T14:33:55.827Z", + "google_talk": null, + "first_name": "user", + "is_superuser": false, + "last_login": "2015-08-24T14:33:55.827Z", + "verification_hash": null, + "role": null, + "email": "user@teste.com", + "username": "user", + "bio": null, + "needs_update": true, + "is_active": true, + "facebook": null, + "groups": [], + "password": "pbkdf2_sha256$15000$9ew6EvFvAIhI$147annuMjzt7em5IRh+3k7wcl7rZ0xjBPSmbUZDdxFo=", + "institution": null, + "github": null, + "modified": "2015-08-24T14:33:55.893Z" + }, + "model": "accounts.user", + "pk": 2 +} +] \ No newline at end of file diff --git a/colab/plugins/gitlab/tests/data.py b/colab/plugins/gitlab/tests/data.py new file mode 100644 index 0000000..f07bfd6 --- /dev/null +++ b/colab/plugins/gitlab/tests/data.py @@ -0,0 +1,57 @@ +colab_apps = {'gitlab': + {'menu_title': None, + 'private_token': 'token', + 'upstream': 'localhost', + 'urls': + {'include': 'colab.plugins.gitlab.urls', + 'namespace': 'gitlab', + 'prefix': 'gitlab/'}}} + +projects_json = [{"id": 32, + "description": "Test Gitlab", + "default_branch": "master", + "public": True, + "archived": False, + "visibility_level": 20, + "ssh_url_to_repo": "git@localhost/gitlabhq.git", + "http_url_to_repo": "localhost/gitlabhq.git", + "web_url": "localhost/gitlabhq", + "name": "Gitlab", + "name_with_namespace": "Test / Gitlab", + "path": "gitlabhq"}] + +groups_json = [{"id": 23, + "name": "Group 1", + "path": "group-1", + "owner_id": None}, + {"id": 27, + "name": "Group 2", + "path": "group-2", + "owner_id": None}] + +merge_json = [{"id": 7, + "iid": 1, + "project_id": 14, + "title": "Merge Title", + "description": "description", + "state": "merged", + "created_at": "2014-10-24T12:05:55.659Z", + "updated_at": "2014-10-24T12:06:15.572Z", + "target_branch": "master", + "source_branch": "settings_fix", + "upvotes": 0, + "downvotes": 0, + "author": {"name": "user", + "username": "user", + "id": 8, + "state": "active", + "avatar_url": "localhost"}, + "assignee": {"name": "user", + "username": "user", + "id": 8, + "state": "active", + "avatar_url": "localhost"}, + "source_project_id": 14, + "target_project_id": 14, + "labels": [], + "milestone": None}] diff --git a/colab/plugins/gitlab/tests/test_data_importer.py b/colab/plugins/gitlab/tests/test_data_importer.py new file mode 100644 index 0000000..d13f91d --- /dev/null +++ b/colab/plugins/gitlab/tests/test_data_importer.py @@ -0,0 +1,62 @@ +from django.test import TestCase +from django.test.utils import override_settings +from ..data_importer import GitlabDataImporter +from ..models import GitlabProject +from mock import patch +import data + + +class GitlabDataImporterTest(TestCase): + + fixtures = ["gitlab_associations"] + + colab_apps = data.colab_apps + projects_json = data.projects_json + groups_json = data.groups_json + merge_json = data.merge_json + + @override_settings(COLAB_APPS=colab_apps) + def setUp(self): + self.api = GitlabDataImporter() + + def test_resquest_url(self): + url = self.api.get_request_url('/gitlab/test/') + expected = 'localhost/gitlab/test/?private_token=token' + self.assertEqual(url, expected) + + def test_resquest_url_with_params(self): + url = self.api.get_request_url('/gitlab/test/', param='param') + expected = 'localhost/gitlab/test/?private_token=token¶m=param' + self.assertEqual(url, expected) + + @patch.object(GitlabDataImporter, 'get_json_data') + def test_fetch_projects(self, mock_json): + mock_json.side_effect = [self.projects_json, []] + + projects = self.api.fetch_projects() + self.assertEqual(len(projects), 1) + + self.assertEqual(projects[0].description, "Test Gitlab") + self.assertEqual(projects[0].public, True) + + @patch.object(GitlabDataImporter, 'get_json_data') + def test_fetch_groups(self, mock_json): + mock_json.side_effect = [self.groups_json, []] + + groups = self.api.fetch_groups() + self.assertEqual(len(groups), 2) + self.assertEqual(groups[0].name, "Group 1") + self.assertEqual(groups[1].name, "Group 2") + + self.assertEqual(groups[0].path, "group-1") + self.assertEqual(groups[1].path, "group-2") + + @patch.object(GitlabDataImporter, 'get_json_data') + def test_fetch_merge(self, mock_json): + mock_json.side_effect = [self.merge_json, []] + + merges = self.api.fetch_merge_request([GitlabProject()]) + self.assertEqual(len(merges), 1) + self.assertEqual(merges[0].title, "Merge Title") + self.assertEqual(merges[0].description, "description") + self.assertEqual(merges[0].get_author().username, "user") -- libgit2 0.21.2