Ejemplo n.º 1
0
    def delete(self, currency):
        active_currency = Sources().get_rates([currency])

        if active_currency[currency]:
            deleteds = Sources().delete_rates([currency])
            return deleteds, 204
        else:
            raise Exception('Currency not found', 404)
Ejemplo n.º 2
0
 def test_actions_currencies(self):
     result1 = Sources().delete_rates(['USD', 'BRL'])
     result2 = Sources().get_rates(['USD', 'BRL'])
     self.assertTrue('USD' in result2)
     self.assertTrue('BRL' in result2)
     self.assertIsNone(result2['USD'])
     self.assertIsNone(result2['BRL'])
     result3 = Sources().add_rates(['USD', 'BRL'])
     self.assertTrue('USD' in result3)
     self.assertTrue('BRL' in result3)
     self.assertIsNotNone(result3['USD'])
     self.assertIsNotNone(result3['BRL'])
Ejemplo n.º 3
0
 def test_post_currencies(self):
     Sources().delete_rates(['BRL'])
     response = self.client.post('/api/v1/currencies',
                                 json={"currency": "BRL"})
     object = response.get_json()
     self.assertEqual(response.status_code, 201)
     self.assertEqual('BRL', object['currency'])
Ejemplo n.º 4
0
 def test_get_currencies(self):
     Sources().add_rates(['BRL'])
     response = self.client.get("/api/v1/currencies",
                                json={'currency': 'BRL'})
     object = response.get_json()
     self.assertEqual(response.status_code, 200)
     self.assertEqual('BRL', object['currency'])
Ejemplo n.º 5
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('currency',
                            type=str,
                            required=True,
                            help="currency cannot be blank!")
        args = parser.parse_args()

        active_currency = Sources().get_rates([args.get('currency')])

        if active_currency[args.get('currency')]:
            currency = active_currency[args.get('currency')]
            return Rate().load(data=currency), 201
        else:
            new_currency = Sources().add_rates([args.get('currency')])
            if new_currency[args.get('currency')]:
                # return new_currency[args.get('currency')], 201
                currency = new_currency[args.get('currency')]
                return Rate().load(data=currency), 201
            else:
                raise Exception('Unknow error, please try again')
Ejemplo n.º 6
0
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('from',
                            type=str,
                            required=True,
                            help="from cannot be blank!")
        parser.add_argument('to',
                            type=str,
                            required=True,
                            help="from cannot be blank!")
        parser.add_argument('amount',
                            type=float,
                            required=True,
                            help="from cannot be blank!")
        args = parser.parse_args()

        rates = Sources().get_rates(
            currencies=[args.get('from'), args.get('to')])

        if rates[args.get('from')] is None:
            raise Exception(args.get('from') + ' not avaliable')

        if rates[args.get('to')] is None:
            raise Exception(args.get('to') + ' not avaliable')

        from_rate = float(rates[args.get('from')]['rate'])
        to_rate = float(rates[args.get('to')]['rate'])
        rate = from_rate * to_rate if 1 / from_rate > 1 / to_rate else (
            1 / to_rate) / from_rate

        value = rate * float(args.get('amount'))

        output = {
            'from': {
                'currency': args.get('from'),
                'value': args.get('amount')
            },
            'to': {
                'currency': args.get('to'),
                'value': value
            },
            'rates': {rate: {
                'rate': rates[rate]['rate']
            }
                      for rate in rates}
        }

        return output, 200
Ejemplo n.º 7
0
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('currency',
                            type=str,
                            required=True,
                            help="currency cannot be blank!")
        args = parser.parse_args()

        active_currency = Sources().get_rates([args.get('currency')])

        if active_currency[args.get('currency')]:
            currency = active_currency[args.get('currency')]
            output = Rate().load(data=currency)
            return output, 200
        else:
            raise Exception('Currency not found', 404)
Ejemplo n.º 8
0
 def test_get_exchanges(self):
     Sources().add_rates(['USD', 'BRL'])
     self.RESOURCE_URL = "/api/v1/exchanges"
     response = self.client.get(self.RESOURCE_URL,
                                json={
                                    'from': 'USD',
                                    'to': 'BRL',
                                    'amount': 123.45
                                })
     self.assertEqual(response.status_code, 200)
     self.assertDictEqual(
         response.get_json(), {
             'from': {
                 'currency': 'USD',
                 'value': 123.45
             },
             'to': {
                 'currency': 'BRL',
                 'value': 688.51
             }
         })
Ejemplo n.º 9
0
 def test_delete_currencies(self):
     Sources().add_rates(['BRL'])
     response = self.client.delete('/api/v1/currencies/BRL')
     self.assertEqual(response.status_code, 204)
Ejemplo n.º 10
0
def defaultize():
    Sources().add_rates(Sources.standard_currencies)
    pass