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

Commit

Permalink
Merge pull request #12 from waveaccounting/DLF-371-duplicate-sixpack-…
Browse files Browse the repository at this point in the history
…entries

[JIRA](DLF-371) Fix the duplicate SixpackParticipant records
  • Loading branch information
squishybear committed Jun 11, 2015
2 parents 1799c1c + 529a137 commit 181a438
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion djsixpack/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.2-wave"
__version__ = "1.1.3-wave"
6 changes: 6 additions & 0 deletions djsixpack/djsixpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def get_participant_bucket(self):
participant = SixpackParticipant.objects.get(unique_attr=self.client_id, experiment_name=self._get_experiment_name())
except SixpackParticipant.DoesNotExist:
return None
except SixpackParticipant.MultipleObjectsReturned:
# clean up duplicate entries
duplicates = SixpackParticipant.objects.filter(unique_attr=self.client_id, experiment_name=self._get_experiment_name())
for dup in duplicates[1:]:
dup.delete()
return duplicates[0].bucket
return participant.bucket

def participate(self, force=None, user_agent=None, ip_address=None, prefetch=False, bucket=None):
Expand Down
46 changes: 45 additions & 1 deletion djsixpack/tests/class_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.test import TestCase

from djsixpack.djsixpack import SixpackTest
from djsixpack.models import SixpackParticipant
from djsixpack.tests.factories import SixpackParticipantFactory


class ClientIdTest(TestCase):
Expand Down Expand Up @@ -158,7 +160,7 @@ class DefaultTest(SixpackTest):
)
self.assertEqual(
sp_mock.Session.return_value.participate.call_args_list,
[call('default', ('FIRST', 'SECOND'), force=None, prefetch=False)]
[call('default', ('FIRST', 'SECOND'), force=None, bucket=None, prefetch=False)]
)

class ConvertTest(TestCase):
Expand Down Expand Up @@ -217,3 +219,45 @@ class AltTest(SixpackTest):

self.assertTrue(hasattr(AltTest, 'FIRST'))
self.assertTrue(hasattr(AltTest, 'SECOND'))


class GetParticipantBucketTest(TestCase):

def test_get_bucket__single_record(self):
mock_user = Mock(pk=10)

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

expt = GetBucketTest(mock_user)
expt.local = True
expt.participate(bucket='FIRST')

self.assertEquals(expt.get_participant_bucket(), 'FIRST')

def test_get_bucket__zero_records(self):
mock_user = Mock(pk=10)

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

expt = GetBucketTest(mock_user)
self.assertIsNone(expt.get_participant_bucket())

def test_get_bucket__multiple_records(self):
mock_user = Mock(pk=10)

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

expt = GetBucketTest(mock_user)
expt.local = True
expt.participate(bucket='FIRST')

# Insert an extra record to mess things up.
SixpackParticipantFactory(experiment_name='get_bucket', unique_attr=10, bucket='SECOND')

self.assertEquals(expt.get_participant_bucket(), 'FIRST')

record_count = SixpackParticipant.objects.filter(experiment_name='get_bucket', unique_attr=10).count()
self.assertEquals(record_count, 1)
12 changes: 12 additions & 0 deletions djsixpack/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from factory import Factory

from djsixpack.models import SixpackParticipant


class SixpackParticipantFactory(Factory):

FACTORY_FOR = SixpackParticipant

experiment_name = 'Science Experiment'
unique_attr = 1
bucket = 'Bucket A'
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
factory-boy==1.1.3

0 comments on commit 181a438

Please sign in to comment.