def test_create_pcpp(self): """Use fake data to generate a new pcpp""" account = Account(name='New Project Effort', code='098-765', category=Account.PROJECT) row = { 'PROJ_NO': '098-765', 'LOCATION': 'CHINA', 'PROJ_NAME1': 'New Project Effort', 'PCV_NAME': 'IN Jones, B.', 'STATE': 'IN', 'OVERS_PART': '1234.56', 'PROJ_REQ': '3,434', 'UNIDENT_BAL': '1,111', 'SECTOR': 'IT', 'SUMMARY': 'sum sum sum'} issue_cache = Mock() issue_cache.find.return_value = Campaign.objects.get(name='Technology') sync.create_pcpp(account, row, issue_cache) project = Project.objects.get(title='New Project Effort') description = json.loads(project.description) description = description['data'][0]['data']['text'] self.assertEqual(project.account.code, '098-765') self.assertEqual(project.country.name, 'China') self.assertEqual(project.volunteername, 'Jones, B.') self.assertEqual(project.volunteerhomestate, 'IN') self.assertEqual(project.account.community_contribution, 123456) self.assertEqual(project.account.goal, 343400) self.assertEqual(project.account.current, (343400 - 111100)) self.assertEqual(project.overflow.name, 'Information Technology') self.assertEqual(project.campaigns.all()[0].name, 'Technology') self.assertEqual(description, 'sum sum sum') self.assertEqual(project.slug, 'new-project-effort') self.assertFalse(project.published) project.delete() account.delete()
def test_create_pcpp_empty(self): """Community contribution might be empty""" account = Account(name='New Project Effort', code='098-765', category=Account.PROJECT) row = { 'PROJ_NO': '098-765', 'LOCATION': 'CHINA', 'PROJ_NAME1': 'New Project Effort', 'PCV_NAME': 'IN Jones, B.', 'STATE': 'IN', 'OVERS_PART': '', 'PROJ_REQ': '3,434', 'UNIDENT_BAL': '1,111', 'SECTOR': 'IT', 'SUMMARY': 'sum sum sum' } issue_cache = Mock() issue_cache.find.return_value = Campaign.objects.get(name='Technology') sync.create_pcpp(account, row, issue_cache) project = Project.objects.get(title='New Project Effort') self.assertEqual(project.account.community_contribution, 0) self.assertEqual(project.account.goal, 343400) self.assertEqual(project.account.current, (343400 - 111100)) account.delete() # cascades
def test_custom_per_project_upper_limit(self): """Error if trying to donate more than a project needs""" data = {'payment_amount': '50.00'} account = Account(goal=8000, current=3001) form = DonationAmountForm(data=data, account=account) self.assertFalse(form.is_valid()) errors = form.errors.as_data() self.assertEqual('max_value', errors['payment_amount'][0].code) self.assertTrue('$49.99' in errors['payment_amount'][0].message) account.current = 3000 form = DonationAmountForm(data=data, account=account) self.assertTrue(form.is_valid())
def test_create_pcpp_no_sector(self): """The ominous "None" sector has special significance""" account = Account(name='New Project Effort', code='098-765', category=Account.PROJECT) row = { 'PROJ_NO': '098-765', 'LOCATION': 'CHINA', 'PROJ_NAME1': 'New Project Effort', 'PCV_NAME': 'IN Jones, B.', 'STATE': 'IN', 'OVERS_PART': '', 'PROJ_REQ': '3,434', 'UNIDENT_BAL': '1,111', 'SECTOR': 'None', 'SUMMARY': 'sum sum sum'} issue_cache = Mock() issue_cache.find.return_value = None sync.create_pcpp(account, row, issue_cache) project = Project.objects.get(title='New Project Effort') self.assertEqual(project.overflow, None) self.assertEqual(project.campaigns.all().count(), 0) account.delete() # cascades
def test_create_pcpp_empty(self): """Community contribution might be empty""" account = Account(name='New Project Effort', code='098-765', category=Account.PROJECT) row = { 'PROJ_NO': '098-765', 'LOCATION': 'CHINA', 'PROJ_NAME1': 'New Project Effort', 'PCV_NAME': 'IN Jones, B.', 'STATE': 'IN', 'OVERS_PART': '', 'PROJ_REQ': '3,434', 'UNIDENT_BAL': '1,111', 'SECTOR': 'IT', 'SUMMARY': 'sum sum sum'} issue_cache = Mock() issue_cache.find.return_value = Campaign.objects.get(name='Technology') sync.create_pcpp(account, row, issue_cache) project = Project.objects.get(title='New Project Effort') self.assertEqual(project.account.community_contribution, 0) self.assertEqual(project.account.goal, 343400) self.assertEqual(project.account.current, (343400 - 111100)) account.delete() # cascades
def create_account(row, issue_map): """This is a new project/campaign. Determine the account type and create the appropriate project, country fund, etc.""" acc_type = account_type(row) name = row['PROJ_NAME1'] if Account.objects.filter(name=name).first(): name = name + ' (' + row['PROJ_NO'] + ')' account = Account(name=name, code=row['PROJ_NO'], category=acc_type) if acc_type == Account.PROJECT: create_pcpp(account, row, issue_map) else: create_campaign(account, row, name, acc_type)
def test_amount_errors(self): pd = api.ProjectDonation() host = 'example.com' account = Account(goal=200, current=0) # no payment amount data = {} result = pd.errors_or_paygov(account, data, host) self.assertEqual(result.status_code, 400) self.assertTrue('amount' in result.content.decode('utf-8')) # low payment amount data['payment_amount'] = '0.99' result = pd.errors_or_paygov(account, data, host) self.assertEqual(result.status_code, 400) self.assertTrue('amount' in result.content.decode('utf-8')) # high payment amount data['payment_amount'] = '10000' result = pd.errors_or_paygov(account, data, host) self.assertEqual(result.status_code, 400) self.assertTrue('amount' in result.content.decode('utf-8')) # too much for project data['payment_amount'] = '3' result = pd.errors_or_paygov(account, data, host) self.assertEqual(result.status_code, 400) self.assertTrue('amount' in result.content.decode('utf-8')) # passes the amount test but not the donor form data['payment_amount'] = '2' result = pd.errors_or_paygov(account, data, host) self.assertEqual(result.status_code, 400) self.assertTrue('amount' not in result.content.decode('utf-8')) # account is full account.current = 200 result = pd.errors_or_paygov(account, data, host) self.assertEqual(result.status_code, 400) self.assertTrue('amount' in result.content.decode('utf-8'))
def test_serialize(self): country = Country(name='Awesomeland') account = Account() project = Project(account=account, country=country) pd = api.ProjectDetail() result = pd.serialize(project) self.assertEqual(result['title'], '') self.assertEqual(result['tagline'], None) self.assertEqual(result['country'], 'Awesomeland') self.assertEqual(result['featured_image'], None) project.title = 'SomeTitle' project.tagline = 'SomeTagline' project.featured_image = Media() project.featured_image.file = Mock() project.featured_image.file.url = 'urlhere' result = pd.serialize(project) self.assertEqual(result['title'], 'SomeTitle') self.assertEqual(result['tagline'], 'SomeTagline') self.assertEqual(result['featured_image'], 'urlhere')
def test_serialize_account(self): account = Account() project = Project(account=account) result = api._serialize_account(project) self.assertEqual(result['goal'], None) self.assertEqual(result['community_contribution'], None) self.assertEqual(result['total_donated'], 0) self.assertEqual(result['total_raised'], 0) self.assertEqual(result['total_cost'], 0) self.assertEqual(result['percent_raised'], 0) self.assertEqual(result['percent_community'], 0) self.assertEqual(result['remaining'], 0) self.assertEqual(result['funded'], False) account.goal = 2400 account.community_contribution = 600 account.current = 900 result = api._serialize_account(project) self.assertEqual(result['goal'], 2400) self.assertEqual(result['community_contribution'], 600) self.assertEqual(result['total_donated'], 900) self.assertEqual(result['total_raised'], 1500) self.assertEqual(result['total_cost'], 3000) self.assertEqual(result['percent_raised'], 50) self.assertEqual(result['percent_community'], 20) self.assertEqual(result['remaining'], 1500) self.assertEqual(result['funded'], False) account.current = 2400 result = api._serialize_account(project) self.assertEqual(result['total_donated'], 2400) self.assertEqual(result['total_raised'], 3000) self.assertEqual(result['percent_raised'], 100) self.assertEqual(result['remaining'], 0) self.assertEqual(result['funded'], True)