def test_all_funds(self): """Ensure get all funds behaves correctly.""" fund1 = UTILS.add_fund('fund_1') fund2 = UTILS.add_fund('fund_2') with self.client as client: response = client.get('/funds') data = json.loads(response.data.decode()) funds = data['data']['funds'] self.assertEqual(response.status_code, 200) self.assertEqual(len(funds), 2) self.assertEqual(fund1.name, funds[0]['name']) self.assertEqual(fund2.name, funds[1]['name']) self.assertEqual([], funds[0]['committments']) self.assertEqual([], funds[1]['committments']) self.assertIn('success', data['status'])
def test_update_fund_no_id(self): """Ensure error is thrown if an id is not provided.""" UTILS.add_fund("fund!") with self.client as client: response = client.put( '/funds/blah', data=json.dumps({ 'name': 'fund_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_update_fund_duplicate_name(self): """Ensure error is thrown if the updated fund already exists.""" fund_1 = UTILS.add_fund("fund_1") fund_2 = UTILS.add_fund("fund_!") with self.client as client: response = client.put( f'/funds/{fund_2.id}', data=json.dumps({ 'name': 'fund_1', }), content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertIn(UTILS.EXISTS(TYPE, 'fund_1'), data['message']) self.assertIn('fail', data['status']) self.assertEqual(fund_1.id, data["data"]["id"])
def test_single_fund(self): """Ensure get single fund behaves correctly.""" fund = UTILS.add_fund('fund_1') with self.client as client: response = client.get(f'/funds/{fund.id}') data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertIn('fund_1', data['data']['name']) self.assertIn('success', data['status'])
def test_delete_fund(self): """Ensure fund is deleted""" fund = UTILS.add_fund('fund_1') with self.client as client: res = client.delete(f'/funds/{fund.id}') data = json.loads(res.data.decode()) self.assertEqual(res.status_code, 200) self.assertIn('success', data['status']) self.assertIn(UTILS.DELETED(TYPE, fund.id), data['message'])
def test_update_fund_invalid_json(self): """Ensure error is thrown if the JSON object is empty.""" fund = UTILS.add_fund('fund_!') with self.client as client: response = client.put(f'/funds/{fund.id}', data=json.dumps({}), content_type='application/json') data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertIn(UTILS.INVALID_PAYLD, data['message']) self.assertIn('fail', data['status']) self.assertFalse(data['data'])
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_fund_no_change(self): """Ensure correct response recieved for no change to updated fund""" fund = UTILS.add_fund('fund_1') with self.client as client: response = client.put( f'/funds/{fund.id}', data=json.dumps({ 'name': 'fund_1', }), content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertIn(UTILS.EXISTS(TYPE, 'fund_1'), data['message'])
def test_update_fund(self): """Ensure a fund name can be updated in the database.""" fund = UTILS.add_fund('fund1') with self.client as client: response = client.put(f'/funds/{fund.id}', data=json.dumps({ 'name': 'fund_1', }), content_type='application/json') data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertIn(UTILS.UPDATED(TYPE, 'fund_1'), data['message']) self.assertIn('success', data['status']) self.assertEqual(fund.id, data["data"]["id"]) self.assertEqual('fund_1', data["data"]["name"])
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)
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__) name = post_data.get('name') try: fund = Fund.query.filter_by(name=name).first() if not fund: fund = UTILS.add_fund(name) return UTILS.api_response(msg=UTILS.ADDED(TYPE, name), code=201, data=fund.to_json()) else: return UTILS.api_response(msg=UTILS.EXISTS(TYPE, name), code=400, data=fund.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 setUp(self): super().setUp() self.fund_1 = UTILS.add_fund('fund_1') self.fund_2 = UTILS.add_fund('fund_2')