Beispiel #1
0
    def test_report_unknown_service(self):
        prepay_entity_customer = {"customer_type": "entity", "detailed_info": {
            "name": "test prepay entity customer", "contract_number": "2015/4568",
            "contract_date": "2015-01-01", "organization_type": "OOO", "name": "Some Company",
            "full_organization_name": "OOO Some Company", "primary_state_registration_number": "159 8525 15552",
            "individual_tax_number": "52 59 5555555", "legal_address_country": "RU", "legal_address_city": "NN",
            "legal_address_address": "Ошарская, 13", "location_country": "RU", "location_city": "NN",
            "location_address": "Ошарская", "general_manager_name": "Васильев Е.В",
            "general_accountant_name": "Иванова В.Н", "contact_person_name": "Петров Василий"
        }}
        self.create_tariff("First Tariff", default=True)
        customer_info = self.create_customer("*****@*****.**", **prepay_entity_customer)
        customer_id = customer_info["customer_id"]

        customer = Customer.get_by_id(customer_id)
        tenant = Tenant.create(uuid.uuid4().hex, "test tenant")
        tenant_id = tenant.tenant_id
        customer.confirm_email()  # clear db session
        customer.os_tenant_id = tenant_id
        db.session.commit()

        start = datetime(2015, 4, 1)
        end = datetime(2015, 4, 1, 10) - timedelta(seconds=1)
        customer = Customer.get_by_id(customer_id)
        Customer.fake_usage(customer, start, end, "service_unknown", "unknown resource", 1)
        db.session.commit()

        report = self.get_customer_report(customer_id, start, end, "pdf", filename="unknown_service.pdf")
        self.assertTrue(report)

        report = self.get_customer_report(customer_id, start, end, "pdf", filename="unknown_service_detailed.pdf",
                                          report_type="detailed")
        self.assertTrue(report)
Beispiel #2
0
    def test_usage_report(self):
        self.create_tariff("default_tariff", default=True)
        customer_info = self.create_customer_by_self("*****@*****.**")

        customer = Customer.get_by_id(customer_info["customer_id"])
        tenant = Tenant.create(uuid.uuid4().hex, "test tenant")

        customer.os_tenant_id = tenant.tenant_id
        db.session.commit()

        start = datetime(2015, 4, 1)
        end = datetime(2015, 4, 25)
        end_report = end + timedelta(hours=24)
        cost1 = Decimal(0)
        cost1 += Customer.fake_usage(customer, start, end,
                                     self.service_nano_id,
                                     uuid.uuid4().hex, 1)
        cost1 += Customer.fake_usage(customer, start + timedelta(days=5), end,
                                     self.service_micro_id,
                                     uuid.uuid4().hex, 1)
        cost1 += Customer.fake_usage(customer, start + timedelta(days=6), end,
                                     self.service_nano_id,
                                     uuid.uuid4().hex, 1)
        cost1 += Customer.fake_usage(customer, start, end, "storage.image",
                                     uuid.uuid4().hex, 60 * conf.GIGA)

        customer2_info = self.create_customer("*****@*****.**")
        tenant2 = Tenant.create(uuid.uuid4().hex, "test tenant 2")
        customer2 = Customer.get_by_id(customer2_info["customer_id"])
        customer2.os_tenant_id = tenant2.tenant_id
        cost2 = Customer.fake_usage(customer2, start, end,
                                    self.service_nano_id,
                                    uuid.uuid4().hex, 1)
        db.session.commit()

        report = self.get_report("usage",
                                 start,
                                 end_report,
                                 "csv",
                                 locale="en_us")
        self.assertTrue(report)

        report = [row.decode("ascii") for row in report.split(b"\r\n") if row]

        parsed_report = csv.reader(report)
        cust_1 = [
            row for row in parsed_report if row[0] == '*****@*****.**'
        ]
        cust_1_cost = sum(Decimal(row[1]) for row in cust_1)
        parsed_report = csv.reader(report)
        cust_2 = [
            row for row in parsed_report if row[0] == '*****@*****.**'
        ]
        cust_2_cost = sum(Decimal(row[1]) for row in cust_2)
        self.assertEqual(len(cust_2), 1)
        self.assertEqual(len(cust_1), 1)
        self.assertEqual(cust_1_cost, cost1)
        self.assertEqual(cust_2_cost, cost2)
