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

Commit

Permalink
CPA-282
Browse files Browse the repository at this point in the history
* [x] Visualization views test with model create
* [x] modified red flag import using only country code
* [x] Fixes test case in management command
* [x] Refactored red flag command
* [x] Check for invalid country code
* [x] Refactored buyer and supplier filter api
* [x] Country map view refactored
  • Loading branch information
bikramtuladhar committed May 3, 2021
1 parent 4f352c8 commit eadbd8e
Show file tree
Hide file tree
Showing 25 changed files with 385 additions and 285 deletions.
40 changes: 40 additions & 0 deletions country/management/commands/evaluate_contract_red_flag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.core.management.base import BaseCommand

from country.models import Country, Tender
from country.tasks import clear_redflag, process_redflag, process_redflag6, process_redflag7


class Command(BaseCommand):
help = "Compute red flag for specific country"

def add_arguments(self, parser):
parser.add_argument("country", nargs="+", type=str)

def handle(self, *args, **options):
country_code = options["country"][0]
country_code = str(country_code).upper()
country_list = Country.objects.exclude(country_code_alpha_2="gl").values_list(
"country_code_alpha_2", flat=True
)

if country_code not in country_list:
self.stderr.write("Country code is invalid.")
return

country = Country.objects.get(country_code_alpha_2=country_code)

self.stdout.write(f"Processing Red flag for {country.name}")

country_tenders = Tender.objects.filter(
country_id=country.id, supplier__isnull=False, buyer__isnull=False
).values("id", "buyer__buyer_name", "supplier__supplier_name", "supplier__supplier_address")

for tender in country_tenders:
tender_id = tender["id"]
self.stdout.write("Created task for id :" + str(tender_id))
clear_redflag.apply_async(args=(tender_id,), queue="covid19")
process_redflag6.apply_async(args=(tender_id,), queue="covid19")
process_redflag7.apply_async(args=(tender_id,), queue="covid19")
process_redflag.apply_async(args=(tender_id,), queue="covid19")

self.stdout.write("Done")
30 changes: 0 additions & 30 deletions country/management/commands/import_red_flag.py

This file was deleted.

1 change: 1 addition & 0 deletions country/tests/commands/test_fetch_covid_active_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def assertCommand(self, *args, expected_out="", expected_err="", **kwargs):
self.assertEqual(out.getvalue(), dedent(expected_out))

def test_command(self):
Country.objects.all().delete()
Country.objects.create(name="Global", country_code="GLO", country_code_alpha_2="gl", currency="USD")
Country.objects.create(name="Kenya", country_code="KEN", country_code_alpha_2="KE", currency="KES")

Expand Down
179 changes: 125 additions & 54 deletions visualization/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,120 @@
from django.core.management import call_command
from django.test import TestCase
from django.urls import reverse
from rest_framework import status

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


def setUpModule():
call_command("loaddata", "country", "redflag", "equitycategory", "equitykeywords")
Language.objects.create(
name="english",
code="en",
)
Topic.objects.create(title="corona virus")
supplier = Supplier.objects.create(
supplier_id="1",
supplier_name="sample supplier",
supplier_address="kathmandu",
)

buyer = Buyer.objects.create(
buyer_id="1",
buyer_name="sample buyer",
buyer_address="kathmandu",
)

Tender.objects.create(
country=Country.objects.all().first(),
supplier=supplier,
buyer=buyer,
contract_id=1,
contract_date="2021-01-01",
procurement_procedure="open",
status="active",
link_to_contract="http://test.com",
link_to_tender="http://test.com",
data_source="http://test.com",
no_of_bidders=1,
contract_title="test",
contract_value_local=1.0,
contract_value_usd=1.0,
contract_desc="test description",
tender_value_local=1.0,
tender_value_usd=1.0,
award_value_local=1.0,
award_value_usd=1.0,
)

CountryPartner.objects.create(
name="united kingdom",
description="country description",
country=Country.objects.all().first(),
email="country@email.com",
website="example.com",
logo="country/partner/logo/gl.jpg",
)


