def test_team_average_cost_no_price(self): team_avg = models.Team(name='TeamAVG', billing_type='AVERAGE') team_avg.save() # daily cost: 200 up = models.UsagePrice( type=self.usage_type, cost=3000, forecast_cost=5000, start=date(2013, 10, 16), end=date(2013, 10, 30), team=team_avg, ) up.save() result = self.plugin._get_team_cost_per_venture( start=date(2013, 11, 2), end=date(2013, 11, 5), team=team_avg, usage_type=self.usage_type, ventures=self.ventures, forecast=False, no_price_msg=True, ) cost_key = 'ut_{0}_team_{1}_cost'.format( self.usage_type.id, team_avg.id, ) percent_key = 'ut_{0}_team_{1}_percent'.format( self.usage_type.id, team_avg.id, ) self.assertEquals(result, { self.venture1.id: { cost_key: _('No price'), percent_key: D(0), }, self.venture2.id: { cost_key: _('No price'), percent_key: D(0), }, self.venture3.id: { cost_key: _('No price'), percent_key: D(0), }, })
def setUp(self): # set up decimal precision to make comparisons easier decimal_context = getcontext() self.__prev_decimal_prec = decimal_context.prec decimal_context.prec = 4 self.plugin = Team # usage type self.usage_type = models.UsageType( name='Teams', symbol='Teams', by_team=True, type='BU', ) self.usage_type.save() # teams self.team_time = models.Team( name='T1', billing_type='TIME', show_percent_column=True, ) self.team_time.save() self.team_devices_cores = models.Team( name='T2', billing_type='DEVICES_CORES', ) self.team_devices_cores.save() self.team_devices = models.Team( name='T3', billing_type='DEVICES', ) self.team_devices.save() self.team_distribute = models.Team( name='T4', billing_type='DISTRIBUTE', ) self.team_distribute.save() self.teams = models.Team.objects.all() # dateranges self.daterange1 = models.TeamDaterange( team=self.team_time, start=date(2013, 10, 1), end=date(2013, 10, 10), ) self.daterange1.save() self.daterange2 = models.TeamDaterange( team=self.team_time, start=date(2013, 10, 11), end=date(2013, 10, 30), ) self.daterange2.save() # costs # team time up = models.UsagePrice( type=self.usage_type, cost=300, forecast_cost=600, start=date(2013, 10, 1), end=date(2013, 10, 15), team=self.team_time, team_members_count=10, ) up.save() up = models.UsagePrice( type=self.usage_type, cost=900, forecast_cost=450, start=date(2013, 10, 16), end=date(2013, 10, 30), team=self.team_time, team_members_count=20, ) up.save() up = models.UsagePrice( type=self.usage_type, cost=300, forecast_cost=600, start=date(2013, 10, 1), end=date(2013, 10, 30), team=self.team_devices_cores, team_members_count=20, ) up.save() up = models.UsagePrice( type=self.usage_type, cost=800, forecast_cost=1600, start=date(2013, 10, 1), end=date(2013, 10, 10), team=self.team_devices, team_members_count=20, ) up.save() up = models.UsagePrice( type=self.usage_type, cost=100, forecast_cost=200, start=date(2013, 10, 11), end=date(2013, 10, 30), team=self.team_devices, team_members_count=10, ) up.save() up = models.UsagePrice( type=self.usage_type, cost=3000, forecast_cost=1500, start=date(2013, 10, 1), end=date(2013, 10, 15), team=self.team_distribute, team_members_count=10, ) up.save() up = models.UsagePrice( type=self.usage_type, cost=6000, forecast_cost=3000, start=date(2013, 10, 16), end=date(2013, 10, 30), team=self.team_distribute, team_members_count=10, ) up.save() # ventures self.venture1 = models.Venture(name='V1', venture_id=1, is_active=True) self.venture1.save() self.venture2 = models.Venture(name='V2', venture_id=2, is_active=True) self.venture2.save() self.venture3 = models.Venture(name='V3', venture_id=3, is_active=True) self.venture3.save() self.ventures = models.Venture.objects.all() # ventures percentage (only for time team) percentage = ( (self.daterange1, [30, 30, 40]), (self.daterange2, [20, 50, 30]), ) for team_daterange, percent in percentage: for venture, p in zip(self.ventures, percent): tvp = models.TeamVenturePercent( team_daterange=team_daterange, venture=venture, percent=p, ) tvp.save()
def test_team_average_cost( self, distributed_mock, devices_mock, devices_cores_mock, time_team_mock, ): team_avg = models.Team(name='TeamAVG', billing_type='AVERAGE') team_avg.save() team_percent_key = lambda t: 'ut_{0}_team_{1}_percent'.format( self.usage_type.id, t.id, ) time_percent_key = team_percent_key(self.team_time) time_team_mock.return_value = { self.venture1.id: {time_percent_key: D('0.3')}, self.venture2.id: {time_percent_key: D('0.3')}, self.venture3.id: {time_percent_key: D('0.4')}, } devices_cores_percent_key = team_percent_key(self.team_devices_cores) devices_cores_mock.return_value = { self.venture1.id: {devices_cores_percent_key: D('1.0')}, self.venture2.id: {devices_cores_percent_key: D('0')}, } devices_percent_key = team_percent_key(self.team_devices) devices_mock.return_value = { self.venture1.id: {devices_percent_key: D('0.1')}, self.venture2.id: {devices_percent_key: D('0.3')}, self.venture3.id: {devices_percent_key: D('0.6')}, } distribted_percent_key = team_percent_key(self.team_distribute) distributed_mock.return_value = { self.venture2.id: {distribted_percent_key: D('0.3')}, self.venture3.id: {distribted_percent_key: D('0.7')}, } # daily cost: 100 up = models.UsagePrice( type=self.usage_type, cost=1500, forecast_cost=2000, start=date(2013, 10, 1), end=date(2013, 10, 15), team=team_avg, ) up.save() # daily cost: 200 up = models.UsagePrice( type=self.usage_type, cost=3000, forecast_cost=5000, start=date(2013, 10, 16), end=date(2013, 10, 30), team=team_avg, ) up.save() result = self.plugin._get_team_cost_per_venture( start=date(2013, 10, 6), end=date(2013, 10, 25), team=team_avg, usage_type=self.usage_type, ventures=self.ventures, forecast=False, no_price_msg=True, ) cost_key = 'ut_{0}_team_{1}_cost'.format( self.usage_type.id, team_avg.id, ) percent_key = 'ut_{0}_team_{1}_percent'.format( self.usage_type.id, team_avg.id, ) # venture1: 0.3 + 1.0 + 0.1 = 1.4 # venture2: 0.3 + 0 + 0.3 + 0.3 = 0.9 # venture3: 0.4 + 0.6 + 0.7 = 1.7 # cost between 6 and 15: 100 * 10 = 1000 # cost between 16 and 25: 200 * 10 = 2000 self.assertEquals(result, { self.venture1.id: { cost_key: D('1050'), # 1000 * 1.4 / 4 + 2000 * 1.4 / 4 percent_key: D('0.35'), # 1050 / 3000 }, self.venture2.id: { cost_key: D('675'), # 1000 * 0.9 / 4 + 2000 * 0.9 / 4 percent_key: D('0.225'), # 675 / 3000 }, self.venture3.id: { cost_key: D('1275'), # 1000 * 1.7 / 4 + 2000 * 1.7 / 4 percent_key: D('0.425'), # 1275 / 3000 }, })