Beispiel #3
0
    def test_report_unknown_service(self):
        prepay_entity_customer = {
            "customer_type": "entity",
            "detailed_info": {
                "name": "test prepay entity customer",
                "contract_number": "2015/4568",
                "contract_date": "2015-01-01",
                "organization_type": "OOO",
                "name": "Some Company",
                "full_organization_name": "OOO Some Company",
                "primary_state_registration_number": "159 8525 15552",
                "individual_tax_number": "52 59 5555555",
                "legal_address_country": "RU",
                "legal_address_city": "NN",
                "legal_address_address": "Ошарская, 13",
                "location_country": "RU",
                "location_city": "NN",
                "location_address": "Ошарская",
                "general_manager_name": "Васильев Е.В",
                "general_accountant_name": "Иванова В.Н",
                "contact_person_name": "Петров Василий"
            }
        }
        self.create_tariff("First Tariff", default=True)
        customer_info = self.create_customer("*****@*****.**",
                                             **prepay_entity_customer)
        customer_id = customer_info["customer_id"]

        customer = Customer.get_by_id(customer_id)
        tenant = Tenant.create(uuid.uuid4().hex, "test tenant")
        tenant_id = tenant.tenant_id
        customer.confirm_email()  # clear db session
        customer.os_tenant_id = tenant_id
        db.session.commit()

        start = datetime(2015, 4, 1)
        end = datetime(2015, 4, 1, 10) - timedelta(seconds=1)
        customer = Customer.get_by_id(customer_id)
        Customer.fake_usage(customer, start, end, "service_unknown",
                            "unknown resource", 1)
        db.session.commit()

        report = self.get_customer_report(customer_id,
                                          start,
                                          end,
                                          "pdf",
                                          filename="unknown_service.pdf")
        self.assertTrue(report)

        report = self.get_customer_report(
            customer_id,
            start,
            end,
            "pdf",
            filename="unknown_service_detailed.pdf",
            report_type="detailed")
        self.assertTrue(report)
Beispiel #4
0
    def test_usage_report(self):
        self.create_tariff("default_tariff", default=True)
        customer_info = self.create_customer_by_self("*****@*****.**")

        customer = Customer.get_by_id(customer_info["customer_id"])
        tenant = Tenant.create(uuid.uuid4().hex, "test tenant")

        customer.os_tenant_id = tenant.tenant_id
        db.session.commit()

        start = datetime(2015, 4, 1)
        end = datetime(2015, 4, 25)
        end_report = end + timedelta(hours=24)
        cost1 = Decimal(0)
        cost1 += Customer.fake_usage(customer, start, end, self.service_nano_id, uuid.uuid4().hex, 1)
        cost1 += Customer.fake_usage(customer, start + timedelta(days=5), end, self.service_micro_id, uuid.uuid4().hex, 1)
        cost1 += Customer.fake_usage(customer, start + timedelta(days=6), end, self.service_nano_id, uuid.uuid4().hex, 1)
        cost1 += Customer.fake_usage(customer, start, end, "storage.image", uuid.uuid4().hex, 60*conf.GIGA)

        customer2_info = self.create_customer("*****@*****.**")
        tenant2 = Tenant.create(uuid.uuid4().hex, "test tenant 2")
        customer2 = Customer.get_by_id(customer2_info["customer_id"])
        customer2.os_tenant_id = tenant2.tenant_id
        cost2 = Customer.fake_usage(customer2, start, end, self.service_nano_id, uuid.uuid4().hex, 1)
        db.session.commit()

        report = self.get_report("usage", start, end_report, "csv", locale="en_us")
        self.assertTrue(report)

        report = [row.decode("ascii") for row in report.split(b"\r\n") if row]

        parsed_report = csv.reader(report)
        cust_1 = [row for row in parsed_report if row[0] == '*****@*****.**']
        cust_1_cost = sum(Decimal(row[1]) for row in cust_1)
        parsed_report = csv.reader(report)
        cust_2 = [row for row in parsed_report if row[0] == '*****@*****.**']
        cust_2_cost = sum(Decimal(row[1]) for row in cust_2)
        self.assertEqual(len(cust_2), 1)
        self.assertEqual(len(cust_1), 1)
        self.assertEqual(cust_1_cost, cost1)
        self.assertEqual(cust_2_cost, cost2)
