def setUp(self):
     self.experiment = Experiment(name="test_experiment")
     self.experiment.save()
     self.experiment.state = Experiment.ENABLED_STATE
     self.experiment.save()
     self.experiment.start_date = (self.experiment.start_date -
                                   timedelta(days=5))
     self.experiment.save()
     
     anonymous_visitor = AnonymousVisitor()
     anonymous_visitor.save()
     anonymous_participant = Participant(anonymous_visitor=anonymous_visitor,
                                         experiment=self.experiment,
                                         group=Participant.TEST_GROUP)
     anonymous_participant.save()
     anonymous_participant.enrollment_date = self.experiment.start_date
     anonymous_participant.save()
     
     self.other_experiment = Experiment(name="test_experiment2")
     self.other_experiment.save()
     self.other_experiment.state = Experiment.DISABLED_STATE
     self.other_experiment.save()
     self.other_experiment.start_date = (date.today() -
                                   timedelta(days=7))
     self.other_experiment.end_date = (date.today() -
                                   timedelta(days=3))
     self.other_experiment.save()
    def testMissingGoalType(self):
        anonymous_visitor = AnonymousVisitor()
        anonymous_visitor.save()

        goal_type = GoalType(name="existing-goal")
        goal_type.save()
        
        nb_types = GoalType.objects.all().count()
        nb_records = GoalRecord.objects.all().count()

        GoalRecord.record('existing-goal', TestUser(anonymous_visitor=anonymous_visitor))
        self.assertEquals(nb_records + 1, GoalRecord.objects.all().count())
        self.assertEqual(nb_types, GoalType.objects.all().count())

        with patch(settings, 'LEAN_AUTOCREATE_GOAL_TYPES', False):
            GoalRecord.record('inexistant-goal', TestUser(anonymous_visitor=anonymous_visitor))
            self.assertEquals(nb_records + 1, GoalRecord.objects.all().count())
            self.assertEqual(nb_types, GoalType.objects.all().count())

        with patch(settings, 'LEAN_AUTOCREATE_GOAL_TYPES', NotImplemented):
            GoalRecord.record('inexistant-goal', TestUser(anonymous_visitor=anonymous_visitor))
            self.assertEquals(nb_records + 1, GoalRecord.objects.all().count())
            self.assertEqual(nb_types, GoalType.objects.all().count())
            
        with patch(settings, 'LEAN_AUTOCREATE_GOAL_TYPES', True):
            GoalRecord.record('inexistant-goal',
                              TestUser(anonymous_visitor=anonymous_visitor))
            self.assertEquals(nb_records + 2, GoalRecord.objects.all().count())
            self.assertEqual(nb_types + 1, GoalType.objects.all().count())
Пример #3
0
    def setUp(self):
        self.experiment = Experiment(name="test_experiment")
        self.experiment.save()
        self.experiment.state = Experiment.ENABLED_STATE
        self.experiment.save()
        self.experiment.start_date = (self.experiment.start_date -
                                      timedelta(days=5))
        self.experiment.save()

        anonymous_visitor = AnonymousVisitor()
        anonymous_visitor.save()
        anonymous_participant = Participant(
            anonymous_visitor=anonymous_visitor,
            experiment=self.experiment,
            group=Participant.TEST_GROUP)
        anonymous_participant.save()
        anonymous_participant.enrollment_date = self.experiment.start_date
        anonymous_participant.save()

        self.other_experiment = Experiment(name="test_experiment2")
        self.other_experiment.save()
        self.other_experiment.state = Experiment.DISABLED_STATE
        self.other_experiment.save()
        self.other_experiment.start_date = (date.today() - timedelta(days=7))
        self.other_experiment.end_date = (date.today() - timedelta(days=3))
        self.other_experiment.save()
Пример #4
0
    def testMissingGoalType(self):
        anonymous_visitor = AnonymousVisitor()
        anonymous_visitor.save()

        goal_type = GoalType(name="existing-goal")
        goal_type.save()

        nb_types = GoalType.objects.all().count()
        nb_records = GoalRecord.objects.all().count()

        GoalRecord.record('existing-goal',
                          TestUser(anonymous_visitor=anonymous_visitor))
        self.assertEquals(nb_records + 1, GoalRecord.objects.all().count())
        self.assertEqual(nb_types, GoalType.objects.all().count())

        with patch(settings, 'LEAN_AUTOCREATE_GOAL_TYPES', False):
            GoalRecord.record('inexistant-goal',
                              TestUser(anonymous_visitor=anonymous_visitor))
            self.assertEquals(nb_records + 1, GoalRecord.objects.all().count())
            self.assertEqual(nb_types, GoalType.objects.all().count())

        with patch(settings, 'LEAN_AUTOCREATE_GOAL_TYPES', NotImplemented):
            GoalRecord.record('inexistant-goal',
                              TestUser(anonymous_visitor=anonymous_visitor))
            self.assertEquals(nb_records + 1, GoalRecord.objects.all().count())
            self.assertEqual(nb_types, GoalType.objects.all().count())

        with patch(settings, 'LEAN_AUTOCREATE_GOAL_TYPES', True):
            GoalRecord.record('inexistant-goal',
                              TestUser(anonymous_visitor=anonymous_visitor))
            self.assertEquals(nb_records + 2, GoalRecord.objects.all().count())
            self.assertEqual(nb_types + 1, GoalType.objects.all().count())
