def test_missing_value(self): """ Checks that nothing breaks when no value could be found. The value should be None, the output blank (if that is appropriate for the field). """ path = "/abc/" self.assertEqual(seo_get_metadata(path, name="WithSites").title.value, None) self.assertEqual(six.text_type(seo_get_metadata(path, name="WithSites").title), "")
def test_i18n(self): """ Tests the i18n support, allowing a language to be associated with metadata entries. """ path = "/abc/" language = 'de' path_metadata = WithI18n._meta.get_model('path').objects.create(_language='de', title="German Path title", _path=path) self.assertEqual(seo_get_metadata(path, name="WithI18n", language="de").title.value, 'German Path title') # Metadata with an explicitly wrong site should not work path_metadata._language = "en" path_metadata.save() self.assertEqual(seo_get_metadata(path, name="WithI18n", language="de").title.value, None)
def test_sites(self): """ Tests the django.contrib.sites support. A separate metadata definition is used, WithSites, which has turned on sites support. """ path = "/abc/" site = Site.objects.get_current() path_metadata = WithSites._meta.get_model('path').objects.create(_site=site, title="Site Path title", _path=path) self.assertEqual(seo_get_metadata(path, name="WithSites").title.value, 'Site Path title') # Metadata with site=null should work path_metadata._site_id = None path_metadata.save() self.assertEqual(seo_get_metadata(path, name="WithSites").title.value, 'Site Path title') # Metadata with an explicitly wrong site should not work path_metadata._site_id = site.id + 1 path_metadata.save() self.assertEqual(seo_get_metadata(path, name="WithSites").title.value, None)
def test_use_cache_i18n(self): """ Checks that the cache plays nicely with i18n. """ if 'dummy' not in settings.CACHE_BACKEND: path = '/' hexpath = hashlib.md5(iri_to_uri(path)).hexdigest() #six.text_type(seo_get_metadata(path, name="Coverage")) six.text_type(seo_get_metadata(path, name="WithCacheI18n", language='de')) self.assertEqual(cache.get('djangoseo.Coverage.%s.de.title' % hexpath), None) self.assertEqual(cache.get('djangoseo.WithCacheI18n.%s.en.title' % hexpath), None) self.assertEqual(cache.get('djangoseo.WithCacheI18n.%s.de.title' % hexpath), "1234") self.assertEqual(cache.get('djangoseo.WithCacheI18n.%s.de.subtitle' % hexpath), "")
def test_use_cache_site(self): """ Checks that the cache plays nicely with sites. """ if 'dummy' not in settings.CACHE_BACKEND: path = '/' site = Site.objects.get_current() hexpath = hashlib.md5(iri_to_uri(site.domain+path)).hexdigest() #six.text_type(seo_get_metadata(path, name="Coverage")) six.text_type(seo_get_metadata(path, name="WithCacheSites", site=site)) self.assertEqual(cache.get('djangoseo.Coverage.%s.title' % hexpath), None) self.assertEqual(cache.get('djangoseo.WithCacheSites.%s.title' % hexpath), "1234") self.assertEqual(cache.get('djangoseo.WithCacheSites.%s.subtitle' % hexpath), "")
def test_use_cache(self): """ Checks that cache is being used when use_cache is set. Will only work if cache backend is not dummy. """ if 'dummy' not in settings.CACHE_BACKEND: path = '/' hexpath = hashlib.md5(iri_to_uri(path)).hexdigest() #six.text_type(seo_get_metadata(path, name="Coverage")) six.text_type(seo_get_metadata(path, name="WithCache")) self.assertEqual(cache.get('djangoseo.Coverage.%s.title' % hexpath), None) self.assertEqual(cache.get('djangoseo.WithCache.%s.title' % hexpath), "1234") self.assertEqual(cache.get('djangoseo.WithCache.%s.subtitle' % hexpath), "")
def test_site(self): new_site = Site.objects.create(domain="new-example.com", name="New example") WithSites._meta.get_model('path').objects.create(_path=self.path, title="A Title", _site=new_site) metadata = seo_get_metadata(path=self.path, name="WithSites", site=new_site) self.compilesTo('{% get_metadata WithI18n on "new-example.com" %}', six.text_type(metadata)) self.compilesTo('{% get_metadata WithI18n in "example.com" %}', "")
def test_language(self): WithI18n._meta.get_model('path').objects.create(_path=self.path, title="A Title", _language="de") metadata = seo_get_metadata(path=self.path, name="WithSites", language="de") self.compilesTo('{% get_metadata WithI18n in "de" %}', six.text_type(metadata)) self.compilesTo('{% get_metadata WithI18n in "en" %}', "")
def get_metadata(path): return seo_get_metadata(path, name="Coverage")