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()
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()
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_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()
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()