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

Adjust API to project model #214

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
71af785
feat: Add Project model to internal app
lucaslinhares Feb 8, 2023
a6be1d1
feat: Create and test GlobalSerializer
Sandro-Meireles Feb 8, 2023
485cd20
feat: create GlobalViewSet with Create, Retrieve, Destroy and List en…
Sandro-Meireles Feb 8, 2023
d4e2020
feat: Add globals URls on internal app
Sandro-Meireles Feb 8, 2023
b196e98
feat: Adjust Statistic app to new Project model.
lucaslinhares Feb 13, 2023
ab69f34
feat: Adjust Users app to new Project model.
lucaslinhares Feb 13, 2023
916dece
feat: Adjust Classifier app to new Project model.
lucaslinhares Feb 14, 2023
b8ea1bc
fix: Remove prints in classifier tests.
lucaslinhares Feb 14, 2023
08aabed
feat: Adjust Flows app to new Project model and update classifier view.
lucaslinhares Feb 14, 2023
7984096
feat: Adjust Orgs app to new Project model.
lucaslinhares Feb 14, 2023
51e0920
fix: Fix a error in destroy function
lucaslinhares Feb 16, 2023
808991d
fix: Fix some errors and adjust apps in nem project model
lucaslinhares Feb 16, 2023
42898b0
fix: Correct POST in flows, add migrations and adjust return after PA…
lucaslinhares Mar 1, 2023
dda5c2d
fix: Fix error in user view
lucaslinhares Mar 2, 2023
8a58b1d
fix: update name project to org in serializers
lucaslinhares Mar 3, 2023
06d0837
feat: add readme file and update tests.
lucaslinhares Mar 14, 2023
fd2449b
fix: Adjust some tests and use black in files
lucaslinhares Mar 15, 2023
4ffe436
feat: Add insert many objects function
lucaslinhares Mar 16, 2023
eedbba1
fix: Adjust serializers and test in internal/channel
lucaslinhares Mar 21, 2023
e6e084c
fix: adjust analytics_api tests.
lucaslinhares Mar 23, 2023
90564f8
Merge branch 'feature/global-internal-endpoints' into feature/project…
lucaslinhares Mar 24, 2023
e7e84e1
Merge branch 'main' into feature/project-adjust-api
lucaslinhares Apr 7, 2023
c68c8db
revert black in channels files and migration file
lucaslinhares Apr 11, 2023
ab81efe
revert black in channels files and migration file
lucaslinhares Apr 11, 2023
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
151 changes: 77 additions & 74 deletions weni/analytics_api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def get_response(self, **query_params):
url = self.reverse(self.get_url_namespace(), query_params=query_params)
token = APIToken.get_or_create(self.org, self.admin, Group.objects.get(name="Administrators"))

return self.client.get(url, HTTP_AUTHORIZATION=f"Token {token.key}")
with self.mockReadOnly(assert_models=None):
return self.client.get(url, HTTP_AUTHORIZATION=f"Token {token.key}")

@abstractmethod
def get_url_namespace(self):
Expand All @@ -48,6 +49,7 @@ def setUp(self, mr_mocks):
for contact in range(0, 10):
contact_name = "Joe Blow " + str(contact)
created_contact = self.create_contact(contact_name)

self.group2.contacts.add(created_contact)

# create a contact without group
Expand Down Expand Up @@ -89,18 +91,19 @@ def get_url_namespace(self):

def test_total_contacts(self):
response = self.get_response()
self.assertEqual(response.json().get("total"), 28)

self.assertEqual(response.data.get("total"), 28)

def test_contacts_by_status(self):
response = self.get_response()
self.assertEqual(response.json().get("current").get("actives"), 13)
self.assertEqual(response.json().get("current").get("blocked"), 5)
self.assertEqual(response.json().get("current").get("stopped"), 5)
self.assertEqual(response.json().get("current").get("archived"), 5)
self.assertEqual(response.data.get("current").get("actives"), 13)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change from response.json() to response.data?

self.assertEqual(response.data.get("current").get("blocked"), 5)
self.assertEqual(response.data.get("current").get("stopped"), 5)
self.assertEqual(response.data.get("current").get("archived"), 5)

def test_by_date(self):
response = self.get_response()
contacts = response.json()["by_date"]
contacts = response.data["by_date"]

today = format_date(tz.now())
yesterday = format_date(tz.now() - tz.timedelta(1))
Expand All @@ -112,52 +115,52 @@ def test_by_date(self):

def test_group_filter(self):
response = self.get_response(group=self.group2.uuid)
self.assertEqual(response.json().get("total"), 10)
self.assertEqual(response.data.get("total"), 10)

def test_non_existent_group_filter(self):
random_uuid = uuid1()
response = self.get_response(group=random_uuid)
self.assertEqual(response.json().get("total"), 0)
self.assertEqual(response.data.get("total"), 0)

