Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore zero value usages #648

Merged
merged 2 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/ralph_scrooge/rest_api/public/v0_9/pricing_service_usages.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,20 +503,22 @@ def get_usages_for_save(pricing_service_usage):
pricing_service_usage['date']
)
)
daily_usage = DailyUsage(
date=pricing_service_usage['date'],
type=usage_type,
value=usage['value'],
daily_pricing_object=daily_pricing_object,
service_environment=(
daily_pricing_object.service_environment
),
remarks=usage.get('remarks', ''),
)
daily_usages.append(daily_usage)
usages_daily_pricing_objects[usage_type].append(
daily_pricing_object
)
if usage['value'] > 0:
daily_usage = DailyUsage(
date=pricing_service_usage['date'],
type=usage_type,
value=usage['value'],
daily_pricing_object=daily_pricing_object,
service_environment=(
daily_pricing_object.service_environment
),
remarks=usage.get('remarks', ''),
)
daily_usages.append(daily_usage)
usages_daily_pricing_objects[usage_type].append(
daily_pricing_object
)

return (daily_usages, usages_daily_pricing_objects)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,42 @@ def setUp(self):
self.client = APIClient()
self.client.force_authenticate(superuser)

def test_ignore_usages_with_zero_value(self):
usage_type2 = UsageTypeFactory()
usage_type3 = UsageTypeFactory()
pricing_service_usage = {
"pricing_service": self.pricing_service.name,
"date": self.date_as_str,
"usages": [
{
"pricing_object": self.pricing_object1.name,
"usages": [
{
"symbol": self.usage_type.symbol,
"value": 0.0,
},
{
"symbol": usage_type2.symbol,
"value": 0,
},
{
"symbol": usage_type3.symbol,
"value": 1,
},

],
},
]
}
resp = self.client.post(
reverse('create_pricing_service_usages'),
json.dumps(pricing_service_usage),
content_type='application/json',
)
self.assertEquals(resp.status_code, 201)
daily_usages = DailyUsage.objects.order_by('id')
self.assertEquals(daily_usages.count(), 1)

def test_save_usages_successfully_when_pricing_object_is_given(self):
pricing_service_usage = {
"pricing_service": self.pricing_service.name,
Expand Down Expand Up @@ -1264,6 +1300,7 @@ def test_pricing_service_usage_api_endpoint_with_filtering_by_service(self): #
self.assertEquals(resp.status_code, 200)
received_response = json.loads(resp.content)
usages = received_response['usages']
usages.sort()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was failing randomly - sorting usages fixes it.


self.assertEqual(len(usages), 2)
self.assertEqual(usages[0]['service_uid'], service1_uid)
Expand Down