def test_0020_product_import(self):
        """Test Product import
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(current_channel=self.channel.id, ps_test=True):
                self.setup_channels()

                self.assertEqual(len(self.ProductTemplate.search([])), 1)
                self.assertEqual(len(self.Product.search([])), 1)
                self.assertEqual(len(self.ChannelListing.search([("channel", "=", self.channel.id)])), 0)

                product = self.Product.create_from(self.channel, get_objectified_xml("combinations", 1))
                # This should create a template and two variants where one
                # is created by template and other by this combination
                self.assertEqual(len(self.ProductTemplate.search([])), 2)
                self.assertEqual(len(self.Product.search([])), 3)
                self.assertEqual(len(self.ChannelListing.search([("channel", "=", self.channel.id)])), 1)

                # Try importing the same product again, it should NOT create a
                # new one.
                self.Product.create_from(self.channel, get_objectified_xml("combinations", 1))
                self.assertEqual(len(self.Product.search([])), 3)

                # Test getting product using prestashop data
                self.assertEqual(
                    product.id, self.Product.create_from(self.channel, get_objectified_xml("combinations", 1)).id
                )

                # Test getting product using prestashop ID
                # Nothing should be created under site_alt
                self.assertEqual(len(self.ChannelListing.search([("channel", "=", self.alt_channel.id)])), 0)
Example #2
0
    def test_0030_check_prestashop_exception_order_total(self):
        """
        Check if exception is created when order total does not match
        """
        ChannelException = POOL.get('channel.exception')

        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(
                    self.User.get_preferences(context_only=True),
                    current_channel=self.channel.id,
                    ps_test=True,
            ):
                self.setup_channels()

                order_data = get_objectified_xml('orders', 1)

                order_data.total_paid_tax_excl = 100

                self.assertFalse(ChannelException.search([]))

                sale = self.Sale.find_or_create_using_ps_data(order_data)

                self.assertNotEqual(sale.total_amount,
                                    order_data.total_paid_tax_excl)

                self.assertTrue(sale.has_channel_exception)

                self.assertTrue(ChannelException.search([]))

                self.assertNotEqual(sale.state, 'done')
    def test_0030_check_prestashop_exception_order_total(self):
        """
        Check if exception is created when order total does not match
        """
        ChannelException = POOL.get('channel.exception')

        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(
                self.User.get_preferences(context_only=True),
                current_channel=self.channel.id, ps_test=True,
            ):
                self.setup_channels()

                order_data = get_objectified_xml('orders', 1)

                order_data.total_paid_tax_excl = 100

                self.assertFalse(ChannelException.search([]))

                sale = self.Sale.find_or_create_using_ps_data(order_data)

                self.assertNotEqual(
                    sale.total_amount, order_data.total_paid_tax_excl
                )

                self.assertTrue(sale.has_channel_exception)

                self.assertTrue(ChannelException.search([]))

                self.assertNotEqual(sale.state, 'done')
    def test_0013_order_import_delivered(self):
        """Import an order that has been delivered on PS
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(
                self.User.get_preferences(context_only=True),
                current_channel=self.channel.id, ps_test=True,
            ):
                self.setup_channels()

                order_data = get_objectified_xml('orders', 1)

                sale = self.Sale.find_or_create_using_ps_data(order_data)

                self.assertEqual(sale.state, 'done')
