11import copy
2- import unittest
2+ from copy import deepcopy
33
4+ import pytest
45import taskw .task
56
67from bugwarrior import db
78
89from .base import ConfigTest
910
11+ ISSUE_DICT = {'annotations' : ['testing' ]}
12+
13+
14+ class TestMergeLeft :
15+ @pytest .mark .parametrize (
16+ 'local' ,
17+ [{}, taskw .task .Task ({}), deepcopy (ISSUE_DICT )],
18+ ids = ['dict' , 'taskw' , 'already-in-sync' ],
19+ )
20+ def test_merge_left (self , local ):
21+ db .merge_left ('annotations' , local , ISSUE_DICT )
22+
23+ assert local == ISSUE_DICT
24+
25+ @pytest .mark .parametrize ('hamming,result' , [(True , 1 ), (False , 2 )])
26+ def test_rough_equality (self , hamming , result ):
27+ """
28+ Rough equivalents are duplicated, are duplicated when hamming=False,
29+ deduplicated when hamming=True
30+ """
31+ issue_dict = deepcopy (ISSUE_DICT )
32+ db .merge_left (
33+ 'annotations' ,
34+ local_task = issue_dict ,
35+ remote_issue = {'annotations' : ['\n testing \n ' ]},
36+ hamming = hamming ,
37+ )
38+ assert len (issue_dict ['annotations' ]) == result
1039
11- class TestMergeLeft (unittest .TestCase ):
12- def setUp (self ):
13- self .issue_dict = {'annotations' : ['testing' ]}
14-
15- def assertMerged (self , local , remote , ** kwargs ):
16- db .merge_left ('annotations' , local , remote , ** kwargs )
17- self .assertEqual (local , remote )
18-
19- def test_with_dict (self ):
20- self .assertMerged ({}, self .issue_dict )
21-
22- def test_with_taskw (self ):
23- self .assertMerged (taskw .task .Task ({}), self .issue_dict )
24-
25- def test_already_in_sync (self ):
26- self .assertMerged (self .issue_dict , self .issue_dict )
27-
28- def test_rough_equality_hamming_false (self ):
29- """When hamming=False, rough equivalents are duplicated."""
30- remote = {'annotations' : ['\n testing \n ' ]}
31-
32- db .merge_left ('annotations' , self .issue_dict , remote , hamming = False )
33- self .assertEqual (len (self .issue_dict ['annotations' ]), 2 )
34-
35- def test_rough_equality_hamming_true (self ):
36- """When hamming=True, rough equivalents are not duplicated."""
37- remote = {'annotations' : ['\n testing \n ' ]}
38-
39- db .merge_left ('annotations' , self .issue_dict , remote , hamming = True )
40- self .assertEqual (len (self .issue_dict ['annotations' ]), 1 )
41-
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 )
5140
52- def test_with_dict ( self ):
53- self . assertReplaced ({}, self . issue_dict )
41+ TAGS_DICT = { 'tags' : [ 'test' , 'test2' ]}
42+ REMOTE_TAGS_DICT = { 'tags' : [ 'remote_tag1' , 'remote_tag2' ]}
5443
55- def test_with_taskw (self ):
56- self .assertReplaced (taskw .task .Task ({}), self .issue_dict )
5744
58- def test_already_in_sync (self ):
59- self .assertReplaced (self .issue_dict , self .issue_dict )
45+ class TestReplaceLeft :
46+ @pytest .mark .parametrize (
47+ 'local, remote' ,
48+ [
49+ ({}, TAGS_DICT ),
50+ (taskw .task .Task ({}), TAGS_DICT ),
51+ (TAGS_DICT , TAGS_DICT ),
52+ (TAGS_DICT , REMOTE_TAGS_DICT ),
53+ ],
54+ ids = ['dict' , 'taskw' , 'already-in-sync' , 'replace' ],
55+ )
56+ def test_replace_left (self , local , remote ):
57+ local = deepcopy (local )
58+ db .replace_left ('tags' , local , deepcopy (remote ))
6059
61- def test_replace (self ):
62- self .assertReplaced (self .issue_dict , self .remote )
60+ assert local == remote
6361
6462 def test_replace_with_keeped_item (self ):
6563 """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 )
64+ issue_dict = deepcopy (TAGS_DICT )
6865 keeped_items = ['test' ]
69- db .replace_left ('tags' , self .issue_dict , self .remote , keeped_items )
70- self .assertEqual (self .issue_dict , result )
66+
67+ db .replace_left ('tags' , issue_dict , deepcopy (REMOTE_TAGS_DICT ), keeped_items )
68+
69+ assert issue_dict == {'tags' : ['test' , 'remote_tag1' , 'remote_tag2' ]}
7170
7271
7372class TestSynchronize (ConfigTest ):
@@ -100,7 +99,7 @@ def get_tasks(tw):
10099 bwconfig = self .validate ()
101100
102101 tw = taskw .TaskWarrior (self .taskrc )
103- self . assertEqual ( tw .load_tasks (), {'completed' : [], 'pending' : []})
102+ assert tw .load_tasks () == {'completed' : [], 'pending' : []}
104103
105104 issue = {
106105 'description' : 'Blah blah blah. ☃' ,
@@ -123,53 +122,47 @@ def get_tasks(tw):
123122 issue_generator = iter ((copy .deepcopy (issue ), duplicate_issue ))
124123 db .synchronize (issue_generator , bwconfig )
125124
126- self .assertEqual (
127- get_tasks (tw ),
128- {
129- 'completed' : [],
130- 'pending' : [
131- {
132- 'project' : 'sample_project' ,
133- 'priority' : 'M' ,
134- 'status' : 'pending' ,
135- 'description' : 'Blah blah blah. ☃' ,
136- 'githuburl' : 'https://example.com' ,
137- 'githubtype' : 'issue' ,
138- 'id' : 1 ,
139- 'tags' : ['bar' , 'foo' ],
140- 'urgency' : 5.8 ,
141- }
142- ],
143- },
144- )
145-
146- # TEST CHANGED ISSUE.
147- issue ['description' ] = 'Yada yada yada.'
148-
149- # Change static field
150- issue ['project' ] = 'other_project'
151-
152- db .synchronize (iter ((copy .deepcopy (issue ),)), bwconfig )
153-
154- self .assertEqual (
155- get_tasks (tw ),
156- {
125+ assert get_tasks (tw ) == {
157126 'completed' : [],
158127 'pending' : [
159128 {
160- 'priority' : 'M' ,
161129 'project' : 'sample_project' ,
130+ 'priority' : 'M' ,
162131 'status' : 'pending' ,
163- 'description' : 'Yada yada yada. ' ,
132+ 'description' : 'Blah blah blah. ☃ ' ,
164133 'githuburl' : 'https://example.com' ,
165134 'githubtype' : 'issue' ,
166135 'id' : 1 ,
167136 'tags' : ['bar' , 'foo' ],
168137 'urgency' : 5.8 ,
169138 }
170139 ],
171- },
172- )
140+ }
141+
142+ # TEST CHANGED ISSUE.
143+ issue ['description' ] = 'Yada yada yada.'
144+
145+ # Change static field
146+ issue ['project' ] = 'other_project'
147+
148+ db .synchronize (iter ((copy .deepcopy (issue ),)), bwconfig )
149+
150+ assert get_tasks (tw ) == {
151+ 'completed' : [],
152+ 'pending' : [
153+ {
154+ 'priority' : 'M' ,
155+ 'project' : 'sample_project' ,
156+ 'status' : 'pending' ,
157+ 'description' : 'Yada yada yada.' ,
158+ 'githuburl' : 'https://example.com' ,
159+ 'githubtype' : 'issue' ,
160+ 'id' : 1 ,
161+ 'tags' : ['bar' , 'foo' ],
162+ 'urgency' : 5.8 ,
163+ }
164+ ],
165+ }
173166
174167 # TEST CLOSED ISSUE.
175168 db .synchronize (iter (()), bwconfig )
@@ -178,54 +171,46 @@ def get_tasks(tw):
178171
179172 tasks = remove_non_deterministic_keys (copy .deepcopy (completed_tasks ))
180173 del tasks ['completed' ][0 ]['end' ]
181- self .assertEqual (
182- tasks ,
183- {
184- 'completed' : [
185- {
186- 'project' : 'sample_project' ,
187- 'description' : 'Yada yada yada.' ,
188- 'githubtype' : 'issue' ,
189- 'githuburl' : 'https://example.com' ,
190- 'id' : 0 ,
191- 'priority' : 'M' ,
192- 'status' : 'completed' ,
193- 'tags' : ['bar' , 'foo' ],
194- 'urgency' : 5.8 ,
195- }
196- ],
197- 'pending' : [],
198- },
199- )
174+ assert tasks == {
175+ 'completed' : [
176+ {
177+ 'project' : 'sample_project' ,
178+ 'description' : 'Yada yada yada.' ,
179+ 'githubtype' : 'issue' ,
180+ 'githuburl' : 'https://example.com' ,
181+ 'id' : 0 ,
182+ 'priority' : 'M' ,
183+ 'status' : 'completed' ,
184+ 'tags' : ['bar' , 'foo' ],
185+ 'urgency' : 5.8 ,
186+ }
187+ ],
188+ 'pending' : [],
189+ }
200190
201191 # TEST REOPENED ISSUE
202192 db .synchronize (iter ((copy .deepcopy (issue ),)), bwconfig )
203193
204194 tasks = tw .load_tasks ()
205- self .assertEqual (
206- completed_tasks ['completed' ][0 ]['uuid' ], tasks ['pending' ][0 ]['uuid' ]
207- )
195+ assert completed_tasks ['completed' ][0 ]['uuid' ] == tasks ['pending' ][0 ]['uuid' ]
208196
209197 tasks = remove_non_deterministic_keys (tasks )
210- self .assertEqual (
211- tasks ,
212- {
213- 'completed' : [],
214- 'pending' : [
215- {
216- 'priority' : 'M' ,
217- 'project' : 'sample_project' ,
218- 'status' : 'pending' ,
219- 'description' : 'Yada yada yada.' ,
220- 'githuburl' : 'https://example.com' ,
221- 'githubtype' : 'issue' ,
222- 'id' : 1 ,
223- 'tags' : ['bar' , 'foo' ],
224- 'urgency' : 5.8 ,
225- }
226- ],
227- },
228- )
198+ assert tasks == {
199+ 'completed' : [],
200+ 'pending' : [
201+ {
202+ 'priority' : 'M' ,
203+ 'project' : 'sample_project' ,
204+ 'status' : 'pending' ,
205+ 'description' : 'Yada yada yada.' ,
206+ 'githuburl' : 'https://example.com' ,
207+ 'githubtype' : 'issue' ,
208+ 'id' : 1 ,
209+ 'tags' : ['bar' , 'foo' ],
210+ 'urgency' : 5.8 ,
211+ }
212+ ],
213+ }
229214
230215
231216class TestUDAs (ConfigTest ):
@@ -242,36 +227,33 @@ def test_udas(self):
242227
243228 conf = self .validate ()
244229 udas = sorted (list (db .get_defined_udas_as_strings (conf )))
245- self .assertEqual (
246- udas ,
247- [
248- 'uda.githubbody.label=Github Body' ,
249- 'uda.githubbody.type=string' ,
250- 'uda.githubclosedon.label=GitHub Closed' ,
251- 'uda.githubclosedon.type=date' ,
252- 'uda.githubcreatedon.label=Github Created' ,
253- 'uda.githubcreatedon.type=date' ,
254- 'uda.githubdraft.label=GitHub Draft' ,
255- 'uda.githubdraft.type=numeric' ,
256- 'uda.githubmilestone.label=Github Milestone' ,
257- 'uda.githubmilestone.type=string' ,
258- 'uda.githubnamespace.label=Github Namespace' ,
259- 'uda.githubnamespace.type=string' ,
260- 'uda.githubnumber.label=Github Issue/PR #' ,
261- 'uda.githubnumber.type=numeric' ,
262- 'uda.githubrepo.label=Github Repo Slug' ,
263- 'uda.githubrepo.type=string' ,
264- 'uda.githubstate.label=GitHub State' ,
265- 'uda.githubstate.type=string' ,
266- 'uda.githubtitle.label=Github Title' ,
267- 'uda.githubtitle.type=string' ,
268- 'uda.githubtype.label=Github Type' ,
269- 'uda.githubtype.type=string' ,
270- 'uda.githubupdatedat.label=Github Updated' ,
271- 'uda.githubupdatedat.type=date' ,
272- 'uda.githuburl.label=Github URL' ,
273- 'uda.githuburl.type=string' ,
274- 'uda.githubuser.label=Github User' ,
275- 'uda.githubuser.type=string' ,
276- ],
277- )
230+ assert udas == [
231+ 'uda.githubbody.label=Github Body' ,
232+ 'uda.githubbody.type=string' ,
233+ 'uda.githubclosedon.label=GitHub Closed' ,
234+ 'uda.githubclosedon.type=date' ,
235+ 'uda.githubcreatedon.label=Github Created' ,
236+ 'uda.githubcreatedon.type=date' ,
237+ 'uda.githubdraft.label=GitHub Draft' ,
238+ 'uda.githubdraft.type=numeric' ,
239+ 'uda.githubmilestone.label=Github Milestone' ,
240+ 'uda.githubmilestone.type=string' ,
241+ 'uda.githubnamespace.label=Github Namespace' ,
242+ 'uda.githubnamespace.type=string' ,
243+ 'uda.githubnumber.label=Github Issue/PR #' ,
244+ 'uda.githubnumber.type=numeric' ,
245+ 'uda.githubrepo.label=Github Repo Slug' ,
246+ 'uda.githubrepo.type=string' ,
247+ 'uda.githubstate.label=GitHub State' ,
248+ 'uda.githubstate.type=string' ,
249+ 'uda.githubtitle.label=Github Title' ,
250+ 'uda.githubtitle.type=string' ,
251+ 'uda.githubtype.label=Github Type' ,
252+ 'uda.githubtype.type=string' ,
253+ 'uda.githubupdatedat.label=Github Updated' ,
254+ 'uda.githubupdatedat.type=date' ,
255+ 'uda.githuburl.label=Github URL' ,
256+ 'uda.githuburl.type=string' ,
257+ 'uda.githubuser.label=Github User' ,
258+ 'uda.githubuser.type=string' ,
259+ ]
0 commit comments