Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
test-coverage-2021-05-03
Browse files Browse the repository at this point in the history
* [x] Added general utility test
* [x] Refactored buyer_trend_views
* [x] refactored contract_status_view.py
* [x] refactored country_partner_view.py
* [x] refactored direct_open.py
* [x] Refactored equity_summary_view.py, filter_parameters_buyers.py, product_timeline_race_view.py
  • Loading branch information
bikramtuladhar committed May 4, 2021
1 parent c3c59b8 commit 8e8836b
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 272 deletions.
4 changes: 4 additions & 0 deletions visualization/helpers/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@


def page_expire_period(page_name=None, default_period=settings.CACHES["default"]["EXPIRE_PERIOD"]):
# DEBUG = True
if settings.DEBUG:
return 1

# list page name it's expire value
# {'page_name':1233}
page_list = {}
Expand Down
Empty file.
32 changes: 32 additions & 0 deletions visualization/tests/helper/test_general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.conf import settings
from django.test import SimpleTestCase
from django.test.utils import override_settings

from visualization.helpers.general import page_expire_period
from visualization.views.lib.general import add_filter_args


class TestGeneral(SimpleTestCase):
@override_settings(DEBUG=False)
def test_page_expire_period(self):
default_timeout = settings.CACHES["default"]["EXPIRE_PERIOD"]
time = page_expire_period()
self.assertEquals(time, default_timeout)

@override_settings(DEBUG=False)
def test_page_expire_period_specific_page(self):
default_timeout = settings.CACHES["default"]["EXPIRE_PERIOD"]
time = page_expire_period("sample_page")
self.assertEquals(time, default_timeout)

def test_add_filter_args(self):
result = add_filter_args("foo", "bar", {})
self.assertEquals(result, {"foo__id": "bar"})

def test_add_filter_args_notnull(self):
result = add_filter_args("foo", "notnull", {})
self.assertEquals(result, {"foo__isnull": False})

def test_add_filter_args_append_only(self):
result = add_filter_args("foo", "bar", {}, append_only=True)
self.assertEquals(result, {"foo": "bar"})
27 changes: 25 additions & 2 deletions visualization/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rest_framework import status

from content.models import CountryPartner
from country.models import Buyer, Country, Language, Supplier, Tender, Topic
from country.models import Buyer, Country, DataProvider, Language, Supplier, Tender, Topic


def setUpModule():
Expand Down Expand Up @@ -51,12 +51,20 @@ def setUpModule():
CountryPartner.objects.create(
name="united kingdom",
description="country description",
country=Country.objects.all().first(),
country=Country.objects.get(country_code_alpha_2="KE"),
email="country@email.com",
website="example.com",
logo="country/partner/logo/gl.jpg",
)

DataProvider.objects.create(
name="",
country=Country.objects.get(country_code_alpha_2="KE"),
website="example.com",
logo="country/partner/logo/gl.jpg",
remark="country description",
)