def test_deleted_contacts(self):
response = self.get_response(deleted=True)
self.assertEqual(response.json().get("total"), 3)
self.assertEqual(response.data.get("total"), 3)

def test_after_and_before(self):
response = self.get_response()
self.assertEqual(response.json().get("total"), 28)
self.assertEqual(response.json().get("current").get("actives"), 13)
self.assertEqual(response.json().get("current").get("blocked"), 5)
self.assertEqual(response.json().get("current").get("stopped"), 5)
self.assertEqual(response.json().get("current").get("archived"), 5)
self.assertEqual(response.data.get("total"), 28)
self.assertEqual(response.data.get("current").get("actives"), 13)
self.assertEqual(response.data.get("current").get("blocked"), 5)
self.assertEqual(response.data.get("current").get("stopped"), 5)
self.assertEqual(response.data.get("current").get("archived"), 5)

response = self.get_response(after=format_date(tz.now()))
self.assertEqual(response.json().get("total"), 26)
self.assertEqual(response.json().get("current").get("actives"), 11)
self.assertEqual(response.json().get("current").get("blocked"), 5)
self.assertEqual(response.json().get("current").get("stopped"), 5)
self.assertEqual(response.json().get("current").get("archived"), 5)
self.assertEqual(response.data.get("total"), 26)
self.assertEqual(response.data.get("current").get("actives"), 11)
self.assertEqual(response.data.get("current").get("blocked"), 5)
self.assertEqual(response.data.get("current").get("stopped"), 5)
self.assertEqual(response.data.get("current").get("archived"), 5)

response = self.get_response(before=format_date(tz.now()))
self.assertEqual(response.json().get("total"), 2)
self.assertEqual(response.json().get("current").get("actives"), 2)
self.assertEqual(response.json().get("current").get("blocked"), 0)
self.assertEqual(response.json().get("current").get("stopped"), 0)
self.assertEqual(response.json().get("current").get("archived"), 0)
self.assertEqual(response.data.get("total"), 2)
self.assertEqual(response.data.get("current").get("actives"), 2)
self.assertEqual(response.data.get("current").get("blocked"), 0)
self.assertEqual(response.data.get("current").get("stopped"), 0)
self.assertEqual(response.data.get("current").get("archived"), 0)

response = self.get_response(before=format_date(tz.now() - tz.timedelta(1)))
self.assertEqual(response.json().get("total"), 1)
self.assertEqual(response.json().get("current").get("actives"), 1)
self.assertEqual(response.json().get("current").get("blocked"), 0)
self.assertEqual(response.json().get("current").get("stopped"), 0)
self.assertEqual(response.json().get("current").get("archived"), 0)
self.assertEqual(response.data.get("total"), 1)
self.assertEqual(response.data.get("current").get("actives"), 1)
self.assertEqual(response.data.get("current").get("blocked"), 0)
self.assertEqual(response.data.get("current").get("stopped"), 0)
self.assertEqual(response.data.get("current").get("archived"), 0)

response = self.get_response(before=format_date(tz.now() - tz.timedelta(7)))
self.assertEqual(response.json().get("total"), 0)
self.assertEqual(response.json().get("current").get("actives"), 0)
self.assertEqual(response.json().get("current").get("blocked"), 0)
self.assertEqual(response.json().get("current").get("stopped"), 0)
self.assertEqual(response.json().get("current").get("archived"), 0)
self.assertEqual(response.data.get("total"), 0)
self.assertEqual(response.data.get("current").get("actives"), 0)
self.assertEqual(response.data.get("current").get("blocked"), 0)
self.assertEqual(response.data.get("current").get("stopped"), 0)
self.assertEqual(response.data.get("current").get("archived"), 0)


class AnalyticsFlowRunTest(TembaTest, TembaRequestMixin):
Expand Down Expand Up @@ -197,32 +200,32 @@ def get_url_namespace(self):

def test_return_is_a_dict(self):
response = self.get_response()
self.assertIsInstance(response.json(), dict)
self.assertIsInstance(response.data, dict)

def test_flow_name_as_key(self):
response = self.get_response()
self.assertEqual(list(response.json().keys())[0], "flow_1")
self.assertIsNone(response.json().get("non_existent_flow", None))
self.assertEqual(list(response.data.keys())[0], "flow_1")
self.assertIsNone(response.data.get("non_existent_flow", None))

def test_uuid_is_returned(self):
response = self.get_response()
self.assertEqual(response.json().get(self.flow.name).get("uuid"), self.flow.uuid)
self.assertEqual(response.data.get(self.flow.name).get("uuid"), self.flow.uuid)

def test_has_stats(self):
response = self.get_response()
self.assertIsNotNone(response.json().get(self.flow.name).get("stats", None))
self.assertIsNotNone(response.data.get(self.flow.name).get("stats", None))

