def test_200_private_trial_is_invited(self): "Ensure privacy" owner = RMUser(email='*****@*****.**', username='******') owner.save() invitee = RMUser(email='*****@*****.**', username='******') invitee.set_password('pass') invitee.save() trial = models.Trial(owner=owner, private=True, title='Foo', min_participants=1) trial.save() var = models.Variable(question='Why?', trial=trial) var.save() invitation = models.Invitation(email=invitee.email, trial=trial, sent=True) invitation.save() self.assertTrue( self.client.login(username='******', password='******')) with self.settings(BASICAUTH=False): response = self.client.get('/trials/rm/{0}'.format(trial.pk)) self.assertEqual(200, response.status_code)
def test_join_finished_trial(self): "Should raise" owner = models.User(pk=1) user = models.User(pk=2) trial = models.Trial(owner=owner, finish_date=self.yesterday) with self.assertRaises(exceptions.TrialFinishedError): trial.join(user)
def test_join_finished_trial(self): "Should raise" owner = models.User(pk=1) user = models.User(pk=2) trial = models.Trial(owner=owner, stopped=True) with self.assertRaises(exceptions.TrialFinishedError): trial.join(user)
def setUp(self): super(ParticipantTestCase, self).setUp() self.user = models.User(email='*****@*****.**') self.trial = models.Trial(pk=1, title='This', group_a='Do it') self.group = models.Group(trial=self.trial, name='A') self.participant = models.Participant(user=self.user, trial=self.trial, group=self.group)
def test_reported_score(self): "predicateability" trial = models.Trial(pk=1) variable = models.Variable(style=models.Variable.SCORE) report = models.Report(trial=trial, variable=variable) self.assertEqual(False, report.reported()) report.score = 3 self.assertEqual(True, report.reported())
def test_randomise_second_time(self, pset): "Should raise" pset.filter.return_value.count.return_value = 2 trial = models.Trial() with self.assertRaises(exceptions.AlreadyRandomisedError): trial.randomise() pset.filter.assert_called_once_with(group__isnull=False) pset.filter.return_value.count.assert_called_once_with()
def test_reported_count(self): "predicateability" trial = models.Trial(pk=1) variable = models.Variable(style=models.Variable.COUNT) report = models.Report(trial=trial, variable=variable) self.assertEqual(False, report.reported()) report.count = 2 self.assertEqual(True, report.reported())
def test_get_value_count(self): "predicateability" trial = models.Trial(pk=1) variable = models.Variable(style=models.Variable.COUNT) report = models.Report(trial=trial, variable=variable) self.assertEqual(None, report.get_value()) report.count = 2 self.assertEqual(2, report.get_value())
def test_reported_binary(self): "predicateability" trial = models.Trial(pk=1) variable = models.Variable(style=models.Variable.BINARY) report = models.Report(trial=trial, variable=variable) self.assertEqual(False, report.reported()) report.binary = True self.assertEqual(True, report.reported())
def test_ensure_groups(self): "Get or greate for a & b groups" trial = models.Trial() with patch.object(models.Group.objects, 'get_or_create') as pgc: trial.ensure_groups() self.assertEqual(2, pgc.call_count) pgc.assert_any_call(trial=trial, name='A') pgc.assert_any_call(trial=trial, name='B')
def test_get_value_binary(self): "predicateability" trial = models.Trial(pk=1) variable = models.Variable(style=models.Variable.BINARY) report = models.Report(trial=trial, variable=variable) self.assertEqual(None, report.get_value()) report.binary = True self.assertEqual(True, report.get_value())
def test_send_instructions(self, pset): "Should email participants." part1, part2 = MagicMock(), MagicMock() pset.all.return_value = [part1, part2] trial = models.Trial() trial.send_instructions() for participant in [part1, part2]: participant.send_instructions.assert_called_once_with()
def test_get_value_score(self): "predicateability" trial = models.Trial(pk=1) variable = models.Variable(style=models.Variable.SCORE) report = models.Report(trial=trial, variable=variable) self.assertEqual(None, report.get_value()) report.score = 3 self.assertEqual(3, report.get_value())
def test_join_is_owner(self): "Should raise" user = models.User(pk=2, email='*****@*****.**') trial = models.Trial(owner=user, min_participants=20) trial.save() variable = models.Variable(question="Why", trial=trial) variable.save() trial.join(user) self.assertEqual(1, trial.participant_set.filter(user=user).count())
def test_join_too_many_participants(self, pset): "should raise" owner = models.User(pk=1) trial = models.Trial(owner=owner, max_participants=2, finish_date=self.tomorrow) pset.count.return_value = 2 with self.assertRaises(exceptions.TooManyParticipantsError): trial.join(models.User())
def test_join_second_time(self): "should raise" owner = models.User(pk=1) with patch.object(models.Participant.objects, 'filter') as pfilt: pfilt.return_value.count.return_value = 1 trial = models.Trial(owner=owner) user = models.User() with self.assertRaises(exceptions.AlreadyJoinedError): trial.join(user) pfilt.assert_called_once_with(trial=trial, user=user)
def setUp(self): super(ParticipantTestCase, self).setUp() self.user = models.User(email='*****@*****.**', pk=1) self.trial = models.Trial(pk=1, title='This', group_a='Do it', min_participants=20, owner=self.user) self.trial.save() self.variable = models.Variable(question='Why?', trial=self.trial) self.variable.save() self.group = models.Group(trial=self.trial, name='A') self.participant = models.Participant(user=self.user, trial=self.trial, group=self.group)
def test_join(self): "Should create participant" owner = models.User(pk=1) trial = models.Trial(owner=owner, min_participants=2) trial.save() user = models.User(pk=2) with patch.object(models, 'Participant') as ppart: ppart.objects.filter.return_value.count.return_value = 0 trial.join(user) ppart.objects.filter.assert_called_once_with(trial=trial, user=user) ppart.objects.filter.return_value.count.assert_called_once_with() ppart.assert_called_once_with(trial=trial, user=user)
def test_200_not_logged_in(self): " Assert status Regression #60" myuser = RMUser(email='*****@*****.**') myuser.set_password('thepass') myuser.save() trial = models.Trial(owner=myuser, title='Dummy Trial', min_participants=1) trial.save() with self.settings(BASICAUTH=False): response = self.client.get('/trials/rm/1') self.assertEqual(200, response.status_code)
def test_401_private_trial(self): "Ensure privacy" myuser = RMUser(email='*****@*****.**') myuser.set_password('thepass') myuser.save() trial = models.Trial(owner=myuser, private=True, title='Dummy Trial', min_participants=1) trial.save() with self.settings(BASICAUTH=False): response = self.client.get('/trials/rm/{0}'.format(trial.pk)) self.assertEqual(401, response.status_code)
def test_randomise(self, pset): "Randomise the participants" trial = models.Trial() part1, part2 = MagicMock(), MagicMock() pset.all.return_value = [part1, part2] pset.filter.return_value.count.return_value = 0 groups = [models.Group(trial=trial, name='A'), models.Group(trial=trial, name='B')] with patch.object(trial, 'ensure_groups') as psure: psure.return_value = groups trial.randomise() for participant in [part1, part2]: self.assertTrue(participant.group in groups) participant.save.assert_called_once_with()
def test_200_private_trial_is_owner(self): "Ensure Privacy" myuser = RMUser(email='*****@*****.**', username='******') myuser.set_password('thepass') myuser.save() trial = models.Trial(owner=myuser, private=True, title='Dummy Trial', min_participants=1) trial.save() var = models.Variable(question='Why?', trial=trial) var.save() self.assertTrue( self.client.login(username='******', password='******')) with self.settings(BASICAUTH=False): response = self.client.get('/trials/rm/{0}'.format(trial.pk)) self.assertEqual(200, response.status_code)
def test_can_join_full(self, pset): "Can join an open trial" trial = models.Trial(finish_date=self.tomorrow, max_participants=2) pset.count.return_value = 2 self.assertEqual(2, trial.participant_set.count()) self.assertEqual(False, trial.can_join())
def test_can_join_open(self, pset): "Can join an open trial" trial = models.Trial(finish_date=self.tomorrow, max_participants=2) pset.count.return_value = 1 self.assertEqual(True, trial.can_join())
def test_send_instructions_finished(self): "Should raise" trial = models.Trial(stopped=True) with self.assertRaises(exceptions.TrialFinishedError): trial.send_instructions()
def test_is_invitation_only(self): "Model predicates" trial = models.Trial() self.assertEqual(False, trial.is_invitation_only) trial.recruitment = trial.INVITATION self.assertEqual(True, trial.is_invitation_only)
def test_can_join_with_participants(self, pset): "Can join an open trial" trial = models.Trial() pset.count.return_value = 1 self.assertEqual(True, trial.can_join())
def test_can_join_n1trial(self): "Can't join a n=1 trial" trial = models.Trial(n1trial=True) self.assertEqual(False, trial.can_join())
def test_can_join_finished(self): "Can't join a finished trial" trial = models.Trial(stopped=True) self.assertEqual(False, trial.can_join())
def test_join_is_owner(self): "Should raise" user = models.User() trial = models.Trial(owner=user) with self.assertRaises(exceptions.TrialOwnerError): trial.join(user)