Skip to content

Commit

Permalink
Merge pull request #288 from cfpb/fix-tests
Browse files Browse the repository at this point in the history
Cleanup and fix unit tests when run as part of cfgov-refresh
  • Loading branch information
chosak committed Nov 8, 2016
2 parents 7c546ad + 3c21b81 commit f91ebb0
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 23 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ All notable changes to this project will be documented in this file.
We follow the [Semantic Versioning 2.0.0](http://semver.org/) format.

## Unreleased
-
- Fixed Python unit tests when run as part of cfgov-refresh.
- Removed debug print statements from Python unit tests.

## 2.2.7
- Updated the `load_programs` script to be compatible with djangorestframework 3.1.3
Expand Down
22 changes: 14 additions & 8 deletions paying_for_college/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function

import datetime
from django.db import models
try:
Expand Down Expand Up @@ -745,15 +747,19 @@ def print_vals(obj, val_list=False, val_dict=False, noprint=False):
keylist = sorted([key for key in obj._meta.get_all_field_names()],
key=lambda s: s.lower())
if val_list:
newlist = []
values = []
for key in keylist:
try:
print "%s: %s" % (key, obj.__getattribute__(key))
except:
pass
else:
newlist.append(key)
return [obj.__getattribute__(key) for key in newlist]
value = obj.__getattribute__(key)
except AttributeError:
continue

values.append(value)

if not noprint:
print('%s: %s' % (key, value))

return values
elif val_dict:
return obj.__dict__
else:
Expand All @@ -768,6 +774,6 @@ def print_vals(obj, val_list=False, val_dict=False, noprint=False):
except: # pragma: no cover
pass
if noprint is False:
print msg
print(msg)
else:
return msg
4 changes: 4 additions & 0 deletions paying_for_college/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@


class CommandTests(unittest.TestCase):
def setUp(self):
stdout_patch = mock.patch('sys.stdout')
stdout_patch.start()
self.addCleanup(stdout_patch.stop)

@mock.patch('paying_for_college.management.commands.'
'update_pfc_national_stats.nat_stats.'
Expand Down
7 changes: 7 additions & 0 deletions paying_for_college/tests/test_load_programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ class TestLoadPrograms(django.test.TestCase):
u'completers': 'NUMBER',
u'completion_cohort': 'NUMBER'}

def setUp(self):
print_patch = mock.patch(
'paying_for_college.disclosures.scripts.load_programs.print'
)
print_patch.start()
self.addCleanup(print_patch.stop)

def test_standardize_rate(self):
self.assertTrue(standardize_rate(u'1.7') == u'0.017')
self.assertTrue(standardize_rate(u'0.017') == u'0.017')
Expand Down
18 changes: 15 additions & 3 deletions paying_for_college/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,22 @@ def test_school_related_models(self):
noti = self.create_notification(s)
self.assertTrue(isinstance(noti, Notification))
self.assertTrue(noti.oid in noti.__unicode__())
self.assertTrue(print_vals(s) is None)
self.assertTrue("Emerald City" in print_vals(s, val_list=True))
self.assertTrue("Emerald City" in print_vals(s, val_dict=True)['city'])
self.assertIsInstance(print_vals(s, noprint=True), basestring)
self.assertTrue(
'Emerald City' in print_vals(s, val_list=True, noprint=True)
)
self.assertTrue("Emerald City" in print_vals(s, val_dict=True, noprint=True)['city'])
self.assertTrue("Emerald City" in print_vals(s, noprint=True))

print_patcher = mock.patch('paying_for_college.models.print')
with print_patcher as mock_print:
self.assertIsInstance(print_vals(s, val_list=True), list)
self.assertTrue(mock_print.called)

with print_patcher as mock_print:
self.assertIsNone(print_vals(s))
self.assertTrue(mock_print.called)

self.assertTrue(s.convert_ope6() == '005555')
self.assertTrue(s.convert_ope8() == '00555500')
self.assertTrue('Bachelor' in s.get_highest_degree())
Expand Down
10 changes: 9 additions & 1 deletion paying_for_college/tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import datetime
import string
import os

import mock
from mock import mock_open, patch
Expand All @@ -20,7 +21,7 @@
OID, ERRORS)
from django.conf import settings

PFC_ROOT = settings.REPOSITORY_ROOT
PFC_ROOT = os.path.join(os.path.dirname(__file__), '../..')
YEAR = api_utils.LATEST_YEAR
MOCK_YAML = """\
completion_rate:\n\
Expand Down Expand Up @@ -146,6 +147,13 @@ class TestScripts(django.test.TestCase):
'metadata': {'page': 0}
}

def setUp(self):
for method in ('print', 'sys.stdout'):
base = 'paying_for_college.disclosures.scripts.update_colleges.'
patcher = patch(base + method)
patcher.start()
self.addCleanup(patcher.stop)

def test_icomma(self):
icomma_test = update_ipeds.icomma(445999)
self.assertTrue(icomma_test == '445,999')
Expand Down
16 changes: 8 additions & 8 deletions paying_for_college/tests/test_search_index.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
import json
from paying_for_college.models import School
from paying_for_college.search_indexes import SchoolIndex

from django.test import TestCase
from paying_for_college.search_indexes import SchoolIndex
from paying_for_college.models import School


class SchoolIndexTest(TestCase):

fixtures = ['paying_for_college/fixtures/test_fixture.json']
fixtures = ['test_fixture.json']
MOCK_INDEX = SchoolIndex()
mock_obj = School.objects.get(pk=155317)

def test_index(self):
self.assertTrue(self.MOCK_INDEX.get_model() == School)
self.assertTrue(self.MOCK_INDEX.index_queryset().count() ==
School.objects.count())
self.assertTrue('Jayhawks' in
self.MOCK_INDEX.prepare_autocomplete(self.mock_obj))

mock_obj = School.objects.get(pk=155317)
self.assertTrue(
'Jayhawks' in self.MOCK_INDEX.prepare_autocomplete(mock_obj)
)
4 changes: 2 additions & 2 deletions paying_for_college/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Feedback,
EmailLink,
SchoolRepresentation,
STANDALONE,
school_search_api)

client = Client()
Expand Down Expand Up @@ -104,6 +105,7 @@ def test_get_program_length(self):
# response = client.get(reverse(url_name))
# self.assertTrue('base_template' in response.context_data.keys())

@unittest.skipIf(not STANDALONE, 'not running as standalone project')
def test_standalone_landing_views(self):
for url_name in self.standalone_landing_page_views:
response = client.get(reverse(url_name))
Expand Down Expand Up @@ -383,9 +385,7 @@ def test_verify_view_school_has_no_contact(self):
post_data = copy.copy(self.post_data)
post_data['iped'] = '408039'
post_data['oid'] = 'f38283b5b7c939a058889f997949efa566c616c4'
print("\n\n\n***SCHOOL is {}\nSCHOOL CONTACT IS {}\n".format(School.objects.get(pk=408039), School.objects.get(pk=408039).contact))
resp = client.post(self.url, data=post_data)
print("RESPONSE is {}\n***\n\n\n".format(resp.content))
self.assertTrue(resp.status_code == 400)

def test_verify_view_bad_id(self):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def read_file(filename):
packages=find_packages(),
package_data={'paying_for_college':
['data_sources/ipeds/*cleaned.csv',
'data_sources/ipeds/test.txt.zip',
'fixtures/*.json',
'templates/*.txt',
'templates/*.html',
Expand Down

0 comments on commit f91ebb0

Please sign in to comment.