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

#8689u4y5t native land resource update testcases #368

Merged
6 changes: 3 additions & 3 deletions helpers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def native_land_data(request):
"""
slug = request.GET.get('slug')
if slug is None:
return Http404('Slug Variable Is Not Defined In Request')
raise Http404('Slug Variable Is Not Defined In Request')

# get all slug data from cache
all_slug_data = cache.get('all_slug_data')
Expand All @@ -156,11 +156,11 @@ def native_land_data(request):
requests.exceptions.ConnectionError,
requests.exceptions.HTTPError
):
return Http404('Unable to Retrieve All NLD Slug Data')
raise Http404('Unable to Retrieve All NLD Slug Data')
cache.set('all_slug_data', all_slug_data)

slug_data = all_slug_data.get(slug)
if slug_data is None:
return Http404(f'Unable to Retrieve Specific NLD Slug Data for {slug}')
raise Http404(f'Unable to Retrieve Specific NLD Slug Data for {slug}')

return JsonResponse(slug_data)
93 changes: 93 additions & 0 deletions tests/functional/test_native_land_data_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import requests
from unittest.mock import patch

from django.urls import reverse
from django.core.cache import cache
from django.contrib.auth.models import User
from django.test import Client, TransactionTestCase


class TestFeatures(TransactionTestCase):
def setUp(self):
self.client = Client()
username = 'user'
pw = 'pw'
self.user = User.objects.create_user(
username=username,
password=pw
)
logged_in = self.client.login(
username=username,
password=pw
)
self.assertTrue(logged_in, 'Login failed')

# clear cache since view function modifies the cache
cache.clear()

def test_error_raised_when_slug_is_not_defined_in_get_request(self):
"""
When the get request does not contain a slug, it should raise an error.
"""
expected_message = 'Slug Variable Is Not Defined In Request'
response = self.client.get(
reverse('nld-data')
)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.context['exception'], expected_message)

def test_error_raised_when_slug_is_not_in_all_nld_data(self):
"""
When the retrieve_native_land_all_slug_data returns an empty dictionary
which does not contain the slug, an error should be raised.
"""
slug = 'placeholder_slug'
expected_message = f'Unable to Retrieve Specific NLD Slug Data for {slug}'
get_variables = {
'slug': slug,
}
with patch('helpers.views.retrieve_native_land_all_slug_data') as mock_function:
mock_function.return_value = {}
response = self.client.get(
reverse('nld-data'),
data=get_variables
)
self.assertEqual(response.context['exception'], expected_message)

def test_error_raised_when_error_occurs_retrieving_all_nld_data(self):
"""
When the retrieve_native_land_all_slug_data fails
an error should be raised.
"""
slug = 'placeholder_slug'
expected_message = f'Unable to Retrieve All NLD Slug Data'
get_variables = {
'slug': slug,
}
with patch('helpers.views.retrieve_native_land_all_slug_data') as mock_function:
mock_function.side_effect = requests.exceptions.HTTPError('Some Error Occurred')
response = self.client.get(
reverse('nld-data'),
data=get_variables
)
self.assertEqual(response.context['exception'], expected_message)

def test_no_error_raised_when_getting_data_for_slug(self):
"""
No error is raised when getting data for slug
"""
slug = 'placeholder_slug'
get_variables = {
'slug': slug,
}
with patch('helpers.views.retrieve_native_land_all_slug_data') as mock_function:
specific_slug_data = {'foo': 'bar'}
mock_function.return_value = {
slug: specific_slug_data
}
response = self.client.get(
reverse('nld-data'),
data=get_variables
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), specific_slug_data)
5 changes: 0 additions & 5 deletions tests/unit/test_unit_example.py

This file was deleted.

17 changes: 17 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from unittest.mock import patch
from django.test import TestCase

from helpers.utils import retrieve_native_land_all_slug_data


class TestUtils(TestCase):

def test_retrieve_native_land_all_slug_data(self):
with patch('requests.get') as mock_get:
retrieve_native_land_all_slug_data()
url = (
'https://raw.githubusercontent.com/biocodellc/'
'localcontexts_json/refs/heads/main/data/'
'nativeland_slug_coordinates_description_dict.json'
)
mock_get.assert_called_with(url)
Loading