class EmbedModelLayoutTestCase(TemplateCompareTestMixin, TestCase):
    fixtures = ['embed_backends']

    def setUp(self):
        # Remove everything but the default Backend
        Backend.objects.exclude(name="default").delete()

        self.embed = Embed(
            url="http://www.testme.com",
            backend=Backend.objects.get(name='default'))
        self.tpl_name = "tpl"
        self.type_name = EmbedType()._meta.object_name.lower()
        self.type_slug = "photo"

    def test_no_backend_uses_fallback_template(self):
        e = Embed(url="http://www.testme.com")
        self.assertFalse(hasattr(e, 'backend'))

        expected = ['%(base)s/%(app)s/%(model)s/%(tpl)s.html']
        self.compare_templates(e, expected, use_fallback=True)

    def test_valid_response_without_a_type(self):
        self.embed.update_response()

        self.assertTrue(self.embed.response.is_valid())
        self.assertIsNone(self.embed.type)

        expected = ['%(base)s/%(app)s/%(model)s/%(tpl)s.html']
        self.compare_templates(self.embed, expected)

    def test_invalid_response_without_a_type_uses_fallback(self):
        response = self.embed.get_response()
        response.is_valid = lambda: False
        self.embed.response = response

        self.assertFalse(self.embed.response.is_valid())
        self.assertIsNone(self.embed.type)

        expected = ['%(base)s/%(app)s/%(model)s/%(tpl)s.html']
        self.compare_templates(self.embed, expected, use_fallback=True)

    def test_invalid_response_with_a_type_uses_fallback(self):
        response = self.embed.get_response()
        response.is_valid = lambda: False
        self.embed.response = response
        self.embed.type = EmbedType(slug=self.type_slug)

        self.assertFalse(self.embed.response.is_valid())

        expected = ['%(base)s/%(app)s/%(model)s/%(tpl)s.html']
        self.compare_templates(self.embed, expected, use_fallback=True, use_type=True)

    def test_valid_response_with_a_type(self):
        self.embed.update_response()
        self.embed.type = EmbedType(slug=self.type_slug)

        self.assertTrue(self.embed.response.is_valid())

        expected = [
            '%(base)s/%(app)s/%(typemodel)s/%(type)s/%(tpl)s.html',
            '%(base)s/%(app)s/%(model)s/%(tpl)s.html']
        self.compare_templates(self.embed, expected, use_type=True)
 def test_get_response_returns_correct_response(self):
     e = Embed(url=self.url, backend=self.backend)
     response = e.get_response()
     self.assertTrue(isinstance(response, self.response_cls))
     self.assertEqual(response._data['url'], self.url)
 def test_get_response_returns_same_as_direct_call(self):
     e = Embed(url=self.url, backend=self.backend)
     self.assertEqual(e.get_response(), self.backend.call(self.url))
 def test_get_response_without_url_returns_none(self):
     e = Embed(backend=self.backend)
     self.assertIsNone(e.get_response())