Ejemplo n.º 1
0
 def test_create_price_when_date_has_wrong_format(self):
     self.price_data['date_from'] = '19-11-2020'
     with self.assertRaisesMessage(
         ValueError,
         "time data '19-11-2020' does not match format '%Y-%m-%d'"
     ):
         create_prices(self.price_data, 'USD')
Ejemplo n.º 2
0
 def test_create_price_when_to_date_less_than_from_date(self):
     self.price_data['date_to'] = '2020-11-19'
     with self.assertRaisesMessage(
         ValueError,
         "`date_to` must be greater than equal to `date_from`"
     ):
         create_prices(self.price_data, 'USD')
Ejemplo n.º 3
0
    def test_create_price_when_invalid_price(self):
        self.price_data['price'] = -1
        with self.assertRaisesMessage(
            ValueError,
            "Price should be positive number"
        ):
            create_prices(self.price_data, 'USD')

        self.price_data['price'] = 'abc'
        with self.assertRaisesMessage(
            ValueError,
            "Please provide valid price"
        ):
            create_prices(self.price_data, 'USD')
Ejemplo n.º 4
0
def create_prices_view(request):
    """
    Fare list for given dates between origin and destination
    """
    # todo: this block can be moved into custom exception handler middleware
    # https://www.django-rest-framework.org/api-guide/exceptions/
    try:
        is_created, message = create_prices(request.data,
                                            request.GET.get('currency', 'USD'))
    except (ValueError, TypeError) as e:
        return Response({'error': str(e)}, status.HTTP_400_BAD_REQUEST)
    if is_created:
        return Response(message, status.HTTP_201_CREATED)
    return Response(message, status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 5
0
 def test_create_price_when_inr_currency(self, req_mock):
     res = mock.MagicMock()
     res.ok.return_value = True
     res.json.return_value = {
         'rates': {
             'INR': 74
         }
     }
     req_mock.return_value = res
     self.price_data['date_to'] = '2020-11-20'
     is_created, message = create_prices(self.price_data, 'INR')
     self.assertTrue(is_created)
     self.assertEqual(message, {'success': True})
     self.assertEqual(1, Price.objects.count())
     self.assertEqual(Price.objects.first().price, 2)
Ejemplo n.º 6
0
 def test_create_price(self):
     is_created, message = create_prices(self.price_data, 'USD')
     self.assertTrue(is_created)
     self.assertEqual(message, {'success': True})
     self.assertEqual(5, Price.objects.count())