Beispiel #5
0
    def test_report_with_two_tariffs(self):
        prepay_entity_customer = {
            "customer_type": "entity",
            "detailed_info": {
                "name": "test prepay entity customer",
                "contract_number": "2015/4568",
                "contract_date": "2015-01-01",
                "organization_type": "OOO",
                "name": "Some Company",
                "full_organization_name": "OOO Some Company",
                "primary_state_registration_number": "159 8525 15552",
                "individual_tax_number": "52 59 5555555",
                "legal_address_country": "RU",
                "legal_address_city": "NN",
                "legal_address_address": "Ошарская, 13",
                "location_country": "RU",
                "location_city": "NN",
                "location_address": "Ошарская",
                "general_manager_name": "Васильев Е.В",
                "general_accountant_name": "Иванова В.Н",
                "contact_person_name": "Петров Василий"
            }
        }
        self.create_tariff("First Tariff", default=True)
        customer_info = self.create_customer("*****@*****.**",
                                             **prepay_entity_customer)
        customer_id = customer_info["customer_id"]

        customer = Customer.get_by_id(customer_id)
        tenant = Tenant.create(uuid.uuid4().hex, "test tenant")
        tenant_id = tenant.tenant_id

        customer.os_tenant_id = tenant_id
        db.session.commit()

        start = datetime(2015, 4, 1)
        middle_end = datetime(2015, 4, 1, 15) - timedelta(seconds=1)
        end = datetime(2015, 4, 2) - timedelta(seconds=1)
        end_second = datetime(2015, 4, 3) - timedelta(seconds=1)
        end_report = end_second + timedelta(hours=24)
        vm_nano = uuid.uuid4().hex
        vm_small = uuid.uuid4().hex
        vm_nano2 = "nano2"

        storage_id = "disk1"

        Customer.fake_usage(customer,
                            start,
                            end,
                            self.service_nano_id,
                            vm_nano,
                            1,
                            resource_name="nano1")
        Customer.fake_usage(customer,
                            start,
                            end,
                            self.service_small_id,
                            vm_small,
                            1,
                            resource_name="my little pony")
        Customer.fake_usage(customer,
                            start,
                            middle_end,
                            self.service_nano_id,
                            vm_nano2,
                            1,
                            resource_name="pico")
        Customer.fake_usage(customer, start, middle_end, "storage.volume",
                            storage_id, 1237852421)
        Customer.fake_usage(customer, middle_end + timedelta(hours=1), end,
                            "storage.volume", storage_id, 77777777777)
        Customer.fake_usage(customer,
                            start,
                            end,
                            "net.allocated_ip",
                            "ip_floating_1",
                            3,
                            resource_name="192.168.1.1")
        Customer.fake_usage(customer, start, end, "net.allocated_ip",
                            "ip_floating_2", 7)
        db.session.commit()

        second_tariff = self.create_tariff("Second Tariff", default=True)
        self.admin_client.customer.update(customer_id,
                                          tariff=second_tariff["tariff_id"])

        customer = Customer.get_by_id(customer_id)
        customer.confirm_email()  # clear db session

        customer = Customer.get_by_id(customer_id)
        customer.os_tenant_id = tenant_id  # confirm email cleared tenant id
        Customer.fake_usage(customer,
                            end + timedelta(minutes=1),
                            end_second,
                            self.service_nano_id,
                            vm_nano,
                            1,
                            resource_name="nano1")
        db.session.commit()

        report_tsv = self.get_customer_report(customer_id, start, end_report,
                                              "tsv", "detailed")
        report_csv = self.get_customer_report(customer_id, start, end_report,
                                              "csv", "detailed")

        report = self.get_customer_report(customer_id,
                                          start,
                                          end_report,
                                          "pdf",
                                          "detailed",
                                          filename="detailed.pdf")

        report_json = self.get_customer_report(customer_id, start, end_report,
                                               "json", "detailed")

        nano_cost = self.service_cost("2.23", start, end)
        nano2_cost = self.service_cost("2.23", start, middle_end)
        small_cost = self.service_cost("12.23", start, end)
        ip_floating_1_cost = self.service_cost("43.45", start, end) * 3
        ip_floating_2_cost = self.service_cost("43.45", start, end) * 7

        t1_cost = nano_cost + small_cost + nano2_cost + ip_floating_1_cost + ip_floating_2_cost
        self.assertEqual(len(report_json["tariffs"]), 2)
        self.assertEqual(Decimal(report_json["tariffs"][0]["total_cost"]),
                         t1_cost)

        t2_cost = self.service_cost("2.23", end + timedelta(minutes=1),
                                    end_second)
        self.assertEqual(Decimal(report_json["tariffs"][1]["total_cost"]),
                         t2_cost)

        self.assertEqual(Decimal(report_json["total"]["RUB"]),
                         t1_cost + t2_cost)

        report = self.get_customer_report(customer_id,
                                          start,
                                          end_report,
                                          "pdf",
                                          filename="two_tariff.pdf")
        self.assertTrue(report)

        report = self.get_customer_report(customer_id,
                                          start,
                                          end_report,
                                          "csv",
                                          filename="two_tariff.csv")
        self.assertTrue(report)

        report = self.get_customer_report(customer_id,
                                          start,
                                          end_report,
                                          "tsv",
                                          filename="two_tariff.tsv")
        self.assertTrue(report)

        report_simple_json = self.get_customer_report(
            customer_id, start, end_report, "json", filename="two_tariff.pdf")
        self.assertTrue(report_simple_json)

        self.assertEqual(Decimal(report_simple_json["total"]["RUB"]),
                         t1_cost + t2_cost)

        report = self.get_customer_report(customer_id, start, end_report,
                                          "pdf", "acceptance_act")

        report = self.get_customer_report(customer_id,
                                          end_report + timedelta(days=1),
                                          end_second + timedelta(days=31),
                                          "pdf",
                                          filename="empty.pdf")