Пример #5
0
    def testGoals(self):
        anonymous_visitor = AnonymousVisitor()
        anonymous_visitor.save()

        # required fields for GoalType
        self.assertRaises(Exception, lambda: GoalType(name=None).save())

        goal_type = GoalType(name="test-goal")
        goal_type.save()

        # unique constraint on GoalType.name
        self.assertRaises(Exception, lambda: GoalType(name="test-goal").save())

        # required fields for GoalRecord
        self.assertRaises(Exception, lambda: GoalRecord().save())
        self.assertRaises(
            Exception,
            lambda: GoalRecord(anonymous_visitor=anonymous_visitor).save())
        self.assertRaises(Exception,
                          lambda: GoalRecord(goal_type=goal_type).save())

        now = datetime.now()

        goal_record = GoalRecord(anonymous_visitor=anonymous_visitor,
                                 goal_type=goal_type)
        goal_record.save()

        self.assertTrue(goal_record.created >= now
                        and goal_record.created <= datetime.now())

        # it's OK for the same user to record the same goal multiple times
        goal_record2 = GoalRecord(anonymous_visitor=anonymous_visitor,
                                  goal_type=goal_type)
        goal_record2.save()

        nb_records = GoalRecord.objects.all().count()
        GoalRecord.record('test-goal', TestUser())
        self.assertEquals(nb_records, GoalRecord.objects.all().count())
        GoalRecord.record('test-goal', TestUser(username='******'))
        self.assertEquals(nb_records, GoalRecord.objects.all().count())
        GoalRecord.record('test-goal',
                          TestUser(anonymous_visitor=anonymous_visitor))
        self.assertEquals(nb_records + 1, GoalRecord.objects.all().count())
        GoalRecord.record('test-goal',
                          TestUser(anonymous_visitor=anonymous_visitor))
        self.assertEquals(nb_records + 2, GoalRecord.objects.all().count())
 def testGoals(self):
     anonymous_visitor = AnonymousVisitor()
     anonymous_visitor.save()
     
     # required fields for GoalType
     self.assertRaises(Exception, lambda: GoalType(name=None).save())
     
     goal_type = GoalType(name="test-goal")
     goal_type.save()
     
     # unique constraint on GoalType.name
     self.assertRaises(Exception, lambda: GoalType(name="test-goal").save())
     
     # required fields for GoalRecord
     self.assertRaises(Exception, lambda: GoalRecord().save())
     self.assertRaises(Exception, lambda: GoalRecord(anonymous_visitor=anonymous_visitor).save())
     self.assertRaises(Exception, lambda: GoalRecord(goal_type=goal_type).save())
     
     now = datetime.now()
     
     goal_record = GoalRecord(anonymous_visitor=anonymous_visitor, goal_type=goal_type)
     goal_record.save()
     
     self.assertTrue(goal_record.created >= now and goal_record.created <= datetime.now())
     
     # it's OK for the same user to record the same goal multiple times
     goal_record2 = GoalRecord(anonymous_visitor=anonymous_visitor, goal_type=goal_type)
     goal_record2.save()
     
     nb_records = GoalRecord.objects.all().count()
     GoalRecord.record('test-goal', TestUser())
     self.assertEquals(nb_records, GoalRecord.objects.all().count())
     GoalRecord.record('test-goal', TestUser(username='******'))
     self.assertEquals(nb_records, GoalRecord.objects.all().count())
     GoalRecord.record('test-goal', TestUser(anonymous_visitor=anonymous_visitor))
     self.assertEquals(nb_records + 1, GoalRecord.objects.all().count())
     GoalRecord.record('test-goal', TestUser(anonymous_visitor=anonymous_visitor))
     self.assertEquals(nb_records + 2, GoalRecord.objects.all().count())
 def testParticipants(self):
     user1 = User(username="******", email="*****@*****.**")
     user1.save()
     user2 = User(username="******", email="*****@*****.**")
     user2.save()
     user3 = User(username="******", email="*****@*****.**")
     user3.save()
     
     anonymous_visitor1 = AnonymousVisitor()
     anonymous_visitor1.save()
     anonymous_visitor2 = AnonymousVisitor()
     anonymous_visitor2.save()
     anonymous_visitor3 = AnonymousVisitor()
     anonymous_visitor3.save()
     
     experiment1 = Experiment(name="Experiment 1")
     experiment1.save()
     experiment2 = Experiment(name="Experiment 2")
     experiment2.save()
     
     participant11 = Participant(user=user1,
                                 experiment= experiment1,
                                 group=Participant.CONTROL_GROUP)
     participant11.save()
     participant12 = Participant(user=user1,
                                 experiment= experiment2,
                                 group=Participant.TEST_GROUP)
     participant12.save()
     participant21 = Participant(user=user2,
                                 experiment= experiment1,
                                 group=Participant.CONTROL_GROUP)
     participant21.save()
     participant22 = Participant(user=user2,
                                 experiment= experiment2,
                                 group=Participant.CONTROL_GROUP)
     participant22.save()
     
     self.assertRaises(Exception,
                       lambda: Participant(user=user2,
                                           experiment= experiment2,
                                           group=Participant.TEST_GROUP).save())
     
     # create anonymous participants
     participant31 = Participant(anonymous_visitor=anonymous_visitor1,
                                 experiment=experiment1,
                                 group=Participant.CONTROL_GROUP)
     participant31.save()
     participant32 = Participant(anonymous_visitor=anonymous_visitor1,
                                 experiment=experiment2,
                                 group=Participant.TEST_GROUP)
     participant32.save()
     participant42 = Participant(anonymous_visitor=anonymous_visitor2,
                                 experiment=experiment2,
                                 group=Participant.CONTROL_GROUP)
     participant42.save()
     
     self.assertRaises(Exception,
                       lambda: Participant(anonymous_visitor=anonymous_visitor1,
                                           experiment=experiment1,
                                           group=Participant.TEST_GROUP).save())
     
     self.assertRaises(Exception,
                       lambda: Participant(experiment=experiment1,
                                           group=Participant.TEST_GROUP).save())
     
     self.assertRaises(Exception,
                       lambda: Participant(user=user3,
                                           anonymous_visitor=anonymous_visitor3,
                                           experiment=experiment1,
                                           group=Participant.TEST_GROUP))
     self.assertRaises(Exception,
                       lambda: Participant(user=user3,
                                           experiment=experiment1).save())
     
     self.assertNotEquals(participant11.enrollment_date, None)
     
     participant11.enrollment_date = None
     self.assertRaises(Exception,
                       lambda: participant11.save())
     
     participant12.group = None
     self.assertRaises(Exception,
                       lambda: participant12.save())
