Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #195 from mkurek/fix/api-services-logger-error
Browse files Browse the repository at this point in the history
fixed services usages api logging
  • Loading branch information
kula1922 committed Jul 2, 2014
2 parents 866b101 + 892c51f commit 02c6840
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
21 changes: 15 additions & 6 deletions src/ralph_pricing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(self, service=None, venture_usages=None, date=None, **kwargs):
self.service = service
self.date = date
self.venture_usages = venture_usages or []
self.overwrite = None
self.overwrite = 'no' # default overwrite

def to_dict(self):
result = {
Expand Down Expand Up @@ -199,7 +199,7 @@ class ServiceUsageResource(Resource):
"""
service = fields.CharField(attribute='service')
date = fields.DateTimeField(attribute='date')
overwrite = fields.CharField(attribute='overwrite', default='values_only')
overwrite = fields.CharField(attribute='overwrite')
venture_usages = fields.ToManyField(
VentureUsageResource,
'venture_usages',
Expand Down Expand Up @@ -229,7 +229,9 @@ def save_usages(cls, service_usages):
Service.objects.get(symbol=service_usages.service)
except Service.DoesNotExist:
raise ImmediateHttpResponse(
response=http.HttpBadRequest("Invalid service symbol")
response=http.HttpBadRequest(
"Invalid service symbol: {}".format(service_usages.service)
)
)

# check if date is properly set
Expand Down Expand Up @@ -260,21 +262,28 @@ def save_usages(cls, service_usages):
except UsageType.DoesNotExist:
raise ImmediateHttpResponse(
response=http.HttpBadRequest(
"Invalid usage type symbol"
"Invalid usage type symbol: {}".format(
usage.symbol,
)
)
)
except Venture.DoesNotExist:
raise ImmediateHttpResponse(
response=http.HttpBadRequest(
"Invalid venture symbol or venture is inactive"
"Invalid or inactive venture (symbol: {})".format(
venture_usages.venture
)
)
)
logger.error(
logger.info(
"Saving usages for service {0}".format(service_usages.service)
)

# remove previous daily usages
if service_usages.overwrite in ('values_only', 'delete_all_previous'):
logger.debug('Remove previous values ({})'.format(
service_usages.overwrite,
))
for usage_type, ventures in usages_ventures.iteritems():
previuos_usages = DailyUsage.objects.filter(
date=service_usages.date,
Expand Down
27 changes: 19 additions & 8 deletions src/ralph_pricing/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_to_dict(self):
self.assertEquals(service_usages.to_dict(), {
'service': self.service.symbol,
'date': self.date,
'overwrite': None,
'overwrite': 'no',
'venture_usages': [
{
'venture': self.venture1.symbol,
Expand Down Expand Up @@ -172,21 +172,26 @@ def test_api(self):
def test_api_invalid_service(self):
service_usages = self._get_sample_service_usages_object()
data = service_usages.to_dict()
data['service'] = 'invalid_service'
service_symbol = 'invalid_service'
data['service'] = service_symbol
resp = self.api_client.post(
'/scrooge/api/v0.9/{0}/'.format(self.resource),
format='json',
authentication=self.api_key,
data=data
)
self.assertEquals(resp.status_code, 400)
self.assertEquals(resp.content, 'Invalid service symbol')
self.assertEquals(
resp.content,
'Invalid service symbol: {}'.format(service_symbol)
)
self.assertEquals(DailyUsage.objects.count(), 0)

def test_api_invalid_venture(self):
service_usages = self._get_sample_service_usages_object()
data = service_usages.to_dict()
data['venture_usages'][1]['venture'] = 'invalid_venture'
venture_symbol = 'invalid_venture'
data['venture_usages'][1]['venture'] = venture_symbol
resp = self.api_client.post(
'/scrooge/api/v0.9/{0}/'.format(self.resource),
format='json',
Expand All @@ -196,22 +201,26 @@ def test_api_invalid_venture(self):
self.assertEquals(resp.status_code, 400)
self.assertEquals(
resp.content,
'Invalid venture symbol or venture is inactive'
'Invalid or inactive venture (symbol: {})'.format(venture_symbol)
)
self.assertEquals(DailyUsage.objects.count(), 0)

def test_api_invalid_usage(self):
service_usages = self._get_sample_service_usages_object()
data = service_usages.to_dict()
data['venture_usages'][1]['usages'][1]['symbol'] = 'invalid_usage'
usage_type_symbol = 'invalid_usage'
data['venture_usages'][1]['usages'][1]['symbol'] = usage_type_symbol
resp = self.api_client.post(
'/scrooge/api/v0.9/{0}/'.format(self.resource),
format='json',
authentication=self.api_key,
data=data
)
self.assertEquals(resp.status_code, 400)
self.assertEquals(resp.content, 'Invalid usage type symbol')
self.assertEquals(
resp.content,
'Invalid usage type symbol: {}'.format(usage_type_symbol)
)
self.assertEquals(DailyUsage.objects.count(), 0)

def test_api_inactive_venture(self):
Expand All @@ -227,7 +236,9 @@ def test_api_inactive_venture(self):
self.assertEquals(resp.status_code, 400)
self.assertEquals(
resp.content,
'Invalid venture symbol or venture is inactive'
'Invalid or inactive venture (symbol: {})'.format(
self.inactive_venture.symbol,
)
)
self.assertEquals(DailyUsage.objects.count(), 0)

Expand Down

0 comments on commit 02c6840

Please sign in to comment.