class VisualizationViewTest(TestCase):
def setUp(self):
self.total_contract_list_url = "total_contracts"
self.total_contracts_url = "total_contracts"
self.total_spending_url = "total_spending"
self.average_bids_url = "average_bids"
self.world_map_race_url = "world_map_race"
self.top_suppliers_url = "top_suppliers"
self.top_buyers_url = "top_buyers"
self.direct_open_url = "direct_open"
self.buyer_detail_url = "buyer_detail"
self.buyer_summary_url = "buyer_summary"
self.buyer_trend_url = "buyer_trend"
self.contract_red_flags_url = "contract_red_flags"
self.contract_status_url = "contract_status"
self.quantity_correlation_url = "quantity_correlation"
self.monopolization_url = "monopolization"
self.country_suppliers_url = "country_suppliers"
self.country_map_url = "country_map"
self.world_map_url = "world_map"
self.country_map_api_url = "country_map_api"
self.global_suppliers_url = "global_suppliers"
self.product_distribution_url = "product_distribution"
self.equity_indicators_url = "equity_indicators"
self.product_timeline_url = "product_timeline"
self.product_timeline_race_url = "product_timeline_race"
self.supplier_detail_url = "supplier_detail"
self.buyer_detail_url = "buyer_detail"
self.country_map_url = "country_map"
self.country_partners_url = "country_partners"
self.country_suppliers_url = "country_suppliers"
self.data_providers_url = "data_providers"
self.buyer_summary_url = "buyer_summary"
self.supplier_summary_url = "supplier_summary"
self.filter_parameters_url = "filter_parameters"
self.product_summary_url = "product_summary"
self.direct_open_contract_trend_url = "direct_open_contract_trend"
self.direct_open_url = "direct_open"
self.equity_indicators_url = "equity_indicators"
self.equity_summary_url = "equity_summary"
self.products_url = "products"
self.filters_parameters_suppliers_url = "filters_parameters_suppliers"
self.filter_parameters_url = "filter_parameters"
self.filters_parameters_buyers_url = "filters_parameters_buyers"
self.filters_parameters_static_url = "filters_parameters_static"
self.filters_parameters_suppliers_url = "filters_parameters_suppliers"
self.global_suppliers_url = "global_suppliers"
self.monopolization_url = "monopolization"
self.product_distribution_url = "product_distribution"
self.product_flow_view_url = "product_flow_view"
self.product_spending_comparison_url = "product_spending_comparison"
self.buyer_trend_url = "buyer_trend"
self.supplier_trend_url = "supplier_trend"
self.direct_open_contract_trend_url = "direct_open_contract_trend"
self.contract_red_flags_url = "contract_red_flags"
self.product_summary_url = "product_summary"
self.product_timeline_race_url = "product_timeline_race"
self.product_timeline_url = "product_timeline"
self.products_url = "products"
self.quantity_correlation_url = "quantity_correlation"
self.red_flag_summary_url = "red_flag_summary"
self.product_flow_view_url = "product_flow_view"
self.supplier_detail_url = "supplier_detail"
self.supplier_flow_view_url = "supplier_flow_view"
self.supplier_summary_url = "supplier_summary"
self.supplier_trend_url = "supplier_trend"
self.top_buyers_url = "top_buyers"
self.top_suppliers_url = "top_suppliers"
self.total_contract_list_url = "total_contracts"
self.total_contracts_url = "total_contracts"
self.total_spending_url = "total_spending"
self.world_map_race_url = "world_map_race"
self.world_map_url = "world_map"

