# Path: oscar_mws/test/factories.py
# class UserFactory(factory.DjangoModelFactory):
# class CountryFactory(factory.DjangoModelFactory):
# class ProductClassFactory(factory.DjangoModelFactory):
# class BasketFactory(factory.DjangoModelFactory):
# class AmazonProfileFactory(factory.DjangoModelFactory):
# class ProductFactory(factory.DjangoModelFactory):
# class MerchantAccountFactory(factory.DjangoModelFactory):
# class AmazonMarketplaceFactory(factory.DjangoModelFactory):
# class FeedSubmissionFactory(factory.DjangoModelFactory):
# class PartnerFactory(factory.DjangoModelFactory):
# class StockRecordFactory(factory.DjangoModelFactory):
# class ShippingAddressFactory(factory.DjangoModelFactory):
# class OrderLineFactory(factory.DjangoModelFactory):
# class OrderFactory(factory.DjangoModelFactory):
# class FulfillmentOrderFactory(factory.DjangoModelFactory):
#     FACTORY_FOR = get_model('auth', 'User')
#     FACTORY_FOR = get_model('address', 'Country')
#     FACTORY_FOR = get_model('catalogue', 'ProductClass')
#     FACTORY_FOR = get_model('basket', 'Basket')
#     FACTORY_FOR = get_model('oscar_mws', 'AmazonProfile')
#     FACTORY_DJANGO_GET_OR_CREATE = ('product',)
#     FACTORY_FOR = get_model('catalogue', 'Product')
#     FACTORY_FOR = get_model('oscar_mws', 'MerchantAccount')
#     FACTORY_FOR = get_model('oscar_mws', 'AmazonMarketplace')
#     FACTORY_FOR = get_model('oscar_mws', 'FeedSubmission')
#     FACTORY_DJANGO_GET_OR_CREATE = ('submission_id',)
#     FACTORY_FOR = get_model('partner', 'Partner')
#     FACTORY_FOR = get_model('partner', 'StockRecord')
#     FACTORY_FOR = get_model('order', 'ShippingAddress')
#     FACTORY_FOR = get_model('order', 'Line')
#     FACTORY_FOR = get_model('order', 'Order')
#     FACTORY_FOR = get_model('oscar_mws', 'FulfillmentOrder')
#     def stockrecord(self, created, extracted, **kwargs):
# 
# Path: oscar_mws/mixins.py
# class AmazonStockTrackingMixin(object):
#     """
#     A mixin to make stock tracking for Amazon MWS fulfilled products possible.
#     The way stock tracking works in Oscar doesn't play nicely with the details
#     returned from MWS. Basically Amazon provides a single value which is the
#     amount of items still available to be fulfilled. In Oscar, we track the
#     number in stock as well as the allocated number of products.
#     ``num_in_stock - num_allocated`` is the number of items actually avaiable
#     to buy and both number in stock and number allocated are only decremented
#     whenever an item is marked as shipped.
# 
#     To handle this properly and be able to synchronise the fulfillable number
#     of products available from Amazon, we use this mixin to override the
#     """
# 
#     def set_amazon_supply_quantity(self, quantity, commit=True):
#         """
#         Convenience method to set the field ``num_in_stock`` to *quantity* and
#         reset the allocated stock in ``num_allocated`` to zero. We don't care
#         about allocation for MWS stock and therefore just reset it.
# 
#         :param integer quantity: The quantity currently available on Amazon for
#             Fulfillment by Amazon (FBA).
#         :param boolean commit: Allows to prevent immediate saving of the
#             changes to the database. This is useful if you want to save on
#             database queries when making other changes to the stock record.
#         """
#         logger.info(
#             'setting stock record to MWS supply quantity: {}'.format(quantity))
#         self.num_in_stock = int(quantity)
#         self.num_allocated = 0
#         if commit:
#             self.save()
#     set_amazon_supply_quantity.alters_data = True
# 
#     def consume_allocation(self, quantity):
#         """
#         This is used when an item is shipped. We remove the original
#         allocation and adjust the number in stock accordingly
# 
#         :param integer quantity: The quantity to be consumed.
#         """
#         if self.is_mws_record:
#             logger.debug(
#                 'stock record is MWS record. Skipping consume allocation on '
#                 'stock record ', extra={'stockrecord_id': unicode(self.id)})
#             return
#         super(AmazonStockTrackingMixin, self).consume_allocation(quantity)
#     consume_allocation.alters_data = True
# 
#     @property
#     def is_mws_record(self):
#         """
#         Checks whether this stock record is associated with an Amazon merchant
#         account.
# 
#         :rtype bool: ``True`` if the stockrecord is Amazon stock,
#             ``False`` otherwise.
#         """
#         try:
#             self.partner.amazon_merchant
#         except ObjectDoesNotExist:
#             return False
#         else:
#             return True