Example #5
0
    def test_0013_order_import_delivered(self):
        """Import an order that has been delivered on PS
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(
                    self.User.get_preferences(context_only=True),
                    current_channel=self.channel.id,
                    ps_test=True,
            ):
                self.setup_channels()

                order_data = get_objectified_xml('orders', 1)

                sale = self.Sale.find_or_create_using_ps_data(order_data)

                self.assertEqual(sale.state, 'done')
    def test_0016_order_import_canceled(self):
        """Import an order which was canceled on PS
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(
                self.User.get_preferences(context_only=True),
                current_channel=self.channel.id, ps_test=True,
            ):
                self.setup_channels()

                order_data = get_objectified_xml('orders', 2)

                sale = self.Sale.find_or_create_using_ps_data(order_data)

                # As canceled orders are marked as do not import
                self.assertEqual(sale.state, 'draft')
    def test_0010_product_template_import(self):
        """Test Product Template import
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT) as txn:
            # Call method to setup defaults
            self.setup_defaults()

            with txn.set_context(current_channel=self.channel.id, ps_test=True):
                self.setup_channels()

                self.assertEqual(len(self.ProductTemplate.search([])), 1)
                self.assertEqual(len(self.Product.search([])), 1)
                self.assertEqual(len(self.ChannelListing.search([("channel", "=", self.channel.id)])), 0)

                product_data = get_objectified_xml("products", 1)
                template = self.Product.create_from(self.channel, product_data).template

                self.assertEqual(len(self.ProductTemplate.search([])), 2)
                self.assertEqual(len(self.Product.search([])), 2)
                self.assertEqual(len(self.ChannelListing.search([("channel", "=", self.channel.id)])), 1)

                # Product name should be in english and french
                with txn.set_context(language="en_US"):
                    template = self.ProductTemplate(template.id)
                    self.assertEqual(template.name, "iPod Nano")
                with txn.set_context(language="fr_FR"):
                    template = self.ProductTemplate(template.id)
                    self.assertEqual(template.name, "iPod Nano French")

                # Product description should be in english only
                with txn.set_context(language="en_US"):
                    product_desc_en = self.Product(template.products[0].id).description
                with txn.set_context(language="fr_FR"):
                    product_desc_fr = self.Product(template.products[0].id).description
                self.assertEqual(product_desc_en, product_desc_fr)

                # Nothing should be created under site_alt
                self.assertEqual(len(self.ChannelListing.search([("channel", "=", self.alt_channel.id)])), 0)

                # Get template using prestashop data
                self.assertEqual(template.id, self.Product.create_from(self.channel, product_data).template.id)
    def test_0010_party_import(self):
        """Test Party import
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT) as txn:
            # Call method to setup defaults
            self.setup_defaults()

            with txn.set_context(
                current_channel=self.channel.id, ps_test=True
            ):
                self.setup_channels()

                self.channel.get_prestashop_client()

                self.assertEqual(len(self.Party.search([
                    ('channel', '=', self.channel.id)
                ])), 0)
                self.assertEqual(len(self.ContactMechanism.search([])), 0)

                # Create a party using prestashop data
                customer_data = get_objectified_xml('customers', 1)
                party = self.Party.create_using_ps_data(customer_data)
                self.assertEqual(len(self.Party.search([
                    ('channel', '=', self.channel.id)
                ])), 1)
                self.assertEqual(len(self.ContactMechanism.search([])), 1)

                # Assert that the language set on party is same as the language
                # in channel languages
                self.assertEqual(
                    party.lang,
                    self.Lang.get_using_ps_id(customer_data.id_lang.pyval)
                )

                # Try importing the same party, it should NOT create a
                # new one.
                party = self.Party.find_or_create_using_ps_data(
                    customer_data
                )
                self.assertEqual(len(self.Party.search([
                    ('channel', '=', self.channel.id)
                ])), 1)
                self.assertEqual(len(self.ContactMechanism.search([])), 1)

                # Search for the same party in tryton using a different method
                # It should return the same party
                self.assertEqual(
                    party.id,
                    self.Party.get_party_using_ps_data(customer_data).id
                )

                # Create the same party, it should NOT create a new one
                # Instead, it should blow up with a UserError sue to sql
                # constraints
                self.assertRaises(
                    UserError,
                    self.Party.create_using_ps_data, customer_data
                )

                txn.cursor.rollback()

        with Transaction().start(DB_NAME, USER, context=CONTEXT) as txn:
            # Call method to setup defaults
            self.setup_defaults()

            with txn.set_context(
                current_channel=self.alt_channel.id, ps_test=True
            ):
                self.setup_channels()
                self.alt_channel.get_prestashop_client()

                # Nothing should be linked to alt_channel
                self.assertEqual(len(self.Party.search([
                    ('channel', '=', self.alt_channel.id)
                ])), 0)

                # Create a party using prestashop data
                customer_data = get_objectified_xml('customers', 1)
                party = self.Party.create_using_ps_data(customer_data)
                self.assertEqual(len(self.Party.search([
                    ('channel', '=', self.alt_channel.id)
                ])), 1)
                txn.cursor.rollback()
    def test_0020_address_import_n_matching(self):
        """Test address import and pattern matching
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(
                current_channel=self.channel.id, ps_test=True
            ):
                self.setup_channels()

                self.channel.get_prestashop_client()

                self.assertEqual(len(self.Address.search([
                    ('party.channel', '=', self.channel.id)
                ])), 0)
                self.assertEqual(len(self.ContactMechanism.search([])), 0)
                self.assertEqual(len(self.CountryPrestashop.search([
                    ('channel', '=', self.channel.id)
                ])), 0)

                # Create a party
                party = self.Party.find_or_create_using_ps_data(
                    get_objectified_xml('customers', 1)
                )

                # Create an address using prestashop data

                # This address has a country but not a state
                # So, it should proceed without breaking and creating a
                # cache record for country
                address_data = get_objectified_xml('addresses', 2)
                address = self.Address.find_or_create_for_party_using_ps_data(
                    party, address_data
                )
                self.assertEqual(len(self.Address.search([
                    ('party.channel', '=', self.channel.id)
                ])), 1)
                self.assertEqual(len(self.ContactMechanism.search([])), 3)
                self.assertEqual(len(self.CountryPrestashop.search([
                    ('channel', '=', self.channel.id)
                ])), 1)

                # Make sure the country cached is the right one
                ps_country_id = address_data.id_country.pyval
                self.assertEqual(
                    self.Country.get_using_ps_id(ps_country_id).id,
                    address.country.id
                )

                # Find or create the same address, it should not create a new
                # one
                address = \
                    self.Address.find_or_create_for_party_using_ps_data(
                        party, get_objectified_xml('addresses', 2)
                    )
                self.assertEqual(len(self.Address.search([
                    ('party.channel', '=', self.channel.id)
                ])), 1)
                self.assertEqual(len(self.ContactMechanism.search([])), 3)
                self.assertEqual(len(self.CountryPrestashop.search([
                    ('channel', '=', self.channel.id)
                ])), 1)

                # Test with an exactly same address with same ID
                self.assertTrue(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2001))
                )

                # Test with a nearly same address with same ID and street2
                # missing
                self.assertFalse(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2002))
                )

                # Test with a nearly same address with same ID and different
                # country
                self.assertFalse(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2003))
                )

                # Test with a nearly same address with same ID and non ascii
                # characters in name
                self.assertFalse(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2004))
                )

                # Test with a nearly same address with same ID and postcode
                # missing
                self.assertFalse(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2005))
                )

                # Test with a nearly same address with same ID and different
                # city
                self.assertFalse(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2006))
                )

                # No subdivision has been cached till now
                self.assertEqual(len(self.SubdivisionPrestashop.search([
                    ('channel', '=', self.channel.id)
                ])), 0)

                get_objectified_xml('states', 1)

                # Cache a subdivision
                subdivision = self.Subdivision.cache_prestashop_id(1)
                self.assertEqual(len(self.SubdivisionPrestashop.search([
                    ('channel', '=', self.channel.id)
                ])), 1)
                self.assertEqual(
                    self.Subdivision.get_using_ps_id(1).id, subdivision.id
                )

                # Nothing should be created under alt_channel
                self.assertEqual(len(self.Address.search([
                    ('party.channel', '=', self.alt_channel.id)
                ])), 0)
                self.assertEqual(len(self.CountryPrestashop.search([
                    ('channel', '=', self.alt_channel.id)
                ])), 0)
Example #10
0
    def test_0010_order_import(self):
        """Test Order import
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(
                    self.User.get_preferences(context_only=True),
                    current_channel=self.channel.id,
                    ps_test=True,
            ):
                self.setup_channels()

                self.channel.get_prestashop_client()

                self.assertEqual(
                    len(self.Sale.search([('channel', '=', self.channel.id)])),
                    0)
                self.assertEqual(
                    len(self.Party.search([('channel', '=', self.channel.id)
                                           ])), 0)
                self.assertEqual(
                    len(
                        self.Address.search([('party.channel', '=',
                                              self.channel.id)])), 0)
                self.assertEqual(len(self.ContactMechanism.search([])), 0)

                order_data = get_objectified_xml('orders', 1)

                self.Sale.find_or_create_using_ps_data(order_data)
                self.assertEqual(
                    len(self.Sale.search([('channel', '=', self.channel.id)])),
                    1)
                self.assertEqual(
                    len(self.Party.search([('channel', '=', self.channel.id)
                                           ])), 1)
                self.assertEqual(
                    len(
                        self.Address.search([('party.channel', '=',
                                              self.channel.id)])), 1)
                self.assertEqual(len(self.ContactMechanism.search([])), 3)

                # Try importing the same sale again, it should NOT create a
                # new one.
                self.Sale.find_or_create_using_ps_data(order_data)
                self.assertEqual(
                    len(self.Sale.search([('channel', '=', self.channel.id)])),
                    1)

                sale, = self.Sale.search([('channel', '=', self.channel.id)])

                # Test getting sale using prestashop data
                self.assertEqual(
                    sale.id,
                    self.Sale.get_order_using_ps_data(order_data).id)

                self.assertEqual(sale.state, 'done')

                self.assertEqual(sale.total_amount,
                                 Decimal(str(order_data.total_paid_tax_excl)))
                # Sale should not be created under alt_channel
                self.assertEqual(
                    len(
                        self.Sale.search([('channel', '=', self.alt_channel.id)
                                          ])), 0)

                # Creating the order again should blow up with a usererror
                # due to sql constraints
                self.assertRaises(UserError, self.Sale.create_using_ps_data,
                                  order_data)
