def test_update_committment_no_id(self): """Ensure error is thrown if an id is not provided.""" UTILS.add_committment(self.fund_1.id, 1000) with self.client as client: response = client.put( '/committments/blah', data=json.dumps({'fund_id': 1}), content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 404) self.assertIn(UTILS.NOT_EXISTS(TYPE, 'blah'), data['message']) self.assertIn('fail', data['status']) self.assertFalse(data['data'])
def test_all_committments(self): """Ensure get all committments behaves correctly.""" committment1 = UTILS.add_committment(self.fund_1.id, 1000) committment2 = UTILS.add_committment(self.fund_2.id, 2000) with self.client as client: response = client.get('/committments') data = json.loads(response.data.decode()) committments = data['data']['committments'] self.assertEqual(response.status_code, 200) self.assertEqual(len(committments), 2) self.assertEqual(committment1.id, committments[0]['id']) self.assertEqual(committment2.id, committments[1]['id']) self.assertEqual(committment1.fund_id, committments[0]['fund_id']) self.assertEqual(committment2.fund_id, committments[1]['fund_id']) self.assertEqual(committment1.amount, committments[0]['amount']) self.assertEqual(committment2.amount, committments[1]['amount']) self.assertIn('success', data['status']) self.assertEqual(UTILS.SUCCESS(TYPE, ""), data['message'])
def test_delete_committment(self): """Ensure committment is deleted""" committment = UTILS.add_committment(self.fund_1.id, 1000) with self.client as client: res = client.delete(f'/committments/{committment.id}') data = json.loads(res.data.decode()) self.assertEqual(res.status_code, 200) self.assertIn('success', data['status']) self.assertIn(UTILS.DELETED(TYPE, committment.id), data['message'])
def test_update_committment_incorrect_id(self): """ Ensure error is thrown if the id is incorrect for updating committment. """ UTILS.add_committment(self.fund_1.id, 1000) with self.client as client: response = client.put( '/committments/999', data=json.dumps({ 'fund_id': '1', }), content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 404) self.assertIn(UTILS.NOT_EXISTS(TYPE, '999'), data['message']) self.assertIn('fail', data['status']) self.assertFalse(data['data'])
def test_single_committment(self): """Ensure get single committment behaves correctly.""" committment = UTILS.add_committment(self.fund_1.id, 1000) with self.client as client: response = client.get(f'/committments/{committment.id}') data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(1000, data['data']['amount']) self.assertIn('success', data['status'])
def setUp(self): super().setUp() self.fund_1 = UTILS.add_fund('fund_1') self.fund_2 = UTILS.add_fund('fund_2') self.fund_3 = UTILS.add_fund('fund_3') self.fund_4 = UTILS.add_fund('fund_4') self.fund_5 = UTILS.add_fund('fund_5') self.committment_1 = UTILS.add_committment( 1, 1000, datetime.strptime('31/12/2017', '%d/%m/%Y')) self.committment_2 = UTILS.add_committment( 2, 1500, datetime.strptime('31/03/2018', '%d/%m/%Y')) self.committment_3 = UTILS.add_committment( 3, 1000, datetime.strptime('30/06/2018', '%d/%m/%Y')) self.committment_4 = UTILS.add_committment( 4, 1500, datetime.strptime('30/09/2018', '%d/%m/%Y')) self.committment_5 = UTILS.add_committment( 1, 1000, datetime.strptime('31/12/2018', '%d/%m/%Y')) self.capitalcall_1 = UTILS.add_capitalcall( 'investment_1', 950, datetime.strptime('31/01/2018', '%d/%m/%Y')) self.capitalcall_2 = UTILS.add_capitalcall( 'investment_2', 2000, datetime.strptime('30/04/2018', '%d/%m/%Y'))
def test_update_committment(self): """Ensure a committment amount can be updated in the database.""" committment = UTILS.add_committment(self.fund_2.id, 2000) with self.client as client: response = client.put(f'/committments/{committment.id}', data=json.dumps({'amount': 4000}), content_type='application/json') data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertIn( UTILS.UPDATED(TYPE, f'{committment.id} in fund {self.fund_2.id}'), data['message']) self.assertIn('success', data['status']) self.assertEqual(committment.id, data["data"]["id"]) self.assertEqual(4000, data["data"]["amount"])
def test_update_committment_no_change(self): """ Ensure correct response recieved for no change to updated committment """ committment = UTILS.add_committment(self.fund_1.id, 1000) with self.client as client: response = client.put( f'/committments/{committment.id}', data=json.dumps({ 'amount': 1000, }), content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertIn( UTILS.NO_CHANGE( TYPE, f'{committment.id} in fund {committment.fund_id}'), data['message'])
def post(self): post_data = request.get_json() if not post_data: return UTILS.api_response(msg=UTILS.INVALID_PAYLD, code=400, data=self.__name__) fund_id = post_data.get('fund_id') amount = post_data.get('amount') try: committment = UTILS.add_committment(fund_id, amount) return UTILS.api_response(msg=UTILS.ADDED( TYPE, f'{committment.id} in fund {fund_id}'), code=201, data=committment.to_json()) except exc.IntegrityError as e: db.session.rollback() return UTILS.api_response( msg=f'{UTILS.INTEGRITY_ERR} {self.__name__}', code=400, data=f'{str(e)}')
def seed_db(): """Seeds the database.""" db.session.add(User(username='******', email="*****@*****.**")) db.session.add(User(username='******', email="*****@*****.**")) db.session.commit() UTILS.add_fund('fund_1') UTILS.add_fund('fund_2') UTILS.add_fund('fund_3') UTILS.add_fund('fund_4') UTILS.add_fund('fund_5') committment_1 = UTILS.add_committment( 1, 10000000, datetime.strptime('31/12/2017', '%d/%m/%Y') ) committment_2 = UTILS.add_committment( 2, 15000000, datetime.strptime('31/03/2018', '%d/%m/%Y') ) UTILS.add_committment( 3, 10000000, datetime.strptime('30/06/2018', '%d/%m/%Y') ) UTILS.add_committment( 4, 15000000, datetime.strptime('30/09/2018', '%d/%m/%Y') ) UTILS.add_committment( 1, 10000000, datetime.strptime('31/12/2018', '%d/%m/%Y') ) capitalcall_1 = UTILS.add_capitalcall( 'investment_1', 9500000, datetime.strptime('31/01/2018', '%d/%m/%Y') ) capitalcall_2 = UTILS.add_capitalcall( 'investment_2', 10000000, datetime.strptime('30/04/2018', '%d/%m/%Y') ) UTILS.add_fundinvestment(9500000, committment_1, capitalcall_1) UTILS.add_fundinvestment(500000, committment_1, capitalcall_2) UTILS.add_fundinvestment(9500000, committment_2, capitalcall_2)