# 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/seller/gateway.py
# def update_marketplaces(merchant):
#     sellers_api = get_merchant_connection(merchant.seller_id, 'sellers')
# 
#     try:
#         response = sellers_api.list_marketplace_participations().parsed
#     except MWSError:
#         logger.error(
#             "could not retrieve marketplaces for merchant {}".format(
#                 merchant.seller_id),
#             exc_info=1, extra={'seller_id': merchant.seller_id})
#         return []
# 
#     marketplaces = []
#     for rsp_marketplace in response.ListMarketplaces.get_list('Marketplace'):
#         try:
#             marketplace = AmazonMarketplace.objects.get(
#                 marketplace_id=rsp_marketplace.MarketplaceId,
#                 merchant=merchant)
#         except AmazonMarketplace.DoesNotExist:
#             marketplace = AmazonMarketplace(
#                 marketplace_id=rsp_marketplace.MarketplaceId,
#                 merchant=merchant)
#         marketplace.name = rsp_marketplace.Name
#         marketplace.domain = rsp_marketplace.DomainName
#         marketplace.region = rsp_marketplace.DefaultCountryCode
#         marketplace.currency_code = rsp_marketplace.DefaultCurrencyCode
#         marketplace.save()
# 
#         marketplaces.append(marketplace)
#     return marketplaces


import os
import pytest
from oscar_mws.test import factories
from oscar_mws.seller.gateway import update_marketplaces
    from oscar_mws import connection
@pytest.fixture
def merchant(transactional_db):
    # make sure that the connection cache is cleared
    connection._mws_connections = {}

    merchant = factories.MerchantAccountFactory(
        name="Integration Test Account",
        seller_id=os.getenv('SELLER_ID'),
        aws_api_key=os.getenv('AWS_ACCESS_KEY_ID'),
        aws_api_secret=os.getenv('AWS_SECRET_ACCESS_KEY'))
    return merchant


@pytest.mark.integration
def test_marketplaces_can_be_retrieved_for_merchant_account(merchant):
    assert merchant.marketplaces.count() == 0
    update_marketplaces(merchant)

    market_ids = [m.marketplace_id for m in merchant.marketplaces.all()]
    assert sorted(market_ids) == sorted([u'ATVPDKIKX0DER', u'A2ZV50J4W1RKNI'])


@pytest.mark.integration
def test_fails_gracefully_if_no_marketplace_found(merchant):
    assert merchant.marketplaces.count() == 0

    merchant.aws_api_key = 'invalidkey'
    merchant.aws_api_secret = 'invalidsecret'
    merchant.save()


# 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/seller/gateway.py
# def update_marketplaces(merchant):
#     sellers_api = get_merchant_connection(merchant.seller_id, 'sellers')
# 
#     try:
#         response = sellers_api.list_marketplace_participations().parsed
#     except MWSError:
#         logger.error(
#             "could not retrieve marketplaces for merchant {}".format(
#                 merchant.seller_id),
#             exc_info=1, extra={'seller_id': merchant.seller_id})
#         return []
# 
#     marketplaces = []
#     for rsp_marketplace in response.ListMarketplaces.get_list('Marketplace'):
#         try:
#             marketplace = AmazonMarketplace.objects.get(
#                 marketplace_id=rsp_marketplace.MarketplaceId,
#                 merchant=merchant)
#         except AmazonMarketplace.DoesNotExist:
#             marketplace = AmazonMarketplace(
#                 marketplace_id=rsp_marketplace.MarketplaceId,
#                 merchant=merchant)
#         marketplace.name = rsp_marketplace.Name
#         marketplace.domain = rsp_marketplace.DomainName
#         marketplace.region = rsp_marketplace.DefaultCountryCode
#         marketplace.currency_code = rsp_marketplace.DefaultCurrencyCode
#         marketplace.save()
# 
#         marketplaces.append(marketplace)
#     return marketplaces


# Path: tests/integration/test_marketplaces.py
import os
import pytest
from oscar_mws.test import factories
from oscar_mws.seller.gateway import update_marketplaces
    from oscar_mws import connection

@pytest.fixture
def merchant(transactional_db):
    # make sure that the connection cache is cleared
    connection._mws_connections = {}

    merchant = factories.MerchantAccountFactory(
        name="Integration Test Account",
        seller_id=os.getenv('SELLER_ID'),
        aws_api_key=os.getenv('AWS_ACCESS_KEY_ID'),
        aws_api_secret=os.getenv('AWS_SECRET_ACCESS_KEY'))
    return merchant


@pytest.mark.integration
def test_marketplaces_can_be_retrieved_for_merchant_account(merchant):
    assert merchant.marketplaces.count() == 0
    update_marketplaces(merchant)

    market_ids = [m.marketplace_id for m in merchant.marketplaces.all()]
    assert sorted(market_ids) == sorted([u'ATVPDKIKX0DER', u'A2ZV50J4W1RKNI'])


@pytest.mark.integration
def test_fails_gracefully_if_no_marketplace_found(merchant):
    assert merchant.marketplaces.count() == 0

    merchant.aws_api_key = 'invalidkey'
    merchant.aws_api_secret = 'invalidsecret'
    merchant.save()


    update_marketplaces(merchant)