Example #11
0
    def test_0010_product_template_import(self):
        """Test Product Template import
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT) as txn:
            # Call method to setup defaults
            self.setup_defaults()

            with txn.set_context(
                    current_channel=self.channel.id,
                    ps_test=True,
            ):
                self.setup_channels()

                self.assertEqual(len(self.ProductTemplate.search([])), 1)
                self.assertEqual(
                    len(
                        self.TemplatePrestashop.search([
                            ('channel', '=', self.channel.id)
                        ])), 0)
                self.assertEqual(len(self.Product.search([])), 1)
                self.assertEqual(
                    len(
                        self.ProductPrestashop.search([('channel', '=',
                                                        self.channel.id)])), 0)

                product_data = get_objectified_xml('products', 1)
                template = self.ProductTemplate.find_or_create_using_ps_data(
                    product_data)
                # This should create a template and two variants where one
                # is created by template and other by this combination
                self.assertEqual(len(self.ProductTemplate.search([])), 2)
                self.assertEqual(
                    len(
                        self.TemplatePrestashop.search([
                            ('channel', '=', self.channel.id)
                        ])), 1)
                self.assertEqual(len(self.Product.search([])), 2)
                self.assertEqual(
                    len(
                        self.ProductPrestashop.search([('channel', '=',
                                                        self.channel.id)])), 1)

                # Product name should be in english and french
                with txn.set_context(language='en_US'):
                    template = self.ProductTemplate(template.id)
                    self.assertEqual(template.name, 'iPod Nano')
                with txn.set_context(language='fr_FR'):
                    template = self.ProductTemplate(template.id)
                    self.assertEqual(template.name, 'iPod Nano French')

                # Product description should be in english only
                with txn.set_context(language='en_US'):
                    product_desc_en = self.Product(
                        template.products[0].id).description
                with txn.set_context(language='fr_FR'):
                    product_desc_fr = self.Product(
                        template.products[0].id).description
                self.assertEqual(product_desc_en, product_desc_fr)

                # Nothing should be created under site_alt
                self.assertEqual(
                    len(
                        self.TemplatePrestashop.search([
                            ('channel', '=', self.alt_channel.id)
                        ])), 0)
                self.assertEqual(
                    len(
                        self.ProductPrestashop.search([
                            ('channel', '=', self.alt_channel.id)
                        ])), 0)

                # Get template using prestashop data
                self.assertEqual(
                    template.id,
                    self.ProductTemplate.get_template_using_ps_data(
                        product_data).id)

                # Get template using prestashop ID
                self.assertEqual(
                    template.id,
                    self.ProductTemplate.get_template_using_ps_id(1).id)

                # Try creating the same product again, it should NOT create a
                # new one and blow with user error due to sql constraint
                self.assertRaises(UserError,
                                  self.ProductTemplate.create_using_ps_data,
                                  product_data)
Example #12
0
    def test_0020_product_import(self):
        """Test Product import
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(
                    current_channel=self.channel.id,
                    ps_test=True,
            ):
                self.setup_channels()

                self.assertEqual(len(self.ProductTemplate.search([])), 1)
                self.assertEqual(
                    len(
                        self.TemplatePrestashop.search([
                            ('channel', '=', self.channel.id)
                        ])), 0)
                self.assertEqual(len(self.Product.search([])), 1)
                self.assertEqual(
                    len(
                        self.ProductPrestashop.search([('channel', '=',
                                                        self.channel.id)])), 0)

                product = self.Product.find_or_create_using_ps_data(
                    get_objectified_xml('combinations', 1))
                # This should create a template and two variants where one
                # is created by template and other by this combination
                self.assertEqual(len(self.ProductTemplate.search([])), 2)
                self.assertEqual(
                    len(
                        self.TemplatePrestashop.search([
                            ('channel', '=', self.channel.id)
                        ])), 1)
                self.assertEqual(len(self.Product.search([])), 3)
                self.assertEqual(
                    len(
                        self.ProductPrestashop.search([('channel', '=',
                                                        self.channel.id)])), 2)

                # Try importing the same product again, it should NOT create a
                # new one.
                self.Product.find_or_create_using_ps_data(
                    get_objectified_xml('combinations', 1))
                self.assertEqual(len(self.Product.search([])), 3)

                # Test getting product using prestashop data
                self.assertEqual(
                    product.id,
                    self.Product.get_product_using_ps_data(
                        get_objectified_xml('combinations', 1)).id)

                # Test getting product using prestashop ID
                self.assertEqual(product.id,
                                 self.Product.get_product_using_ps_id(1).id)

                # Nothing should be created under site_alt
                self.assertEqual(
                    len(
                        self.TemplatePrestashop.search([
                            ('channel', '=', self.alt_channel.id)
                        ])), 0)
                self.assertEqual(
                    len(
                        self.ProductPrestashop.search([
                            ('channel', '=', self.alt_channel.id)
                        ])), 0)
