| 1 |
from django.core.cache import caches |
| 2 |
from django.core.files.uploadedfile import SimpleUploadedFile |
| 3 |
from django.template import Context, Template, TemplateSyntaxError |
| 4 |
from django.test import TestCase |
| 5 |
from django.test.utils import override_settings |
| 6 |
|
| •7 |
from django_solo.tests.models import SiteConfiguration, SiteConfigurationWithExplicitlyGivenId |
| 8 |
from django_solo.tests.testapp2.models import SiteConfiguration as SiteConfiguration2 |
| 9 |
|
| 10 |
|
| 11 |
class SingletonTest(TestCase): |
| 12 |
def setUp(self): |
| 13 |
self.template = Template( |
| 14 |
"{% load solo_tags %}" |
| 15 |
'{% get_solo "tests.SiteConfiguration" as site_config %}' |
| 16 |
"{{ site_config.site_name }}" |
| 17 |
"{{ site_config.file.url }}" |
| 18 |
) |
| 19 |
self.template_invalid_app = Template( |
| 20 |
"{% load solo_tags %}" |
| 21 |
'{% get_solo "invalid_app.SiteConfiguration" as site_config %}' |
| 22 |
"{{ site_config.site_name }}" |
| 23 |
"{{ site_config.file.url }}" |
| 24 |
) |
| 25 |
self.template_invalid_model = Template( |
| 26 |
"{% load solo_tags %}" |
| 27 |
'{% get_solo "tests.InvalidModel" as site_config %}' |
| 28 |
"{{ site_config.site_name }}" |
| 29 |
"{{ site_config.file.url }}" |
| 30 |
) |
| 31 |
self.cache = caches["default"] |
| 32 |
self.cache_key = SiteConfiguration.get_cache_key() |
| 33 |
self.cache.clear() |
| 34 |
SiteConfiguration.objects.all().delete() |
| 35 |
|
| 36 |
def test_template_tag_renders_default_site_config(self): |
| 37 |
SiteConfiguration.objects.all().delete() |
| 38 |
# At this point, there is no configuration object and we expect a |
| 39 |
# one to be created automatically with the default name value as |
| 40 |
# defined in models. |
| 41 |
output = self.template.render(Context()) |
| 42 |
self.assertIn("Default Config", output) |
| 43 |
|
| 44 |
def test_template_tag_renders_site_config(self): |
| 45 |
SiteConfiguration.objects.create(site_name="Test Config") |
| 46 |
output = self.template.render(Context()) |
| 47 |
self.assertIn("Test Config", output) |
| 48 |
|
| 49 |
@override_settings(SOLO_CACHE="default") |
| 50 |
def test_template_tag_uses_cache_if_enabled(self): |
| 51 |
SiteConfiguration.objects.create(site_name="Config In Database") |
| 52 |
fake_configuration = {"site_name": "Config In Cache"} |
| 53 |
self.cache.set(self.cache_key, fake_configuration, 10) |
| 54 |
output = self.template.render(Context()) |
| 55 |
self.assertNotIn("Config In Database", output) |
| 56 |
self.assertNotIn("Default Config", output) |
| 57 |
self.assertIn("Config In Cache", output) |
| 58 |
|
| 59 |
@override_settings(SOLO_CACHE=None) |
| 60 |
def test_template_tag_uses_database_if_cache_disabled(self): |
| 61 |
SiteConfiguration.objects.create(site_name="Config In Database") |
| 62 |
fake_configuration = {"site_name": "Config In Cache"} |
| 63 |
self.cache.set(self.cache_key, fake_configuration, 10) |
| 64 |
output = self.template.render(Context()) |
| 65 |
self.assertNotIn("Config In Cache", output) |
| 66 |
self.assertNotIn("Default Config", output) |
| 67 |
self.assertIn("Config In Database", output) |
| 68 |
|
| 69 |
@override_settings(SOLO_CACHE="default") |
| 70 |
def test_delete_if_cache_enabled(self): |
| 71 |
self.assertEqual(SiteConfiguration.objects.count(), 0) |
| 72 |
self.assertIsNone(self.cache.get(self.cache_key)) |
| 73 |
|
| 74 |
one_cfg = SiteConfiguration.get_solo() |
| 75 |
one_cfg.site_name = "TEST SITE PLEASE IGNORE" |
| 76 |
one_cfg.save() |
| 77 |
self.assertEqual(SiteConfiguration.objects.count(), 1) |
| 78 |
self.assertIsNotNone(self.cache.get(self.cache_key)) |
| 79 |
|
| 80 |
one_cfg.delete() |
| 81 |
self.assertEqual(SiteConfiguration.objects.count(), 0) |
| 82 |
self.assertIsNone(self.cache.get(self.cache_key)) |
| 83 |
self.assertEqual(SiteConfiguration.get_solo().site_name, "Default Config") |
| 84 |
|
| 85 |
@override_settings(SOLO_CACHE=None) |
| 86 |
def test_delete_if_cache_disabled(self): |
| 87 |
# As above, but without the cache checks |
| 88 |
self.assertEqual(SiteConfiguration.objects.count(), 0) |
| 89 |
one_cfg = SiteConfiguration.get_solo() |
| 90 |
one_cfg.site_name = "TEST (uncached) SITE PLEASE IGNORE" |
| 91 |
one_cfg.save() |
| 92 |
self.assertEqual(SiteConfiguration.objects.count(), 1) |
| 93 |
one_cfg.delete() |
| 94 |
self.assertEqual(SiteConfiguration.objects.count(), 0) |
| 95 |
self.assertEqual(SiteConfiguration.get_solo().site_name, "Default Config") |
| 96 |
|
| 97 |
@override_settings(SOLO_CACHE="default") |
| 98 |
def test_file_upload_if_cache_enabled(self): |
| 99 |
cfg = SiteConfiguration.objects.create( |
| 100 |
site_name="Test Config", file=SimpleUploadedFile("file.pdf", None) |
| 101 |
) |
| 102 |
output = self.template.render(Context()) |
| 103 |
self.assertIn(cfg.file.url, output) |
| 104 |
|
| 105 |
@override_settings(SOLO_CACHE_PREFIX="other") |
| 106 |
def test_cache_prefix_overriding(self): |
| 107 |
key = SiteConfiguration.get_cache_key() |
| 108 |
prefix = key.partition(":")[0] |
| 109 |
self.assertEqual(prefix, "other") |
| 110 |
|
| 111 |
def test_template_tag_invalid_app_name(self): |
| 112 |
with self.assertRaises(TemplateSyntaxError): |
| 113 |
self.template_invalid_app.render(Context()) |
| 114 |
|
| 115 |
def test_template_invalid_model_name(self): |
| 116 |
with self.assertRaises(TemplateSyntaxError): |
| 117 |
self.template_invalid_model.render(Context()) |
| 118 |
|
| 119 |
|
| 120 |
class SingletonWithExplicitIdTest(TestCase): |
| 121 |
def setUp(self): |
| 122 |
SiteConfigurationWithExplicitlyGivenId.objects.all().delete() |
| 123 |
|
| 124 |
def test_when_singleton_instance_id_is_given_created_item_will_have_given_instance_id(self): |
| 125 |
item = SiteConfigurationWithExplicitlyGivenId.get_solo() |
| 126 |
self.assertEqual(item.pk, SiteConfigurationWithExplicitlyGivenId.singleton_instance_id) |
| 127 |
|
| 128 |
|
| 129 |
class SingletonsWithAmbiguousNameTest(TestCase): |
| 130 |
def test_cache_key_is_not_ambiguous(self): |
| 131 |
assert SiteConfiguration.get_cache_key() != SiteConfiguration2.get_cache_key() |
| 132 |
|
| 133 |
def test_get_solo_returns_the_correct_singleton(self): |
| 134 |
assert SiteConfiguration.get_solo() != SiteConfiguration2.get_solo() |