Ejemplo n.º 1
0
 def test_get_service_extra_cost(self, plugin_run_mock):
     plugin_run_mock.side_effect = [122]
     self.assertEqual(
         122,
         ServicePlugin._get_service_extra_cost(datetime.date(2013, 10, 10),
                                               datetime.date(2013, 10, 20),
                                               [self.venture1]))
     plugin_run_mock.side_effect = KeyError()
     self.assertEqual(
         D(0),
         ServicePlugin._get_service_extra_cost(datetime.date(2013, 10, 10),
                                               datetime.date(2013, 10, 20),
                                               [self.venture1]))
Ejemplo n.º 2
0
 def test_schema(self):
     result = ServicePlugin(service=self.service, type='schema')
     self.assertEquals(
         result,
         OrderedDict([
             ('sut_3_count', {
                 'name': _('ServiceUsageType1 count'),
                 'divide_by': 5,
                 'rounding': 2,
             }),
             ('sut_3_cost', {
                 'name': _('ServiceUsageType1 cost'),
                 'currency': True,
                 'total_cost': False,
             }),
             ('sut_4_count', {
                 'name': _('ServiceUsageType2 count'),
                 'divide_by': 0,
                 'rounding': 0,
             }),
             ('sut_4_cost', {
                 'name': _('ServiceUsageType2 cost'),
                 'currency': True,
                 'total_cost': False,
             }),
             ('1_service_cost', {
                 'name': _('Service1 cost'),
                 'currency': True,
                 'total_cost': True,
             }),
         ]))
Ejemplo n.º 3
0
 def test_usage(self):
     result = ServicePlugin(
         service=self.service,
         start=datetime.date(2013, 10, 10),
         end=datetime.date(2013, 10, 20),
         ventures=self.ventures,
         forecast=False,
     )
     self.assertEquals(
         result, {
             3: {
                 'sut_3_count': 220.0,
                 'sut_3_cost': D('1510'),
                 'sut_4_count': 440.0,
                 'sut_4_cost': D('2730'),
                 '1_service_cost': D('4240'),
             },
             4: {
                 'sut_3_count': 440.0,
                 'sut_3_cost': D('3020'),
                 'sut_4_count': 880.0,
                 'sut_4_cost': D('5460'),
                 '1_service_cost': D('8480'),
             },
         })
Ejemplo n.º 4
0
 def test_get_service_extra_cost(self, plugin_run_mock):
     plugin_run_mock.side_effect = [122]
     self.assertEqual(
         122,
         ServicePlugin._get_service_extra_cost(
             datetime.date(2013, 10, 10),
             datetime.date(2013, 10, 20),
             [self.venture1]
         )
     )
     plugin_run_mock.side_effect = KeyError()
     self.assertEqual(
         D(0),
         ServicePlugin._get_service_extra_cost(
             datetime.date(2013, 10, 10),
             datetime.date(2013, 10, 20),
             [self.venture1]
         )
     )
Ejemplo n.º 5
0
 def test_get_usage_type_cost_with_exception(self, plugin_run_mock):
     for exc in (KeyError(), AttributeError()):
         plugin_run_mock.side_effect = exc
         result = ServicePlugin._get_usage_type_cost(
             start=datetime.date(2013, 10, 10),
             end=datetime.date(2013, 10, 20),
             usage_type=self.usage_type,
             forecast=True,
             ventures=self.ventures,
         )
         self.assertEquals(result, 0)
Ejemplo n.º 6
0
 def test_get_service_base_usage_types_cost_forecast(self):
     result = ServicePlugin._get_service_base_usage_types_cost(
         start=datetime.date(2013, 10, 10),
         end=datetime.date(2013, 10, 20),
         service=self.service,
         forecast=True,
         ventures=[self.venture1],
     )
     # aggregate from test_get_usage_type_cost_by_cost_forecast and
     # test_get_usage_type_cost_forecast
     self.assertEquals(result, D('8580'))
Ejemplo n.º 7
0
 def test_get_usage_type_cost_with_exception(self, plugin_run_mock):
     for exc in (KeyError(), AttributeError()):
         plugin_run_mock.side_effect = exc
         result = ServicePlugin._get_usage_type_cost(
             start=datetime.date(2013, 10, 10),
             end=datetime.date(2013, 10, 20),
             usage_type=self.usage_type,
             forecast=True,
             ventures=self.ventures,
         )
         self.assertEquals(result, 0)
Ejemplo n.º 8
0
 def test_total_cost(self):
     self.assertEqual(
         D(4240),
         ServicePlugin.total_cost(
             datetime.date(2013, 10, 10),
             datetime.date(2013, 10, 20),
             self.service,
             False,
             [self.venture1]
         )
     )