Example #13
0
    def test_0020_address_import_n_matching(self):
        """Test address import and pattern matching
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(current_channel=self.channel.id,
                                           ps_test=True):
                self.setup_channels()

                self.channel.get_prestashop_client()

                self.assertEqual(
                    len(
                        self.Address.search([('party.channel', '=',
                                              self.channel.id)])), 0)
                self.assertEqual(len(self.ContactMechanism.search([])), 0)
                self.assertEqual(
                    len(
                        self.CountryPrestashop.search([('channel', '=',
                                                        self.channel.id)])), 0)

                # Create a party
                party = self.Party.find_or_create_using_ps_data(
                    get_objectified_xml('customers', 1))

                # Create an address using prestashop data

                # This address has a country but not a state
                # So, it should proceed without breaking and creating a
                # cache record for country
                address_data = get_objectified_xml('addresses', 2)
                address = self.Address.find_or_create_for_party_using_ps_data(
                    party, address_data)
                self.assertEqual(
                    len(
                        self.Address.search([('party.channel', '=',
                                              self.channel.id)])), 1)
                self.assertEqual(len(self.ContactMechanism.search([])), 3)
                self.assertEqual(
                    len(
                        self.CountryPrestashop.search([('channel', '=',
                                                        self.channel.id)])), 1)

                # Make sure the country cached is the right one
                ps_country_id = address_data.id_country.pyval
                self.assertEqual(
                    self.Country.get_using_ps_id(ps_country_id).id,
                    address.country.id)

                # Find or create the same address, it should not create a new
                # one
                address = \
                    self.Address.find_or_create_for_party_using_ps_data(
                        party, get_objectified_xml('addresses', 2)
                    )
                self.assertEqual(
                    len(
                        self.Address.search([('party.channel', '=',
                                              self.channel.id)])), 1)
                self.assertEqual(len(self.ContactMechanism.search([])), 3)
                self.assertEqual(
                    len(
                        self.CountryPrestashop.search([('channel', '=',
                                                        self.channel.id)])), 1)

                # Test with an exactly same address with same ID
                self.assertTrue(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2001)))

                # Test with a nearly same address with same ID and street2
                # missing
                self.assertFalse(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2002)))

                # Test with a nearly same address with same ID and different
                # country
                self.assertFalse(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2003)))

                # Test with a nearly same address with same ID and non ascii
                # characters in name
                self.assertFalse(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2004)))

                # Test with a nearly same address with same ID and postcode
                # missing
                self.assertFalse(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2005)))

                # Test with a nearly same address with same ID and different
                # city
                self.assertFalse(
                    address.match_with_ps_data(
                        get_objectified_xml('addresses', 2006)))

                # No subdivision has been cached till now
                self.assertEqual(
                    len(
                        self.SubdivisionPrestashop.search([
                            ('channel', '=', self.channel.id)
                        ])), 0)

                get_objectified_xml('states', 1)

                # Cache a subdivision
                subdivision = self.Subdivision.cache_prestashop_id(1)
                self.assertEqual(
                    len(
                        self.SubdivisionPrestashop.search([
                            ('channel', '=', self.channel.id)
                        ])), 1)
                self.assertEqual(
                    self.Subdivision.get_using_ps_id(1).id, subdivision.id)

                # Nothing should be created under alt_channel
                self.assertEqual(
                    len(
                        self.Address.search([('party.channel', '=',
                                              self.alt_channel.id)])), 0)
                self.assertEqual(
                    len(
                        self.CountryPrestashop.search([
                            ('channel', '=', self.alt_channel.id)
                        ])), 0)
Example #14
0
    def test_0010_party_import(self):
        """Test Party import
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT) as txn:
            # Call method to setup defaults
            self.setup_defaults()

            with txn.set_context(current_channel=self.channel.id,
                                 ps_test=True):
                self.setup_channels()

                self.channel.get_prestashop_client()

                self.assertEqual(
                    len(self.Party.search([('channel', '=', self.channel.id)
                                           ])), 0)
                self.assertEqual(len(self.ContactMechanism.search([])), 0)

                # Create a party using prestashop data
                customer_data = get_objectified_xml('customers', 1)
                party = self.Party.create_using_ps_data(customer_data)
                self.assertEqual(
                    len(self.Party.search([('channel', '=', self.channel.id)
                                           ])), 1)
                self.assertEqual(len(self.ContactMechanism.search([])), 1)

                # Assert that the language set on party is same as the language
                # in channel languages
                self.assertEqual(
                    party.lang,
                    self.Lang.get_using_ps_id(customer_data.id_lang.pyval))

                # Try importing the same party, it should NOT create a
                # new one.
                party = self.Party.find_or_create_using_ps_data(customer_data)
                self.assertEqual(
                    len(self.Party.search([('channel', '=', self.channel.id)
                                           ])), 1)
                self.assertEqual(len(self.ContactMechanism.search([])), 1)

                # Search for the same party in tryton using a different method
                # It should return the same party
                self.assertEqual(
                    party.id,
                    self.Party.get_party_using_ps_data(customer_data).id)

                # Create the same party, it should NOT create a new one
                # Instead, it should blow up with a UserError sue to sql
                # constraints
                self.assertRaises(UserError, self.Party.create_using_ps_data,
                                  customer_data)

                txn.cursor.rollback()

        with Transaction().start(DB_NAME, USER, context=CONTEXT) as txn:
            # Call method to setup defaults
            self.setup_defaults()

            with txn.set_context(current_channel=self.alt_channel.id,
                                 ps_test=True):
                self.setup_channels()
                self.alt_channel.get_prestashop_client()

                # Nothing should be linked to alt_channel
                self.assertEqual(
                    len(
                        self.Party.search([('channel', '=',
                                            self.alt_channel.id)])), 0)

                # Create a party using prestashop data
                customer_data = get_objectified_xml('customers', 1)
                party = self.Party.create_using_ps_data(customer_data)
                self.assertEqual(
                    len(
                        self.Party.search([('channel', '=',
                                            self.alt_channel.id)])), 1)
                txn.cursor.rollback()
    def test_0010_order_import(self):
        """Test Order import
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT):
            # Call method to setup defaults
            self.setup_defaults()

            with Transaction().set_context(
                self.User.get_preferences(context_only=True),
                current_channel=self.channel.id, ps_test=True,
            ):
                self.setup_channels()

                self.channel.get_prestashop_client()

                self.assertEqual(len(self.Sale.search([
                    ('channel', '=', self.channel.id)
                ])), 0)
                self.assertEqual(len(self.Party.search([
                    ('channel', '=', self.channel.id)
                ])), 0)
                self.assertEqual(len(self.Address.search([
                    ('party.channel', '=', self.channel.id)
                ])), 0)
                self.assertEqual(len(self.ContactMechanism.search([])), 0)

                order_data = get_objectified_xml('orders', 1)

                self.Sale.find_or_create_using_ps_data(order_data)
                self.assertEqual(len(self.Sale.search([
                    ('channel', '=', self.channel.id)
                ])), 1)
                self.assertEqual(len(self.Party.search([
                    ('channel', '=', self.channel.id)
                ])), 1)
                self.assertEqual(len(self.Address.search([
                    ('party.channel', '=', self.channel.id)
                ])), 1)
                self.assertEqual(len(self.ContactMechanism.search([])), 3)

                # Try importing the same sale again, it should NOT create a
                # new one.
                self.Sale.find_or_create_using_ps_data(order_data)
                self.assertEqual(len(self.Sale.search([
                    ('channel', '=', self.channel.id)
                ])), 1)

                sale, = self.Sale.search([
                    ('channel', '=', self.channel.id)
                ])

                # Test getting sale using prestashop data
                self.assertEqual(
                    sale.id,
                    self.Sale.get_order_using_ps_data(order_data).id
                )

                self.assertEqual(sale.state, 'done')

                self.assertEqual(
                    sale.total_amount,
                    Decimal(str(order_data.total_paid_tax_excl))
                )
                # Sale should not be created under alt_channel
                self.assertEqual(len(self.Sale.search([
                    ('channel', '=', self.alt_channel.id)
                ])), 0)

                # Creating the order again should blow up with a usererror
                # due to sql constraints
                self.assertRaises(
                    UserError,
                    self.Sale.create_using_ps_data, order_data
                )
    def test_0010_product_template_import(self):
        """Test Product Template import
        """
        with Transaction().start(DB_NAME, USER, context=CONTEXT) as txn:
            # Call method to setup defaults
            self.setup_defaults()

            with txn.set_context(
                current_channel=self.channel.id, ps_test=True,
            ):
                self.setup_channels()

                self.assertEqual(len(self.ProductTemplate.search([])), 1)
                self.assertEqual(len(self.TemplatePrestashop.search([
                    ('channel', '=', self.channel.id)
                ])), 0)
                self.assertEqual(len(self.Product.search([])), 1)
                self.assertEqual(len(self.ProductPrestashop.search([
                    ('channel', '=', self.channel.id)
                ])), 0)

                product_data = get_objectified_xml('products', 1)
                template = self.ProductTemplate.find_or_create_using_ps_data(
                    product_data
                )
                # This should create a template and two variants where one
                # is created by template and other by this combination
                self.assertEqual(len(self.ProductTemplate.search([])), 2)
                self.assertEqual(len(self.TemplatePrestashop.search([
                    ('channel', '=', self.channel.id)
                ])), 1)
                self.assertEqual(len(self.Product.search([])), 2)
                self.assertEqual(len(self.ProductPrestashop.search([
                    ('channel', '=', self.channel.id)
                ])), 1)

                # Product name should be in english and french
                with txn.set_context(language='en_US'):
                    template = self.ProductTemplate(template.id)
                    self.assertEqual(
                        template.name, 'iPod Nano'
                    )
                with txn.set_context(language='fr_FR'):
                    template = self.ProductTemplate(template.id)
                    self.assertEqual(
                        template.name, 'iPod Nano French'
                    )

                # Product description should be in english only
                with txn.set_context(language='en_US'):
                    product_desc_en = self.Product(
                        template.products[0].id
                    ).description
                with txn.set_context(language='fr_FR'):
                    product_desc_fr = self.Product(
                        template.products[0].id
                    ).description
                self.assertEqual(product_desc_en, product_desc_fr)

                # Nothing should be created under site_alt
                self.assertEqual(len(self.TemplatePrestashop.search([
                    ('channel', '=', self.alt_channel.id)
                ])), 0)
                self.assertEqual(len(self.ProductPrestashop.search([
                    ('channel', '=', self.alt_channel.id)
                ])), 0)

                # Get template using prestashop data
                self.assertEqual(
                    template.id,
                    self.ProductTemplate.get_template_using_ps_data(
                        product_data
                    ).id
                )

                # Get template using prestashop ID
                self.assertEqual(
                    template.id,
                    self.ProductTemplate.get_template_using_ps_id(1).id
                )

                # Try creating the same product again, it should NOT create a
                # new one and blow with user error due to sql constraint
                self.assertRaises(
                    UserError,
                    self.ProductTemplate.create_using_ps_data, product_data
                )