def test_0110_export_catalog(self): """ Check the export of product catalog to magento. This method does not check the API calls. """ product_obj = POOL.get('product.product') category_obj = POOL.get('product.category') website_obj = POOL.get('magento.instance.website') with Transaction().start(DB_NAME, USER, CONTEXT) as txn: self.setup_defaults(txn) context = deepcopy(CONTEXT) context.update({ 'magento_instance': self.instance_id1, 'magento_website': self.website_id1, 'magento_attribute_set': 1, }) website = website_obj.browse( txn.cursor, txn.user, self.website_id1, txn.context ) if settings.MOCK: category_data = load_json('categories', '17') else: with magento.Category(*settings.ARGS) as category_api: category_tree = category_api.tree( website.magento_root_category_id ) category_data = category_api.info( category_tree['children'][0]['category_id'] ) category = category_obj.create_using_magento_data( txn.cursor, txn.user, category_data, context=context ) product_id = product_obj.create( txn.cursor, txn.user, { 'name': 'Test product', 'default_code': 'code', 'description': 'This is a product description', 'list_price': 100, }, context=context ) product = product_obj.browse( txn.cursor, txn.user, product_id, context=context ) if settings.MOCK: with patch( 'magento.Product', mock_product_api(), create=True ): product_obj.export_to_magento( txn.cursor, txn.user, product, category, context ) else: product_obj.export_to_magento( txn.cursor, txn.user, product, category, context )
def find_or_create_using_magento_id(self, cursor, user, magento_id, parent=None, context=None): """Find or Create category using magento ID of category :param cursor: Database cursor :param user: ID of current user :param magento_id: Category ID from magento :param parent: openerp ID of parent if present else None :param context: Application context :returns: Browse record of category found/created """ instance_obj = self.pool.get('magento.instance') category = self.find_using_magento_id(cursor, user, magento_id, context) if not category: instance = instance_obj.browse(cursor, user, context['magento_instance'], context=context) with magento.Category(instance.url, instance.api_user, instance.api_key) as category_api: category_data = category_api.info(magento_id) category = self.create_using_magento_data(cursor, user, category_data, parent, context) return category
def find_or_create_using_magento_id( cls, magento_id, parent=None ): """ Find or Create Category Using Magento ID of Category :param category_data: Category Data from Magento :param parent: Browse record of Parent if present, else None :returns: Active record of category found/created """ Instance = Pool().get('magento.instance') category = cls.find_using_magento_id(magento_id) if not category: instance = Instance( Transaction().context.get('magento_instance') ) with magento.Category( instance.url, instance.api_user, instance.api_key ) as category_api: category_data = category_api.info(magento_id) category = cls.create_using_magento_data( category_data, parent ) return category
def test_0040_import_configurable_product(self): """Test the import of a configurable product using magento data """ category_obj = POOL.get('product.category') product_obj = POOL.get('product.product') website_obj = POOL.get('magento.instance.website') with Transaction().start(DB_NAME, USER, CONTEXT) as txn: self.setup_defaults(txn) context = deepcopy(CONTEXT) context.update({ 'magento_instance': self.instance_id1, 'magento_website': self.website_id1, }) website = website_obj.browse( txn.cursor, txn.user, self.website_id1, txn.context ) if settings.MOCK: category_data = load_json('categories', '17') else: with magento.Category(*settings.ARGS) as category_api: category_tree = category_api.tree( website.magento_root_category_id ) category_data = category_api.info( category_tree['children'][0]['category_id'] ) category_obj.create_using_magento_data( txn.cursor, txn.user, category_data, context=context ) magento_store_id = website.stores[0].store_views[0].magento_id if settings.MOCK: product_data = load_json('products', '135') else: with magento.Product(*settings.ARGS) as product_api: product_list = product_api.list( store_view=magento_store_id ) for product in product_list: if product['type'] == 'configurable': product_data = product_api.info( product=product['product_id'], store_view=magento_store_id ) break product = product_obj.find_or_create_using_magento_data( txn.cursor, txn.user, product_data, context ) self.assertTrue( str(product.categ_id.magento_ids[0].magento_id) in product_data['categories'] ) self.assertEqual( product.magento_product_type, product_data['type'] )
def category_tree(parent_id=None): """ Lista a arvore de categorias """ with magento.Category(api.url, api.user, api.passwd) as category: categorys = category.tree(parent_id=parent_id) return categorys
def find_or_create_using_magento_id( cls, magento_id, parent=None ): """ Find or Create Category Using Magento ID of Category :param category_data: Category Data from Magento :param parent: Browse record of Parent if present, else None :returns: Active record of category found/created """ Channel = Pool().get('sale.channel') category = cls.find_using_magento_id(magento_id) if not category: channel = Channel.get_current_magento_channel() with magento.Category( channel.magento_url, channel.magento_api_user, channel.magento_api_key ) as category_api: category_data = category_api.info(magento_id) category = cls.create_using_magento_data( category_data, parent ) return category
def import_category_tree(self, website): """ Imports the category tree and creates categories in a hierarchy same as that on Magento :param website: Active record of website """ Category = Pool().get('product.category') instance = website.instance Transaction().set_context({'magento_instance': instance.id}) with magento.Category( instance.url, instance.api_user, instance.api_key ) as category_api: category_tree = category_api.tree(website.magento_root_category_id) Category.create_tree_using_magento_data(category_tree)
def import_category_tree(self): """ Imports the category tree and creates categories in a hierarchy same as that on Magento :param website: Active record of website """ Category = Pool().get('product.category') self.validate_magento_channel() with Transaction().set_context({'current_channel': self.id}): with magento.Category(self.magento_url, self.magento_api_user, self.magento_api_key) as category_api: category_tree = category_api.tree( self.magento_root_category_id) Category.create_tree_using_magento_data(category_tree)
def test_0103_update_product_using_magento_id(self): """Check if the product gets updated """ product_obj = POOL.get('product.product') category_obj = POOL.get('product.category') website_obj = POOL.get('magento.instance.website') with Transaction().start(DB_NAME, USER, CONTEXT) as txn: self.setup_defaults(txn) context = deepcopy(CONTEXT) context.update({ 'magento_instance': self.instance_id1, 'magento_website': self.website_id1, 'magento_store': self.store_id, }) website = website_obj.browse( txn.cursor, txn.user, self.website_id1, txn.context ) if settings.MOCK: category_data = load_json('categories', '17') else: with magento.Category(*settings.ARGS) as category_api: category_tree = category_api.tree( website.magento_root_category_id ) category_data = category_api.info( category_tree['children'][0]['category_id'] ) category_obj.create_using_magento_data( txn.cursor, txn.user, category_data, context=context ) if settings.MOCK: product_data = load_json('products', '135001') else: with magento.Product(*settings.ARGS) as product_api: product_list = product_api.list() product_data = product_api.info( product=product_list[0]['product_id'], ) product = product_obj.find_or_create_using_magento_data( txn.cursor, txn.user, product_data, context ) product_before_updation = product_obj.read( txn.cursor, txn.user, product.id, [], context=txn.context ) if settings.MOCK: with patch('magento.Product', mock_product_api(), create=True): product = product_obj.update_from_magento( txn.cursor, txn.user, product, context ) else: product_data['name'] = 'Updated-product' product_data['default_code'] = 'Updated-sku' product_data['description'] = 'Updated-description' product = product_obj.update_from_magento( txn.cursor, txn.user, product, context ) product_after_updation = product_obj.read( txn.cursor, txn.user, product.id, [], context=txn.context ) self.assertEqual( product_before_updation['id'], product_after_updation['id'] ) self.assertNotEqual( product_before_updation['name'], product_after_updation['name'] ) self.assertNotEqual( product_before_updation['default_code'], product_after_updation['default_code'] ) self.assertNotEqual( product_before_updation['description'], product_after_updation['description'] )
def test_0090_tier_prices(self): """Checks the function field on product price tiers """ product_obj = POOL.get('product.product') store_obj = POOL.get('magento.website.store') category_obj = POOL.get('product.category') pricelist_item_obj = POOL.get('product.pricelist.item') product_price_tier = POOL.get('product.price_tier') website_obj = POOL.get('magento.instance.website') with Transaction().start(DB_NAME, USER, CONTEXT) as txn: self.setup_defaults(txn) context = deepcopy(CONTEXT) context.update({ 'magento_instance': self.instance_id1, 'magento_website': self.website_id1, 'magento_store': self.store_id, }) store = store_obj.browse( txn.cursor, txn.user, self.store_id, context=context ) website = website_obj.browse( txn.cursor, txn.user, self.website_id1, txn.context ) if settings.MOCK: category_data = load_json('categories', '17') else: with magento.Category(*settings.ARGS) as category_api: category_tree = category_api.tree( website.magento_root_category_id ) category_data = category_api.info( category_tree['children'][0]['category_id'] ) category_obj.create_using_magento_data( txn.cursor, txn.user, category_data, context=context ) magento_store_id = website.stores[0].store_views[0].magento_id if settings.MOCK: product_data = load_json('products', '135') else: with magento.Product(*settings.ARGS) as product_api: product_list = product_api.list( store_view=magento_store_id ) product_data = product_api.info( product=product_list[0]['product_id'], store_view=magento_store_id ) product = product_obj.find_or_create_using_magento_data( txn.cursor, txn.user, product_data, context ) pricelist_item_obj.create(txn.cursor, txn.user, { 'name': 'Test line', 'price_version_id': store.shop.pricelist_id.version_id[0].id, 'product_id': product.id, 'min_quantity': 10, 'base': 1, 'price_surcharge': -5, }, context=context) tier_id = product_price_tier.create(txn.cursor, txn.user, { 'product': product.id, 'quantity': 10, }, context) tier = product_price_tier.browse( txn.cursor, txn.user, tier_id, context ) self.assertEqual(product.lst_price - 5, tier.price)
def test_0010_import_product_categories(self): """Test the import of product category using magento data """ website_obj = POOL.get('magento.instance.website') category_obj = POOL.get('product.category') magento_category_obj = POOL.get( 'magento.instance.product_category' ) with Transaction().start(DB_NAME, USER, CONTEXT) as txn: self.setup_defaults(txn) context = deepcopy(CONTEXT) context.update({ 'magento_instance': self.instance_id1, 'magento_website': self.website_id1 }) website = website_obj.browse( txn.cursor, txn.user, self.website_id1, txn.context ) categories_before_import = category_obj.search( txn.cursor, txn.user, [], count=True ) if settings.MOCK: category_tree = load_json('categories', 'category_tree') else: with magento.Category(*settings.ARGS) as category_api: category_tree = category_api.tree( website.magento_root_category_id ) category_obj.create_tree_using_magento_data( txn.cursor, txn.user, category_tree, context ) categories_after_import = category_obj.search( txn.cursor, txn.user, [], count=True ) self.assertTrue(categories_before_import < categories_after_import) # Look for root category root_category_id, = category_obj.search( txn.cursor, txn.user, [ ('magento_ids', '!=', []), ('parent_id', '=', None) ] ) root_category = category_obj.browse( txn.cursor, txn.user, root_category_id, context ) self.assertEqual( root_category.magento_ids[0].magento_id, website.magento_root_category_id ) self.assertTrue(len(root_category.child_id) > 0) self.assertTrue(len(root_category.child_id[0].child_id) > 0) # Make sure the categs created only in instance1 and not in # instance2 self.assertTrue( magento_category_obj.search(txn.cursor, txn.user, [ ('instance', '=', self.instance_id1) ], count=True) > 0 ) self.assertTrue( magento_category_obj.search(txn.cursor, txn.user, [ ('instance', '=', self.instance_id2) ], count=True) == 0 )
def test_0080_export_product_stock_information(self): """ This test checks if the method to call for updation of product stock info does not break anywhere in between. This method does not check the API calls """ product_obj = POOL.get('product.product') website_obj = POOL.get('magento.instance.website') category_obj = POOL.get('product.category') with Transaction().start(DB_NAME, USER, CONTEXT) as txn: self.setup_defaults(txn) context = deepcopy(CONTEXT) context.update({ 'magento_instance': self.instance_id1, 'magento_website': self.website_id1, }) website = website_obj.browse( txn.cursor, txn.user, self.website_id1, txn.context ) if settings.MOCK: category_data = load_json('categories', '17') else: with magento.Category(*settings.ARGS) as category_api: category_tree = category_api.tree( website.magento_root_category_id ) category_data = category_api.info( category_tree['children'][0]['category_id'] ) category_obj.create_using_magento_data( txn.cursor, txn.user, category_data, context=context ) magento_store_id = website.stores[0].store_views[0].magento_id if settings.MOCK: product_data = load_json('products', '135') else: with magento.Product(*settings.ARGS) as product_api: product_list = product_api.list( store_view=magento_store_id ) product_data = product_api.info( product=product_list[0]['product_id'], store_view=magento_store_id ) product_obj.find_or_create_using_magento_data( txn.cursor, txn.user, product_data, context ) website = website_obj.browse( txn.cursor, txn.user, self.website_id1, context ) with patch( 'magento.Inventory', mock_inventory_api(), create=True ): website_obj.export_inventory_to_magento( txn.cursor, txn.user, website, context )
def test_0020_import_simple_product(self): """Test the import of simple product using magento data """ category_obj = POOL.get('product.category') product_obj = POOL.get('product.product') magento_product_obj = POOL.get('magento.website.product') website_obj = POOL.get('magento.instance.website') with Transaction().start(DB_NAME, USER, CONTEXT) as txn: self.setup_defaults(txn) context = deepcopy(CONTEXT) context.update({ 'magento_instance': self.instance_id1, 'magento_website': self.website_id1, }) website = website_obj.browse( txn.cursor, txn.user, self.website_id1, txn.context ) if settings.MOCK: category_data = load_json('categories', '8') else: with magento.Category(*settings.ARGS) as category_api: category_tree = category_api.tree( website.magento_root_category_id ) category_data = category_api.info( category_tree['children'][0]['category_id'] ) category_obj.create_using_magento_data( txn.cursor, txn.user, category_data, context=context ) products_before_import = product_obj.search( txn.cursor, txn.user, [], context=context, count=True ) magento_store_id = website.stores[0].store_views[0].magento_id if settings.MOCK: product_data = load_json('products', '17') else: with magento.Product(*settings.ARGS) as product_api: product_list = product_api.list( store_view=magento_store_id ) for product in product_list: if product['type'] == 'simple': product_data = product_api.info( product=product['product_id'], ) break product = product_obj.find_or_create_using_magento_data( txn.cursor, txn.user, product_data, context ) self.assertTrue( str(product.categ_id.magento_ids[0].magento_id) in product_data['categories'] ) self.assertEqual( product.magento_product_type, product_data['type'] ) self.assertEqual(product.name, product_data['name']) products_after_import = product_obj.search( txn.cursor, txn.user, [], context=context, count=True ) self.assertTrue(products_after_import > products_before_import) self.assertEqual( product, product_obj.find_using_magento_data( txn.cursor, txn.user, product_data, context ) ) # Make sure the categs created only in website1 and not in # website2 self.assertTrue( magento_product_obj.search(txn.cursor, txn.user, [ ('website', '=', self.website_id1) ], count=True) > 0 ) self.assertTrue( magento_product_obj.search(txn.cursor, txn.user, [ ('website', '=', self.website_id2) ], count=True) == 0 )