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

Commit

Permalink
Merge pull request #9 from cfpb/json-handling
Browse files Browse the repository at this point in the history
Adds demo option for handling json
  • Loading branch information
higs4281 committed Nov 1, 2016
2 parents d235286 + ad86fb9 commit 7d962e7
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 85 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased][unreleased]
- Removed view code to parse narratives as jsonp (now straight json)
- Added a url and view option to demo the landing page with a local json file

## [1.2.6] - 2016-10-06
- Copy updated for `submit-a-complaint` and `process`
- Added travis and coveralls setup
- Updated the download 4x4 codes for Money transfers
- Copy updates on `submit-a-complaint` and `process`

## [1.2.2] - 2016-06-06
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion complaint/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_get_context_data_exist(self):


class URLTest(TestCase):
url_names = ['ccdb_landing', 'ccdb_data_use', 'ccdb_process']
url_names = ['ccdb_submit', 'ccdb_data_use', 'ccdb_process']

def test_complaint_urls(self):
for url_name in self.url_names:
Expand Down
2 changes: 1 addition & 1 deletion complaint/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from complaint.views import SubmitView, DataUseView, ProcessView

urlpatterns = [
url(r'^$', SubmitView.as_view(), name='ccdb_landing'),
url(r'^$', SubmitView.as_view(), name='ccdb_submit'),
url(r'^data-use/', DataUseView.as_view(), name='ccdb_data_use'),
url(r'^process/', ProcessView.as_view(), name='ccdb_process'),
]
3 changes: 3 additions & 0 deletions complaint/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
else: # pragma: no cover
BASE_TEMPLATE = "front/base_update.html"


class SubmitView(TemplateView):
template_name = "submit-a-complaint.html"

Expand All @@ -24,6 +25,7 @@ def get_context_data(self, **kwargs):
context['base_template'] = BASE_TEMPLATE
return context


class DataUseView(TemplateView):
template_name = "data-use.html"

Expand All @@ -32,6 +34,7 @@ def get_context_data(self, **kwargs):
context['base_template'] = BASE_TEMPLATE
return context


class ProcessView(TemplateView):
template_name = "process.html"

Expand Down
74 changes: 35 additions & 39 deletions complaintdatabase/tests.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import collections
from mock import patch, Mock
from mock import patch, Mock, MagicMock, mock_open

from requests.exceptions import ConnectionError
from django.test import RequestFactory, TestCase
from django.core.urlresolvers import reverse
from django.test import Client
from datetime import datetime
from StringIO import StringIO
from .views import (LandingView, DocsView, get_narratives_json,
format_narratives, get_stats, get_count_info,
is_data_not_updated)

MOCK_404 = ConnectionError(Mock(return_value={'status': 404}), 'not found')
client = Client()


class LandingViewTest(TestCase):
Expand All @@ -28,17 +31,36 @@ def test_get_context_data_exist(self):
self.assertTrue('total_complaints' in response.context_data.keys())
self.assertTrue('timely_responses' in response.context_data.keys())

def test_demo_json(self):
"""Test demo version of landing page"""
response = client.get(reverse("complaintdatabase:ccdb-demo",
kwargs={'demo_json': 'demo.json'}))
self.assertEqual(response.status_code, 200)
self.assertTrue('base_template' in response.context_data.keys())
self.assertTrue('narratives' in response.context_data.keys())
self.assertTrue('stats' in response.context_data.keys())


class NarrativeJsonTest(TestCase):

@patch('complaintdatabase.views.requests.get')
def test_get_narratives_json(self, mock_requests_get):
# Using namedtuple to mock out the attribute text in response
# not sure if this is the best way though
Response = collections.namedtuple('Response', 'text')
mock_requests_get.return_value = Response(text="narratives({});")
def test_get_narratives_json(self, mock_get):
mock_return = MagicMock()
mock_return.json.return_value = {}
mock_get.return_value = mock_return
res_json = get_narratives_json()
self.assertEqual(res_json, {})
self.assertTrue(mock_get.call_count == 1)