Beispiel #6
0
    def test_report_with_two_tariffs(self):
        prepay_entity_customer = {"customer_type": "entity", "detailed_info": {
            "name": "test prepay entity customer", "contract_number": "2015/4568",
            "contract_date": "2015-01-01", "organization_type": "OOO", "name": "Some Company",
            "full_organization_name": "OOO Some Company", "primary_state_registration_number": "159 8525 15552",
            "individual_tax_number": "52 59 5555555", "legal_address_country": "RU", "legal_address_city": "NN",
            "legal_address_address": "Ошарская, 13", "location_country": "RU", "location_city": "NN",
            "location_address": "Ошарская", "general_manager_name": "Васильев Е.В",
            "general_accountant_name": "Иванова В.Н", "contact_person_name": "Петров Василий"
        }}
        self.create_tariff("First Tariff", default=True)
        customer_info = self.create_customer("*****@*****.**", **prepay_entity_customer)
        customer_id = customer_info["customer_id"]

        customer = Customer.get_by_id(customer_id)
        tenant = Tenant.create(uuid.uuid4().hex, "test tenant")
        tenant_id = tenant.tenant_id

        customer.os_tenant_id = tenant_id
        db.session.commit()

        start = datetime(2015, 4, 1)
        middle_end = datetime(2015, 4, 1, 15) - timedelta(seconds=1)
        end = datetime(2015, 4, 2) - timedelta(seconds=1)
        end_second = datetime(2015, 4, 3) - timedelta(seconds=1)
        end_report = end_second + timedelta(hours=24)
        vm_nano = uuid.uuid4().hex
        vm_small = uuid.uuid4().hex
        vm_nano2 = "nano2"

        storage_id = "disk1"

        Customer.fake_usage(customer, start, end, self.service_nano_id, vm_nano, 1, resource_name="nano1")
        Customer.fake_usage(customer, start, end, self.service_small_id, vm_small, 1, resource_name="my little pony")
        Customer.fake_usage(customer, start, middle_end , self.service_nano_id, vm_nano2, 1, resource_name="pico")
        Customer.fake_usage(customer, start, middle_end, "storage.volume", storage_id, 1237852421)
        Customer.fake_usage(customer, middle_end + timedelta(hours=1), end, "storage.volume", storage_id, 77777777777)
        Customer.fake_usage(customer, start, end, "net.allocated_ip", "ip_floating_1", 3, resource_name="192.168.1.1")
        Customer.fake_usage(customer, start, end, "net.allocated_ip", "ip_floating_2", 7)
        db.session.commit()

        second_tariff = self.create_tariff("Second Tariff", default=True)
        self.admin_client.customer.update(customer_id, tariff=second_tariff["tariff_id"])

        customer = Customer.get_by_id(customer_id)
        customer.confirm_email()  # clear db session

        customer = Customer.get_by_id(customer_id)
        customer.os_tenant_id = tenant_id  # confirm email cleared tenant id
        Customer.fake_usage(customer, end + timedelta(minutes=1), end_second, self.service_nano_id, vm_nano, 1,
                            resource_name="nano1")
        db.session.commit()

        report_tsv = self.get_customer_report(customer_id, start, end_report, "tsv", "detailed")
        report_csv = self.get_customer_report(customer_id, start, end_report, "csv", "detailed")

        report = self.get_customer_report(customer_id, start, end_report, "pdf", "detailed", filename="detailed.pdf")

        report_json = self.get_customer_report(customer_id, start, end_report, "json", "detailed")

        nano_cost = self.service_cost("2.23", start, end)
        nano2_cost = self.service_cost("2.23", start, middle_end)
        small_cost = self.service_cost("12.23", start, end)
        ip_floating_1_cost = self.service_cost("43.45", start, end)*3
        ip_floating_2_cost = self.service_cost("43.45", start, end)*7

        t1_cost = nano_cost + small_cost + nano2_cost + ip_floating_1_cost + ip_floating_2_cost
        self.assertEqual(len(report_json["tariffs"]), 2)
        self.assertEqual(Decimal(report_json["tariffs"][0]["total_cost"]), t1_cost)

        t2_cost = self.service_cost("2.23", end + timedelta(minutes=1), end_second)
        self.assertEqual(Decimal(report_json["tariffs"][1]["total_cost"]), t2_cost)

        self.assertEqual(Decimal(report_json["total"]["RUB"]), t1_cost + t2_cost)


        report = self.get_customer_report(customer_id, start, end_report, "pdf", filename="two_tariff.pdf")
        self.assertTrue(report)

        report = self.get_customer_report(customer_id, start, end_report, "csv", filename="two_tariff.csv")
        self.assertTrue(report)

        report = self.get_customer_report(customer_id, start, end_report, "tsv", filename="two_tariff.tsv")
        self.assertTrue(report)

        report_simple_json = self.get_customer_report(customer_id, start, end_report, "json", filename="two_tariff.pdf")
        self.assertTrue(report_simple_json)

        self.assertEqual(Decimal(report_simple_json["total"]["RUB"]), t1_cost + t2_cost)

        report = self.get_customer_report(customer_id, start, end_report, "pdf", "acceptance_act")

        report = self.get_customer_report(customer_id, end_report + timedelta(days=1),
                                          end_second + timedelta(days=31), "pdf", filename="empty.pdf")