Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(archive): Add is_archived to /projects/{project_pk}/features/ #329

Merged
merged 4 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions api/features/migrations/0033_auto_20210918_1048.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.24 on 2021-09-18 10:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("features", "0032_update_feature_type"),
]

operations = [
migrations.AddField(
model_name="feature",
name="is_archived",
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name="historicalfeature",
name="is_archived",
field=models.BooleanField(default=False),
),
]
2 changes: 2 additions & 0 deletions api/features/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ class Feature(CustomLifecycleModelMixin, models.Model):
type = models.CharField(max_length=50, null=True, blank=True)
history = HistoricalRecords()
tags = models.ManyToManyField(Tag, blank=True)
is_archived = models.BooleanField(default=False)

class Meta:
# Note: uniqueness is changed to reference lowercase name in explicit SQL in the migrations
unique_together = ("name", "project")
ordering = ("id",) # explicit ordering to prevent pagination warnings

@hook(AFTER_CREATE)
def create_feature_states(self):
Expand Down
1 change: 1 addition & 0 deletions api/features/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Meta:
"description",
"tags",
"multivariate_options",
"is_archived",
)
read_only_fields = ("feature_segments", "created_date")

Expand Down
53 changes: 52 additions & 1 deletion api/features/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
FeatureStateValue,
)
from features.multivariate.models import MultivariateFeatureOption
from features.value_types import STRING, INTEGER, BOOLEAN
from features.value_types import BOOLEAN, INTEGER, STRING
from organisations.models import Organisation, OrganisationRole
from projects.models import Project
from projects.tags.models import Tag
Expand Down Expand Up @@ -83,6 +83,36 @@ def setUp(self):
project=self.project2,
)

def test_default_is_archived_is_false(self):
# Given - set up data
data = {
"name": "test feature",
}
url = reverse("api-v1:projects:project-features-list", args=[self.project.id])

# When
response = self.client.post(
url, data=json.dumps(data), content_type="application/json"
).json()

# Then
assert response["is_archived"] is False

def test_update_is_archived_works(self):
# Given
feature = Feature.objects.create(name="test feature", project=self.project)
url = reverse(
"api-v1:projects:project-features-detail",
args=[self.project.id, feature.id],
)
data = {"name": "test feature", "is_archived": True}

# When
response = self.client.put(url, data=data).json()

# Then
assert response["is_archived"] is True

def test_should_create_feature_states_when_feature_created(self):
# Given - set up data
default_value = "This is a value"
Expand Down Expand Up @@ -494,6 +524,27 @@ def test_list_features_return_tags(self):
feature = response_json["results"][0]
assert "tags" in feature

def test_list_features_is_archived_filter(self):
# Firstly, let's setup the initial data
feature = Feature.objects.create(name="test_feature", project=self.project)
archived_feature = Feature.objects.create(
name="archived_feature", project=self.project, is_archived=True
)
base_url = reverse(
"api-v1:projects:project-features-list", args=[self.project.id]
)
# Next, let's test true filter
url = f"{base_url}?is_archived=true"
response = self.client.get(url)
assert len(response.json()["results"]) == 1
assert response.json()["results"][0]["id"] == archived_feature.id

# Finally, let's test false filter
url = f"{base_url}?is_archived=false"
response = self.client.get(url)
assert len(response.json()["results"]) == 1
assert response.json()["results"][0]["id"] == feature.id

def test_put_feature_does_not_update_feature_states(self):
# Given
feature = Feature.objects.create(
Expand Down
1 change: 1 addition & 0 deletions api/features/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

class FeatureViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated, FeaturePermissions]
filterset_fields = ["is_archived"]

def get_serializer_class(self):
return {
Expand Down