Debian Codegraph @

tests/test_db.py

raw ◀ ▶
1 Debian patch touch this file
1 # -*- coding: utf-8 -*-
2
3 import unittest
4 from six.moves import configparser
5
6 import taskw.task
7 from bugwarrior import db
8
9 from .base import ConfigTest
10
11
12 class TestMergeLeft(unittest.TestCase):
13 def setUp(self):
14 self.issue_dict = {'annotations': ['testing']}
15
16 def assertMerged(self, local, remote, **kwargs):
17 db.merge_left('annotations', local, remote, **kwargs)
18 self.assertEqual(local, remote)
19
20 def test_with_dict(self):
21 self.assertMerged({}, self.issue_dict)
22
23 def test_with_taskw(self):
24 self.assertMerged(taskw.task.Task({}), self.issue_dict)
25
26 def test_already_in_sync(self):
27 self.assertMerged(self.issue_dict, self.issue_dict)
28
29 def test_rough_equality_hamming_false(self):
30 """ When hamming=False, rough equivalents are duplicated. """
31 remote = {'annotations': ['\n testing \n']}
32
33 db.merge_left('annotations', self.issue_dict, remote, hamming=False)
34 self.assertEqual(len(self.issue_dict['annotations']), 2)
35
36 def test_rough_equality_hamming_true(self):
37 """ When hamming=True, rough equivalents are not duplicated. """
38 remote = {'annotations': ['\n testing \n']}
39
40 db.merge_left('annotations', self.issue_dict, remote, hamming=True)
41 self.assertEqual(len(self.issue_dict['annotations']), 1)
42
43 class TestReplaceLeft(unittest.TestCase):
44 def setUp(self):
45 self.issue_dict = {'tags': ['test', 'test2'] }
46 self.remote = { 'tags': ['remote_tag1', 'remote_tag2'] }
47
48 def assertReplaced(self, local, remote, **kwargs):
49 db.replace_left('tags', local, remote, **kwargs)
50 self.assertEqual(local, remote)
51
52 def test_with_dict(self):
53 self.assertReplaced({}, self.issue_dict)
54
55 def test_with_taskw(self):
56 self.assertReplaced(taskw.task.Task({}), self.issue_dict)
57
58 def test_already_in_sync(self):
59 self.assertReplaced(self.issue_dict, self.issue_dict)
60
61 def test_replace(self):
62 self.assertReplaced(self.issue_dict, self.remote)
63
64 def test_replace_with_keeped_item(self):
65 """ When keeped_item is set, all item in this list are keeped """
66 result = {'tags': ['test', 'remote_tag1', 'remote_tag2'] }
67 print(self.issue_dict)
68 keeped_items = [ 'test' ]
69 db.replace_left('tags', self.issue_dict, self.remote, keeped_items)
70 self.assertEqual(self.issue_dict, result)
71
72
73 class TestSynchronize(ConfigTest):
74
•75 @unittest.skip("Fails with pytest")
76 def test_synchronize(self):
77
78 def get_tasks(tw):
79 tasks = tw.load_tasks()
80
81 # Remove non-deterministic keys.
82 del tasks['pending'][0]['modified']
83 del tasks['pending'][0]['entry']
84 del tasks['pending'][0]['uuid']
85
86 return tasks
87
88 config = configparser.RawConfigParser()
89 config.add_section('general')
90 config.set('general', 'targets', 'my_service')
91 config.set('general', 'static_fields', 'project, priority')
92 config.add_section('my_service')
93 config.set('my_service', 'service', 'github')
94
95 tw = taskw.TaskWarrior(self.taskrc)
96 self.assertEqual(tw.load_tasks(), {'completed': [], 'pending': []})
97
98 issue = {
99 'description': 'Blah blah blah. ☃',
100 'project': 'sample_project',
101 'githubtype': 'issue',
102 'githuburl': 'https://example.com',
103 'priority': 'M',
104 }
105
106 # TEST NEW ISSUE AND EXISTING ISSUE.
107 for _ in range(2):
108 # Use an issue generator with two copies of the same issue.
109 # These should be de-duplicated in db.synchronize before
110 # writing out to taskwarrior.
111 # https://github.com/ralphbean/bugwarrior/issues/601
112 issue_generator = iter((issue, issue,))
113 db.synchronize(issue_generator, config, 'general')
114
115 self.assertEqual(get_tasks(tw), {
116 'completed': [],
117 'pending': [{
118 u'project': u'sample_project',
119 u'priority': u'M',
120 u'status': u'pending',
121 u'description': u'Blah blah blah. ☃',
122 u'githuburl': u'https://example.com',
123 u'githubtype': u'issue',
124 u'id': 1,
125 u'urgency': 4.9,
126 }]})
127
128 # TEST CHANGED ISSUE.
129 issue['description'] = 'Yada yada yada.'
130
131 # Change static field
132 issue['project'] = 'other_project'
133
134 db.synchronize(iter((issue,)), config, 'general')
135
136 self.assertEqual(get_tasks(tw), {
137 'completed': [],
138 'pending': [{
139 u'priority': u'M',
140 u'project': u'sample_project',
141 u'status': u'pending',
142 u'description': u'Yada yada yada.',
143 u'githuburl': u'https://example.com',
144 u'githubtype': u'issue',
145 u'id': 1,
146 u'urgency': 4.9,
147 }]})
148
149 # TEST CLOSED ISSUE.
150 db.synchronize(iter(()), config, 'general')
151
152 tasks = tw.load_tasks()
153
154 # Remove non-deterministic keys.
155 del tasks['completed'][0]['modified']
156 del tasks['completed'][0]['entry']
157 del tasks['completed'][0]['end']
158 del tasks['completed'][0]['uuid']
159
160 self.assertEqual(tasks, {
161 'completed': [{
162 u'project': u'sample_project',
163 u'description': u'Yada yada yada.',
164 u'githubtype': u'issue',
165 u'githuburl': u'https://example.com',
166 u'id': 0,
167 u'priority': u'M',
168 u'status': u'completed',
169 u'urgency': 4.9,
170 }],
171 'pending': []})
172
173 class TestUDAs(ConfigTest):
•174 @unittest.skip("Fails with pytest")
175 def test_udas(self):
176 config = configparser.RawConfigParser()
177 config.add_section('general')
178 config.set('general', 'targets', 'my_service')
179 config.add_section('my_service')
180 config.set('my_service', 'service', 'github')
181
182 udas = sorted(list(db.get_defined_udas_as_strings(config, 'general')))
183 self.assertEqual(udas, [
184 u'uda.githubbody.label=Github Body',
185 u'uda.githubbody.type=string',
186 u'uda.githubclosedon.label=GitHub Closed',
187 u'uda.githubclosedon.type=date',
188 u'uda.githubcreatedon.label=Github Created',
189 u'uda.githubcreatedon.type=date',
190 u'uda.githubmilestone.label=Github Milestone',
191 u'uda.githubmilestone.type=string',
192 u'uda.githubnamespace.label=Github Namespace',
193 u'uda.githubnamespace.type=string',
194 u'uda.githubnumber.label=Github Issue/PR #',
195 u'uda.githubnumber.type=numeric',
196 u'uda.githubrepo.label=Github Repo Slug',
197 u'uda.githubrepo.type=string',
198 u'uda.githubstate.label=GitHub State',
199 u'uda.githubstate.type=string',
200 u'uda.githubtitle.label=Github Title',
201 u'uda.githubtitle.type=string',
202 u'uda.githubtype.label=Github Type',
203 u'uda.githubtype.type=string',
204 u'uda.githubupdatedat.label=Github Updated',
205 u'uda.githubupdatedat.type=date',
206 u'uda.githuburl.label=Github URL',
207 u'uda.githuburl.type=string',
208 u'uda.githubuser.label=Github User',
209 u'uda.githubuser.type=string',
210 ])