Skip to content

Commit

Permalink
chore: Convert unit features models TestCase to normal test function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Dec 18, 2023
1 parent 3fb0076 commit 2251a1f
Showing 1 changed file with 159 additions and 141 deletions.
300 changes: 159 additions & 141 deletions api/tests/unit/features/test_unit_features_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,180 +26,198 @@
tomorrow = now + timedelta(days=1)


@pytest.mark.django_db
class FeatureTestCase(TestCase):
def setUp(self):
self.organisation = Organisation.objects.create(name="Test Org")
self.project = Project.objects.create(
name="Test Project", organisation=self.organisation
)
self.environment_one = Environment.objects.create(
name="Test Environment 1", project=self.project
)
self.environment_two = Environment.objects.create(
name="Test Environment 2", project=self.project
)
def test_feature_should_create_feature_states_for_environments(
db: None,
environment: Environment,
project: Project,
) -> None:
# Given
Environment.objects.create(name="Test Environment 2", project=project)

def test_feature_should_create_feature_states_for_environments(self):
feature = Feature.objects.create(name="Test Feature", project=self.project)
# When
feature = Feature.objects.create(name="Test Feature", project=project)

feature_states = FeatureState.objects.filter(feature=feature)
# Then
feature_states = FeatureState.objects.filter(feature=feature)
assert feature_states.count() == 2

self.assertEquals(feature_states.count(), 2)

def test_save_existing_feature_should_not_change_feature_state_enabled(self):
# Given
default_enabled = True
feature = Feature.objects.create(
name="Test Feature", project=self.project, default_enabled=default_enabled
)
def test_save_existing_feature_should_not_change_feature_state_enabled(
db: None,
project: Project,
) -> None:
# Given
default_enabled = True
feature = Feature.objects.create(
name="Test Feature", project=project, default_enabled=default_enabled
)

# When
# we update the default_enabled state of the feature and save it again
feature.default_enabled = not default_enabled
feature.save()
# When
# we update the default_enabled state of the feature and save it again
feature.default_enabled = not default_enabled
feature.save()

# Then
# we expect that the feature state enabled values have not changed
assert all(fs.enabled == default_enabled for fs in feature.feature_states.all())
# Then
# we expect that the feature state enabled values have not changed
assert all(fs.enabled == default_enabled for fs in feature.feature_states.all())

def test_creating_feature_with_initial_value_should_set_value_for_all_feature_states(
self,
):
feature = Feature.objects.create(
name="Test Feature",
project=self.project,
initial_value="This is a value",
)

feature_states = FeatureState.objects.filter(feature=feature)
def test_creating_feature_with_initial_value_should_set_value_for_all_feature_states(
project: Project,
environment: Environment,
) -> None:
# Given
Environment.objects.create(name="Test Environment 2", project=project)

for feature_state in feature_states:
self.assertEquals(
feature_state.get_feature_state_value(), "This is a value"
)
# When
value = "This is a value"
feature = Feature.objects.create(
name="Test Feature", project=project, initial_value=value
)

def test_creating_feature_with_integer_initial_value_should_set_integer_value_for_all_feature_states(
self,
):
# Given
initial_value = 1
feature = Feature.objects.create(
name="Test feature",
project=self.project,
initial_value=initial_value,
)
# Then
feature_states = FeatureState.objects.filter(feature=feature)

# When
feature_states = FeatureState.objects.filter(feature=feature)
assert feature_states.count() == 2
for feature_state in feature_states:
feature_state.get_feature_state_value() == value

# Then
for feature_state in feature_states:
assert feature_state.get_feature_state_value() == initial_value

def test_creating_feature_with_boolean_initial_value_should_set_boolean_value_for_all_feature_states(
self,
):
# Given
initial_value = False
feature = Feature.objects.create(
name="Test feature",
project=self.project,
initial_value=initial_value,
)
def test_creating_feature_with_integer_initial_value_should_set_integer_value_for_all_feature_states(
project: Project,
environment: Environment,
) -> None:
# Given
Environment.objects.create(name="Test Environment 2", project=project)

# When
feature_states = FeatureState.objects.filter(feature=feature)
initial_value = 1
feature = Feature.objects.create(
name="Test feature",
project=project,
initial_value=initial_value,
)

# Then
for feature_state in feature_states:
assert feature_state.get_feature_state_value() == initial_value
# When
feature_states = FeatureState.objects.filter(feature=feature)

# Then
assert feature_states.count() == 2
for feature_state in feature_states:
assert feature_state.get_feature_state_value() == initial_value

def test_updating_feature_state_should_trigger_webhook(self):
Feature.objects.create(name="Test Feature", project=self.project)
# TODO: implement webhook test method

