예제 #1
0
def add_new_stock():
    post_data = request.get_json()
    response_object = {'status': 'fail', 'message': 'Invalid payload.'}
    ticker = post_data.get('ticker')
    name = post_data.get('name')
    if not post_data:
        return jsonify(response_object), 400
    if ticker is None:
        response_object['message'] += ' Missing `ticker` key.'
        return jsonify(response_object), 400
    if name is None:
        response_object['message'] += ' Missing `name` key.'
        return jsonify(response_object), 400
    try:
        stock = get_stock(ticker)
        if not stock:
            add_stock(name, ticker)
            response_object['status'] = 'success'
            response_object[
                'message'] = f'Stock with ticker `{ticker}` was added!'
            return jsonify(response_object), 201
        else:
            response_object[
                'message'] = f'A stock with ticker `{ticker}` already exists.'
            return jsonify(response_object), 400
    except exc.IntegrityError:
        db.session.rollback()
        return jsonify(response_object), 400
예제 #2
0
    def test_get_revenue_data_update(self):
        """Ensure get_eps_data behaves correctly when the update query argument is supplied."""
        add_stock('Facebook, Inc.', 'FB')
        with self.client:
            response = self.client.get('/api/revenue/FB')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 404)
            self.assertIn('fail', data['status'])

            response = self.client.get('/api/revenue/FB?update=true')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertTrue(len(data['data']) > 0)
예제 #3
0
 def test_all_users(self):
     """Ensure get all stocks behaves correctly."""
     add_stock('Apple, Inc.', 'AAPL')
     add_stock('Facebook, Inc.', 'FB')
     with self.client:
         response = self.client.get('/api/stocks')
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 200)
         self.assertEqual(len(data['data']['stocks']), 2)
         self.assertIn('AAPL', data['data']['stocks'][0]['ticker'])
         self.assertIn('Apple, Inc.', data['data']['stocks'][0]['name'])
         self.assertIn('FB', data['data']['stocks'][1]['ticker'])
         self.assertIn('Facebook, Inc.', data['data']['stocks'][1]['name'])
         self.assertIn('success', data['status'])
예제 #4
0
    def test_single_stock(self):
        """Ensure get single stock behaves correctly, with both upper and lowercase."""
        add_stock('Apple, Inc.', 'AAPL')
        stock = get_stock('AAPL')
        with self.client:
            response = self.client.get(f'/api/stocks/{stock.ticker.lower()}')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertIn('AAPL', data['data']['ticker'])
            self.assertIn('Apple, Inc.', data['data']['name'])
            self.assertIn('success', data['status'])

            response = self.client.get(f'/api/stocks/{stock.ticker.upper()}')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertIn('AAPL', data['data']['ticker'])
            self.assertIn('Apple, Inc.', data['data']['name'])
            self.assertIn('success', data['status'])
예제 #5
0
    def test_get_revenue_data(self):
        """Ensure get_revenue_data behaves correctly."""
        add_stock('Facebook, Inc.', 'FB')
        test_revenues = [
            Revenues('FB', datetime(year=2015, month=1, day=15), 500),
            Revenues('FB', datetime(year=2015, month=5, day=3), 800),
            Revenues('FB', datetime(year=2015, month=9, day=8), 1200)
        ]
        with self.client:
            response = self.client.get('/api/revenue/FB')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 404)
            self.assertIn('fail', data['status'])
            for revenue in test_revenues:
                add_revenue(revenue.ticker, revenue.time, revenue.revenue)

            response = self.client.get('/api/revenue/FB')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertEqual(3, len(data['data']))
            for revenue in test_revenues:
                self.assertTrue(revenue.to_json() in data['data'])
예제 #6
0
    def test_get_eps_data(self):
        """Ensure get_eps_data behaves correctly."""
        add_stock('Facebook, Inc.', 'FB')
        test_earnings = [
            EPS('FB', datetime(year=2015, month=1, day=15), 1.25),
            EPS('FB', datetime(year=2015, month=5, day=3), 1.5),
            EPS('FB', datetime(year=2015, month=9, day=8), 1.75)
        ]
        with self.client:
            response = self.client.get('/api/eps/FB')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 404)
            self.assertIn('fail', data['status'])
            for earning in test_earnings:
                add_eps(earning.ticker, earning.time, earning.earnings)

            response = self.client.get('/api/eps/FB')
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertEqual(3, len(data['data']))
            for earning in test_earnings:
                self.assertTrue(earning.to_json() in data['data'])