Debian Codegraph @

tests/test_db.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 def test_synchronize(self):
76
77 def get_tasks(tw):
78 tasks = tw.load_tasks()
79
80 # Remove non-deterministic keys.
81 del tasks['pending'][0]['modified']
82 del tasks['pending'][0]['entry']
83 del tasks['pending'][0]['uuid']
84
85 return tasks
86
87 config = configparser.RawConfigParser()
88 config.add_section('general')
89 config.set('general', 'targets', 'my_service')
90 config.set('general', 'static_fields', 'project, priority')
91 config.add_section('my_service')
92 config.set('my_service', 'service', 'github')
93
94 tw = taskw.TaskWarrior(self.taskrc)
95 self.assertEqual(tw.load_tasks(), {'completed': [], 'pending': []})
96
97 issue = {
98 'description': 'Blah blah blah. ☃',
99 'project': 'sample_project',
100 'githubtype': 'issue',
101 'githuburl': 'https://example.com',
102 'priority': 'M',
103 }
104
105 # TEST NEW ISSUE AND EXISTING ISSUE.
106 for _ in range(2):
107 # Use an issue generator with two copies of the same issue.
108 # These should be de-duplicated in db.synchronize before
109 # writing out to taskwarrior.
110 # https://github.com/ralphbean/bugwarrior/issues/601
111 issue_generator = iter((issue, issue,))
112 db.synchronize(issue_generator, config, 'general')
113
114 self.assertEqual(get_tasks(tw), {
115 'completed': [],
116 'pending': [{
117 u'project': u'sample_project',
118 u'priority': u'M',
119 u'status': u'pending',
120 u'description': u'Blah blah blah. ☃',
121 u'githuburl': u'https://example.com',
122 u'githubtype': u'issue',
123 u'id': 1,
124 u'urgency': 4.9,
125 }]})
126
127 # TEST CHANGED ISSUE.
128 issue['description'] = 'Yada yada yada.'
129
130 # Change static field
131 issue['project'] = 'other_project'
132
133 db.synchronize(iter((issue,)), config, 'general')
134
135 self.assertEqual(get_tasks(tw), {
136 'completed': [],
137 'pending': [{
138 u'priority': u'M',
139 u'project': u'sample_project',
140 u'status': u'pending',
141 u'description': u'Yada yada yada.',
142 u'githuburl': u'https://example.com',
143 u'githubtype': u'issue',
144 u'id': 1,
145 u'urgency': 4.9,
146 }]})
147
148 # TEST CLOSED ISSUE.
149 db.synchronize(iter(()), config, 'general')
150
151 tasks = tw.load_tasks()
152
153 # Remove non-deterministic keys.
154 del tasks['completed'][0]['modified']
155 del tasks['completed'][0]['entry']
156 del tasks['completed'][0]['end']
157 del tasks['completed'][0]['uuid']
158
159 self.assertEqual(tasks, {
160 'completed': [{
161 u'project': u'sample_project',
162 u'description': u'Yada yada yada.',
163 u'githubtype': u'issue',
164 u'githuburl': u'https://example.com',
165 u'id': 0,
166 u'priority': u'M',
167 u'status': u'completed',
168 u'urgency': 4.9,
169 }],
170 'pending': []})
171
172 class TestUDAs(ConfigTest):
173 def test_udas(self):
174 config = configparser.RawConfigParser()
175 config.add_section('general')
176 config.set('general', 'targets', 'my_service')
177 config.add_section('my_service')
178 config.set('my_service', 'service', 'github')
179
180 udas = sorted(list(db.get_defined_udas_as_strings(config, 'general')))
181 self.assertEqual(udas, [
182 u'uda.githubbody.label=Github Body',
183 u'uda.githubbody.type=string',
184 u'uda.githubclosedon.label=GitHub Closed',
185 u'uda.githubclosedon.type=date',
186 u'uda.githubcreatedon.label=Github Created',
187 u'uda.githubcreatedon.type=date',
188 u'uda.githubmilestone.label=Github Milestone',
189 u'uda.githubmilestone.type=string',
190 u'uda.githubnamespace.label=Github Namespace',
191 u'uda.githubnamespace.type=string',
192 u'uda.githubnumber.label=Github Issue/PR #',
193 u'uda.githubnumber.type=numeric',
194 u'uda.githubrepo.label=Github Repo Slug',
195 u'uda.githubrepo.type=string',
196 u'uda.githubstate.label=GitHub State',
197 u'uda.githubstate.type=string',
198 u'uda.githubtitle.label=Github Title',
199 u'uda.githubtitle.type=string',
200 u'uda.githubtype.label=Github Type',
201 u'uda.githubtype.type=string',
202 u'uda.githubupdatedat.label=Github Updated',
203 u'uda.githubupdatedat.type=date',
204 u'uda.githuburl.label=Github URL',
205 u'uda.githuburl.type=string',
206 u'uda.githubuser.label=Github User',
207 u'uda.githubuser.type=string',
208 ])