# 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/fulfillment/creator.py
# class FulfillmentOrderCreator(object):
# 
#     def __init__(self):
#         self.order_adapter_class = adapters.get_order_adapter()
#         self.errors = {}
#         try:
#             self.find_fulfillment_merchant = load_class(
#                 getattr(settings, 'MWS_FULFILLMENT_MERCHANT_FINDER', None)
#             )
#         except ImportError:
#             raise ImproperlyConfigured(
#                 "no fulfillment merchant finder callable configured in "
#                 "MWS_FULFILLMENT_MERCHANT_FINDER setting. Check your settings "
#                 "file and try again."
#             )
# 
#     def get_order_adapter(self, order):
#         return self.order_adapter_class(order=order)
# 
#     def create_fulfillment_order(self, order, lines=None, **kwargs):
#         adapter = self.get_order_adapter(order)
# 
#         fulfillment_orders = []
#         for address in adapter.addresses:
#             fulfillment_id = adapter.get_seller_fulfillment_order_id(address)
#             order_kw = adapter.get_fields(address=address)
# 
#             try:
#                 merchant = self.find_fulfillment_merchant(order, address)
#             except MwsFulfillmentError:
#                 merchant = None
#             if not merchant:
#                 self.errors[fulfillment_id] = _(
#                     "could not find suitable merchant for fulfillemnt order "
#                     "{}". format(fulfillment_id)
#                 )
#                 continue
# 
#             try:
#                 fulfillment_order = FulfillmentOrder.objects.get(
#                     fulfillment_id=fulfillment_id, order=order,
#                     merchant=merchant)
#             except FulfillmentOrder.DoesNotExist:
#                 fulfillment_order = FulfillmentOrder.objects.create(
#                     fulfillment_id=fulfillment_id,
#                     order=order,
#                     merchant=merchant,
#                     shipping_address=order_kw.get('DestinationAddress'),
#                     shipping_speed=order_kw.get('ShippingSpeedCategory'),
#                     comments=order_kw.get('DisplayableOrderComment'),
#                 )
#             else:
#                 self.errors[fulfillment_id] = _("Order already created.")
# 
#             fulfillment_orders.append(fulfillment_order)
# 
#             for line_adapter in adapter.get_lines(address=address):
#                 self.create_fulfillment_line(line_adapter, fulfillment_order)
#         return fulfillment_orders
# 
#     def create_fulfillment_line(self, line_adapter, fulfillment_order):
#         try:
#             line = FulfillmentOrderLine.objects.get(
#                 line=line_adapter.line, fulfillment_order=fulfillment_order)
#         except FulfillmentOrderLine.DoesNotExist:
#             line = FulfillmentOrderLine(
#                 line=line_adapter.line, fulfillment_order=fulfillment_order)
# 
#         line_kwargs = line_adapter.get_fields()
#         line.order_item_id = \
#             line_adapter.get_seller_fulfillment_order_item_id()
#         line.quantity = line_kwargs.get('Quantity')
#         line.comment = line_kwargs.get('DisplayableOrderComment', '')
#         price = line_kwargs.get('PerUnitDeclaredValue')
#         if price and price.get('Value'):
#             line.price_incl_tax = D(price.get('Value'))
#             line.price_currency = price.get('Currency')
#         line.save()


import mock
from django.test import TestCase
from django.db.models import get_model
from oscar.test.factories import create_order
from oscar_mws.test import factories
from oscar_mws.fulfillment.creator import FulfillmentOrderCreator




Country = get_model('address', 'Country')
Product = get_model('catalogue', 'Product')
ShippingAddress = get_model('order', 'ShippingAddress')
FulfillmentOrder = get_model('oscar_mws', 'FulfillmentOrder')


