-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add `utils` to encode and decode `hashids` - Add tests for `utils`
- Loading branch information
Showing
7 changed files
with
187 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.apps import AppConfig | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
from dynamic_rest.conf import settings | ||
|
||
|
||
class DynamicRestConfig(AppConfig): | ||
name = "dynamic_rest" | ||
verbose_name = "Django Dynamic Rest" | ||
|
||
def ready(self): | ||
|
||
if hasattr(settings, "ENABLE_HASHID_FIELDS") and settings.ENABLE_HASHID_FIELDS: | ||
if not hasattr(settings, "HASHIDS_SALT") or settings.HASHIDS_SALT is None: | ||
raise ImproperlyConfigured( | ||
"You have set ENABLE_HASHID_FIELDS to True in your dynamic_rest setting. " | ||
"Then, you must set a HASHIDS_SALT string as well." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ pytest==3.7.1 | |
Sphinx==1.7.5 | ||
tox-pyenv==1.1.0 | ||
tox==3.14.6 | ||
six==1.16.0 | ||
six==1.16.0 | ||
hashids==1.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.test import TestCase, override_settings | ||
|
||
from dynamic_rest.utils import ( | ||
is_truthy, | ||
unpack, | ||
external_id_from_model_and_internal_id, | ||
internal_id_from_model_and_external_id, | ||
) | ||
from tests.models import User | ||
|
||
|
||
class UtilsTestCase(TestCase): | ||
def setUp(self): | ||
User.objects.create(name="Marie") | ||
User.objects.create(name="Rosalind") | ||
|
||
def test_is_truthy(self): | ||
self.assertTrue(is_truthy("faux")) | ||
self.assertTrue(is_truthy(1)) | ||
self.assertFalse(is_truthy("0")) | ||
self.assertFalse(is_truthy("False")) | ||
self.assertFalse(is_truthy("false")) | ||
self.assertFalse(is_truthy("")) | ||
|
||
def test_unpack_empty_value(self): | ||
self.assertIsNone(unpack(None)) | ||
|
||
def test_unpack_non_empty_value(self): | ||
content = {"hello": "world", "meta": "worldpeace", "missed": "a 't'"} | ||
self.assertIsNotNone(unpack(content)) | ||
|
||
def test_unpack_meta_first_key(self): | ||
content = {"meta": "worldpeace", "missed": "a 't'"} | ||
self.assertEqual(unpack(content), "a 't'") | ||
|
||
def test_unpack_meta_not_first_key(self): | ||
content = {"hello": "world", "meta": "worldpeace", "missed": "a 't'"} | ||
self.assertEqual(unpack(content), "world") | ||
|
||
@override_settings( | ||
ENABLE_HASHID_FIELDS=True, | ||
HASHIDS_SALT="If my calculations are correct, when this vaby hits 88 miles per hour, you're gonna see some serious s***.", | ||
) | ||
def test_internal_id_from_model_and_external_id_model_object_does_not_exits(self): | ||
self.assertRaises( | ||
User.DoesNotExist, | ||
internal_id_from_model_and_external_id, | ||
model=User, | ||
external_id="skdkahh", | ||
) |