def test_stats_is_a_dict(self):
response = self.get_response()
self.assertIsInstance(response.json().get(self.flow.name).get("stats"), dict)
self.assertIsInstance(response.data.get(self.flow.name).get("stats"), dict)

def test_total(self):
response = self.get_response()
self.assertEqual(response.json().get(self.flow.name).get("stats").get("total"), 6)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("total"), 6)

def test_by_status(self):
response = self.get_response()
by_status = response.json().get(self.flow.name).get("stats").get("by_status")
by_status = response.data.get(self.flow.name).get("stats").get("by_status")
self.assertEqual(by_status.get("active"), 1)
self.assertEqual(by_status.get("waiting"), 1)
self.assertEqual(by_status.get("completed"), 1)
Expand All @@ -232,48 +235,48 @@ def test_by_status(self):

def test_filter_after_and_before(self):
response = self.get_response(after=format_date(tz.now()))
self.assertEqual(response.json().get(self.flow.name).get("stats").get("total"), 4)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("active"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("interrupted"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("waiting"), 1)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("completed"), 1)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("expired"), 1)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("failed"), 1)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("total"), 4)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("active"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("interrupted"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("waiting"), 1)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("completed"), 1)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("expired"), 1)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("failed"), 1)

response = self.get_response(before=format_date(tz.now()))
self.assertEqual(response.json().get(self.flow.name).get("stats").get("total"), 2)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("active"), 1)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("interrupted"), 1)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("waiting"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("completed"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("expired"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("failed"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("total"), 2)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("active"), 1)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("interrupted"), 1)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("waiting"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("completed"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("expired"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("failed"), 0)

response = self.get_response(
after=format_date(tz.now() - tz.timedelta(3)), before=format_date(tz.now() - tz.timedelta(2))
)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("total"), 1)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("active"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("interrupted"), 1)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("waiting"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("completed"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("expired"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("failed"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("total"), 1)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("active"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("interrupted"), 1)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("waiting"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("completed"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("expired"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("failed"), 0)

response = self.get_response(
after=format_date(tz.now() - tz.timedelta(7)), before=format_date(tz.now() - tz.timedelta(6))
)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("total"), 1)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("active"), 1)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("interrupted"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("waiting"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("completed"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("expired"), 0)
self.assertEqual(response.json().get(self.flow.name).get("stats").get("by_status").get("failed"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("total"), 1)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("active"), 1)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("interrupted"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("waiting"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("completed"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("expired"), 0)
self.assertEqual(response.data.get(self.flow.name).get("stats").get("by_status").get("failed"), 0)

def test_filter_by_flow_uuid(self):
response = self.get_response(flow_uuid=self.flow.uuid)
self.assertEqual(len(response.json()), 1)
self.assertEqual(list(response.json().keys())[0], "flow_1")
self.assertEqual(len(response.data), 1)
self.assertEqual(list(response.data.keys())[0], "flow_1")
response = self.get_response(flow_uuid="00000000-0000-0000-0000-000000000000") # fake uuid
self.assertEqual(response.json(), {})
self.assertEqual(response.data, {})
3 changes: 1 addition & 2 deletions weni/analytics_api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.db.models import Count, Prefetch, Q
from django.urls import reverse
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response

from temba.api.v2.views_base import BaseAPIView, ListAPIMixin
Expand Down Expand Up @@ -133,7 +132,7 @@ class FlowRunAnalyticsEndpoint(BaseAPIView, ListAPIMixin):

## List Analytics Flow Runs data

A **GET** returns analytical data related to flows, containing information about the type
A **GET** returns analytical data related to flows, containing information about the type
of runs and being able to segment by date

* **flow_uuid** - A flow UUID to filter by, ex: f5901b62-ba76-4003-9c62-72fdacc1b7b7.
Expand Down
8 changes: 7 additions & 1 deletion weni/grpc/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rest_framework import serializers

from temba.orgs.models import Org
from weni.internal.models import Project


class UserEmailRelatedField(relations.SlugRelatedField):
Expand All @@ -15,6 +16,11 @@ def __init__(self, **kwargs):
super().__init__(slug_field="uuid", queryset=Org.objects.all(), **kwargs)


class ProjectUUIDRelatedField(relations.SlugRelatedField):
def __init__(self, **kwargs):
super().__init__(slug_field="project_uuid", queryset=Project.objects.all(), **kwargs)


class SerializerMethodCharField(serializers.CharField):
"""
Read and write class, using custom methods.
Expand All @@ -28,7 +34,7 @@ def __init__(self, method_name=None, **kwargs):
def bind(self, field_name, parent):
# The method name defaults to `get_{field_name}`.
if self.method_name is None:
self.method_name = 'get_{field_name}'.format(field_name=field_name)
self.method_name = "get_{field_name}".format(field_name=field_name)

super().bind(field_name, parent)

Expand Down
Loading