Skip to content

Commit

Permalink
wip: Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi committed Aug 11, 2021
1 parent e0e52d3 commit 789ac8b
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ multi_line_output=3
include_trailing_comma=true
line_length=79
known_first_party=analytics,app,custom_auth,environments,integrations,organisations,projects,segments,users,webhooks,api,audit,e2etests,features,permissions,util
known_third_party=apiclient,app_analytics,axes,chargebee,core,coreapi,corsheaders,dateutil,dj_database_url,django,django_lifecycle,djoser,drf_writable_nested,drf_yasg2,environs,google,influxdb_client,ordered_model,pyotp,pytest,pytz,requests,responses,rest_framework,rest_framework_nested,rest_framework_recursive,sentry_sdk,shortuuid,simple_history,six,telemetry,tests,trench,whitenoise
known_third_party=apiclient,app_analytics,axes,chargebee,core,coreapi,corsheaders,dateutil,dj_database_url,django,django_lifecycle,djoser,drf_writable_nested,drf_yasg2,environs,freezegun,google,influxdb_client,ordered_model,pyotp,pytest,pytz,requests,responses,rest_framework,rest_framework_nested,rest_framework_recursive,sentry_sdk,shortuuid,simple_history,six,telemetry,tests,trench,whitenoise
2 changes: 1 addition & 1 deletion api/features/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def get_queryset(self):
def get_serializer_context(self):
context = super().get_serializer_context()
queryset = self.get_base_qs()
# Add emptry order by to force group by to happen on featur__id
# Add emptry order by to force group by to happen on feature__id
feature_updated_at_cache = list(
queryset.values("feature__id")
.annotate(updated_at=Max("updated_at"))
Expand Down
1 change: 1 addition & 0 deletions api/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ black
pip-tools
responses
pre-commit
freezegun
32 changes: 1 addition & 31 deletions api/tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import uuid
from typing import Callable, Mapping

import pytest
from django.test import Client as DjangoClient
Expand Down Expand Up @@ -60,22 +61,6 @@ def environment(admin_client, project, environment_api_key) -> int:
return response.json()["id"]


@pytest.fixture()
def segment(admin_client, project) -> int:
url = reverse("api-v1:projects:project-segments-list", args=[project])
segment_data = {
"name": "Test Segment",
"project": project,
"rules": [{"type": "ALL", "rules": [], "conditions": []}],
}

response = admin_client.post(
url, data=json.dumps(segment_data), content_type="application/json"
)
print(response.content)
return response.json()["id"]


@pytest.fixture()
def identity_identifier():
return uuid.uuid4()
Expand All @@ -91,21 +76,6 @@ def identity(admin_client, identity_identifier, environment, environment_api_key
return response.json()["id"]


@pytest.fixture()
def feature(admin_client, project, environment):
default_value = "This is a value"
data = {
"name": "test feature",
"initial_value": default_value,
"project": project,
}
url = reverse("api-v1:projects:project-features-list", args=[project])

# When
response = admin_client.post(url, data=data)
return response.json()["id"]


@pytest.fixture()
def sdk_client(environment_api_key):
client = APIClient()
Expand Down
116 changes: 116 additions & 0 deletions api/tests/integration/features/test_feature_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import json
from datetime import datetime, timedelta

import pytest
from django.urls import reverse
from django.utils import timezone
from freezegun import freeze_time
from rest_framework import status
from tests.integration.helpers import (
get_env_feature_states_list_with_api,
update_feature_state_with_api,
)


@pytest.fixture()
def frozen_time():
with freeze_time(timezone.now() - timedelta(days=2)) as f:
yield f


def test_updates_days_since_feature_last_updated_is_part_of_respone(
frozen_time, admin_client, environment_api_key, environment, feature
):
# Given
url = reverse(
"api-v1:environments:environment-featurestates-list",
args=[environment_api_key],
)

# When
response = admin_client.get(url)
# Then
assert response.json()["results"][0]["days_since_feature_last_updated"] == 2


def test_updating_feature_state_updates_days_since_feature_last_updated(
frozen_time, admin_client, environment, environment_api_key, project, feature
):
# Given
url = reverse(
"api-v1:environments:environment-featurestates-list",
args=[environment_api_key],
)
# Get the feature state to update
feature_state = get_env_feature_states_list_with_api(
admin_client, {"environment": environment}
)["results"][0]["id"]

# When

# Firstly, let's reset the time so that feature state update happen at t
# instead of t-2
frozen_time.move_to(datetime.now() + timedelta(days=2))

# Update the feature state
update_feature_state_with_api(
admin_client,
{
"id": feature_state,
"feature_state_value": {"type": "unicode", "string_value": "new_value"},
"enabled": False,
"feature": feature,
"environment": environment,
"identity": None,
"feature_segment": None,
},
)
response = admin_client.get(url)

# Then
assert response.json()["results"][0]["days_since_feature_last_updated"] == 0


def test_adding_feature_segment_udpates_days_since_feature_last_updated(
frozen_time,
admin_client,
environment,
environment_api_key,
project,
feature,
segment,
feature_segment,
):
# Given
frozen_time.move_to(datetime.now() + timedelta(days=2))

# Let's create a feature segment override
create_url = reverse("api-v1:features:featurestates-list")
data = {
"feature_state_value": {
"type": "unicode",
"boolean_value": None,
"integer_value": None,
"string_value": "dumb",
},
"multivariate_feature_state_values": [],
"enabled": False,
"feature": feature,
"environment": environment,
"identity": None,
"feature_segment": feature_segment,
}
seg_override_response = admin_client.post(
create_url, data=json.dumps(data), content_type="application/json"
)
# Make sure that override was a success
assert seg_override_response.status_code == status.HTTP_201_CREATED

url = reverse(
"api-v1:environments:environment-featurestates-list",
args=[environment_api_key],
)
response = admin_client.get(url)

# Then
assert response.json()["results"][0]["days_since_feature_last_updated"] == 0
11 changes: 11 additions & 0 deletions api/tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,14 @@ def get_json_response(client: APIClient, url: str, query_params: dict) -> dict:
if query_params:
url = f"{url}?{urlencode(query_params)}"
return client.get(url).json()


def update_feature_state_with_api(client: APIClient, data: dict):
feature_state = data["id"]
fs_update_url = reverse(
"api-v1:features:featurestates-detail", args=[feature_state]
)
response = client.put(
fs_update_url, data=json.dumps(data), content_type="application/json"
)
return response.json()

0 comments on commit 789ac8b

Please sign in to comment.