@patch('complaintdatabase.views.requests.get')
def test_get_demo_narratives_json(self, mock_get):
mock_return = MagicMock()
mock_return.json.return_value = {}
mock_get.return_value = mock_return
m = mock_open(read_data='{"mock_data": ""}')
with patch("__builtin__.open", m, create=True):
res_json = get_narratives_json(demo_json='/fake/path')
self.assertEqual(res_json, {"mock_data": ""})

@patch('complaintdatabase.views.requests.get')
def test_request_exception_get_narratives_json(self, mock_requests_get):
Expand All @@ -50,14 +72,15 @@ def test_request_exception_get_narratives_json(self, mock_requests_get):
fakeOutput.getvalue().strip())

@patch('complaintdatabase.views.requests.get')
def test_incorrect_text_get_narratives_json(self, mock_requests_get):
Response = collections.namedtuple('Response', 'text')
mock_requests_get.return_value = Response(text=("This is not a correct"
" set of narratives"))
with patch('sys.stdout', new=StringIO()) as fakeOutput:
def test_incorrect_text_get_narratives_json(self, mock_get):
mock_return = MagicMock()
mock_return.json.return_value = {}
mock_get.return_value = mock_return
with patch('sys.stdout', new=StringIO('ValueError')) as fakeOutput:
res_json = get_narratives_json()
self.assertEqual(res_json, {})
self.assertIn('ValueError', fakeOutput.getvalue().strip())
self.assertIn('ValueError', fakeOutput.getvalue())
self.assertTrue(mock_get.call_count == 1)


class FormatNarrativesTest(TestCase):
Expand Down Expand Up @@ -302,33 +325,6 @@ def test_data_not_updated_friday_narratives_down(self, mock_get_now):
self.assertFalse(data_down)
self.assertTrue(narratives_down)

# @patch('complaintdatabase.views.get_now')
# def test_data_not_updated_saturday_down(self, mock_get_now):
# mock_get_now.return_value = datetime(2015, 12, 26, 19, 20, 10, 975427)
# input_json = {'stats': {'last_updated': "2015-12-18",
# 'last_updated_narratives': "2015-12-18"}}
# data_down, narratives_down = is_data_not_updated(input_json)
# self.assertTrue(data_down)
# self.assertFalse(narratives_down)

# @patch('complaintdatabase.views.get_now')
# def test_data_not_updated_saturday_up(self, mock_get_now):
# mock_get_now.return_value = datetime(2015, 12, 26, 19, 20, 10, 975427)
# input_json = {'stats': {'last_updated': "2015-12-21",
# 'last_updated_narratives': "2015-12-21"}}
# data_down, narratives_down = is_data_not_updated(input_json)
# self.assertFalse(data_down)
# self.assertFalse(narratives_down)

# @patch('complaintdatabase.views.get_now')
# def test_data_not_updated_saturday_narratives_down(self, mock_get_now):
# mock_get_now.return_value = datetime(2015, 12, 26, 19, 20, 10, 975427)
# input_json = {'stats': {'last_updated': "2015-12-21",
# 'last_updated_narratives': "2015-12-18"}}
# data_down, narratives_down = is_data_not_updated(input_json)
# self.assertFalse(data_down)
# self.assertTrue(narratives_down)

@patch('complaintdatabase.views.get_now')
def test_data_not_updated_saturday_down(self, mock_get_now):
mock_get_now.return_value = datetime(2015, 12, 27, 19, 20, 10, 975427)
Expand Down
15 changes: 14 additions & 1 deletion complaintdatabase/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
from django.conf import settings
from django.conf.urls import url
from complaintdatabase.views import LandingView

try:
STANDALONE = settings.STANDALONE
except AttributeError: # pragma: no cover
STANDALONE = False

urlpatterns = [
url(r'^$', LandingView.as_view()),
url(r'^$', LandingView.as_view(),
name='ccdb-landing-page')
]

if STANDALONE:
urlpatterns += [
url(r'^demo/(?P<demo_json>[^/]+)/$', LandingView.as_view(),
name='ccdb-demo')
]
Loading

0 comments on commit 7d962e7

Please sign in to comment.