from mock import Mock
from django.test import TestCase
from oscar.apps.partner.models import StockRecord
from oscar_mws.test import factories
from oscar_mws.mixins import AmazonStockTrackingMixin
        self.stock_record.set_amazon_supply_quantity('12')
        self.assertEquals(self.stock_record.num_in_stock, 12)
        self.assertEquals(self.stock_record.num_allocated, 0)
        self.stock_record.save.assert_called_once_with()

    def test_when_stock_is_allocated(self):
        self.stock_record.num_in_stock = 10
        self.stock_record.num_allocated = 5
        self.stock_record.set_amazon_supply_quantity('12')
        self.assertEquals(self.stock_record.num_in_stock, 12)
        self.assertEquals(self.stock_record.num_allocated, 0)
        self.stock_record.save.assert_called_once_with()


class TestStockRecord(TestCase):

    def setUp(self):
        super(TestStockRecord, self).setUp()
        self.merchant = factories.MerchantAccountFactory()
        self.original_bases = StockRecord.__bases__
        StockRecord.__bases__ = \
            (AmazonStockTrackingMixin,) + StockRecord.__bases__

    def tearDown(self):
        super(TestStockRecord, self).tearDown()
        StockRecord.__bases__ = self.original_bases

    def test_returns_false_if_it_isnt_mws_stockrecord(self):
        stock_record = StockRecord.objects.create(
            product=factories.ProductFactory(),

# Path: oscar_mws/test/factories.py
# class UserFactory(factory.DjangoModelFactory):
# class CountryFactory(factory.DjangoModelFactory):
# class ProductClassFactory(factory.DjangoModelFactory):
# class BasketFactory(factory.DjangoModelFactory):
# class AmazonProfileFactory(factory.DjangoModelFactory):
# class ProductFactory(factory.DjangoModelFactory):
# class MerchantAccountFactory(factory.DjangoModelFactory):
# class AmazonMarketplaceFactory(factory.DjangoModelFactory):
# class FeedSubmissionFactory(factory.DjangoModelFactory):
# class PartnerFactory(factory.DjangoModelFactory):
# class StockRecordFactory(factory.DjangoModelFactory):
# class ShippingAddressFactory(factory.DjangoModelFactory):
# class OrderLineFactory(factory.DjangoModelFactory):
# class OrderFactory(factory.DjangoModelFactory):
# class FulfillmentOrderFactory(factory.DjangoModelFactory):
#     FACTORY_FOR = get_model('auth', 'User')
#     FACTORY_FOR = get_model('address', 'Country')
#     FACTORY_FOR = get_model('catalogue', 'ProductClass')
#     FACTORY_FOR = get_model('basket', 'Basket')
#     FACTORY_FOR = get_model('oscar_mws', 'AmazonProfile')
#     FACTORY_DJANGO_GET_OR_CREATE = ('product',)
#     FACTORY_FOR = get_model('catalogue', 'Product')
#     FACTORY_FOR = get_model('oscar_mws', 'MerchantAccount')
#     FACTORY_FOR = get_model('oscar_mws', 'AmazonMarketplace')
#     FACTORY_FOR = get_model('oscar_mws', 'FeedSubmission')
#     FACTORY_DJANGO_GET_OR_CREATE = ('submission_id',)
#     FACTORY_FOR = get_model('partner', 'Partner')
#     FACTORY_FOR = get_model('partner', 'StockRecord')
#     FACTORY_FOR = get_model('order', 'ShippingAddress')
#     FACTORY_FOR = get_model('order', 'Line')
#     FACTORY_FOR = get_model('order', 'Order')
#     FACTORY_FOR = get_model('oscar_mws', 'FulfillmentOrder')
#     def stockrecord(self, created, extracted, **kwargs):
# 
# Path: oscar_mws/mixins.py
# class AmazonStockTrackingMixin(object):
#     """
#     A mixin to make stock tracking for Amazon MWS fulfilled products possible.
#     The way stock tracking works in Oscar doesn't play nicely with the details
#     returned from MWS. Basically Amazon provides a single value which is the
#     amount of items still available to be fulfilled. In Oscar, we track the
#     number in stock as well as the allocated number of products.
#     ``num_in_stock - num_allocated`` is the number of items actually avaiable
#     to buy and both number in stock and number allocated are only decremented
#     whenever an item is marked as shipped.
# 
#     To handle this properly and be able to synchronise the fulfillable number
#     of products available from Amazon, we use this mixin to override the
#     """
# 
#     def set_amazon_supply_quantity(self, quantity, commit=True):
#         """
#         Convenience method to set the field ``num_in_stock`` to *quantity* and
#         reset the allocated stock in ``num_allocated`` to zero. We don't care
#         about allocation for MWS stock and therefore just reset it.
# 
#         :param integer quantity: The quantity currently available on Amazon for
#             Fulfillment by Amazon (FBA).
#         :param boolean commit: Allows to prevent immediate saving of the
#             changes to the database. This is useful if you want to save on
#             database queries when making other changes to the stock record.
#         """
#         logger.info(
#             'setting stock record to MWS supply quantity: {}'.format(quantity))
#         self.num_in_stock = int(quantity)
#         self.num_allocated = 0
#         if commit:
#             self.save()
#     set_amazon_supply_quantity.alters_data = True
# 
#     def consume_allocation(self, quantity):
#         """
#         This is used when an item is shipped. We remove the original
#         allocation and adjust the number in stock accordingly
# 
#         :param integer quantity: The quantity to be consumed.
#         """
#         if self.is_mws_record:
#             logger.debug(
#                 'stock record is MWS record. Skipping consume allocation on '
#                 'stock record ', extra={'stockrecord_id': unicode(self.id)})
#             return
#         super(AmazonStockTrackingMixin, self).consume_allocation(quantity)
#     consume_allocation.alters_data = True
# 
#     @property
#     def is_mws_record(self):
#         """
#         Checks whether this stock record is associated with an Amazon merchant
#         account.
# 
#         :rtype bool: ``True`` if the stockrecord is Amazon stock,
#             ``False`` otherwise.
#         """
#         try:
#             self.partner.amazon_merchant
#         except ObjectDoesNotExist:
#             return False
#         else:
#             return True


# Path: tests/unit/test_mixins.py
from mock import Mock
from django.test import TestCase
from oscar.apps.partner.models import StockRecord
from oscar_mws.test import factories
from oscar_mws.mixins import AmazonStockTrackingMixin

        self.stock_record.set_amazon_supply_quantity('12')
        self.assertEquals(self.stock_record.num_in_stock, 12)
        self.assertEquals(self.stock_record.num_allocated, 0)
        self.stock_record.save.assert_called_once_with()

    def test_when_stock_is_allocated(self):
        self.stock_record.num_in_stock = 10
        self.stock_record.num_allocated = 5
        self.stock_record.set_amazon_supply_quantity('12')
        self.assertEquals(self.stock_record.num_in_stock, 12)
        self.assertEquals(self.stock_record.num_allocated, 0)
        self.stock_record.save.assert_called_once_with()


class TestStockRecord(TestCase):

    def setUp(self):
        super(TestStockRecord, self).setUp()
        self.merchant = factories.MerchantAccountFactory()
        self.original_bases = StockRecord.__bases__
        StockRecord.__bases__ = \
            (AmazonStockTrackingMixin,) + StockRecord.__bases__

    def tearDown(self):
        super(TestStockRecord, self).tearDown()
        StockRecord.__bases__ = self.original_bases

    def test_returns_false_if_it_isnt_mws_stockrecord(self):
        stock_record = StockRecord.objects.create(
            product=factories.ProductFactory(),

            partner=factories.PartnerFactory())