def test_total_contracts_GET(self):
response = self.client.get(reverse(self.total_contracts_url))
url = "%s?country=KE&buyer=1&supplier=1" % reverse(self.total_contracts_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_total_spending_GET(self):
url = "%s?country=GB&buyer=1&supplier=1" % reverse(self.total_spending_url)
url = "%s?country=KE&buyer=1&supplier=1" % reverse(self.total_spending_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_average_bids_GET(self):
response = self.client.get(reverse(self.average_bids_url))
url = "%s?country=KE&buyer=1" % reverse(self.average_bids_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_world_map_race_GET(self):
Expand All @@ -73,31 +130,32 @@ def test_top_buyers_GET(self):
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_direct_open_GET(self):
response = self.client.get(reverse(self.direct_open_url))
url = "%s?country=KE&buyer=1&supplier=1" % 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=GB&buyer=1" % reverse(self.contract_status_url)
url = "%s?country=KE&buyer=1" % reverse(self.contract_status_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

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

def test_monopolization_GET(self):
url = "%s?country=GB&buyer=1" % reverse(self.monopolization_url)
url = "%s?country=KE&buyer=1" % reverse(self.monopolization_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

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

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

Expand All @@ -107,26 +165,31 @@ def test_world_map_GET(self):
self.assertEquals(response.status_code, status.HTTP_200_OK)

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

def test_invalid_country_map_api_GET(self):
url = "%s?country=AB" % reverse(self.country_map_api_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)

def test_global_suppliers_GET(self):
response = self.client.get(reverse(self.global_suppliers_url))
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_product_distribution_GET(self):
url = "%s?country=GB&buyer=1" % reverse(self.product_distribution_url)
url = "%s?country=KE&buyer=1" % reverse(self.product_distribution_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_equity_indicators_GET(self):
url = "%s?country=GB&buyer=1" % reverse(self.equity_indicators_url)
url = "%s?country=KE&buyer=1" % reverse(self.equity_indicators_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_product_timeline_GET(self):
url = "%s?country=GB&buyer=1&supplier=1" % reverse(self.product_timeline_url)
url = "%s?country=KE&buyer=1&supplier=1" % reverse(self.product_timeline_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

Expand All @@ -139,26 +202,30 @@ def test_supplier_detail_GET(self):
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_buyer_detail_GET(self):
response = self.client.get(reverse(self.buyer_detail_url, args=["1"]))
response = self.client.get(reverse(self.buyer_detail_url, args=[1]))
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_buyer_detail_failed_GET(self):
response = self.client.get(reverse(self.buyer_detail_url, args=[99999]))
self.assertEquals(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)

def test_country_partners_GET(self):
url = "%s?country=GB" % reverse(self.country_partners_url)
url = "%s?country=KE" % 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=GB" % reverse(self.data_providers_url)
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_buyer_summary_GET(self):
url = "%s?country=GB" % reverse(self.buyer_summary_url)
url = "%s?country=gl" % reverse(self.buyer_summary_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

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

Expand All @@ -167,25 +234,28 @@ def test_filter_parameters_GET(self):
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_product_summary_GET(self):
response = self.client.get(reverse(self.product_summary_url))
url = "%s?country=KE" % reverse(self.product_summary_url)
response = self.client.get(url)
print(response.json())
self.assertEquals(response.status_code, status.HTTP_200_OK)

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

def test_products_GET(self):
response = self.client.get(reverse(self.products_url))
url = "%s?country=KE&buyer=1" % reverse(self.products_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_filters_parameters_suppliers_GET(self):
url = "%s?country=GB&buyer=1" % reverse(self.filters_parameters_suppliers_url)
url = "%s?country=KE&buyer=1" % reverse(self.filters_parameters_suppliers_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_filters_parameters_buyers_GET(self):
url = "%s?country=GB&supplier=1" % reverse(self.filters_parameters_buyers_url)
url = "%s?country=KE&supplier=1" % reverse(self.filters_parameters_buyers_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

Expand All @@ -211,7 +281,8 @@ def test_direct_open_contract_trend_GET(self):
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_contract_red_flags_GET(self):
response = self.client.get(reverse(self.contract_red_flags_url))
url = "%s?country=KE&buyer=1&supplier=1&product=1" % reverse(self.contract_red_flags_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

def test_red_flag_summary_GET(self):
Expand Down
Loading

0 comments on commit eadbd8e

Please sign in to comment.