Esempio n. 1
0
    def test_with_invalid_product_id(self):
        """test the behavior of the import function if a product was not found in the database"""
        global CURRENT_PRODUCT_MIGRATION_TEST_DATA
        CURRENT_PRODUCT_MIGRATION_TEST_DATA = pd.DataFrame(
            [
                [
                    "Product that is not in the Database",
                    "Existing Migration Source",
                    "Replacement Product ID",
                    "comment of the migration",
                    "Invalid URL"
                ]
            ], columns=PRODUCT_MIGRATION_TEST_DATA_COLUMNS
        )
        mixer.blend("productdb.Product", product_id="Product A", vendor=Vendor.objects.get(id=1))
        mixer.blend("productdb.ProductMigrationSource", name="Existing Migration Source")

        product_migrations_file = ProductMigrationsExcelImporter("virtual_file.xlsx")
        assert product_migrations_file.is_valid_file() is False

        product_migrations_file.verify_file()
        assert product_migrations_file.is_valid_file() is True

        product_migrations_file.import_to_database()
        assert ProductMigrationOption.objects.count() == 0
        assert "Product Product that is not in the Database not found in database, skip " \
               "entry" in product_migrations_file.import_result_messages

        Product.objects.all().delete()
        ProductMigrationOption.objects.all().delete()
        ProductMigrationSource.objects.all().delete()
Esempio n. 2
0
    def test_import_with_invalid_url_format(self):
        """test what happens if the data contains invalid data"""
        global CURRENT_PRODUCT_MIGRATION_TEST_DATA
        CURRENT_PRODUCT_MIGRATION_TEST_DATA = pd.DataFrame(
            [
                [
                    "Product A",
                    "Existing Migration Source",
                    "Replacement Product ID",
                    "comment of the migration",
                    "Invalid URL"
                ]
            ], columns=PRODUCT_MIGRATION_TEST_DATA_COLUMNS
        )
        mixer.blend("productdb.Product", product_id="Product A", vendor=Vendor.objects.get(id=1))
        mixer.blend("productdb.ProductMigrationSource", name="Existing Migration Source")

        product_migrations_file = ProductMigrationsExcelImporter("virtual_file.xlsx")
        assert product_migrations_file.is_valid_file() is False

        product_migrations_file.verify_file()
        assert product_migrations_file.is_valid_file() is True

        product_migrations_file.import_to_database()
        assert ProductMigrationOption.objects.count() == 0
        assert "cannot save Product Migration for Product A: {'migration_product_info_url': " \
               "['Enter a valid URL.']}" in product_migrations_file.import_result_messages

        Product.objects.all().delete()
        ProductMigrationOption.objects.all().delete()
        ProductMigrationSource.objects.all().delete()
Esempio n. 3
0
    def test_import_with_missing_product_value(self):
        """test import with missing product (ignore it)"""
        global CURRENT_PRODUCT_MIGRATION_TEST_DATA
        CURRENT_PRODUCT_MIGRATION_TEST_DATA = pd.DataFrame(
            [
                [
                    None,
                    "Existing Migration Source",
                    "Replacement Product ID",
                    "comment of the migration",
                    "Invalid URL"
                ]
            ], columns=PRODUCT_MIGRATION_TEST_DATA_COLUMNS
        )
        mixer.blend("productdb.Product", product_id="Product A", vendor=Vendor.objects.get(id=1))
        mixer.blend("productdb.ProductMigrationSource", name="Existing Migration Source")

        product_migrations_file = ProductMigrationsExcelImporter("no file.xlsx")
        assert product_migrations_file.is_valid_file() is False

        product_migrations_file.verify_file()
        assert product_migrations_file.is_valid_file() is True

        product_migrations_file.import_to_database()
        assert ProductMigrationOption.objects.count() == 0
        assert len(product_migrations_file.import_result_messages) == 0

        Product.objects.all().delete()
        ProductMigrationOption.objects.all().delete()
        ProductMigrationSource.objects.all().delete()
Esempio n. 4
0
    def test_valid_import(self):
        mixer.blend("productdb.Product", product_id="Product A", vendor=Vendor.objects.get(id=1))
        mixer.blend("productdb.ProductMigrationSource", name="Existing Migration Source")

        product_migrations_file = ProductMigrationsExcelImporter("virtual_file.xlsx")
        assert product_migrations_file.is_valid_file() is False

        product_migrations_file.verify_file()
        assert product_migrations_file.is_valid_file() is True

        product_migrations_file.import_to_database()
        assert ProductMigrationSource.objects.count() == 2
        assert ProductMigrationOption.objects.count() == 2
        assert len(product_migrations_file.import_result_messages) == 3
        assert "Product Migration Source \"New Migration Source\" was created with a preference of 10" in product_migrations_file.import_result_messages
        assert "create Product Migration path \"New Migration Source\" for Product \"Product A\"" in product_migrations_file.import_result_messages
        assert "create Product Migration path \"Existing Migration Source\" for Product \"Product A\"" in product_migrations_file.import_result_messages

        product_migrations_file.import_to_database()
        assert ProductMigrationSource.objects.count() == 2
        assert ProductMigrationOption.objects.count() == 2
        assert len(product_migrations_file.import_result_messages) == 2
        assert "update Product Migration path \"New Migration Source\" for Product \"Product A\"" in product_migrations_file.import_result_messages
        assert "update Product Migration path \"Existing Migration Source\" for Product \"Product A\"" in product_migrations_file.import_result_messages

        del product_migrations_file
        Product.objects.all().delete()
        ProductMigrationOption.objects.all().delete()
        ProductMigrationSource.objects.all().delete()
    def test_import_with_missing_migration_source(self):
        """test import with missing migration source (ignore it)"""
        global CURRENT_PRODUCT_MIGRATION_TEST_DATA
        CURRENT_PRODUCT_MIGRATION_TEST_DATA = pd.DataFrame(
            [[
                "Product A", "Cisco Systems", "", "Replacement Product ID",
                "comment of the migration", "Invalid URL"
            ]],
            columns=PRODUCT_MIGRATION_TEST_DATA_COLUMNS)
        models.Product.objects.create(product_id="Product A",
                                      vendor=Vendor.objects.get(id=1))
        models.ProductMigrationSource.objects.create(
            name="Existing Migration Source")

        product_migrations_file = ProductMigrationsExcelImporter(
            "virtual_file.xlsx")
        assert product_migrations_file.is_valid_file() is False

        product_migrations_file.verify_file()
        assert product_migrations_file.is_valid_file() is True

        product_migrations_file.import_to_database()
        assert ProductMigrationOption.objects.count() == 0

        Product.objects.all().delete()
        ProductMigrationOption.objects.all().delete()
        ProductMigrationSource.objects.all().delete()
