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())
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 testPngResponse(self): experiment = Experiment(name="test-experiment") experiment.save() experiment.state = Experiment.ENABLED_STATE experiment.save() goal_type = GoalType(name='test-goal') goal_type.save() experiment_url = reverse( "django_lean.experiments.tests.views.experiment_test", args=[experiment.name]) confirm_human_url = reverse( "django_lean.experiments.views.confirm_human") client = Client() # we can call with invalid or inexisting names, the response is the same self.call_goal(client, '') self.call_goal(client, 'unknown-goal') # this is an anonymous visitor not enrolled in an experiment, # so no records should be created self.call_goal(client, goal_type.name) self.assertEquals( 0, GoalRecord.objects.filter(goal_type=goal_type).count()) nb_anonymous_visitors = AnonymousVisitor.objects.count() # force the user to be a verified human response = client.get(confirm_human_url) self.assertEquals(response.status_code, 204) # force the anonymous visitor to be enrolled in an experiment response = client.get(experiment_url) self.assertEquals(response.status_code, 200) self.assertEquals(nb_anonymous_visitors + 1, AnonymousVisitor.objects.count()) client.get('/test-experiment/%s' % experiment.name) self.assertEquals(nb_anonymous_visitors + 1, AnonymousVisitor.objects.count()) # now call an existing goal again - it should be recorded self.call_goal(client, goal_type.name) self.assertEquals( 1, GoalRecord.objects.filter(goal_type=goal_type).count()) # should be recorded again self.call_goal(client, goal_type.name) self.assertEquals( 2, GoalRecord.objects.filter(goal_type=goal_type).count()) # validate that both of the records have the same anonymous_visitor two_goal_records = GoalRecord.objects.filter(goal_type=goal_type) self.assertEquals(two_goal_records[0].anonymous_visitor, two_goal_records[1].anonymous_visitor) # try it with a registered user client = Client() user = User(username="******", email="*****@*****.**") user.set_password("password") user.save() response = client.login(username=user.username, password='******') self.assertTrue(response) # force the registered user to be enrolled in an experiment client.get('/test-experiment/%s' % experiment.name) self.call_goal(client, goal_type.name) # since the user was registered, no new records should be created self.assertEquals( 2, GoalRecord.objects.filter(goal_type=goal_type).count())
def testPngResponse(self): experiment = Experiment(name="test-experiment") experiment.save() experiment.state = Experiment.ENABLED_STATE experiment.save() goal_type = GoalType(name='test-goal') goal_type.save() experiment_url = reverse("django_lean.experiments.tests.views.experiment_test", args=[experiment.name]) confirm_human_url = reverse("django_lean.experiments.views.confirm_human") client = Client() # we can call with invalid or inexisting names, the response is the same self.call_goal(client, '') self.call_goal(client, 'unknown-goal') # this is an anonymous visitor not enrolled in an experiment, # so no records should be created self.call_goal(client, goal_type.name) self.assertEqual(0, GoalRecord.objects.filter(goal_type=goal_type).count()) nb_anonymous_visitors = AnonymousVisitor.objects.count() # force the user to be a verified human response = client.get(confirm_human_url) self.assertEqual(response.status_code, 204) # force the anonymous visitor to be enrolled in an experiment response = client.get(experiment_url) self.assertEqual(response.status_code, 200) self.assertEqual(nb_anonymous_visitors + 1, AnonymousVisitor.objects.count()) client.get('/test-experiment/%s' % experiment.name) self.assertEqual(nb_anonymous_visitors + 1, AnonymousVisitor.objects.count()) # now call an existing goal again - it should be recorded self.call_goal(client, goal_type.name) self.assertEqual(1, GoalRecord.objects.filter(goal_type=goal_type).count()) # should be recorded again self.call_goal(client, goal_type.name) self.assertEqual(2, GoalRecord.objects.filter(goal_type=goal_type).count()) # validate that both of the records have the same anonymous_visitor two_goal_records = GoalRecord.objects.filter(goal_type=goal_type) self.assertEqual(two_goal_records[0].anonymous_visitor, two_goal_records[1].anonymous_visitor) # try it with a registered user client = Client() user = User(username="******", email="*****@*****.**") user.set_password("password") user.save() response = client.login(username=user.username, password='******') self.assertTrue(response) # force the registered user to be enrolled in an experiment client.get('/test-experiment/%s' % experiment.name) self.call_goal(client, goal_type.name) # since the user was registered, no new records should be created self.assertEqual(2, GoalRecord.objects.filter(goal_type=goal_type).count())