from django.urls import reverse
from rest_framework import status

from dcim.models import Site
from utilities.testing import APITestCase


class TaggedItemTest(APITestCase):
    """
    Test the application of Tags to and item (a Site, for example) upon creation (POST) and modification (PATCH).
    """

    def setUp(self):

        super().setUp()

    def test_create_tagged_item(self):
        data = {
            'name': 'Test Site',
            'slug': 'test-site',
            'tags': ['Foo', 'Bar', 'Baz']
        }
        url = reverse('dcim-api:site-list')
        self.add_permissions('dcim.add_site')

        response = self.client.post(url, data, format='json', **self.header)
        self.assertHttpStatus(response, status.HTTP_201_CREATED)
        self.assertEqual(sorted(response.data['tags']), sorted(data['tags']))
        site = Site.objects.get(pk=response.data['id'])
        tags = [tag.name for tag in site.tags.all()]
        self.assertEqual(sorted(tags), sorted(data['tags']))

    def test_update_tagged_item(self):
        site = Site.objects.create(
            name='Test Site',
            slug='test-site'
        )
        site.tags.add('Foo', 'Bar', 'Baz')
        data = {
            'tags': ['Foo', 'Bar', 'New Tag']
        }
        self.add_permissions('dcim.change_site')
        url = reverse('dcim-api:site-detail', kwargs={'pk': site.pk})

        response = self.client.patch(url, data, format='json', **self.header)
        self.assertHttpStatus(response, status.HTTP_200_OK)
        self.assertEqual(sorted(response.data['tags']), sorted(data['tags']))
        site = Site.objects.get(pk=response.data['id'])
        tags = [tag.name for tag in site.tags.all()]
        self.assertEqual(sorted(tags), sorted(data['tags']))

@@ -298,25 +298,24 @@
     %22%22%22%0A
-%0A
     def 
 setUp(se
@@ -310,21 +310,39 @@
 def 
-setUp
+test_create_tagged_item
 (self):%0A
 %0A   
@@ -337,25 +337,24 @@
 (self):%0A
-%0A
         
 super().
@@ -349,63 +349,52 @@
     
-super().setUp()%0A%0A    def test_create_tagged_item(self):
+tags = self.create_tags(%22Foo%22, %22Bar%22, %22Baz%22)
 %0A   
@@ -494,35 +494,34 @@
 tags': %5B
-'Foo', 'Bar', 'Baz'
+t.pk for t in tags
 %5D%0A      
@@ -772,37 +772,72 @@
  self.assert
+List
 Equal(
-sorted(
+%0A            sorted(%5Bt%5B'id'%5D for t in 
 response.dat
@@ -837,34 +837,47 @@
 nse.data%5B'tags'%5D
+%5D
 ),
+%0A           
  sorted(data%5B'ta
@@ -873,32 +873,41 @@
 ed(data%5B'tags'%5D)
+%0A        
 )%0A        site =
@@ -952,35 +952,67 @@
 '%5D)%0A        
-tags = %5Btag
+self.assertListEqual(%0A            sorted(%5Bt
 .name for ta
@@ -1002,34 +1002,32 @@
 ed(%5Bt.name for t
-ag
  in site.tags.al
@@ -1022,32 +1022,34 @@
 site.tags.all()%5D
+),
 %0A        self.as
@@ -1045,59 +1045,50 @@
     
-self.assertEqual(sorted(tags), sorted(data%5B'tags'%5D)
+    sorted(%5B%22Foo%22, %22Bar%22, %22Baz%22%5D)%0A        
 )%0A%0A 
@@ -1256,27 +1256,63 @@
 add(
-'Foo', 'Bar', 'Baz'
+%22Foo%22, %22Bar%22, %22Baz%22)%0A        self.create_tags(%22New Tag%22
 )%0A  
@@ -1351,31 +1351,124 @@
 ': %5B
-'Foo', 'Bar', 'New Tag'
+%0A                %7B%22name%22: %22Foo%22%7D,%0A                %7B%22name%22: %22Bar%22%7D,%0A                %7B%22name%22: %22New Tag%22%7D,%0A            
 %5D%0A  
@@ -1752,29 +1752,66 @@
 f.assert
+List
 Equal(
-sorted(
+%0A            sorted(%5Bt%5B'name'%5D for t in 
 response
@@ -1823,26 +1823,59 @@
 %5B'tags'%5D
+%5D
 ),
- sorted(
+%0A            sorted(%5Bt%5B'name'%5D for t in 
 data%5B'ta
@@ -1878,17 +1878,27 @@
 %5B'tags'%5D
-)
+%5D)%0A        
 )%0A      
@@ -1959,19 +1959,51 @@
     
-tags = %5Btag
+self.assertListEqual(%0A            sorted(%5Bt
 .nam
@@ -2009,18 +2009,16 @@
 me for t
-ag
  in site
@@ -2029,25 +2029,30 @@
 s.all()%5D
-%0A
+),%0A   
         
 self.ass
@@ -2047,57 +2047,49 @@
     
-self.assertEqual(sorted(tags), sorted(data%5B'tags'%5D)
+ sorted(%5B%22Foo%22, %22Bar%22, %22New Tag%22%5D)%0A        
 )%0A

Update TaggedItemTest