Ejemplo n.º 9
0
 def test_get_service_base_usage_types_cost_forecast(self):
     result = ServicePlugin._get_service_base_usage_types_cost(
         start=datetime.date(2013, 10, 10),
         end=datetime.date(2013, 10, 20),
         service=self.service,
         forecast=True,
         ventures=[self.venture1],
     )
     # aggregate from test_get_usage_type_cost_by_cost_forecast and
     # test_get_usage_type_cost_forecast
     self.assertEquals(result, D('8580'))
Ejemplo n.º 10
0
 def test_get_date_ranges_percentage(self):
     result = ServicePlugin._get_date_ranges_percentage(
         start=datetime.date(2013, 10, 10),
         end=datetime.date(2013, 10, 20),
         service=self.service,
     )
     self.assertEquals(result, {
         (datetime.date(2013, 10, 10), datetime.date(2013, 10, 15)): {
             3: 30.0,
             4: 70.0,
         },
         (datetime.date(2013, 10, 16), datetime.date(2013, 10, 20)): {
             3: 40.0,
             4: 60.0,
         },
     })
Ejemplo n.º 11
0
 def test_get_date_ranges_percentage(self):
     result = ServicePlugin._get_date_ranges_percentage(
         start=datetime.date(2013, 10, 10),
         end=datetime.date(2013, 10, 20),
         service=self.service,
     )
     self.assertEquals(result, {
         (datetime.date(2013, 10, 10), datetime.date(2013, 10, 15)): {
             3: 30.0,
             4: 70.0,
         },
         (datetime.date(2013, 10, 16), datetime.date(2013, 10, 20)): {
             3: 40.0,
             4: 60.0,
         },
     })
Ejemplo n.º 12
0
    def test_total_cost(self):
        # add regular usage type
        regular_usage_type = models.UsageType(
            name='RUsageType1',
            symbol='rut1',
            by_cost=True,
            type='RU',
        )
        regular_usage_type.save()
        models.DailyUsage(
            type=regular_usage_type,
            pricing_venture=self.venture1,
            value=100,
            date=datetime.date(2013, 10, 11)
        ).save()
        models.DailyUsage(
            type=regular_usage_type,
            pricing_venture=self.venture2,
            value=300,
            date=datetime.date(2013, 10, 12)
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type1,
            service=self.service,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 30),
            percent=100,
        ).save()
        models.UsagePrice(
            type=regular_usage_type,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 30),
            cost=1000
        ).save()
        self.service.regular_usage_types.add(regular_usage_type)
        self.service.save()

        self.assertEqual(
            D(4490),
            ServicePlugin.total_cost(
                datetime.date(2013, 10, 10),
                datetime.date(2013, 10, 20),
                self.service,
                False,
                [self.venture1]
            )
        )
Ejemplo n.º 13
0
 def test_get_usage_type_cost(self, plugin_run_mock):
     plugin_run_mock.return_value = 100
     result = ServicePlugin._get_usage_type_cost(
         start=datetime.date(2013, 10, 10),
         end=datetime.date(2013, 10, 20),
         usage_type=self.usage_type,
         forecast=True,
         ventures=self.ventures,
     )
     self.assertEquals(result, 100)
     plugin_run_mock.assert_called_with(
         'reports',
         self.usage_type.get_plugin_name(),
         start=datetime.date(2013, 10, 10),
         end=datetime.date(2013, 10, 20),
         usage_type=self.usage_type,
         forecast=True,
         ventures=self.ventures,
         type='total_cost',
     )
Ejemplo n.º 14
0
 def test_get_usage_type_cost(self, plugin_run_mock):
     plugin_run_mock.return_value = 100
     result = ServicePlugin._get_usage_type_cost(
         start=datetime.date(2013, 10, 10),
         end=datetime.date(2013, 10, 20),
         usage_type=self.usage_type,
         forecast=True,
         ventures=self.ventures,
     )
     self.assertEquals(result, 100)
     plugin_run_mock.assert_called_with(
         'reports',
         self.usage_type.get_plugin_name(),
         start=datetime.date(2013, 10, 10),
         end=datetime.date(2013, 10, 20),
         usage_type=self.usage_type,
         forecast=True,
         ventures=self.ventures,
         type='total_cost',
     )
Ejemplo n.º 15
0
    def test_total_cost(self):
        # add regular usage type
        regular_usage_type = models.UsageType(
            name='RUsageType1',
            symbol='rut1',
            by_cost=True,
            type='RU',
        )
        regular_usage_type.save()
        models.DailyUsage(type=regular_usage_type,
                          pricing_venture=self.venture1,
                          value=100,
                          date=datetime.date(2013, 10, 11)).save()
        models.DailyUsage(type=regular_usage_type,
                          pricing_venture=self.venture2,
                          value=300,
                          date=datetime.date(2013, 10, 12)).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type1,
            service=self.service,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 30),
            percent=100,
        ).save()
        models.UsagePrice(type=regular_usage_type,
                          start=datetime.date(2013, 10, 1),
                          end=datetime.date(2013, 10, 30),
                          cost=1000).save()
        self.service.regular_usage_types.add(regular_usage_type)
        self.service.save()

        self.assertEqual(
            D(4490),
            ServicePlugin.total_cost(datetime.date(2013, 10, 10),
                                     datetime.date(2013, 10, 20), self.service,
                                     False, [self.venture1]))
