def test_new_estimates_are_created_in_new_month(self): month_start = datetime.datetime(2016, 9, 1, 0, 0) month_end = datetime.datetime(2016, 9, 30, 23, 59, 59) calculation_time = datetime.datetime(2016, 9, 1, 1, 0) with freeze_time(calculation_time): tasks.recalculate_estimate() price_estimates = [ models.PriceEstimate.objects.get_current(scope=scope) for scope in (self.resource, self.service, self.spl, self.project, self.customer) ] total_working_minutes = int( (month_end - month_start).total_seconds() / 60) expected_total = total_working_minutes * self.price_list_item.minute_rate * self.resource.disk for price_estimate in price_estimates: message = 'Price estimate "total" is calculated wrongly for "%s". Real value: %s, expected: %s.' % ( price_estimate.scope, price_estimate.total, expected_total) self.assertAlmostEqual(price_estimate.total, expected_total, msg=message) working_minutes = int( (calculation_time - month_start).total_seconds() / 60) expected_consumed = working_minutes * self.price_list_item.minute_rate * self.resource.disk for price_estimate in price_estimates: message = 'Price estimate "consumed" is calculated wrongly for "%s". Real value: %s, expected: %s.' % ( price_estimate.scope, price_estimate.consumed, expected_consumed) self.assertAlmostEqual(price_estimate.consumed, expected_consumed, msg=message)
def test_consumed_is_recalculated_properly_for_ancestors(self): with freeze_time(self.start_time): self.second_resource = structure_factories.TestNewInstanceFactory( disk=10 * 1024, service_project_link=self.spl) calculation_time = datetime.datetime(2016, 8, 8, 15, 0) with freeze_time(calculation_time): tasks.recalculate_estimate() price_estimates = [ models.PriceEstimate.objects.get_current(scope=ancestor) for ancestor in (self.customer, self.service, self.spl, self.project) ] working_minutes = (calculation_time - self.start_time).total_seconds() / 60 # each ancestor is connected with 2 resources expected = working_minutes * self.price_list_item.minute_rate * ( self.resource.disk + self.second_resource.disk) for price_estimate in price_estimates: message = 'Price estimate "consumed" is calculated wrongly for "%s". Real value: %s, expected: %s.' % ( price_estimate.scope, price_estimate.consumed, expected) self.assertAlmostEqual(price_estimate.consumed, expected, msg=message)
def recalulate_current_estimates(self, request): tasks.recalculate_estimate(recalculate_total=True) self.message_user( request, _('Total and consumed value were successfully recalculated for all price estimates.' )) return redirect( reverse('admin:cost_tracking_defaultpricelistitem_changelist'))
def test_consumed_is_recalculated_properly_for_resource(self): calculation_time = datetime.datetime(2016, 8, 8, 15, 0) with freeze_time(calculation_time): tasks.recalculate_estimate() price_estimate = models.PriceEstimate.objects.get_current( scope=self.resource) working_minutes = (calculation_time - self.start_time).total_seconds() / 60 expected = working_minutes * self.price_list_item.minute_rate * self.resource.disk self.assertAlmostEqual(price_estimate.consumed, expected)
def handle(self, *args, **options): today = timezone.now() with transaction.atomic(): # Delete current month price estimates models.PriceEstimate.objects.filter(month=today.month, year=today.year).delete() # Create new estimates for resources and ancestors for resource_model in CostTrackingRegister.registered_resources: for resource in resource_model.objects.all(): configuration = CostTrackingRegister.get_configuration(resource) date = max(core_utils.month_start(today), resource.created) models.PriceEstimate.create_historical(resource, configuration, date) # recalculate consumed estimate tasks.recalculate_estimate()
def test_estimated_is_removed_on_resource_unlink(self): tasks.recalculate_estimate( ) # recalculate to create new estimates in new month. self.resource.unlink() self.resource.delete() self.assertFalse( models.PriceEstimate.objects.filter(scope=self.resource).exists()) details_names = [ estimate.details.get('name') for estimate in models.PriceEstimate.objects.all() ] self.assertNotIn(self.resource.name, details_names) for scope in (self.spl, self.service, self.project, self.customer): for price_estimate in models.PriceEstimate.objects.filter( scope=scope): self.assertEqual(price_estimate.total, 0)