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-05
Browse files Browse the repository at this point in the history
* [x] Added debugbar and enabled for json response
* [x] Added 404 views def test
* [x] Updated requirement dev txt
* [x] country view test added
* [x] Added test_import_tender_from_id.py test_generate_excel_summary.py test_generate_country_contract_excel.py test_evaluate_contract_red_flag.py test
* [x] refactored import_tender_from_id.py
  • Loading branch information
bikramtuladhar committed May 6, 2021
1 parent 0b8105e commit 46466e4
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 19 deletions.
2 changes: 1 addition & 1 deletion country/management/commands/import_tender_from_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def handle(self, *args, **kwargs):
self.stdout.write(f"Fetching tender data for Batch id {batch_id} for country {country}")

try:
result = Country.objects.filter(name=country).first()
result = Country.objects.get(name=country)
country = result.name
currency = result.currency

Expand Down
16 changes: 16 additions & 0 deletions country/tests/commands/test_evaluate_contract_red_flag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.core.management import call_command
from django.core.management.base import CommandError
from django.test import TransactionTestCase

from country.models import Country


class EvaluateContractRedFlagTests(TransactionTestCase):
def test_without_country_code(self):
with self.assertRaises(CommandError):
call_command("evaluate_contract_red_flag")

def test_with_country_code(self):
Country.objects.all().delete()
Country.objects.create(name="Mexico", country_code="MEX", country_code_alpha_2="MX", currency="MXN")
self.assertEquals(call_command("evaluate_contract_red_flag", "mx"), None)
7 changes: 7 additions & 0 deletions country/tests/commands/test_fill_contract_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.core.management import call_command
from django.test import TransactionTestCase


class FillContractValuesTests(TransactionTestCase):
def test_command(self):
self.assertEquals(call_command("fill_contract_values"), None)
11 changes: 11 additions & 0 deletions country/tests/commands/test_generate_country_contract_excel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.core.management import CommandError, call_command
from django.test import TransactionTestCase


class GenerateCountryContractExcelTests(TransactionTestCase):
def test_command(self):
with self.assertRaises(CommandError):
call_command("generate_country_contract_excel")

def test_command_with_country(self):
self.assertEquals(call_command("generate_country_contract_excel", "MX"), None)
7 changes: 7 additions & 0 deletions country/tests/commands/test_generate_excel_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.core.management import call_command
from django.test import TransactionTestCase


class GenerateExcelSummaryTests(TransactionTestCase):
def test_command(self):
self.assertEquals(call_command("generate_excel_summary"), None)
8 changes: 8 additions & 0 deletions country/tests/commands/test_import_tender_from_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.core.management import CommandError, call_command
from django.test import TransactionTestCase


class TenderImportCommand(TransactionTestCase):
def test_command_without_country_batch(self):
with self.assertRaises(CommandError):
call_command("import_tender_from_id")
7 changes: 7 additions & 0 deletions country/tests/commands/test_process_currency_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.core.management import call_command
from django.test import TransactionTestCase


class ProcessCurrencyConversionCommandTest(TransactionTestCase):
def test_command(self):
self.assertEquals(call_command("process_currency_conversion"), None)
8 changes: 8 additions & 0 deletions country/tests/commands/test_save_database_equity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.core.management import CommandError, call_command
from django.test import TransactionTestCase


class SaveDatabaseEquityCommand(TransactionTestCase):
def test_command_without_country_batch(self):
with self.assertRaises(CommandError):
call_command("save_database_equity")
93 changes: 93 additions & 0 deletions country/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from django.test import TestCase
from django.urls import reverse
from rest_framework import status


class CountryViewTest(TestCase):
def setUp(self):
self.buyer_list_api_url = "BuyerView-list"
self.country_list_api_url = "country-list"
self.language_list_api_url = "language-list"
self.supplier_list_api_url = "SupplierView-list"
self.tender_list_api_url = "TenderView-list"
self.overview_summary_list_api_url = "OverallStatSummaryView-list"
self.country_choices_api_url = "country-choices"
self.data_edit_url = "data_edits"
self.data_imports_url = "data_imports"
self.data_validate_url = "data_validate"
self.data_delete_url = "data_delete"

def test_buyer_list_GET(self):
url = "%s?country=KE&buyer_name=buyer2&product=1" % reverse(self.buyer_list_api_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

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

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

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

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

def test_tender_list_GET(self):
url_string = "%s?&country=1&buyer=1&supplier=1&product=1&status=open&procurement_procedure=direct&title=title"
url_string += (
"&date_from=2020-01-01&date_to=2021-01-01&contract_value_usd=112&value_comparison=true&equity_id=1"
)
url = url_string % reverse(self.tender_list_api_url)

response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_200_OK)

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

def test_data_edit_GET(self):
url = "%s?data_import_id=1" % reverse(self.data_edit_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_302_FOUND)

def test_data_imports_GET(self):
url = "%s?country=1&data_import_id=1&validated=true" % reverse(self.data_imports_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_302_FOUND)

def test_data_imports_without_validate_GET(self):
url = "%s?country=1&data_import_id=1" % reverse(self.data_imports_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_302_FOUND)

def test_data_validate_GET(self):
url = "%s?data_import_id=1" % reverse(self.data_validate_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_302_FOUND)

def test_data_validate_without_import_id_GET(self):
url = reverse(self.data_validate_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_302_FOUND)

def test_data_delete_GET(self):
url = "%s?data_import_id=1" % reverse(self.data_delete_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_302_FOUND)

