Skip to content

Commit

Permalink
[#813] Rewrote the script to require input to prevent human errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kardan committed Oct 24, 2014
1 parent a109cfa commit 3489154
Showing 1 changed file with 150 additions and 19 deletions.
169 changes: 150 additions & 19 deletions val.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@

import os
import sys
# import json
import argparse
#import hashlib
import unittest


###############################################################################
### Generate

def addCounts(models, data):
"""
For all Models count the number of objects
"""
# Fast to type but not beutiful imports :-(
from akvo.rsr.models import (Benchmark,
Benchmarkname,
BudgetItem,
Expand Down Expand Up @@ -62,45 +71,70 @@ def addCounts(models, data):
data[model] = {}
obj = eval(model)
data[model]['count'] = obj.objects.all().count()
print("\tAdded counts for all models")
return data


def addProjectData(data):
"""
For each project add data specific to projects
"""
from akvo.rsr.models import Project

projects = Project.objects.published()
projects = Project.objects.all()
data['Project']['items'] = {}

for project in projects:
# Make sure we have the project item
# Setup and ensure project key exists
pid = repr(project.id)
if project.id not in data['Project']['items']:
data['Project']['items'][project.id] = {}

# Add project data
data['Project']['items'][project.id]['budget'] = project.budget
data['Project']['items'][project.id]['funds'] = project.funds
data['Project']['items'][project.id]['keywords'] = \
project.keywords.values_list('label', flat=True)
data['Project']['items'][project.id]['image'] = project.current_image
data['Project']['items'][project.id]['donations_amount'] = \
project.all_donations_amount()
data['Project']['items'][pid] = {}

# Add prject specific items
data['Project']['items'][pid]['budget'] = repr(project.budget)
data['Project']['items'][pid]['funds'] = repr(project.funds)
data['Project']['items'][pid]['keywords'] = \
repr(project.keywords.values_list('label', flat=True))
data['Project']['items'][pid]['image'] = repr(project.current_image)
data['Project']['items'][pid]['donations_amount'] = \
repr(project.all_donations_amount())
print("\tAdded Project specific data")
return data


def persistData(data):
with open('/tmp/rsr_validation_data.py', 'w') as f:
# print(json.dumps(data), file=f)
print(repr(data), file=f)
print(data, file=f)

# Since the bencode format does not support encoding of all
# data types checksum is removed for now
# sorted_data = bencode(raw_data)
# data = {}
# data['data'] = sorted_data
# data['check'] = hashlib.md5(sorted_data).hexdigest()
# print(data, file=f)

def getData(models):
data = {}
data = addCounts(models, data)
data = addCounts(models, {})
data = addProjectData(data)
return data


###############################################################################
### Validate

def validateProjectData(data):
from akvo.rsr.models import Project

projects = Project.objects.all()
for project in projects:
pid = repr(project.id)
assert data['Project']['items'][pid] is not None


###############################################################################
### Validate

models = ['Benchmark',
'Benchmarkname',
'BudgetItem',
Expand Down Expand Up @@ -145,7 +179,96 @@ def getData(models):
'UserProfile', ]


class TestValidate(unittest.TestCase):
"""
Our basic test class
"""

def setUp(self):

with open('/tmp/rsr_validation_data.py', 'rb') as f:
self.ref = eval(f.read())

def test_counts(self):
# Fast to type but not beutiful imports :-(
from akvo.rsr.models import (Benchmark,
Benchmarkname,
BudgetItem,
BudgetItemLabel,
CountryBudgetItem,
Country,
RecipientCountry,
Category,
FocusArea,
Goal,
Indicator,
IndicatorPeriod,
Invoice,
InternalOrganisationID,
Keyword,
LegacyData,
Link,
OrganisationLocation,
ProjectLocation,
ProjectUpdateLocation,
MiniCMS,
Organisation,
OrganisationAccount,
PartnerSite,
PartnerType,
Partnership,
PayPalGateway,
MollieGateway,
PaymentGatewaySelector,
PlannedDisbursement,
PolicyMarker,
Project,
ProjectComment,
ProjectCondition,
ProjectContact,
ProjectUpdate,
PublishingStatus,
RecipientRegion,
Result,
Sector,
Transaction,
UserProfile,)

for model in models:
obj = eval(model)
self.assertEqual(self.ref[model]['count'],
obj.objects.all().count())

def test_project(self):
from akvo.rsr.models import Project
projects = Project.objects.all()

for project in projects:
p = self.ref['Project']['items'][repr(project.id)]
self.assertEqual(p['budget'], repr(project.budget))
self.assertEqual(p['funds'], repr(project.funds))
self.assertEqual(p['keywords'],
repr(project.keywords.values_list('label',
flat=True)))
self.assertEqual(p['image'], repr(project.current_image))
self.assertEqual(p['donations_amount'],
repr(project.all_donations_amount()))


###############################################################################
### Script lifecycle

if __name__ == "__main__":
# Parse arguments
parser = argparse.ArgumentParser(description='Val does ...')
parser.add_argument('mode', choices=['generate', 'validate'])
parser.add_argument('-f','--file', help='Path to file',
required=True)
args = vars(parser.parse_args())
mode = args['mode']
path = args['file']

# Setup the Django env
try:
sys.path.append('/var/akvo/rsr/code/akvo')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
Expand All @@ -156,5 +279,13 @@ def getData(models):
sys.stderr.write("\nImportError: " + str(e) + "\n")
sys.exit(1)

persistData(getData(models))
print("Done collecting reference data")
# Walk the right branch
if mode == 'generate':
print("Start to generate data")
persistData(getData(models))
elif mode == 'validate':
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestValidate)
unittest.TextTestRunner().run(suite)

# Exit
print("\nDone\n")

0 comments on commit 3489154

Please sign in to comment.