def test_cannot_create_feature_with_same_case_insensitive_name(self):
# Given
feature_name = "Test Feature"
def test_creating_feature_with_boolean_initial_value_should_set_boolean_value_for_all_feature_states(
project: Project,
environment: Environment,
) -> None:
# Given
Environment.objects.create(name="Test Environment 2", project=project)

feature_one = Feature(project=self.project, name=feature_name)
feature_two = Feature(project=self.project, name=feature_name.lower())
initial_value = False
feature = Feature.objects.create(
name="Test feature",
project=project,
initial_value=initial_value,
)

# When
feature_one.save()
# When
feature_states = FeatureState.objects.filter(feature=feature)

# Then
with pytest.raises(IntegrityError):
feature_two.save()
# Then
assert feature_states.count() == 2
for feature_state in feature_states:
assert feature_state.get_feature_state_value() == initial_value

def test_updating_feature_name_should_update_feature_states(self):
# Given
old_feature_name = "old_feature"
new_feature_name = "new_feature"

feature = Feature.objects.create(project=self.project, name=old_feature_name)
def test_cannot_create_feature_with_same_case_insensitive_name(
project: Project,
) -> None:
# Given
feature_name = "Test Feature"

# When
feature.name = new_feature_name
feature.save()
feature_one = Feature(project=project, name=feature_name)
feature_two = Feature(project=project, name=feature_name.lower())

# Then
FeatureState.objects.filter(feature__name=new_feature_name).exists()
# When
feature_one.save()

def test_full_clean_fails_when_duplicate_case_insensitive_name(self):
# unit test to validate validate_unique() method
# Then
with pytest.raises(IntegrityError):
feature_two.save()

# Given
feature_name = "Test Feature"
Feature.objects.create(
name=feature_name, initial_value="test", project=self.project
)

# When
with self.assertRaises(ValidationError):
feature_two = Feature(
name=feature_name.lower(),
initial_value="test",
project=self.project,
)
feature_two.full_clean()

def test_updating_feature_should_allow_case_insensitive_name(self):
# Given
feature_name = "Test Feature"
def test_updating_feature_name_should_update_feature_states(
project: Project,
) -> None:
# Given
old_feature_name = "old_feature"
new_feature_name = "new_feature"

feature = Feature.objects.create(
project=self.project, name=feature_name, initial_value="test"
)
feature = Feature.objects.create(project=project, name=old_feature_name)

# When
feature.name = feature_name.lower()
feature.full_clean() # should not raise error as the same Object
# When
feature.name = new_feature_name
feature.save()

def test_when_create_feature_with_tags_then_success(self):
# Given
tag1 = Tag.objects.create(
label="Test Tag",
color="#fffff",
description="Test Tag description",
project=self.project,
)
tag2 = Tag.objects.create(
label="Test Tag",
color="#fffff",
description="Test Tag description",
project=self.project,
# Then
FeatureState.objects.filter(feature__name=new_feature_name).exists()


def test_full_clean_fails_when_duplicate_case_insensitive_name(
project: Project,
) -> None:
# unit test to validate validate_unique() method

# Given
feature_name = "Test Feature"
Feature.objects.create(name=feature_name, initial_value="test", project=project)

# When
with pytest.raises(ValidationError):
feature_two = Feature(
name=feature_name.lower(),
initial_value="test",
project=project,
)
feature = Feature.objects.create(project=self.project, name="test feature")
feature_two.full_clean()

# When
tags_for_feature = Tag.objects.all()
feature.tags.set(tags_for_feature)
feature.save()

self.assertEqual(feature.tags.count(), 2)
self.assertEqual(list(feature.tags.all()), [tag1, tag2])
def test_updating_feature_should_allow_case_insensitive_name(project: Project) -> None:
# Given
feature_name = "Test Feature"
feature = Feature.objects.create(
project=project, name=feature_name, initial_value="test"
)

# When
feature.name = feature_name.lower()
# Should not raise error as the same Object.
feature.full_clean()


def test_when_create_feature_with_tags_then_success(project: Project) -> None:
# Given
tag1 = Tag.objects.create(
label="Test Tag",
color="#fffff",
description="Test Tag description",
project=project,
)
tag2 = Tag.objects.create(
label="Test Tag",
color="#fffff",
description="Test Tag description",
project=project,
)
feature = Feature.objects.create(project=project, name="test feature")

# When
tags_for_feature = Tag.objects.all()
feature.tags.set(tags_for_feature)
feature.save()

assert feature.tags.count() == 2
assert list(feature.tags.all()) == [tag1, tag2]


@pytest.mark.django_db
Expand Down

3 comments on commit 2251a1f

@vercel
Copy link

@vercel vercel bot commented on 2251a1f Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2251a1f Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs.bullet-train.io
docs-git-main-flagsmith.vercel.app
docs.flagsmith.com
docs-flagsmith.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 2251a1f Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.