Skip to content

Commit

Permalink
Merge pull request #8 from jsocol/master
Browse files Browse the repository at this point in the history
updates from source
  • Loading branch information
hugobessa committed May 8, 2018
2 parents 1b03c87 + 486bd04 commit 94abb48
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ dist
.pydevproject
.settings
.tox/
.DS_Store
docs/_*

# Visual Studio Code config files
.vscode
3 changes: 3 additions & 0 deletions waffle/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
class BaseManager(models.Manager):
KEY_SETTING = ''

def get_by_natural_key(self, name):
return self.get(name=name)

def create(self, *args, **kwargs):
ret = super(BaseManager, self).create(*args, **kwargs)
cache_key = get_setting(self.KEY_SETTING)
Expand Down
3 changes: 3 additions & 0 deletions waffle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Meta(object):
def __str__(self):
return self.name

def natural_key(self):
return (self.name,)

@classmethod
def _cache_key(cls, name):
return keyfmt(get_setting(cls.SINGLE_CACHE_KEY), name)
Expand Down
20 changes: 20 additions & 0 deletions waffle/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from __future__ import unicode_literals

from django.test import TestCase

from waffle.models import Flag, Switch, Sample


class ModelsTests(TestCase):
def test_natural_keys(self):
flag = Flag.objects.create(name='test-flag')
switch = Switch.objects.create(name='test-switch')
sample = Sample.objects.create(name='test-sample', percent=0)

self.assertEqual(flag.natural_key(), ('test-flag',))
self.assertEqual(switch.natural_key(), ('test-switch',))
self.assertEqual(sample.natural_key(), ('test-sample',))

self.assertEqual(Flag.objects.get_by_natural_key('test-flag'), flag)
self.assertEqual(Switch.objects.get_by_natural_key('test-switch'), switch)
self.assertEqual(Sample.objects.get_by_natural_key('test-sample'), sample)

0 comments on commit 94abb48

Please sign in to comment.