Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmaurer committed Apr 29, 2015
1 parent a9900a1 commit f9fe83c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
15 changes: 11 additions & 4 deletions djsixpack/djsixpack.py
Expand Up @@ -30,14 +30,14 @@ class SixpackTest(object):
control = None
alternatives = None
local = False
sixpack = True
server = True

def __init__(self, instance, local=False, sixpack=True):
def __init__(self, instance, local=False, server=True):
self._instance = instance
self.host = self.host or getattr(settings, 'SIXPACK_HOST', sixpack.SIXPACK_HOST)
self.timeout = self.timeout or getattr(settings, 'SIXPACK_TIMEOUT', sixpack.SIXPACK_TIMEOUT)
self.local = local
self.local = sixpack
self.server = server

@property
def client_id(self):
Expand Down Expand Up @@ -68,6 +68,13 @@ def get_client_id(self, instance):

return client_id

def get_participant_bucket(self):
try:
participant = SixpackParticipant.objects.get(unique_attr=self.client_id, experiment_name=self._get_experiment_name())
except SixpackParticipant.DoesNotExist:
return None
return participant.bucket

def participate(self, force=None, user_agent=None, ip_address=None, prefetch=False):
if not self.host:
try:
Expand All @@ -81,7 +88,7 @@ def participate(self, force=None, user_agent=None, ip_address=None, prefetch=Fal
experiment_name = self._get_experiment_name()
chosen_alternative = None

if self.local and not self.sixpack:
if self.local and not self.server:
prefetch = True

try:
Expand Down
43 changes: 24 additions & 19 deletions djsixpack/tests/class_tests.py
Expand Up @@ -86,39 +86,44 @@ class DefaultTest(SixpackTest):
self.assertEqual(alternative, 'FIRST')
self.assertFalse(sp_mock.participate.called)

def test_participate_returns_and_saves_local(self):
@patch('djsixpack.djsixpack.SixpackParticipant.objects.get_or_create')
def test_participate_returns_and_saves_local(self, mock_get_or_create):
mock_user = Mock(pk=10)

class DefaultTest(SixpackTest):
alternatives = ('FIRST', 'SECOND')

with patch('djsixpack.djsixpack.SixpackParticipant') as sixpack_local:
with self.settings(SIXPACK_HOST=None):
expt = DefaultTest(mock_user, local=True)
session = expt._get_session()
with patch(session.participate) as session_mock:
alternative = expt.participate(force='SECOND')
session_mock.assert_called_with(expt._get_experiment_name, expt.alternatives, force='SECOND', prefetch=False)
expt = DefaultTest(mock_user, local=True)
alternative = expt.participate(force='SECOND')

self.assertEqual(alternative, 'SECOND')
self.assertTrue(sixpack_local.get_or_create.called)
self.assertTrue(mock_get_or_create.objects.get_or_create.called)

def test_participate_returns_and_saves_local_no_sixpack(self):
@patch('djsixpack.djsixpack.SixpackTest._get_session')
@patch('djsixpack.djsixpack.SixpackParticipant.objects.get_or_create')
def test_participate_returns_and_saves_local_no_sixpack(self, mock_get_or_create, mock_get_session):
mock_user = Mock(pk=10)

class DefaultTest(SixpackTest):
alternatives = ('FIRST', 'SECOND')

with patch('djsixpack.SixpackParticipant') as sixpack_local:
with self.settings(SIXPACK_HOST=None):
expt = DefaultTest(mock_user, local=True, sixpack=False)
session = expt._get_session()
with patch(session.participate) as session_mock:
alternative = expt.participate(force='SECOND')
session_mock.assert_called_with(expt._get_experiment_name, expt.alternatives, force='SECOND', prefetch=True)
class MockSession(object):
def participate(self, experiment_name, alternatives, force, prefetch):
return {
'alternative': {'name': 'SECOND'}
}

self.assertEqual(alternative, 'SECOND')
self.assertTrue(sixpack_local.get_or_create.called)
mock_session = MockSession()
mock_get_session.return_value = mock_session

with patch.object(MockSession, 'participate') as mock_participate:
expt = DefaultTest(mock_user, local=True, server=False)
self.assertEqual(mock_participate.call_args[0][0], expt._get_experiment_name())
self.assertEqual(mock_participate.call_args[0][1], expt.alternatives)
self.assertEqual(mock_participate.call_args[1]['force'], 'SECOND')
self.assertEqual(mock_participate.call_args[1]['prefetch'], True)

This comment has been minimized.

Copy link
@squishybear-zz

squishybear-zz Apr 29, 2015

self.assertTrue(mock_participate.call_args[1]['prefetch'])


self.assertFalse(mock_get_or_create.objects.get_or_create.called)

def test_participate_calls_library(self):
mock_user = Mock(pk=10)
Expand Down

0 comments on commit f9fe83c

Please sign in to comment.