Ejemplo n.º 16
0
    def test_get_dependent_services_cost(self, plugin_run_mock):
        dependent_service1 = models.Service(
            name='Service2'
        )
        dependent_service1.save()
        dependent_service2 = models.Service(
            name='Service3',
            use_universal_plugin=False,
        )
        dependent_service2.save()
        self.service.dependency.add(dependent_service1, dependent_service2)
        self.service.save()

        def pl(chain, func_name, type, service, **kwargs):
            data = {
                'Service2_schema': {
                    'sut_1_count': {},
                    'sut_1_cost': {'total_cost': False},
                    'sut_2_count': {},
                    'sut_2_cost': {'total_cost': False, 'currency': True},
                    '2_service_cost': {'total_cost': True, 'currency': True},
                },
                'Service2_usages': {
                    1: {
                        'sut_1_count': 10,
                        'sut_1_cost': D(100),
                        '2_service_cost': D(100),
                    },
                    2: {
                        'sut_1_count': 10,
                        'sut_1_cost': D(100),
                        'sut_2_count': 10,
                        'sut_2_cost': D(200),
                        '2_service_cost': D(300),
                    }
                },
                'Service3_schema': {
                    'sut_1_count': {},
                    'sut_1_cost': {'total_cost': True, 'currency': True},
                },
                'Service3_usages': {
                    1: {
                        'sut_1_count': 10,
                        'sut_1_cost': D(300),
                    },
                    2: {
                        'sut_1_count': 10,
                        'sut_1_cost': D(500),
                    }
                },
            }
            key = "{0}_{1}".format(service.name, type)
            return data[key]

        plugin_run_mock.side_effect = pl

        start = datetime.date(2013, 10, 10)
        end = datetime.date(2013, 10, 20)
        forecast = True
        result = ServicePlugin._get_dependent_services_cost(
            start=start,
            end=end,
            service=self.service,
            forecast=forecast,
            ventures=self.service_ventures,
        )
        self.assertEquals(result, 1200)
        plugin_run_mock.assert_any_call(
            'reports',
            'service_plugin',
            service=dependent_service1,
            ventures=self.service_ventures,
            start=start,
            end=end,
            forecast=forecast,
            type='usages',
        )
        plugin_run_mock.assert_any_call(
            'reports',
            'service3',
            service=dependent_service2,
            ventures=self.service_ventures,
            start=start,
            end=end,
            forecast=forecast,
            type='usages',
        )
Ejemplo n.º 17
0
    def test_get_date_ranges_percentage2(self):
        self.service = models.Service(name='Service2')
        self.service.save()
        self.service.base_usage_types.add(self.usage_type_cost_wh,
                                          self.usage_type)
        self.service_usage_type3 = models.UsageType(
            name='ServiceUsageType3',
            symbol='sut3',
            type='SU',
        )
        self.service_usage_type3.save()

        # |__________||____|
        # |____||____||____|
        # |____||__________|
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type1,
            service=self.service,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 20),
            percent=50,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type2,
            service=self.service,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 10),
            percent=30,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type3,
            service=self.service,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 10),
            percent=20,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type2,
            service=self.service,
            start=datetime.date(2013, 10, 11),
            end=datetime.date(2013, 10, 20),
            percent=10,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type3,
            service=self.service,
            start=datetime.date(2013, 10, 11),
            end=datetime.date(2013, 10, 30),
            percent=40,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type1,
            service=self.service,
            start=datetime.date(2013, 10, 21),
            end=datetime.date(2013, 10, 30),
            percent=30,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type2,
            service=self.service,
            start=datetime.date(2013, 10, 21),
            end=datetime.date(2013, 10, 30),
            percent=30,
        ).save()
        self.service.save()

        result = ServicePlugin._get_date_ranges_percentage(
            start=datetime.date(2013, 10, 5),
            end=datetime.date(2013, 10, 25),
            service=self.service,
        )

        self.assertEquals(
            result, {
                (datetime.date(2013, 10, 5), datetime.date(2013, 10, 10)): {
                    3: 50.0,
                    4: 30.0,
                    5: 20.0,
                },
                (datetime.date(2013, 10, 11), datetime.date(2013, 10, 20)): {
                    3: 50.0,
                    4: 10.0,
                    5: 40.0,
                },
                (datetime.date(2013, 10, 21), datetime.date(2013, 10, 25)): {
                    3: 30.0,
                    4: 30.0,
                    5: 40.0,
                },
            })