Esempio n. 6
0
def import_product_migrations(self, job_file_id, user_for_revision=None):
    """
    import product migrations from the Excel file
    :param job_file_id: ID within the database that references the Excel file that should be imported
    :param user_for_revision: username that should be used for the revision tracking (only if started manually)
    :return:
    """
    def update_task_state(status_message):
        """Update the status message of the task, which is displayed in the watch view"""
        self.update_state(state=TaskState.PROCESSING, meta={
            "status_message": status_message
        })

    update_task_state("Try to import uploaded file...")

    try:
        import_excel_file = JobFile.objects.get(id=job_file_id)

    except:
        msg = "Cannot find file that was uploaded."
        logger.error(msg, exc_info=True)
        result = {
            "error_message": msg
        }
        return result

    # verify that file exists
    try:
        import_product_migrations_excel = ProductMigrationsExcelImporter(
            path_to_excel_file=import_excel_file.file,
            user_for_revision=User.objects.get(username=user_for_revision)
        )
        import_product_migrations_excel.verify_file()
        update_task_state("File valid, start updating the database...")

        import_product_migrations_excel.import_to_database(status_callback=update_task_state)
        update_task_state("Database import finished, processing results...")

        status_message = "<p style=\"text-align: left\">Product migrations successful updated</p>" \
                         "<ul style=\"text-align: left\">"
        for msg in import_product_migrations_excel.import_result_messages:
            status_message += "<li>%s</li>" % msg
        status_message += "</ul>"

        # drop the JobFile
        import_excel_file.delete()

        result = {
            "status_message": status_message
        }

    except (InvalidImportFormatException, InvalidExcelFileFormat) as ex:
        msg = "import failed, invalid file format (%s)" % ex
        logger.error(msg, ex)
        result = {
            "error_message": msg
        }

    except Exception as ex:  # catch any exception
        msg = "Unexpected exception occurred while importing product list (%s)" % ex
        logger.error(msg, ex)
        result = {
            "error_message": msg
        }

    # if the task was executed eager, set state to SUCCESS (required for testing)
    if self.request.is_eager:
        self.update_state(state=TaskState.SUCCESS, meta=result)

    return result
Esempio n. 7
0
def import_product_migrations(self, job_file_id, user_for_revision=None):
    """
    import product migrations from the Excel file
    :param job_file_id: ID within the database that references the Excel file that should be imported
    :param user_for_revision: username that should be used for the revision tracking (only if started manually)
    :return:
    """
    def update_task_state(status_message):
        """Update the status message of the task, which is displayed in the watch view"""
        self.update_state(state=TaskState.PROCESSING, meta={
            "status_message": status_message
        })

    update_task_state("Try to import uploaded file...")

    try:
        import_excel_file = JobFile.objects.get(id=job_file_id)

    except:
        msg = "Cannot find file that was uploaded."
        logger.error(msg, exc_info=True)
        result = {
            "error_message": msg
        }
        return result

    # verify that file exists
    try:
        import_product_migrations_excel = ProductMigrationsExcelImporter(
            path_to_excel_file=import_excel_file.file,
            user_for_revision=User.objects.get(username=user_for_revision)
        )
        import_product_migrations_excel.verify_file()
        update_task_state("File valid, start updating the database...")

        import_product_migrations_excel.import_to_database(status_callback=update_task_state)
        update_task_state("Database import finished, processing results...")

        status_message = "<p style=\"text-align: left\">Product migrations successful updated</p>" \
                         "<ul style=\"text-align: left\">"
        for msg in import_product_migrations_excel.import_result_messages:
            status_message += "<li>%s</li>" % msg
        status_message += "</ul>"

        # drop the JobFile
        import_excel_file.delete()

        result = {
            "status_message": status_message
        }

    except (InvalidImportFormatException, InvalidExcelFileFormat) as ex:
        msg = "import failed, invalid file format (%s)" % ex
        logger.error(msg, ex)
        result = {
            "error_message": msg
        }

    except Exception as ex:  # catch any exception
        msg = "Unexpected exception occurred while importing product list (%s)" % ex
        logger.error(msg, ex)
        result = {
            "error_message": msg
        }

    # if the task was executed eager, set state to SUCCESS (required for testing)
    if self.request.is_eager:
        self.update_state(state=TaskState.SUCCESS, meta=result)

    return result