11from typing import TYPE_CHECKING
22
3- import pytest
4- from packaging import version
5-
63from posit import connect
74
8- from . import CONNECT_VERSION
9-
105if TYPE_CHECKING :
116 from posit .connect .content import ContentItem
127
138
149# add integration tests here!
15- @pytest .mark .skipif (
16- CONNECT_VERSION < version .parse ("2024.10.0-dev" ),
17- reason = "Packages API unavailable" ,
18- )
1910class TestTags :
2011 @classmethod
2112 def setup_class (cls ):
2213 cls .client = connect .Client ()
23- cls .contentA = cls .client .content .create (name = cls .__name__ )
24- cls .contentB = cls .client .content .create (name = cls .__name__ )
25- cls .contentC = cls .client .content .create (
26- name = cls .__name__ ,
27- )
14+ cls .contentA = cls .client .content .create (name = "Content_A" )
15+ cls .contentB = cls .client .content .create (name = "Content_B" )
16+ cls .contentC = cls .client .content .create (name = "Content_C" )
2817
2918 @classmethod
3019 def teardown_class (cls ):
3120 assert len (cls .client .tags .find ()) == 0
21+ cls .contentA .delete ()
22+ cls .contentB .delete ()
23+ cls .contentC .delete ()
24+ assert len (cls .client .content .find ()) == 0
3225
3326 def test_tags_find (self ):
3427 tags = self .client .tags .find ()
@@ -37,7 +30,7 @@ def test_tags_find(self):
3730 def test_tags_create_destroy (self ):
3831 tagA = self .client .tags .create (name = "tagA" )
3932 tagB = self .client .tags .create (name = "tagB" )
40- tagC = self .client .tags .create (name = "tagC" , parent = self . tagA )
33+ tagC = self .client .tags .create (name = "tagC" , parent = tagA )
4134
4235 assert len (self .client .tags .find ()) == 3
4336
@@ -49,78 +42,133 @@ def test_tags_create_destroy(self):
4942 assert len (self .client .tags .find ()) == 0
5043
5144 def test_tag_descendants (self ):
52- # Have created tags persist
53- self .tagA = self .client .tags .create (name = "tagA" )
54- self .tagB = self .client .tags .create (name = "tagB" )
55- self .tagC = self .client .tags .create (name = "tagC" , parent = self .tagA )
56- self .tagD = self .client .tags .create (name = "tagD" , parent = self .tagC )
45+ tagA = self .client .tags .create (name = "tagA" )
46+ tagB = self .client .tags .create (name = "tagB" )
47+ tagC = self .client .tags .create (name = "tagC" , parent = tagA )
48+ tagD = self .client .tags .create (name = "tagD" , parent = tagC )
5749
58- assert self . tagA .descendant_tags .find () == [self . tagC , self . tagD ]
50+ assert tagA .descendant_tags .find () == [tagC , tagD ]
5951
60- assert len (self .tagB .descendant_tags .find ()) == 0
61- assert len (self .tagC .descendant_tags .find ()) == [self .tagD ]
52+ assert len (tagB .descendant_tags .find ()) == 0
53+ assert tagC .descendant_tags .find () == [tagD ]
54+
55+ # cleanup
56+ tagA .destroy ()
57+ tagB .destroy ()
58+ assert len (self .client .tags .find ()) == 0
6259
6360 def test_tag_children (self ):
64- assert self .tagA .children_tags .find () == [self .tagC ]
65- assert self .tagB .children_tags .find () == []
66- assert self .tagC .children_tags .find () == [self .tagD ]
61+ tagA = self .client .tags .create (name = "tagA_children" )
62+ tagB = self .client .tags .create (name = "tagB_children" )
63+ tagC = self .client .tags .create (name = "tagC_children" , parent = tagA )
64+ tagD = self .client .tags .create (name = "tagD_children" , parent = tagC )
65+
66+ assert tagA .children_tags .find () == [tagC ]
67+ assert tagB .children_tags .find () == []
68+ assert tagC .children_tags .find () == [tagD ]
69+
70+ # cleanup
71+ tagA .destroy ()
72+ tagB .destroy ()
73+ assert len (self .client .tags .find ()) == 0
6774
6875 def test_tag_parent (self ):
69- assert self .tagA .parent_tag is None
70- assert self .tagB .parent_tag is None
71- assert self .tagC .parent_tag == self .tagA
72- assert self .tagD .parent_tag == self .tagC
76+ tagA = self .client .tags .create (name = "tagA_parent" )
77+ tagB = self .client .tags .create (name = "tagB_parent" )
78+ tagC = self .client .tags .create (name = "tagC_parent" , parent = tagA )
79+ tagD = self .client .tags .create (name = "tagD_parent" , parent = tagC )
80+
81+ assert tagA .parent_tag is None
82+ assert tagB .parent_tag is None
83+ assert tagC .parent_tag == tagA
84+ assert tagD .parent_tag == tagC
85+
86+ # cleanup
87+ tagA .destroy ()
88+ tagB .destroy ()
89+ assert len (self .client .tags .find ()) == 0
90+
91+ def test_content_item_tags (self ):
92+ tagRoot = self .client .tags .create (name = "tagRoot_content_item_tags" )
93+ tagA = self .client .tags .create (name = "tagA_content_item_tags" , parent = tagRoot )
94+ tagB = self .client .tags .create (name = "tagB_content_item_tags" , parent = tagRoot )
95+ tagC = self .client .tags .create (name = "tagC_content_item_tags" , parent = tagA )
96+ tagD = self .client .tags .create (name = "tagD_content_item_tags" , parent = tagC )
7397
74- def test_content_a_tags (self ):
7598 assert len (self .contentA .tags .find ()) == 0
7699
77- self .contentA .tags .add (self . tagA )
78- self .contentA .tags .add (self . tagB )
100+ self .contentA .tags .add (tagD )
101+ self .contentA .tags .add (tagB )
79102
80- # tagB, tagC, tagA (parent of tagC)
81- assert len (self .contentA .tags .find ()) == 3
103+ # tagB, tagD, tagC (parent of tagD), tagA (parent of tagC)
104+ # tagD + tagC
105+ # tagRoot is considered a "category" and is "not a tag"
106+ assert len (self .contentA .tags .find ()) == 4
82107
83- self .contentA .tags .delete (self .tagB )
84- assert len (self .contentA .tags .find ()) == 2
108+ # Removes tagB
109+ self .contentA .tags .delete (tagB )
110+ assert len (self .contentA .tags .find ()) == 4 - 1
85111
86- # Removes tagC and tagA (parent of tagC)
87- self .contentA .tags .delete (self . tagA )
88- assert len (self .contentA .tags .find ()) == 0
112+ # Removes tagC and tagD (parent of tagC)
113+ self .contentA .tags .delete (tagC )
114+ assert len (self .contentA .tags .find ()) == 4 - 1 - 2
89115
90- def test_tags_content_items (self ):
91- assert len (self .tagA .content_items .find ()) == 0
92- assert len (self .tagB .content_items .find ()) == 0
93- assert len (self .tagC .content_items .find ()) == 0
116+ # cleanup
117+ tagRoot .destroy ()
118+ assert len (self .client .tags .find ()) == 0
94119
95- self .contentA .tags .add (self .tagA )
96- self .contentA .tags .add (self .tagB )
120+ def test_tag_content_items (self ):
121+ tagRoot = self .client .tags .create (name = "tagRoot_tag_content_items" )
122+ tagA = self .client .tags .create (name = "tagA_tag_content_items" , parent = tagRoot )
123+ tagB = self .client .tags .create (name = "tagB_tag_content_items" , parent = tagRoot )
124+ tagC = self .client .tags .create (name = "tagC_tag_content_items" , parent = tagA )
125+ tagD = self .client .tags .create (name = "tagD_tag_content_items" , parent = tagC )
97126
98- self .contentB .tags .add (self .tagA )
99- self .contentB .tags .add (self .tagC )
127+ assert len (tagA .content_items .find ()) == 0
128+ assert len (tagB .content_items .find ()) == 0
129+ assert len (tagC .content_items .find ()) == 0
130+ assert len (tagD .content_items .find ()) == 0
100131
101- self .contentC .tags .add (self .tagC )
132+ self .contentA .tags .add (tagD )
133+ self .contentA .tags .add (tagB )
102134
103- assert len (self .contentA .tags .find ()) == 2
135+ self .contentB .tags .add (tagA )
136+ self .contentB .tags .add (tagC )
137+
138+ self .contentC .tags .add (tagC )
139+
140+ assert len (self .contentA .tags .find ()) == 4
104141 assert len (self .contentB .tags .find ()) == 2
105- assert len (self .contentC .tags .find ()) == 1
142+ assert len (self .contentC .tags .find ()) == 2
106143
107- assert len (self . tagA .content_items .find ()) == 2
108- assert len (self . tagB .content_items .find ()) == 1
109- assert len (self . tagC .content_items .find ()) == 2
144+ assert len (tagA .content_items .find ()) == 3
145+ assert len (tagB .content_items .find ()) == 1
146+ assert len (tagC .content_items .find ()) == 3
110147
111148 # Make sure unique content items are found
112149 content_items_list : list [ContentItem ] = []
113- for tag in [self . tagA , * self . tagA .descendant_tags .find ()]:
150+ for tag in [tagA , * tagA .descendant_tags .find ()]:
114151 content_items_list .extend (tag .content_items .find ())
115- # Get unique items
116- content_items_list = list (set (content_items_list ))
117-
118- assert content_items_list == [self .contentA , self .contentB , self .contentC ]
119-
120- self .contentA .tags .delete (self .tagA , self .tagB )
121- self .contentB .tags .delete (self .tagA )
122- self .contentC .tags .delete (self .tagC )
123-
124- assert len (self .tagA .content_items .find ()) == 0
125- assert len (self .tagB .content_items .find ()) == 0
126- assert len (self .tagC .content_items .find ()) == 0
152+ # Get unique content_item guids
153+ content_item_guids = set [str ]()
154+ for content_item in content_items_list :
155+ if content_item ["guid" ] not in content_item_guids :
156+ content_item_guids .add (content_item ["guid" ])
157+
158+ assert content_item_guids == {
159+ self .contentA ["guid" ],
160+ self .contentB ["guid" ],
161+ self .contentC ["guid" ],
162+ }
163+
164+ self .contentA .tags .delete (tagRoot )
165+ self .contentB .tags .delete (tagRoot )
166+ self .contentC .tags .delete (tagRoot )
167+
168+ assert len (tagA .content_items .find ()) == 0
169+ assert len (tagB .content_items .find ()) == 0
170+ assert len (tagC .content_items .find ()) == 0
171+
172+ # cleanup
173+ tagRoot .destroy ()
174+ assert len (self .client .tags .find ()) == 0
0 commit comments