def test_should_remove_identityfeature_when_delete(self): # Given client = self.set_up() organisation = Organisation.objects.get(name="test org") project = Project.objects.get(name="test project", organisation=organisation) feature_one = Feature(name='feature1', project=project) feature_one.save() feature_two = Feature(name='feature2', project=project) feature_two.save() environment = Environment.objects.get(name="test env") identity = Identity(identifier="test_identity", environment=environment) identity.save() environment = Environment.objects.get(name="test env") identity_feature_one = FeatureState(feature=feature_one, identity=identity, enabled=False, environment=environment) identity_feature_one.save() identity_feature_two = FeatureState(feature=feature_two, identity=identity, enabled=True, environment=environment) identity_feature_two.save() # When client.delete(self.feature_states_detail_url % (identity.environment.api_key, identity.identifier, identity_feature_one.id), content_type='application/json') # Then identity_features = FeatureState.objects.filter(identity=identity) self.assertEquals(identity_features.count(), 1) Helper.clean_up()
def test_should_change_enabled_state_when_put(self): # Given client = self.set_up() organisation = Organisation.objects.get(name="test org") project = Project.objects.get(name="test project", organisation=organisation) feature = Feature(name='feature1', project=project) feature.save() environment = Environment.objects.get(name="test env") identity = Identity(identifier="test_identity", environment=environment) identity.save() feature_state = FeatureState(feature=feature, identity=identity, enabled=False, environment=environment) feature_state.save() # When response = client.put(self.feature_states_detail_url % (identity.environment.api_key, identity.identifier, feature_state.id), data=self.put_template % True, content_type='application/json') feature_state.refresh_from_db() # Then self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(feature_state.enabled, True) Helper.clean_up()
def generate_database_models(identifier='user1'): organisation = Organisation(name='ssg') organisation.save() project = Project(name='project1', organisation=organisation) project.save() environment = Environment(name='environment1', project=project) environment.save() feature = Feature(name="feature1", project=project) feature.save() identity = Identity(identifier=identifier, environment=environment) identity.save() return identity, project
def test_should_return_identities_for_an_environment(self): client = self.set_up() # Given identifierOne = 'user1' identifierTwo = 'user2' organisation = Organisation(name='ssg') organisation.save() project = Project(name='project1', organisation=organisation) project.save() environment = Environment(name='environment1', project=project) environment.save() identityOne = Identity(identifier=identifierOne, environment=environment) identityOne.save() identityTwo = Identity(identifier=identifierTwo, environment=environment) identityTwo.save() # When response = client.get('/api/v1/environments/%s/identities/' % environment.api_key) # Then self.assertEquals(response.data['results'][0]['identifier'], identifierOne) self.assertEquals(response.data['results'][1]['identifier'], identifierTwo)
def test_percentage_values_should_be_evenly_distributed(self): """ This test checks if the percentage value returned by the method on SegmentRule returns evenly distributed values. Note that since it's technically random, it's not guaranteed to pass every time, however, it should pass 99/100 times. It will likely be more accurate by increasing the test_sample value and / or decreasing the num_test_buckets value. """ test_sample = 10000 # number of identities to create to test with num_test_buckets = 10 # split the sample into 'buckets' to check that the values are evenly distributed test_bucket_size = int(test_sample / num_test_buckets) error_factor = 0.1 # Given segment = Segment.objects.create(name='Test Segment', project=self.project) identities = [] for i in range(test_sample): identities.append( Identity(environment=self.environment, identifier=str(i))) Identity.objects.bulk_create(identities) # When values = [ segment.get_identity_percentage_value(identity) for identity in Identity.objects.all() ] values.sort() # Then for j in range(num_test_buckets): bucket_start = j * test_bucket_size bucket_end = (j + 1) * test_bucket_size bucket_value_limit = min( (j + 1) / num_test_buckets + error_factor * ((j + 1) / num_test_buckets), 1) assert all([ value <= bucket_value_limit for value in values[bucket_start:bucket_end] ])