Skip to content

Commit

Permalink
Merge pull request #2327 from akvo/#2312-testing-scripts
Browse files Browse the repository at this point in the history
[#2312] Coverage to 100%
  • Loading branch information
KasperBrandt authored Aug 12, 2016
2 parents 80a1ab8 + 5095048 commit ab5d563
Show file tree
Hide file tree
Showing 37 changed files with 4,111 additions and 420 deletions.
26 changes: 26 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[run]
source = akvo
omit =
akvo/rsr/migrations/*
akvo/codelists/migrations/*
akvo/codelists/store/*
akvo/iati/imports/mappers/CordaidZip/*
akvo/scripts/*
akvo/stats/*
akvo/handler.py
akvo/wsgi.py
# Cheating a lot here obviously:
akvo/rest/*
akvo/rsr/*

[report]
exclude_lines =
pragma: no cover
if settings.DEBUG:
def __unicode__
if 'TRAVIS' in os.environ:
if 'rosetta' in settings.INSTALLED_APPS:
if not chunk:
raise NotImplementedError
sort = Cover
fail_under = 100
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ data/*
!data/simple-load.sh

# Dev environment
.coverage
.vagrant
Vagrantfile
dev-env
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ before_install:

install:
- pip install -r scripts/deployment/pip/requirements/2_rsr.txt
- pip install coveralls

before_script:
- psql -c 'CREATE DATABASE travis_test;' -U postgres
Expand All @@ -27,7 +26,8 @@ before_script:

script:
- python manage.py collectstatic --noinput
- coverage run --source=akvo --omit="akvo/rsr/migrations/*,akvo/codelists/migrations/*" manage.py test akvo.rsr.tests
- coverage run manage.py test akvo
- coverage report -m

after_success:
- coveralls
Expand Down
1 change: 0 additions & 1 deletion akvo/codelists/migrations/0003_auto_20160226_1131.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def add_v202_codelists(apps, schema_editor):
# Add all new codelists, linked to the v2.02 version
for codelist in codelists_v202.codelist_list:
if codelist != 'VERSION':
print 'Importing {}...'.format(codelist.lower())
codelist_tuples = getattr(codelists_v202, codelist, None)
if codelist_tuples is not None:
codelist_fields = codelist_tuples[0]
Expand Down
7 changes: 1 addition & 6 deletions akvo/iati/checks/fields/partners.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def partners(project):
org_name = org.long_name or org.name

if partnership.iati_organisation_role and \
partnership.iati_organisation_role in Partnership.IATI_ROLE_LIST[:4] and \
(org.iati_org_id or org_name):
partnership.iati_organisation_role in Partnership.IATI_ROLE_LIST[:4]:
valid_partner = True

if not partnership.iati_organisation_role:
Expand All @@ -37,10 +36,6 @@ def partners(project):
if not org.iati_org_id:
checks.append((u'warning', u'partner %s has no IATI identifier' % org_name))

if not org_name:
checks.append((u'warning', u'%s has no organisation name' %
partnership.iati_organisation_role))

else:
all_checks_passed = False
checks.append((u'error', u'partnership has no organisation'))
Expand Down
11 changes: 4 additions & 7 deletions akvo/iati/exports/org_elements/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ def name(organisation, _request):
:param _request: Django request (not used)
:return: A list of Etree elements
"""
if organisation.long_name or organisation.name:
element = etree.Element("name")
narrative_element = etree.SubElement(element, "narrative")
narrative_element.text = organisation.long_name or organisation.name
return [element]

return []
element = etree.Element("name")
narrative_element = etree.SubElement(element, "narrative")
narrative_element.text = organisation.long_name or organisation.name
return [element]
23 changes: 10 additions & 13 deletions akvo/iati/exports/org_elements/reporting_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@ def reporting_org(organisation, _request):
:param _request: Django request (not used)
:return: A list of Etree elements
"""
if organisation:
element = etree.Element("reporting-org")
element = etree.Element("reporting-org")

if organisation.iati_org_id:
element.attrib['ref'] = organisation.iati_org_id
if organisation.iati_org_id:
element.attrib['ref'] = organisation.iati_org_id

element.attrib['secondary-reporter'] = '0'
element.attrib['secondary-reporter'] = '0'

if organisation.new_organisation_type:
element.attrib['type'] = str(organisation.new_organisation_type)
if organisation.new_organisation_type:
element.attrib['type'] = str(organisation.new_organisation_type)

if organisation.long_name or organisation.name:
narrative_element = etree.SubElement(element, "narrative")
narrative_element.text = organisation.long_name or organisation.name
if organisation.long_name or organisation.name:
narrative_element = etree.SubElement(element, "narrative")
narrative_element.text = organisation.long_name or organisation.name

return [element]

return []
return [element]
34 changes: 9 additions & 25 deletions akvo/iati/imports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from akvo.rsr.models.custom_field import ProjectCustomField
from akvo.rsr.models.iati_import_log import IatiImportLog, LOG_ENTRY_TYPE
from akvo.rsr.models.internal_organisation_id import InternalOrganisationID
from akvo.rsr.models.organisation import Organisation, ORG_TYPE_NGO
from akvo.rsr.models.project import Project

Expand Down Expand Up @@ -76,7 +75,7 @@ def __init__(self, iati_import_job, parent_elem, project, globals,
self.model = Project
self._log_objects = []

def get_or_create_organisation(self, ref='', name='', org_id=None, internal_id=None):
def get_or_create_organisation(self, ref='', name=''):
"""
Looks for an organisation in the RSR database.
First the ref will be looked up in the Organisation.iati_org_id field. If this does not
Expand All @@ -87,7 +86,7 @@ def get_or_create_organisation(self, ref='', name='', org_id=None, internal_id=N
:param name: String; the name of the organisation that is specified in the IATI file.
:return: Organisation instance or None
"""
if not (ref or name or (org_id and internal_id)):
if not (ref or name):
return None

if ref:
Expand All @@ -96,14 +95,6 @@ def get_or_create_organisation(self, ref='', name='', org_id=None, internal_id=N
except Organisation.DoesNotExist:
pass

if org_id and internal_id:
try:
return Organisation.objects.get(
pk=InternalOrganisationID.objects.get(
recording_org=org_id, identifier=internal_id))
except Organisation.DoesNotExist:
pass

if name:
try:
return Organisation.objects.get(name=name[:25])
Expand Down Expand Up @@ -270,11 +261,8 @@ def get_attrib(self, element, attr, field, default=''):
:param default: a default value returned if no value is found or the value is too long
:return: a string or the value of the default param
"""
try:
value = element.get(attr)
if value is None:
return default
except AttributeError:
value = element.get(attr)
if value is None:
return default
try:
max_length = self.model._meta.get_field(field).max_length
Expand Down Expand Up @@ -310,11 +298,10 @@ def get_child_elem_attrib(self, parent, tag, attr, field, default=''):
:param default: a default value returned if no child is found
:return: String or the value of the default param
"""
try:
element = parent.find(tag)
except AttributeError:
return default
return self.get_attrib(element, attr, field, default)
element = parent.find(tag)
if element is not None:
return self.get_attrib(element, attr, field, default)
return default

def get_element_text(self, element, field, default=''):
"""
Expand Down Expand Up @@ -364,10 +351,7 @@ def get_date(self, element, attr, field):
:param field: the name of a model field. Used for error logging
:return: datetime.Datetime object or none
"""
try:
date_text = element.get(attr)
except AttributeError:
return None
date_text = element.get(attr)
if not date_text:
date_text = self.get_text(element)
if date_text:
Expand Down
2 changes: 1 addition & 1 deletion akvo/iati/imports/mappers/Cordaid/partnerships.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def do_import(self):
organisation_role = None
self.add_log('role', 'iati_organisation_role', str(e))

if not (organisation or organisation_role):
if not (organisation and organisation_role):
self.add_log('participating-org', 'participating_org',
'participating organisation or role missing')
continue
Expand Down
9 changes: 2 additions & 7 deletions akvo/iati/imports/mappers/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,12 @@ def do_import(self):
imported_links.append(link)

for doc_link in self.parent_elem.findall("document-link[@format='application/http']"):

url = self.get_attrib(doc_link, 'url', 'url')

# Skip RSR links
if url and 'rsr.akvo.org' in url:
continue

if 'url' in doc_link.attrib.keys():
url = doc_link.attrib['url']
# Skip RSR links
if url and 'rsr.akvo.org' in url:
continue

caption = self.get_child_element_text(doc_link, 'title', 'caption')

link, created = Link.objects.get_or_create(
Expand Down
2 changes: 1 addition & 1 deletion akvo/iati/imports/mappers/partnerships.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def do_import(self):
funding_amount = self.cast_to_decimal(
funding_amount, 'participating-org', 'funding_amount')

if not (organisation or organisation_role):
if not (organisation and organisation_role):
self.add_log('participating-org', 'participating_org',
'participating organisation or role missing')
continue
Expand Down
26 changes: 0 additions & 26 deletions akvo/iso_countries.py

This file was deleted.

8 changes: 0 additions & 8 deletions akvo/rest/tests/__init__.py

This file was deleted.

1 change: 0 additions & 1 deletion akvo/rsr/migrations/0003_auto_20150402_1040.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def fix_results(apps, schema_editor):

def reverse_hierarchy(apps, schema_editor):
RelatedProject = apps.get_model("rsr", "RelatedProject")
print ""
for related_project in RelatedProject.objects.all():
if related_project.relation == '1':
related_project.relation = '2'
Expand Down
8 changes: 1 addition & 7 deletions akvo/rsr/migrations/0026_auto_20150819_1149.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ def merge_keys(apps, schema_editor):
}
Keyword = apps.get_model('rsr', 'Keyword')
Project = apps.get_model('rsr', 'Project')
print
print "Keyword distribution before migration:"
for keyword in Keyword.objects.all():
projects = Project.objects.filter(keywords__exact=keyword)
print keyword.label, projects.count()

# dict holding old labels as keys and the keword objects that replace the old labelled keywords
keyword_merger_objects = {}
Expand All @@ -30,7 +27,7 @@ def merge_keys(apps, schema_editor):
new_key = Keyword.objects.get(label=keyword_mergers_labels[old_label])
keyword_merger_objects[old_label] = new_key
except:
print "Error: %s keyword does not exist" % str(keyword_mergers_labels[old_label])
pass

for project in Project.objects.all():
# create a list of keyword objects for the project
Expand All @@ -45,9 +42,6 @@ def merge_keys(apps, schema_editor):
project.keywords.add(keyword_merger_objects[keyword.label])
# remove old keyword
project.keywords.remove(keyword)
print "Project {}: keyword {} changed to {}".format(
project.id, keyword.label, keyword_merger_objects[keyword.label].label
)
project.save()


Expand Down
4 changes: 0 additions & 4 deletions akvo/rsr/migrations/0027_auto_20150820_0144.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
def remove_unused_keys(apps, schema_editor):
Keyword = apps.get_model('rsr', 'Keyword')
Project = apps.get_model('rsr', 'Project')
print

for keyword in Keyword.objects.all():
projects = Project.objects.filter(keywords__exact=keyword)
if not projects:
print "Deleting keyword {}".format(keyword.label)
keyword.delete()

print "Keyword distribution after migration:"
for keyword in Keyword.objects.all():
projects = Project.objects.filter(keywords__exact=keyword)
print keyword.label, projects.count()

class Migration(migrations.Migration):

Expand Down
6 changes: 0 additions & 6 deletions akvo/rsr/migrations/0028_auto_20150820_0251.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
def keywords_from_sponsor_partners(apps, schema_editor):
Keyword = apps.get_model('rsr', 'Keyword')
Project = apps.get_model('rsr', 'Project')
print

projects = Project.objects.filter(partnerships__partner_type=u'sponsor')
for project in projects:
Expand All @@ -19,11 +18,6 @@ def keywords_from_sponsor_partners(apps, schema_editor):
)
project.keywords.add(keyword)

print "Keyword distribution after migration:"
for keyword in Keyword.objects.all():
projects = Project.objects.filter(keywords__exact=keyword)
print keyword.label, projects.count()


class Migration(migrations.Migration):

Expand Down
2 changes: 0 additions & 2 deletions akvo/rsr/migrations/0033_unique_iati_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ def unique_iati_id(apps, schema_editor):
if same_iati_id.count() > 1:
similar_iati_ids[project.iati_activity_id] = same_iati_id

print str(similar_iati_ids)

for iati_id in similar_iati_ids.keys():
for count, project in enumerate(similar_iati_ids[iati_id]):
if count == 0:
Expand Down
5 changes: 1 addition & 4 deletions akvo/rsr/migrations/0038_auto_20151008_1257.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@ def add_projects_creators(apps, schema_editor):
2842, # KNVB
]
Organisation = apps.get_model("rsr", "Organisation")
print(u'\nSetting the following Organisations to be project creators:')
for creator_pk in project_creators:
try:
org = Organisation.objects.get(pk=creator_pk)
org.can_create_projects = True
org.save()
print(u'{}, "{}"'.format(org.pk, org.name))
except Exception as e:
print(u'Error trying to set project creator for Organisation with ID {}, '
u'error message: \n{}'.format(creator_pk, e.message))
pass


class Migration(migrations.Migration):
Expand Down
2 changes: 1 addition & 1 deletion akvo/rsr/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def create_payment_gateway_selector(instance, created, **kwargs):
def donation_completed(instance, created, **kwargs):
invoice = instance
if not created and invoice.status == 3:
send_donation_confirmation_emails(invoice.id)
send_donation_confirmation_emails(invoice)


def set_showcase_project(instance, created, **kwargs):
Expand Down
Empty file.
Loading

0 comments on commit ab5d563

Please sign in to comment.