Пример #8
0
    def testParticipants(self):
        user1 = User(username="******", email="*****@*****.**")
        user1.save()
        user2 = User(username="******", email="*****@*****.**")
        user2.save()
        user3 = User(username="******", email="*****@*****.**")
        user3.save()

        anonymous_visitor1 = AnonymousVisitor()
        anonymous_visitor1.save()
        anonymous_visitor2 = AnonymousVisitor()
        anonymous_visitor2.save()
        anonymous_visitor3 = AnonymousVisitor()
        anonymous_visitor3.save()

        experiment1 = Experiment(name="Experiment 1")
        experiment1.save()
        experiment2 = Experiment(name="Experiment 2")
        experiment2.save()

        participant11 = Participant(user=user1,
                                    experiment=experiment1,
                                    group=Participant.CONTROL_GROUP)
        participant11.save()
        participant12 = Participant(user=user1,
                                    experiment=experiment2,
                                    group=Participant.TEST_GROUP)
        participant12.save()
        participant21 = Participant(user=user2,
                                    experiment=experiment1,
                                    group=Participant.CONTROL_GROUP)
        participant21.save()
        participant22 = Participant(user=user2,
                                    experiment=experiment2,
                                    group=Participant.CONTROL_GROUP)
        participant22.save()

        self.assertRaises(
            Exception,
            lambda: Participant(user=user2,
                                experiment=experiment2,
                                group=Participant.TEST_GROUP).save())

        # create anonymous participants
        participant31 = Participant(anonymous_visitor=anonymous_visitor1,
                                    experiment=experiment1,
                                    group=Participant.CONTROL_GROUP)
        participant31.save()
        participant32 = Participant(anonymous_visitor=anonymous_visitor1,
                                    experiment=experiment2,
                                    group=Participant.TEST_GROUP)
        participant32.save()
        participant42 = Participant(anonymous_visitor=anonymous_visitor2,
                                    experiment=experiment2,
                                    group=Participant.CONTROL_GROUP)
        participant42.save()

        self.assertRaises(
            Exception,
            lambda: Participant(anonymous_visitor=anonymous_visitor1,
                                experiment=experiment1,
                                group=Participant.TEST_GROUP).save())

        self.assertRaises(
            Exception,
            lambda: Participant(experiment=experiment1,
                                group=Participant.TEST_GROUP).save())

        self.assertRaises(
            Exception,
            lambda: Participant(user=user3,
                                anonymous_visitor=anonymous_visitor3,
                                experiment=experiment1,
                                group=Participant.TEST_GROUP))
        self.assertRaises(
            Exception,
            lambda: Participant(user=user3, experiment=experiment1).save())

        self.assertNotEquals(participant11.enrollment_date, None)

        participant11.enrollment_date = None
        self.assertRaises(Exception, lambda: participant11.save())

        participant12.group = None
        self.assertRaises(Exception, lambda: participant12.save())