class VisualizationViewTest(TestCase):
def setUp(self):
Expand Down Expand Up @@ -134,6 +142,11 @@ def test_direct_open_GET(self):
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_direct_open_all_GET(self):
url = reverse(self.direct_open_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_contract_status_GET(self):
url = "%s?country=KE&buyer=1" % reverse(self.contract_status_url)
response = self.client.get(url)
Expand Down Expand Up @@ -214,11 +227,21 @@ def test_country_partners_GET(self):
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_all_country_partners_GET(self):
url = reverse(self.country_partners_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_data_providers_GET(self):
url = "%s?country=KE" % reverse(self.data_providers_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_data_providers_all_GET(self):
url = reverse(self.data_providers_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_buyer_summary_GET(self):
url = "%s?country=gl" % reverse(self.buyer_summary_url)
response = self.client.get(url)
Expand Down
37 changes: 18 additions & 19 deletions visualization/views/buyer_trend_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ def get(self, request):
)
countries = Country.objects.all()
for i in count:
result = {}
end_date = i["month"] + dateutil.relativedelta.relativedelta(months=1)
start_date = i["month"]
result["details"] = []
result["month"] = str(start_date.year) + "-" + str(start_date.month)
result = {"details": [], "month": str(start_date.year) + "-" + str(start_date.month)}
for j in countries:
b = {}
tender = Tender.objects.filter(
country__country_code_alpha_2=j.country_code_alpha_2,
contract_date__gte=start_date,
Expand All @@ -41,34 +38,36 @@ def get(self, request):
total_buyer_count=Count("buyer__id", distinct=True),
amount_usd=Sum("goods_services__contract_value_usd"),
)
b["country"] = j.name
b["country_code"] = j.country_code_alpha_2
b["country_continent"] = j.continent

b = {"country": j.name, "country_code": j.country_code_alpha_2, "country_continent": j.continent}
buyer_count = tender["total_buyer_count"]
if tender["amount_usd"] is None:
tender_val = 0
else:

tender_val = 0
if tender["amount_usd"] is not None:
tender_val = tender["amount_usd"]
if buyer_count is None:
buyer_val = 0
else:

buyer_val = 0
if buyer_count is not None:
buyer_val = buyer_count

temp[j.name] = tender_val
b["amount_usd"] = tender_val

if bool(temp) and j.name in temp.keys():
current_val = temp[j.name]
cum_value = current_val + tender_val
temp[j.name] = cum_value
b["amount_usd"] = cum_value
else:
temp[j.name] = tender_val
b["amount_usd"] = tender_val

tender_temp[j.name] = buyer_val
b["buyer_count"] = buyer_val

if bool(tender_temp) and j.name in tender_temp.keys():
current_val = tender_temp[j.name]
cum_value = current_val + buyer_val
tender_temp[j.name] = cum_value
b["buyer_count"] = cum_value
else:
tender_temp[j.name] = buyer_val
b["buyer_count"] = buyer_val

result["details"].append(b)
data.append(result)
return JsonResponse({"result": data})
84 changes: 29 additions & 55 deletions visualization/views/contract_status_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from rest_framework import status as status_code
from rest_framework.views import APIView

from country.models import Country, Tender
Expand All @@ -19,70 +20,43 @@ def get(self, request):
filter_args = {}
result = list()
currency_code = ""
status = ["active", "completed", "cancelled", "not_identified"]
country = self.request.GET.get("country", None)

country_code = self.request.GET.get("country", None)
buyer = self.request.GET.get("buyer")

if country:
filter_args["country__country_code_alpha_2"] = country
if country_code:
country_code = str(country_code).upper()
filter_args["country__country_code_alpha_2"] = country_code

if buyer:
filter_args = add_filter_args("buyer", buyer, filter_args)

# Status code wise grouped sum of contract value
contract_value_local_sum = (
Tender.objects.filter(**filter_args)
.values("status")
.annotate(sum=Sum("goods_services__contract_value_local"))
)
contract_value_usd_sum = (
if country_code:
try:
country_res = Country.objects.get(country_code_alpha_2=country_code)
currency_code = country_res.currency
except Exception as e:
return JsonResponse({"error": str(e)}, safe=False, status=status_code.HTTP_500_INTERNAL_SERVER_ERROR)

status_query_set = (
Tender.objects.filter(**filter_args)
.values("status")
.annotate(sum=Sum("goods_services__contract_value_usd"))
.annotate(
amount_usd=Sum("contract_value_usd"),
amount_local=Sum("contract_value_local"),
tender_count=Count("id"),
)
)

# Status code wise grouped total number of contracts
grouped_contract_no = Tender.objects.filter(**filter_args).values("status").annotate(count=Count("id"))

if country:
try:
country_res = Country.objects.get(country_code_alpha_2=country)
currency_code = country_res.currency
except Exception as e:
print(e)
for status in status_query_set:
data = {
"amount_local": status["amount_local"],
"amount_usd": status["amount_usd"],
"tender_count": status["tender_count"],
"local_currency_code": currency_code,
"status": status["status"],
}

status_in_result = [i["status"] for i in contract_value_local_sum]
for i in range(len(status)):
if status[i] in status_in_result:
result.append(
{
"amount_local": [
each["sum"] if each["sum"] else 0
for each in contract_value_local_sum
if status[i] == each["status"]
][0],
"amount_usd": [
each["sum"] if each["sum"] else 0
for each in contract_value_usd_sum
if status[i] == each["status"]
][0],
"tender_count": [
each["count"] if each["count"] else 0
for each in grouped_contract_no
if status[i] == each["status"]
][0],
"local_currency_code": currency_code,
"status": status[i],
}
)
else:
result.append(
{
"amount_local": 0,
"amount_usd": 0,
"tender_count": 0,
"local_currency_code": currency_code,
"status": status[i],
}
)
result.append(data)

return JsonResponse(result, safe=False)
44 changes: 25 additions & 19 deletions visualization/views/country_partner_view.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from rest_framework import status
from rest_framework.views import APIView

from content.models import CountryPartner
Expand All @@ -11,26 +12,31 @@ class CountryPartnerView(APIView):
@method_decorator(cache_page(page_expire_period()))
def get(self, request):
filter_args = {}
country = self.request.GET.get("country", None)
if country:
filter_args["country__country_code_alpha_2"] = country
country_code = self.request.GET.get("country", None)

if country_code:
country_code = str(country_code).upper()
filter_args["country__country_code_alpha_2"] = country_code

try:
data_provider = CountryPartner.objects.filter(**filter_args)
result = []

if data_provider:
for i in data_provider:
data = {
"name": i.name,
"description": i.description,
"email": i.email,
"website": i.website,
"logo": str(i.logo),
"order": i.order,
"country": str(i.country),
}
result.append(data)

return JsonResponse(result, safe=False, status=status.HTTP_200_OK)

except Exception:
data_provider = [{"error": "Country partner does not exist for this country"}]
result = []
if data_provider:
for i in data_provider:
data = {
"name": i.name,
"description": i.description,
"email": i.email,
"website": i.website,
"logo": str(i.logo),
"order": i.order,
"country": str(i.country),
}
result.append(data)
else:
result = {"error": "Country Partner not found for this country"}
return JsonResponse(result, safe=False)
return JsonResponse(result, safe=False, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
42 changes: 24 additions & 18 deletions visualization/views/data_provider_view.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from rest_framework import status
from rest_framework.views import APIView

from country.models import DataProvider
Expand All @@ -11,24 +12,29 @@ class DataProviderView(APIView):
@method_decorator(cache_page(page_expire_period()))
def get(self, request):
filter_args = {}
country = self.request.GET.get("country", None)
if country:
filter_args["country__country_code_alpha_2"] = country
country_code = self.request.GET.get("country", None)

if country_code:
filter_args["country__country_code_alpha_2"] = country_code

try:
data_provider = DataProvider.objects.filter(**filter_args)
result = []

if data_provider:
for i in data_provider:
data = {
"name": i.name,
"country": str(i.country),
"website": i.website,
"logo": str(i.logo),
"remark": i.remark,
}
result.append(data)

return JsonResponse(result, safe=False, status=status.HTTP_200_OK)

except Exception:
data_provider = [{"error": "Data Provider does not exist for this country"}]
result = []
if data_provider:
for i in data_provider:
data = {
"name": i.name,
"country": str(i.country),
"website": i.website,
"logo": str(i.logo),
"remark": i.remark,
}
result.append(data)
else:
result = {"error": "Data Provider not found for this country"}
return JsonResponse(result, safe=False)
error = {"error": "Data Provider not found for this country"}

return JsonResponse(error, safe=False, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Loading

0 comments on commit 8e8836b

Please sign in to comment.