from django.test import TestCase
from mock import patch

from feedhq.feeds.models import Favicon, Feed

from .factories import FeedFactory
from . import responses


class FaviconTests(TestCase):
    @patch("requests.get")
    def test_existing_favicon_new_feed(self, get):
        get.return_value = responses(304)
        FeedFactory.create(url='http://example.com/feed')
        self.assertEqual(Feed.objects.values_list('favicon', flat=True)[0], '')

        # Simulate a 1st call of update_favicon which creates a Favicon entry
        Favicon.objects.create(url='http://example.com/feed',
                               favicon='favicons/example.com.ico')

        Favicon.objects.update_favicon('http://example.com/feed')
        self.assertEqual(Feed.objects.values_list('favicon', flat=True)[0],
                         'favicons/example.com.ico')

from mock import patch

from feedhq.feeds.models import Favicon, Feed

from .factories import FeedFactory
from . import responses, TestCase


class FaviconTests(TestCase):
    @patch("requests.get")
    def test_existing_favicon_new_feed(self, get):
        get.return_value = responses(304)
        FeedFactory.create(url='http://example.com/feed')
        self.assertEqual(Feed.objects.values_list('favicon', flat=True)[0], '')

        # Simulate a 1st call of update_favicon which creates a Favicon entry
        Favicon.objects.create(url='http://example.com/feed',
                               favicon='favicons/example.com.ico')

        Favicon.objects.update_favicon('http://example.com/feed')
        self.assertEqual(Feed.objects.values_list('favicon', flat=True)[0],
                         'favicons/example.com.ico')

Use base TestCase to properly cleanup indices
Use base TestCase to properly cleanup indices

- from django.test import TestCase
  from mock import patch
  
  from feedhq.feeds.models import Favicon, Feed
  
  from .factories import FeedFactory
- from . import responses
+ from . import responses, TestCase
  
  
  class FaviconTests(TestCase):
      @patch("requests.get")
      def test_existing_favicon_new_feed(self, get):
          get.return_value = responses(304)
          FeedFactory.create(url='http://example.com/feed')
          self.assertEqual(Feed.objects.values_list('favicon', flat=True)[0], '')
  
          # Simulate a 1st call of update_favicon which creates a Favicon entry
          Favicon.objects.create(url='http://example.com/feed',
                                 favicon='favicons/example.com.ico')
  
          Favicon.objects.update_favicon('http://example.com/feed')
          self.assertEqual(Feed.objects.values_list('favicon', flat=True)[0],
                           'favicons/example.com.ico')
  
Use base TestCase to properly cleanup indices
## Code Before:
from django.test import TestCase
from mock import patch

from feedhq.feeds.models import Favicon, Feed

from .factories import FeedFactory
from . import responses


class FaviconTests(TestCase):
    @patch("requests.get")
    def test_existing_favicon_new_feed(self, get):
        get.return_value = responses(304)
        FeedFactory.create(url='http://example.com/feed')
        self.assertEqual(Feed.objects.values_list('favicon', flat=True)[0], '')

        # Simulate a 1st call of update_favicon which creates a Favicon entry
        Favicon.objects.create(url='http://example.com/feed',
                               favicon='favicons/example.com.ico')

        Favicon.objects.update_favicon('http://example.com/feed')
        self.assertEqual(Feed.objects.values_list('favicon', flat=True)[0],
                         'favicons/example.com.ico')

## Instruction:
Use base TestCase to properly cleanup indices
## Code After:
from mock import patch

from feedhq.feeds.models import Favicon, Feed

from .factories import FeedFactory
from . import responses, TestCase


class FaviconTests(TestCase):
    @patch("requests.get")
    def test_existing_favicon_new_feed(self, get):
        get.return_value = responses(304)
        FeedFactory.create(url='http://example.com/feed')
        self.assertEqual(Feed.objects.values_list('favicon', flat=True)[0], '')

        # Simulate a 1st call of update_favicon which creates a Favicon entry
        Favicon.objects.create(url='http://example.com/feed',
                               favicon='favicons/example.com.ico')

        Favicon.objects.update_favicon('http://example.com/feed')
        self.assertEqual(Feed.objects.values_list('favicon', flat=True)[0],
                         'favicons/example.com.ico')
