Commit 517db36e11d52d0d5ec8b252509296ba911bd2e2
1 parent
69735235
Exists in
community_association
Improved data_api tests
Signed-off-by: Matheus Fernandes <matheus.souza.fernandes@gmail.com> Signed-off-by: Lucas Moura <lucas.mouta128@gmail.com>
Showing
3 changed files
with
141 additions
and
108 deletions
Show diff stats
colab/plugins/gitlab/data_api.py
@@ -65,138 +65,82 @@ class GitlabDataAPI(ProxyDataAPI): | @@ -65,138 +65,82 @@ class GitlabDataAPI(ProxyDataAPI): | ||
65 | 65 | ||
66 | return _object | 66 | return _object |
67 | 67 | ||
68 | - def fetch_projects(self): | 68 | + def fetch(self, url, gitlab_class): |
69 | page = 1 | 69 | page = 1 |
70 | - projects = [] | 70 | + obj_list = [] |
71 | 71 | ||
72 | while True: | 72 | while True: |
73 | - json_data = self.get_json_data('/api/v3/projects/all', page) | 73 | + json_data = self.get_json_data(url, page) |
74 | page = page + 1 | 74 | page = page + 1 |
75 | 75 | ||
76 | if not len(json_data): | 76 | if not len(json_data): |
77 | break | 77 | break |
78 | 78 | ||
79 | for element in json_data: | 79 | for element in json_data: |
80 | - project = GitlabProject() | ||
81 | - self.fill_object_data(element, project) | ||
82 | - projects.append(project) | ||
83 | - | ||
84 | - return projects | ||
85 | - | ||
86 | - def fetch_groups(self): | ||
87 | - page = 1 | ||
88 | - groups = [] | 80 | + obj = gitlab_class() |
81 | + self.fill_object_data(element, obj) | ||
82 | + obj_list.append(obj) | ||
89 | 83 | ||
90 | - while True: | ||
91 | - json_data = self.get_json_data('/api/v3/groups', page) | ||
92 | - page = page + 1 | 84 | + return obj_list |
93 | 85 | ||
94 | - if not len(json_data): | ||
95 | - break | ||
96 | - | ||
97 | - for element in json_data: | ||
98 | - group = GitlabGroup() | ||
99 | - self.fill_object_data(element, group) | ||
100 | - groups.append(group) | ||
101 | - | ||
102 | - return groups | ||
103 | - | ||
104 | - def fetch_merge_request(self, projects): | ||
105 | - all_merge_request = [] | 86 | + def fetch_comments(self, url, parent_class, issue_comment): |
87 | + all_comments = [] | ||
88 | + all_parent_objects = parent_class.objects.all() | ||
106 | 89 | ||
107 | - for project in projects: | 90 | + for parent_obj in all_parent_objects: |
108 | page = 1 | 91 | page = 1 |
109 | while True: | 92 | while True: |
110 | - url = '/api/v3/projects/{}/merge_requests'.format(project.id) | ||
111 | - json_data_mr = self.get_json_data(url, page) | 93 | + formated_url = url.format(parent_obj.project_id, parent_obj.id) |
94 | + json_data = self.get_json_data(formated_url, page) | ||
112 | page = page + 1 | 95 | page = page + 1 |
113 | 96 | ||
114 | - if len(json_data_mr) == 0: | 97 | + if len(json_data) == 0: |
115 | break | 98 | break |
116 | 99 | ||
117 | - for element in json_data_mr: | ||
118 | - single_merge_request = GitlabMergeRequest() | ||
119 | - self.fill_object_data(element, single_merge_request) | ||
120 | - all_merge_request.append(single_merge_request) | ||
121 | - | ||
122 | - return all_merge_request | 100 | + for element in json_data: |
101 | + single_comment = GitlabComment() | ||
102 | + self.fill_object_data(element, single_comment) | ||
103 | + single_comment.project = parent_obj.project | ||
104 | + single_comment.issue_comment = issue_comment | ||
105 | + single_comment.parent_id = parent_obj.id | ||
106 | + all_comments.append(single_comment) | ||
123 | 107 | ||
124 | - def fetch_issue(self, projects): | ||
125 | - all_issues = [] | 108 | + return all_comments |
126 | 109 | ||
127 | - for project in projects: | ||
128 | - page = 1 | ||
129 | - while True: | ||
130 | - url = '/api/v3/projects/{}/issues'.format(project.id) | ||
131 | - json_data_issue = self.get_json_data(url, page) | ||
132 | - page = page + 1 | 110 | + def fetch_projects(self): |
111 | + return self.fetch('/api/v3/projects/all', GitlabProject) | ||
133 | 112 | ||
134 | - if len(json_data_issue) == 0: | ||
135 | - break | 113 | + def fetch_groups(self): |
114 | + return self.fetch('/api/v3/groups', GitlabGroup) | ||
136 | 115 | ||
137 | - for element in json_data_issue: | ||
138 | - single_issue = GitlabIssue() | ||
139 | - self.fill_object_data(element, single_issue) | ||
140 | - all_issues.append(single_issue) | 116 | + def fetch_merge_request(self, projects): |
117 | + merge_requests = [] | ||
118 | + for project in projects: | ||
119 | + url = '/api/v3/projects/{}/merge_requests'.format(project.id) | ||
120 | + merge_requests.extend(self.fetch(url, GitlabMergeRequest)) | ||
121 | + return merge_requests | ||
141 | 122 | ||
142 | - return all_issues | 123 | + def fetch_issue(self, projects): |
124 | + issues = [] | ||
125 | + for project in projects: | ||
126 | + url = '/api/v3/projects/{}/issues'.format(project.id) | ||
127 | + issues.extend(self.fetch(url, GitlabIssue)) | ||
128 | + return issues | ||
143 | 129 | ||
144 | - def fetch_comments(self): | 130 | + def fetch_all_comments(self): |
145 | all_comments = [] | 131 | all_comments = [] |
146 | - all_comments.extend(self.fetch_comments_MR()) | 132 | + all_comments.extend(self.fetch_comments_mr()) |
147 | all_comments.extend(self.fetch_comments_issues()) | 133 | all_comments.extend(self.fetch_comments_issues()) |
148 | 134 | ||
149 | return all_comments | 135 | return all_comments |
150 | 136 | ||
151 | - def fetch_comments_MR(self): | ||
152 | - all_comments = [] | ||
153 | - all_merge_requests = GitlabMergeRequest.objects.all() | ||
154 | - | ||
155 | - for merge_request in all_merge_requests: | ||
156 | - page = 1 | ||
157 | - while True: | ||
158 | - url = '/api/v3/projects/{}/merge_requests/{}/notes'.format( | ||
159 | - merge_request.project_id, merge_request.id) | ||
160 | - json_data_mr = self.get_json_data(url, page) | ||
161 | - page = page + 1 | ||
162 | - | ||
163 | - if len(json_data_mr) == 0: | ||
164 | - break | ||
165 | - | ||
166 | - for element in json_data_mr: | ||
167 | - single_comment = GitlabComment() | ||
168 | - self.fill_object_data(element, single_comment) | ||
169 | - single_comment.project = merge_request.project | ||
170 | - single_comment.issue_comment = False | ||
171 | - single_comment.parent_id = merge_request.id | ||
172 | - all_comments.append(single_comment) | ||
173 | - | ||
174 | - return all_comments | 137 | + def fetch_comments_mr(self): |
138 | + url = '/api/v3/projects/{}/merge_requests/{}/notes' | ||
139 | + return self.fetch_comments(url, GitlabMergeRequest, False) | ||
175 | 140 | ||
176 | def fetch_comments_issues(self): | 141 | def fetch_comments_issues(self): |
177 | - all_comments = [] | ||
178 | - all_issues = GitlabIssue.objects.all() | ||
179 | - | ||
180 | - for issue in all_issues: | ||
181 | - page = 1 | ||
182 | - while True: | ||
183 | - url = '/api/v3/projects/{}/issues/{}/notes'.format( | ||
184 | - issue.project_id, issue.id) | ||
185 | - json_data_mr = self.get_json_data(url, page) | ||
186 | - page = page + 1 | ||
187 | - | ||
188 | - if len(json_data_mr) == 0: | ||
189 | - break | ||
190 | - | ||
191 | - for element in json_data_mr: | ||
192 | - single_comment = GitlabComment() | ||
193 | - self.fill_object_data(element, single_comment) | ||
194 | - single_comment.project = issue.project | ||
195 | - single_comment.issue_comment = True | ||
196 | - single_comment.parent_id = issue.id | ||
197 | - all_comments.append(single_comment) | ||
198 | - | ||
199 | - return all_comments | 142 | + url = '/api/v3/projects/{}/issues/{}/notes' |
143 | + return self.fetch_comments(url, GitlabIssue, True) | ||
200 | 144 | ||
201 | def fetch_data(self): | 145 | def fetch_data(self): |
202 | LOGGER.info("Importing Projects") | 146 | LOGGER.info("Importing Projects") |
@@ -209,7 +153,7 @@ class GitlabDataAPI(ProxyDataAPI): | @@ -209,7 +153,7 @@ class GitlabDataAPI(ProxyDataAPI): | ||
209 | issues = self.fetch_issue(projects) | 153 | issues = self.fetch_issue(projects) |
210 | 154 | ||
211 | LOGGER.info("Importing Comments") | 155 | LOGGER.info("Importing Comments") |
212 | - comments = self.fetch_comments() | 156 | + comments = self.fetch_all_comments() |
213 | 157 | ||
214 | LOGGER.info("Importing Groups") | 158 | LOGGER.info("Importing Groups") |
215 | groups = self.fetch_groups() | 159 | groups = self.fetch_groups() |
@@ -0,0 +1,52 @@ | @@ -0,0 +1,52 @@ | ||
1 | +proxied_app = {'menu_title': None, | ||
2 | + 'private_token': 'token', | ||
3 | + 'upstream': 'localhost/gitlab/'} | ||
4 | + | ||
5 | +projects_json = [{"id": 32, | ||
6 | + "description": "Test Gitlab", | ||
7 | + "default_branch": "master", | ||
8 | + "public": True, | ||
9 | + "archived": False, | ||
10 | + "visibility_level": 20, | ||
11 | + "ssh_url_to_repo": "git@localhost/gitlabhq.git", | ||
12 | + "http_url_to_repo": "localhost/gitlabhq.git", | ||
13 | + "web_url": "localhost/gitlabhq", | ||
14 | + "name": "Gitlab", | ||
15 | + "name_with_namespace": "Test / Gitlab", | ||
16 | + "path": "gitlabhq"}] | ||
17 | + | ||
18 | +groups_json = [{"id": 23, | ||
19 | + "name": "Group 1", | ||
20 | + "path": "group-1", | ||
21 | + "owner_id": None}, | ||
22 | + {"id": 27, | ||
23 | + "name": "Group 2", | ||
24 | + "path": "group-2", | ||
25 | + "owner_id": None}] | ||
26 | + | ||
27 | +merge_json = [{"id": 7, | ||
28 | + "iid": 1, | ||
29 | + "project_id": 14, | ||
30 | + "title": "Merge Title", | ||
31 | + "description": "description", | ||
32 | + "state": "merged", | ||
33 | + "created_at": "2014-10-24T12:05:55.659Z", | ||
34 | + "updated_at": "2014-10-24T12:06:15.572Z", | ||
35 | + "target_branch": "master", | ||
36 | + "source_branch": "settings_fix", | ||
37 | + "upvotes": 0, | ||
38 | + "downvotes": 0, | ||
39 | + "author": {"name": "user", | ||
40 | + "username": "user", | ||
41 | + "id": 8, | ||
42 | + "state": "active", | ||
43 | + "avatar_url": "localhost"}, | ||
44 | + "assignee": {"name": "user", | ||
45 | + "username": "user", | ||
46 | + "id": 8, | ||
47 | + "state": "active", | ||
48 | + "avatar_url": "localhost"}, | ||
49 | + "source_project_id": 14, | ||
50 | + "target_project_id": 14, | ||
51 | + "labels": [], | ||
52 | + "milestone": None}] |
colab/plugins/gitlab/tests/test_data_api.py
1 | from django.test import TestCase | 1 | from django.test import TestCase |
2 | -from colab.plugins.gitlab.data_api import GitlabDataAPI | 2 | +from ..data_api import GitlabDataAPI |
3 | +from colab.plugins.gitlab.models import GitlabProject | ||
3 | from mock import patch | 4 | from mock import patch |
5 | +import data | ||
4 | 6 | ||
5 | 7 | ||
6 | class GitlabDataApiTest(TestCase): | 8 | class GitlabDataApiTest(TestCase): |
7 | 9 | ||
8 | - data = {'menu_title': None, | ||
9 | - 'private_token': 'token', | ||
10 | - 'upstream': 'localhost/gitlab/'} | 10 | + proxied_app = data.proxied_app |
11 | + projects_json = data.projects_json | ||
12 | + groups_json = data.groups_json | ||
13 | + merge_json = data.merge_json | ||
11 | 14 | ||
12 | def setUp(self): | 15 | def setUp(self): |
13 | self.api = GitlabDataAPI() | 16 | self.api = GitlabDataAPI() |
@@ -15,14 +18,48 @@ class GitlabDataApiTest(TestCase): | @@ -15,14 +18,48 @@ class GitlabDataApiTest(TestCase): | ||
15 | def tearDown(self): | 18 | def tearDown(self): |
16 | pass | 19 | pass |
17 | 20 | ||
18 | - @patch.dict('django.conf.settings.PROXIED_APPS', gitlab=data) | 21 | + @patch.dict('django.conf.settings.PROXIED_APPS', gitlab=proxied_app) |
19 | def test_resquest_url(self): | 22 | def test_resquest_url(self): |
20 | url = self.api.get_request_url('/test/') | 23 | url = self.api.get_request_url('/test/') |
21 | expected = 'localhost/gitlab/test/?private_token=token' | 24 | expected = 'localhost/gitlab/test/?private_token=token' |
22 | self.assertEqual(url, expected) | 25 | self.assertEqual(url, expected) |
23 | 26 | ||
24 | - @patch.dict('django.conf.settings.PROXIED_APPS', gitlab=data) | 27 | + @patch.dict('django.conf.settings.PROXIED_APPS', gitlab=proxied_app) |
25 | def test_resquest_url_with_params(self): | 28 | def test_resquest_url_with_params(self): |
26 | url = self.api.get_request_url('/test/', param='param') | 29 | url = self.api.get_request_url('/test/', param='param') |
27 | expected = 'localhost/gitlab/test/?private_token=token¶m=param' | 30 | expected = 'localhost/gitlab/test/?private_token=token¶m=param' |
28 | self.assertEqual(url, expected) | 31 | self.assertEqual(url, expected) |
32 | + | ||
33 | + @patch.object(GitlabDataAPI, 'get_json_data') | ||
34 | + def test_fetch_projects(self, mock_json): | ||
35 | + mock_json.side_effect = [self.projects_json, []] | ||
36 | + | ||
37 | + projects = self.api.fetch_projects() | ||
38 | + self.assertEqual(len(projects), 1) | ||
39 | + | ||
40 | + self.assertEqual(projects[0].description, "Test Gitlab") | ||
41 | + self.assertEqual(projects[0].public, True) | ||
42 | + | ||
43 | + @patch.object(GitlabDataAPI, 'get_json_data') | ||
44 | + def test_fetch_groups(self, mock_json): | ||
45 | + mock_json.side_effect = [self.groups_json, []] | ||
46 | + | ||
47 | + groups = self.api.fetch_groups() | ||
48 | + self.assertEqual(len(groups), 2) | ||
49 | + self.assertEqual(groups[0].name, "Group 1") | ||
50 | + self.assertEqual(groups[1].name, "Group 2") | ||
51 | + | ||
52 | + self.assertEqual(groups[0].path, "group-1") | ||
53 | + self.assertEqual(groups[1].path, "group-2") | ||
54 | + | ||
55 | + @patch.object(GitlabDataAPI, 'get_json_data') | ||
56 | + def test_fetch_merge(self, mock_json): | ||
57 | + mock_json.side_effect = [self.groups_json, []] | ||
58 | + | ||
59 | + merges = self.api.fetch_merge_request([GitlabProject()]) | ||
60 | + print merges[0].title | ||
61 | + print merges[1].title | ||
62 | + self.assertEqual(len(merges), 1) | ||
63 | + self.assertEqual(merges[0].title, "Merge Title") | ||
64 | + self.assertEqual(merges[0].description, "description") | ||
65 | + self.assertEqual(merges[0].user.username, "user") |