def test_data_delete_without_id_GET(self):
url = reverse(self.data_delete_url)
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_302_FOUND)
20 changes: 12 additions & 8 deletions country/views/delete_data_set_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

class DeleteDataSetView(APIView):
def get(self, request):
data_import_id = self.request.GET.get("data_import_id", None)
if data_import_id is not None:
data_import = DataImport.objects.get(page_ptr_id=data_import_id)
data_import.delete()
delete_dataset.apply_async(args=(data_import_id,), queue="covid19")
messages.info(request, "Your dataset has been successfully deleted from the system !!")
return HttpResponseRedirect("/admin/content/dataimport")
else:
try:
data_import_id = self.request.GET.get("data_import_id", None)
if data_import_id is not None:
data_import = DataImport.objects.get(page_ptr_id=data_import_id)
data_import.delete()
delete_dataset.apply_async(args=(data_import_id,), queue="covid19")
messages.info(request, "Your dataset has been successfully deleted from the system !!")
return HttpResponseRedirect("/admin/content/dataimport")
else:
messages.error(request, "Invalid data import id !!")
return HttpResponseRedirect("/admin/content/dataimport")
except Exception:
messages.error(request, "Invalid data import id !!")
return HttpResponseRedirect("/admin/content/dataimport")
25 changes: 25 additions & 0 deletions covidadmin/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json

from django.conf import settings
from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin


class NonHtmlDebugToolbarMiddleware(MiddlewareMixin):
def process_response(self, request, response):
debug = request.GET.get("debug", "UNSET")

if debug != "UNSET" and settings.DEBUG:
if response["Content-Type"] == "application/octet-stream":
new_content = "<html><body>Binary Data, " "Length: {}</body></html>".format(len(response.content))
response = HttpResponse(new_content)
elif response["Content-Type"] != "text/html":
content = response.content
try:
json_ = json.loads(content)
content = json.dumps(json_, sort_keys=True, indent=2)
except ValueError:
pass
response = HttpResponse("<html><body><pre>{}" "</pre></body></html>".format(content))

return response
12 changes: 3 additions & 9 deletions covidadmin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

import os
from pathlib import Path

Expand Down Expand Up @@ -54,7 +53,6 @@
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

Expand Down Expand Up @@ -99,7 +97,7 @@
"taggit",
"wagtail.api.v2",
"ckeditor",
# 'debug_toolbar',
"debug_toolbar",
]

MIDDLEWARE = [
Expand All @@ -114,7 +112,8 @@
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
"wagtail.contrib.redirects.middleware.RedirectMiddleware",
# 'debug_toolbar.middleware.DebugToolbarMiddleware',
"debug_toolbar.middleware.DebugToolbarMiddleware",
"covidadmin.middleware.NonHtmlDebugToolbarMiddleware",
]

ROOT_URLCONF = "covidadmin.urls"
Expand All @@ -137,7 +136,6 @@

WSGI_APPLICATION = "covidadmin.wsgi.application"


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

Expand All @@ -152,7 +150,6 @@
}
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

Expand All @@ -171,7 +168,6 @@
},
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

Expand All @@ -185,7 +181,6 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static/images/")]
Expand All @@ -204,7 +199,6 @@
"SERIALIZER_EXTENSIONS": dict(AUTO_OPTIMIZE=True),
}


CELERY_BROKER_URL = env("CELERY_BROKER_URL")
CELERY_TIMEZONE = env("CELERY_TIMEZONE")
CELERY_TASK_ROUTES = {
Expand Down
Empty file added covidadmin/tests/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions covidadmin/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.test import TestCase
from rest_framework import status


class CovidAdminViewTest(TestCase):
def setUp(self):
self.not_found_url = "404"

def test_not_found_GET(self):
url = "/api/v1/404"
response = self.client.get(url)
self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND)
9 changes: 8 additions & 1 deletion covidadmin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.urls import include, path, re_path
from wagtail.admin import urls as wagtailadmin_urls

# from wagtail.core import urls as wagtail_urls
Expand Down Expand Up @@ -45,3 +45,10 @@
path("documents/", include(wagtaildocs_urls)),
path("api/v2/", api_router.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if settings.DEBUG:
import debug_toolbar

urlpatterns += [
re_path(r"^__debug__/", include(debug_toolbar.urls)),
]
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ sentry_sdk
xlsxwriter
wagtail
djangorestframework-serializer-extensions
django-debug-toolbar
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ django-ckeditor==6.0.0
# via -r requirements.in
django-cors-headers==3.5.0
# via -r requirements.in
django-debug-toolbar==3.2.1
# via -r requirements.in
django-environ==0.4.5
# via -r requirements.in
django-filter==2.4.0
Expand Down
1 change: 1 addition & 0 deletions requirements_dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pip-tools
pre-commit
psycopg2-binary
transifex-client
django-debug-toolbar
4 changes: 4 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ decorator==4.4.2
# via
# ipython
# traitlets
django-debug-toolbar==3.2.1
# via -r requirements_dev.in
distlib==0.3.1
# via virtualenv
django-ckeditor==6.0.0
Expand Down Expand Up @@ -105,6 +107,7 @@ django==3.1.8
# via
# -r requirements.txt
# django-cors-headers
# django-debug-toolbar
# django-filter
# django-taggit
# django-treebeard
Expand Down Expand Up @@ -278,6 +281,7 @@ sqlparse==0.4.1
# via
# -r requirements.txt
# django
# django-debug-toolbar
tablib[xls,xlsx]==2.0.0
# via
# -r requirements.txt
Expand Down

0 comments on commit 46466e4

Please sign in to comment.