class TestFulfillmentShipmentCreator(TestCase):
    fixtures = ['countries']

    def setUp(self):
        super(TestFulfillmentShipmentCreator, self).setUp()
        self.merchant = factories.MerchantAccountFactory()
        self.creator = FulfillmentOrderCreator()
        self.address = ShippingAddress.objects.create(
            first_name='test',

# 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/fulfillment/creator.py
# class FulfillmentOrderCreator(object):
# 
#     def __init__(self):
#         self.order_adapter_class = adapters.get_order_adapter()
#         self.errors = {}
#         try:
#             self.find_fulfillment_merchant = load_class(
#                 getattr(settings, 'MWS_FULFILLMENT_MERCHANT_FINDER', None)
#             )
#         except ImportError:
#             raise ImproperlyConfigured(
#                 "no fulfillment merchant finder callable configured in "
#                 "MWS_FULFILLMENT_MERCHANT_FINDER setting. Check your settings "
#                 "file and try again."
#             )
# 
#     def get_order_adapter(self, order):
#         return self.order_adapter_class(order=order)
# 
#     def create_fulfillment_order(self, order, lines=None, **kwargs):
#         adapter = self.get_order_adapter(order)
# 
#         fulfillment_orders = []
#         for address in adapter.addresses:
#             fulfillment_id = adapter.get_seller_fulfillment_order_id(address)
#             order_kw = adapter.get_fields(address=address)
# 
#             try:
#                 merchant = self.find_fulfillment_merchant(order, address)
#             except MwsFulfillmentError:
#                 merchant = None
#             if not merchant:
#                 self.errors[fulfillment_id] = _(
#                     "could not find suitable merchant for fulfillemnt order "
#                     "{}". format(fulfillment_id)
#                 )
#                 continue
# 
#             try:
#                 fulfillment_order = FulfillmentOrder.objects.get(
#                     fulfillment_id=fulfillment_id, order=order,
#                     merchant=merchant)
#             except FulfillmentOrder.DoesNotExist:
#                 fulfillment_order = FulfillmentOrder.objects.create(
#                     fulfillment_id=fulfillment_id,
#                     order=order,
#                     merchant=merchant,
#                     shipping_address=order_kw.get('DestinationAddress'),
#                     shipping_speed=order_kw.get('ShippingSpeedCategory'),
#                     comments=order_kw.get('DisplayableOrderComment'),
#                 )
#             else:
#                 self.errors[fulfillment_id] = _("Order already created.")
# 
#             fulfillment_orders.append(fulfillment_order)
# 
#             for line_adapter in adapter.get_lines(address=address):
#                 self.create_fulfillment_line(line_adapter, fulfillment_order)
#         return fulfillment_orders
# 
#     def create_fulfillment_line(self, line_adapter, fulfillment_order):
#         try:
#             line = FulfillmentOrderLine.objects.get(
#                 line=line_adapter.line, fulfillment_order=fulfillment_order)
#         except FulfillmentOrderLine.DoesNotExist:
#             line = FulfillmentOrderLine(
#                 line=line_adapter.line, fulfillment_order=fulfillment_order)
# 
#         line_kwargs = line_adapter.get_fields()
#         line.order_item_id = \
#             line_adapter.get_seller_fulfillment_order_item_id()
#         line.quantity = line_kwargs.get('Quantity')
#         line.comment = line_kwargs.get('DisplayableOrderComment', '')
#         price = line_kwargs.get('PerUnitDeclaredValue')
#         if price and price.get('Value'):
#             line.price_incl_tax = D(price.get('Value'))
#             line.price_currency = price.get('Currency')
#         line.save()


# Path: tests/unit/test_fulfillment.py
import mock
from django.test import TestCase
from django.db.models import get_model
from oscar.test.factories import create_order
from oscar_mws.test import factories
from oscar_mws.fulfillment.creator import FulfillmentOrderCreator





Country = get_model('address', 'Country')
Product = get_model('catalogue', 'Product')
ShippingAddress = get_model('order', 'ShippingAddress')
FulfillmentOrder = get_model('oscar_mws', 'FulfillmentOrder')


class TestFulfillmentShipmentCreator(TestCase):
    fixtures = ['countries']

    def setUp(self):
        super(TestFulfillmentShipmentCreator, self).setUp()
        self.merchant = factories.MerchantAccountFactory()
        self.creator = FulfillmentOrderCreator()
        self.address = ShippingAddress.objects.create(
            first_name='test',

            last_name='man',