Ejemplo n.º 18
0
    def test_get_date_ranges_percentage2(self):
        self.service = models.Service(
            name='Service2'
        )
        self.service.save()
        self.service.base_usage_types.add(
            self.usage_type_cost_wh,
            self.usage_type
        )
        self.service_usage_type3 = models.UsageType(
            name='ServiceUsageType3',
            symbol='sut3',
            type='SU',
        )
        self.service_usage_type3.save()

        # |__________||____|
        # |____||____||____|
        # |____||__________|
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type1,
            service=self.service,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 20),
            percent=50,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type2,
            service=self.service,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 10),
            percent=30,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type3,
            service=self.service,
            start=datetime.date(2013, 10, 1),
            end=datetime.date(2013, 10, 10),
            percent=20,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type2,
            service=self.service,
            start=datetime.date(2013, 10, 11),
            end=datetime.date(2013, 10, 20),
            percent=10,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type3,
            service=self.service,
            start=datetime.date(2013, 10, 11),
            end=datetime.date(2013, 10, 30),
            percent=40,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type1,
            service=self.service,
            start=datetime.date(2013, 10, 21),
            end=datetime.date(2013, 10, 30),
            percent=30,
        ).save()
        models.ServiceUsageTypes(
            usage_type=self.service_usage_type2,
            service=self.service,
            start=datetime.date(2013, 10, 21),
            end=datetime.date(2013, 10, 30),
            percent=30,
        ).save()
        self.service.save()

        result = ServicePlugin._get_date_ranges_percentage(
            start=datetime.date(2013, 10, 5),
            end=datetime.date(2013, 10, 25),
            service=self.service,
        )

        self.assertEquals(result, {
            (datetime.date(2013, 10, 5), datetime.date(2013, 10, 10)): {
                3: 50.0,
                4: 30.0,
                5: 20.0,
            },
            (datetime.date(2013, 10, 11), datetime.date(2013, 10, 20)): {
                3: 50.0,
                4: 10.0,
                5: 40.0,
            },
            (datetime.date(2013, 10, 21), datetime.date(2013, 10, 25)): {
                3: 30.0,
                4: 30.0,
                5: 40.0,
            },
        })
Ejemplo n.º 19
0
    def test_get_dependent_services_cost(self, plugin_run_mock):
        dependent_service1 = models.Service(name='Service2')
        dependent_service1.save()
        dependent_service2 = models.Service(
            name='Service3',
            use_universal_plugin=False,
        )
        dependent_service2.save()
        self.service.dependency.add(dependent_service1, dependent_service2)
        self.service.save()

        def pl(chain, func_name, type, service, **kwargs):
            data = {
                'Service2_schema': {
                    'sut_1_count': {},
                    'sut_1_cost': {
                        'total_cost': False
                    },
                    'sut_2_count': {},
                    'sut_2_cost': {
                        'total_cost': False,
                        'currency': True
                    },
                    '2_service_cost': {
                        'total_cost': True,
                        'currency': True
                    },
                },
                'Service2_costs': {
                    1: {
                        'sut_1_count': 10,
                        'sut_1_cost': D(100),
                        '2_service_cost': D(100),
                    },
                    2: {
                        'sut_1_count': 10,
                        'sut_1_cost': D(100),
                        'sut_2_count': 10,
                        'sut_2_cost': D(200),
                        '2_service_cost': D(300),
                    }
                },
                'Service3_schema': {
                    'sut_1_count': {},
                    'sut_1_cost': {
                        'total_cost': True,
                        'currency': True
                    },
                },
                'Service3_costs': {
                    1: {
                        'sut_1_count': 10,
                        'sut_1_cost': D(300),
                    },
                    2: {
                        'sut_1_count': 10,
                        'sut_1_cost': D(500),
                    }
                },
            }
            key = "{0}_{1}".format(service.name, type)
            return data[key]

        plugin_run_mock.side_effect = pl

        start = datetime.date(2013, 10, 10)
        end = datetime.date(2013, 10, 20)
        forecast = True
        result = ServicePlugin._get_dependent_services_cost(
            start=start,
            end=end,
            service=self.service,
            forecast=forecast,
            ventures=self.service_ventures,
        )
        self.assertEquals(result, 1200)
        plugin_run_mock.assert_any_call(
            'reports',
            'service_plugin',
            service=dependent_service1,
            ventures=self.service_ventures,
            start=start,
            end=end,
            forecast=forecast,
            type='costs',
        )
        plugin_run_mock.assert_any_call(
            'reports',
            'service3',
            service=dependent_service2,
            ventures=self.service_ventures,
            start=start,
            end=end,
            forecast=forecast,
            type='costs',
        )